|
6 | 6 | import dev.langchain4j.data.message.SystemMessage;
|
7 | 7 | import dev.langchain4j.data.message.UserMessage;
|
8 | 8 | import dev.langchain4j.data.message.ToolExecutionResultMessage;
|
| 9 | + |
9 | 10 | import dev.langchain4j.model.chat.ChatLanguageModel;
|
10 | 11 | import dev.langchain4j.model.input.PromptTemplate;
|
11 | 12 | import dev.langchain4j.model.output.Response;
|
|
16 | 17 | import java.util.List;
|
17 | 18 | import java.util.Map;
|
18 | 19 |
|
| 20 | +import static java.lang.String.format; |
| 21 | + |
19 | 22 | @Builder
|
20 | 23 | public class Agent {
|
21 | 24 |
|
22 | 25 | private final ChatLanguageModel chatLanguageModel;
|
23 | 26 | @Singular private final List<ToolSpecification> tools;
|
24 | 27 |
|
25 | 28 |
|
26 |
| - public Response<AiMessage> execute( Map<String,Object> inputs ) { |
27 |
| - var messages = new ArrayList<ChatMessage>(); |
28 |
| - var promptTemplate = PromptTemplate.from( "USER: {{input}}" ).apply(inputs); |
29 |
| - |
30 |
| - messages.add(new SystemMessage("You are a helpful assistant")); |
31 |
| - |
32 |
| - messages.add( new UserMessage(promptTemplate.text()) ); |
33 |
| - |
34 |
| - return chatLanguageModel.generate( messages, tools ); |
35 |
| - } |
36 |
| - |
37 |
| - private PromptTemplate getToolResponseTemplate( ) { |
38 |
| - var TEMPLATE_TOOL_RESPONSE = """ |
39 |
| - TOOL RESPONSE: |
40 |
| - --------------------- |
41 |
| - {{observation}} |
42 |
| - -------------------- |
43 |
| - """; |
44 |
| - return PromptTemplate.from(TEMPLATE_TOOL_RESPONSE); |
45 |
| - } |
46 |
| - |
47 | 29 | public Response<AiMessage> execute( String input, List<AgentExecutor.IntermediateStep> intermediateSteps ) {
|
48 |
| - var agentScratchpadTemplate = getToolResponseTemplate(); |
49 |
| - var userMessageTemplate = PromptTemplate.from( "USER'S INPUT: {{input}}" ) |
| 30 | + var userMessageTemplate = PromptTemplate.from( "{{input}}" ) |
50 | 31 | .apply( Map.of( "input", input));
|
51 | 32 |
|
52 | 33 | var messages = new ArrayList<ChatMessage>();
|
53 | 34 |
|
54 | 35 | messages.add(new SystemMessage("You are a helpful assistant"));
|
| 36 | + messages.add(new UserMessage(userMessageTemplate.text())); |
55 | 37 |
|
56 |
| - if( intermediateSteps.isEmpty()) { |
57 |
| - messages.add(new UserMessage(userMessageTemplate.text())); |
58 |
| - } |
| 38 | + if (!intermediateSteps.isEmpty()) { |
59 | 39 |
|
60 |
| - for( AgentExecutor.IntermediateStep step: intermediateSteps ) { |
61 |
| - var agentScratchpad = agentScratchpadTemplate |
62 |
| - .apply( Map.of("observation", step.observation()) ); |
63 |
| - messages.add(new UserMessage(agentScratchpad.text())); |
64 |
| - } |
| 40 | + var toolRequests = intermediateSteps.stream() |
| 41 | + .map(AgentExecutor.IntermediateStep::action) |
| 42 | + .map(AgentExecutor.AgentAction::toolExecutionRequest) |
| 43 | + .toList(); |
| 44 | + |
| 45 | + messages.add(new AiMessage(toolRequests)); // reply with tool requests |
65 | 46 |
|
| 47 | + for (AgentExecutor.IntermediateStep step : intermediateSteps) { |
| 48 | + var toolRequest = step.action().toolExecutionRequest(); |
| 49 | + |
| 50 | + messages.add(new ToolExecutionResultMessage(toolRequest.id(), toolRequest.name(), step.observation())); |
| 51 | + } |
| 52 | + } |
66 | 53 | return chatLanguageModel.generate( messages, tools );
|
67 | 54 | }
|
68 | 55 | }
|
0 commit comments