feat: Enhance tool call handling with support for multiple tools in conversation
This commit is contained in:
@@ -307,10 +307,9 @@ public interface BaseAgent {
|
||||
}
|
||||
|
||||
private void appendToolCallResults(final Conversation conversation, final Completion completion, final List<ToolCallingAgentResponseHandler.ToolCallResult> toolCallResults) {
|
||||
conversation.addTurn(new Conversation.Turn(TurnType.ASSISTANT, completion.response() == null ? "" : completion.response(), completion.toolCalls(), null));
|
||||
for (final var toolCallResult : toolCallResults) {
|
||||
conversation.addTurn(new Conversation.Turn(TurnType.TOOL, toolCallResult.output(), null, toolCallResult.toolCallId()));
|
||||
}
|
||||
conversation.appendToolCallRound(completion, toolCallResults.stream()
|
||||
.map(toolCallResult -> new Conversation.ToolResult(toolCallResult.output(), toolCallResult.toolCallId()))
|
||||
.toList());
|
||||
}
|
||||
|
||||
private List<Split> applyTemplate(final AgentContext context) {
|
||||
|
||||
@@ -55,6 +55,23 @@ public final class Conversation {
|
||||
}
|
||||
}
|
||||
|
||||
public void appendToolCallRound(final Completion completion, final List<ToolResult> toolResults) {
|
||||
if (completion == null) {
|
||||
throw new IllegalArgumentException("completion must not be null");
|
||||
}
|
||||
this.appendToolCallRound(completion.response(), completion.toolCalls(), toolResults);
|
||||
}
|
||||
|
||||
public void appendToolCallRound(final String assistantText, final List<ToolCall> toolCalls, final List<ToolResult> toolResults) {
|
||||
this.addTurn(new Turn(TurnType.ASSISTANT, assistantText == null ? "" : assistantText, toolCalls, null));
|
||||
if (toolResults == null || toolResults.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
for (final var toolResult : toolResults) {
|
||||
this.addTurn(new Turn(TurnType.TOOL, toolResult.text(), null, toolResult.toolCallId()));
|
||||
}
|
||||
}
|
||||
|
||||
public void rewindTurn() {
|
||||
synchronized (this.conversation) {
|
||||
if (this.conversation.size() > 1) {
|
||||
@@ -93,5 +110,12 @@ public final class Conversation {
|
||||
this(type, text, List.of(), null);
|
||||
}
|
||||
}
|
||||
|
||||
public record ToolResult(String text, String toolCallId) {
|
||||
|
||||
public ToolResult {
|
||||
text = text == null ? "" : text;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,6 +27,10 @@ public record ToolCallData(List<ToolDefinition> tools, String toolChoice, String
|
||||
return new ToolCallData(List.copyOf(tools), null, forcedToolName, null);
|
||||
}
|
||||
|
||||
public ToolCallData withParallelToolCalls(final boolean parallelToolCalls) {
|
||||
return new ToolCallData(this.tools, this.toolChoice, this.forcedToolName, parallelToolCalls);
|
||||
}
|
||||
|
||||
public boolean hasTools() {
|
||||
return !this.tools.isEmpty();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user