feat(tools): Add tool call history to ToolContext
- Add TOOL_CALL_HISTORY constant to store tool call history - Extend ToolContext with getToolConversationHistory method - Include tool call history in tool context during function execution - Add test coverage for tool call history verification Resolves #1202
This commit is contained in:
committed by
Ilayaperumal Gopinathan
parent
edc70031dc
commit
0eacc9193b
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.ai.anthropic.client;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@@ -27,6 +28,7 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.ai.anthropic.AnthropicTestConfiguration;
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.chat.messages.Message;
|
||||
import org.springframework.ai.chat.model.ChatModel;
|
||||
import org.springframework.ai.chat.model.ToolContext;
|
||||
import org.springframework.ai.model.function.FunctionCallback;
|
||||
@@ -158,6 +160,9 @@ class AnthropicChatClientMethodInvokingFunctionCallbackIT {
|
||||
|
||||
assertThat(response).contains("30", "10", "15");
|
||||
assertThat(arguments).containsEntry("tool", "value");
|
||||
assertThat(arguments).containsKey(ToolContext.TOOL_CALL_HISTORY);
|
||||
List<Message> tootConversationMessages = (List<Message>) arguments.get(ToolContext.TOOL_CALL_HISTORY);
|
||||
assertThat(tootConversationMessages).hasSize(6);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -252,6 +257,7 @@ class AnthropicChatClientMethodInvokingFunctionCallbackIT {
|
||||
|
||||
public String getWeatherWithContext(String city, Unit unit, ToolContext context) {
|
||||
arguments.put("tool", context.getContext().get("tool"));
|
||||
arguments.put(ToolContext.TOOL_CALL_HISTORY, context.getToolCallHistory());
|
||||
return getWeatherStatic(city, unit);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.ai.chat.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -142,12 +143,23 @@ public abstract class AbstractToolCallSupport {
|
||||
Map<String, Object> toolContextMap = Map.of();
|
||||
if (prompt.getOptions() instanceof FunctionCallingOptions functionCallOptions
|
||||
&& !CollectionUtils.isEmpty(functionCallOptions.getToolContext())) {
|
||||
toolContextMap = functionCallOptions.getToolContext();
|
||||
|
||||
toolContextMap = new HashMap<>(functionCallOptions.getToolContext());
|
||||
|
||||
List<Message> toolCallHistory = new ArrayList<>(prompt.copy().getInstructions());
|
||||
toolCallHistory.add(new AssistantMessage(assistantMessage.getContent(), assistantMessage.getMetadata(),
|
||||
assistantMessage.getToolCalls()));
|
||||
|
||||
toolContextMap.put(ToolContext.TOOL_CALL_HISTORY, toolCallHistory);
|
||||
}
|
||||
|
||||
ToolResponseMessage toolMessageResponse = this.executeFunctions(assistantMessage,
|
||||
new ToolContext(toolContextMap));
|
||||
|
||||
return this.buildToolCallConversation(prompt.getInstructions(), assistantMessage, toolMessageResponse);
|
||||
List<Message> toolConversationHistory = this.buildToolCallConversation(prompt.getInstructions(),
|
||||
assistantMessage, toolMessageResponse);
|
||||
|
||||
return toolConversationHistory;
|
||||
}
|
||||
|
||||
protected List<Message> buildToolCallConversation(List<Message> previousMessages, AssistantMessage assistantMessage,
|
||||
|
||||
@@ -17,8 +17,11 @@
|
||||
package org.springframework.ai.chat.model;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.ai.chat.messages.Message;
|
||||
|
||||
/**
|
||||
* Represents the context for tool execution in a function calling scenario.
|
||||
*
|
||||
@@ -33,11 +36,20 @@ import java.util.Map;
|
||||
* {@code FunctionCallingOptions} and is used in the function execution process.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* The context map can contain any information that is relevant to the tool execution.
|
||||
* </p>
|
||||
*
|
||||
* @author Christian Tzolov
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class ToolContext {
|
||||
|
||||
/**
|
||||
* The key for the running, tool call history stored in the context map.
|
||||
*/
|
||||
public static final String TOOL_CALL_HISTORY = "TOOL_CALL_HISTORY";
|
||||
|
||||
private final Map<String, Object> context;
|
||||
|
||||
/**
|
||||
@@ -57,4 +69,13 @@ public class ToolContext {
|
||||
return this.context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the tool call history from the context map.
|
||||
* @return The tool call history.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Message> getToolCallHistory() {
|
||||
return (List<Message>) this.context.get(TOOL_CALL_HISTORY);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user