Bladeren bron

修复 计划执行第二步骤 无法生成执行内容的问题

alibct 1 maand geleden
bovenliggende
commit
7044d78328

+ 47 - 15
pavis-module-aigc/src/main/java/com/pavis/admin/aigc/core/agent/PavisAgent.java

@@ -10,12 +10,15 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.ai.chat.memory.ChatMemory;
 import org.springframework.ai.chat.messages.AssistantMessage;
 import org.springframework.ai.chat.messages.Message;
+import org.springframework.ai.chat.messages.ToolResponseMessage;
 import org.springframework.ai.chat.model.ChatResponse;
 import org.springframework.ai.chat.prompt.ChatOptions;
 import org.springframework.ai.chat.prompt.Prompt;
 import org.springframework.ai.chat.prompt.PromptTemplate;
 import org.springframework.ai.chat.prompt.SystemPromptTemplate;
 import org.springframework.ai.model.tool.ToolCallingChatOptions;
+import org.springframework.ai.model.tool.ToolCallingManager;
+import org.springframework.ai.model.tool.ToolExecutionResult;
 import org.springframework.ai.tool.ToolCallback;
 import org.springframework.ai.tool.metadata.ToolMetadata;
 
@@ -62,18 +65,20 @@ public class PavisAgent extends ReActAgent {
      * 用于获取所有工具集
      */
     private ToolService toolService;
-    ;
+
+    private final ToolCallingManager toolCallingManager;
 
     /**
      * 构造函数
      *
      * @param llmService LLM服务实例,用于处理自然语言交互
      */
-    public PavisAgent(LlmService llmService, Long planId, AgentResp agent, ToolService toolService) {
+    public PavisAgent(LlmService llmService, Long planId, AgentResp agent, ToolService toolService, ToolCallingManager toolCallingManager) {
         super(llmService);
         this.planId = planId;
         this.agent = agent;
         this.toolService = toolService;
+        this.toolCallingManager = toolCallingManager;
     }
 
     @Override
@@ -82,24 +87,32 @@ public class PavisAgent extends ReActAgent {
         try {
             List<Message> messages = new ArrayList<>();
             addThinkPrompt(messages);
-            log.info("start thinking....");
-            ChatOptions chatOptions = ToolCallingChatOptions.builder().internalToolExecutionEnabled(false).build();
+
             Message nextStepMessage = getNextStepWithEnvMessage();
             messages.add(nextStepMessage);
             reActRecord.startThinking(messages.toString());
 
             log.debug("Messages prepared for the prompt: {}", messages);
 
+            ChatOptions chatOptions = ToolCallingChatOptions
+                    .builder()
+                    .toolCallbacks(getToolCallList())
+                    .internalToolExecutionEnabled(false)
+                    .build();
+
             userPrompt = new Prompt(messages, chatOptions);
 
+            log.info("prompt:{}", userPrompt.getContents());
+
             response = llmService.getAgentChatClient(String.valueOf(planId))
                     .getChatClient()
                     .prompt(userPrompt)
                     .advisors(memoryAdvisor -> memoryAdvisor.param(ChatMemory.CONVERSATION_ID, String.valueOf(planId)))
-                    .toolCallbacks(getToolCallList())
                     .call()
                     .chatResponse();
 
+            log.info("end calling...");
+
             String responseByLLm = response.getResult().getOutput().getText();
             if (responseByLLm != null && !responseByLLm.isEmpty()) {
                 log.info(String.format("💬 %s's response: %s", getName(), responseByLLm));
@@ -110,19 +123,38 @@ public class PavisAgent extends ReActAgent {
             log.info(String.format("✨ %s's thoughts: %s", getName(), responseByLLm));
             log.info(String.format("🛠️ %s selected %d tools to use", getName(), toolCalls.size()));
 
+            // 新增逻辑:如果有 tool calls,构造 tool responses 并添加到 messages 中
             if (!toolCalls.isEmpty()) {
-                log.info(String.format("🧰 Tools being prepared: %s",
-                        toolCalls.stream().map(AssistantMessage.ToolCall::name).collect(Collectors.toList())));
-                reActRecord.setExecutionNeeded(true);
-
-                // 修复点:处理所有工具调用,不只是第一个
-                List<ReActToolExecute> toolExecutes = new ArrayList<>();
-                for (AssistantMessage.ToolCall toolCall : toolCalls) {
-                    toolExecutes.add(new ReActToolExecute(toolCall.name(), toolCall.arguments()));
+
+                ToolExecutionResult toolExecutionResult = toolCallingManager.executeToolCalls(userPrompt, response);
+
+                ToolResponseMessage toolResponseMessage = (ToolResponseMessage) toolExecutionResult.conversationHistory()
+                        .get(toolExecutionResult.conversationHistory().size() - 1);
+
+                log.info("re-prompt:{}", userPrompt.getContents());
+
+                llmService.getAgentChatClient(String.valueOf(planId)).getMemory().add(String.valueOf(planId), toolResponseMessage);
+                String llmCallResponse = toolResponseMessage.getResponses().get(0).responseData();
+
+                log.info("end re-calling...");
+
+                if (!llmCallResponse.isEmpty()) {
+                    log.info(String.format("💬 %s's response after tool responses: %s", getName(), llmCallResponse));
+                    log.info(String.format("🧰 Tools being prepared: %s",
+                            toolCalls.stream().map(AssistantMessage.ToolCall::name).collect(Collectors.toList())));
+
+                    reActRecord.setExecutionNeeded(true);
+
+                    // 修复点:处理所有工具调用,不只是第一个
+                    List<ReActToolExecute> toolExecutes = new ArrayList<>();
+                    for (AssistantMessage.ToolCall toolCall : toolCalls) {
+                        toolExecutes.add(new ReActToolExecute(toolCall.name(), toolCall.arguments()));
+                    }
+                    reActRecord.setToolExecutes(toolExecutes);
+
+                    responseByLLm = String.format("Preparing to execute %d tools", toolCalls.size());
                 }
-                reActRecord.setToolExecutes(toolExecutes);
 
-                responseByLLm = String.format("Preparing to execute %d tools", toolCalls.size());
             }
             reActRecord.setStatus("SUCCESS");
             reActRecord.finishThinking(responseByLLm != null ? responseByLLm : "No response from LLM");

+ 4 - 19
pavis-module-aigc/src/main/java/com/pavis/admin/aigc/model/req/ModelReq.java

@@ -1,15 +1,12 @@
 package com.pavis.admin.aigc.model.req;
 
-import jakarta.validation.constraints.*;
-
-import lombok.Data;
-
 import io.swagger.v3.oas.annotations.media.Schema;
-
+import jakarta.validation.constraints.NotBlank;
+import lombok.Data;
 import org.hibernate.validator.constraints.Length;
+
 import java.io.Serial;
 import java.io.Serializable;
-import java.time.*;
 
 /**
  * AIGC模型配置创建或修改参数
@@ -101,18 +98,6 @@ public class ModelReq implements Serializable {
     /**
      * 向量维度
      */
+    @Schema(description = "向量维度")
     private Integer dimension;
-    // /**
-    //  * 创建人
-    //  */
-    // @Schema(description = "创建人")
-    // @NotNull(message = "创建人不能为空")
-    // private Long createUser;
-    //
-    // /**
-    //  * 创建时间
-    //  */
-    // @Schema(description = "创建时间")
-    // @NotNull(message = "创建时间不能为空")
-    // private LocalDateTime createTime;
 }

+ 4 - 1
pavis-module-aigc/src/main/java/com/pavis/admin/aigc/service/impl/AgentPlanServiceImpl.java

@@ -29,6 +29,7 @@ import org.springframework.ai.chat.client.ChatClient;
 import org.springframework.ai.chat.memory.ChatMemory;
 import org.springframework.ai.chat.prompt.Prompt;
 import org.springframework.ai.chat.prompt.PromptTemplate;
+import org.springframework.ai.model.tool.ToolCallingManager;
 import org.springframework.stereotype.Service;
 import top.continew.starter.extension.crud.model.query.SortQuery;
 import top.continew.starter.extension.crud.service.BaseServiceImpl;
@@ -56,6 +57,8 @@ public class AgentPlanServiceImpl extends BaseServiceImpl<AgentPlanMapper, Agent
 
     private final AgentPlanStepService agentPlanStepService;
 
+    private final ToolCallingManager toolCallingManager;
+
     @Override
     public PlanContext createPlan(AgentPlanReq req) {
         // 获取userId
@@ -156,7 +159,7 @@ public class AgentPlanServiceImpl extends BaseServiceImpl<AgentPlanMapper, Agent
         }
         agent.setTools(toolService.selectByAgentId(agent.getId()));
         // 创建派维斯智能体
-        PavisAgent pavisAgent = new PavisAgent(llmService, context.getPlanId(), agent, toolService);
+        PavisAgent pavisAgent = new PavisAgent(llmService, context.getPlanId(), agent, toolService, toolCallingManager);
         // 获取当前执行的计划步骤的序号
         int currentPlanStepIndex = currentPlanStep.getStepIndex();
         // 设置执行当前计划步骤的智能体信息