AgentConfig.java
2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.xly.config;
import com.xly.agent.ReActAgent;
import com.xly.service.SystemPromptService;
import com.xly.tool.ErpReadTool;
import com.xly.tool.KgQueryTool;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.ollama.OllamaStreamingChatModel;
import dev.langchain4j.service.AiServices;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.Duration;
/**
* 组装单一 ReAct agent(M1)。
*
* <p>= 流式 Ollama 模型 + 通用工具(当前只有 {@link KgQueryTool})+ 每会话对话记忆
* + 注入 L1 域图的 system prompt。取代旧的「每表单一个 ToolMeta 工具 + 8 场景路由」。
*/
@Configuration
public class AgentConfig {
@Value("${langchain4j.ollama.base-url}")
private String ollamaUrl;
@Value("${langchain4j.ollama.chat-model-name}")
private String chatModelName;
/** 专供 agent 的流式模型:低温度利于稳定的工具调用,较大 numPredict 避免答复被截断。 */
@Bean("agentStreamingModel")
public OllamaStreamingChatModel agentStreamingModel() {
return OllamaStreamingChatModel.builder()
.baseUrl(ollamaUrl)
.modelName(chatModelName)
.temperature(0.1)
.topP(0.9)
.numPredict(2048)
.timeout(Duration.ofSeconds(180))
.build();
}
@Bean
public ReActAgent reActAgent(SystemPromptService systemPromptService,
KgQueryTool kgQueryTool,
ErpReadTool erpReadTool) {
String systemPrompt = systemPromptService.buildSystemPrompt();
return AiServices.builder(ReActAgent.class)
.streamingChatModel(agentStreamingModel())
.tools(kgQueryTool, erpReadTool)
.chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(20))
.systemMessageProvider(memoryId -> systemPrompt)
.build();
}
}