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 com.xly.tool.ProposeWriteTool; 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)。 * *
= 流式 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) // qwen3 支持「思考」模式,但会显著拖慢交互;关闭它 -> 快,且思考不会混进回答 .think(false) .returnThinking(false) .timeout(Duration.ofSeconds(180)) .build(); } @Bean public ReActAgent reActAgent(SystemPromptService systemPromptService, KgQueryTool kgQueryTool, ErpReadTool erpReadTool, ProposeWriteTool proposeWriteTool, RedisChatMemoryStore memoryStore) { String systemPrompt = systemPromptService.buildSystemPrompt(); return AiServices.builder(ReActAgent.class) .streamingChatModel(agentStreamingModel()) .tools(kgQueryTool, erpReadTool, proposeWriteTool) .chatMemoryProvider(memoryId -> MessageWindowChatMemory.builder() .id(memoryId) .maxMessages(30) .chatMemoryStore(memoryStore) .build()) .systemMessageProvider(memoryId -> systemPrompt) .build(); } }