diff --git a/Partner-Api/src/main/java/work/slhaf/partner/api/chat/ChatClient.java b/Partner-Api/src/main/java/work/slhaf/partner/api/chat/ChatClient.java index ac23418e..30cdee08 100644 --- a/Partner-Api/src/main/java/work/slhaf/partner/api/chat/ChatClient.java +++ b/Partner-Api/src/main/java/work/slhaf/partner/api/chat/ChatClient.java @@ -1,10 +1,12 @@ package work.slhaf.partner.api.chat; +import cn.hutool.core.io.IORuntimeException; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import cn.hutool.json.JSONUtil; import lombok.Data; import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; import work.slhaf.partner.api.chat.constant.ChatConstant; import work.slhaf.partner.api.chat.pojo.ChatBody; import work.slhaf.partner.api.chat.pojo.ChatResponse; @@ -13,6 +15,7 @@ import work.slhaf.partner.api.chat.pojo.PrimaryChatResponse; import java.util.List; +@Slf4j @Data @NoArgsConstructor public class ChatClient { @@ -34,6 +37,8 @@ public class ChatClient { public ChatResponse runChat(List messages) { HttpRequest request = HttpRequest.post(url); + request.setConnectionTimeout(2000); + request.setReadTimeout(15000); request.header("Content-Type", "application/json"); request.header("Authorization", "Bearer " + apikey); @@ -53,17 +58,26 @@ public class ChatClient { .build(); } - HttpResponse response = request.body(JSONUtil.toJsonStr(body)).execute(); ChatResponse finalResponse; - PrimaryChatResponse primaryChatResponse = JSONUtil.toBean(response.body(), PrimaryChatResponse.class); - finalResponse = ChatResponse.builder() - .type(ChatConstant.Response.SUCCESS) - .message(primaryChatResponse.getChoices().get(0).getMessage().getContent()) - .usageBean(primaryChatResponse.getUsage()) - .build(); + try { + HttpResponse response = request.body(JSONUtil.toJsonStr(body)).execute(); + PrimaryChatResponse primaryChatResponse = JSONUtil.toBean(response.body(), PrimaryChatResponse.class); + finalResponse = ChatResponse.builder() + .status(ChatConstant.ResponseStatus.SUCCESS) + .message(primaryChatResponse.getChoices().get(0).getMessage().getContent()) + .usageBean(primaryChatResponse.getUsage()) + .build(); - response.close(); + response.close(); + } catch (IORuntimeException e) { + log.error("请求超时", e); + finalResponse = ChatResponse.builder() + .message("连接超时") + .status(ChatConstant.ResponseStatus.FAILED) + .usageBean(null) + .build(); + } return finalResponse; } diff --git a/Partner-Api/src/main/java/work/slhaf/partner/api/chat/constant/ChatConstant.java b/Partner-Api/src/main/java/work/slhaf/partner/api/chat/constant/ChatConstant.java index eaa3be2d..c805e40b 100644 --- a/Partner-Api/src/main/java/work/slhaf/partner/api/chat/constant/ChatConstant.java +++ b/Partner-Api/src/main/java/work/slhaf/partner/api/chat/constant/ChatConstant.java @@ -8,8 +8,7 @@ public class ChatConstant { public static final String ASSISTANT = "assistant"; } - public static class Response { - public static final String SUCCESS = "success"; - public static final String ERROR = "error"; + public enum ResponseStatus { + SUCCESS, FAILED } } diff --git a/Partner-Api/src/main/java/work/slhaf/partner/api/chat/pojo/ChatResponse.java b/Partner-Api/src/main/java/work/slhaf/partner/api/chat/pojo/ChatResponse.java index 42f12106..77183f91 100644 --- a/Partner-Api/src/main/java/work/slhaf/partner/api/chat/pojo/ChatResponse.java +++ b/Partner-Api/src/main/java/work/slhaf/partner/api/chat/pojo/ChatResponse.java @@ -4,13 +4,14 @@ import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import work.slhaf.partner.api.chat.constant.ChatConstant; @Data @Builder @AllArgsConstructor @NoArgsConstructor public class ChatResponse { - private String type; + private ChatConstant.ResponseStatus status; private String message; private PrimaryChatResponse.UsageBean usageBean; } diff --git a/Partner-Main/src/main/java/work/slhaf/partner/core/action/ActionCapability.java b/Partner-Main/src/main/java/work/slhaf/partner/core/action/ActionCapability.java index 5acccffa..90aa7d7e 100644 --- a/Partner-Main/src/main/java/work/slhaf/partner/core/action/ActionCapability.java +++ b/Partner-Main/src/main/java/work/slhaf/partner/core/action/ActionCapability.java @@ -35,6 +35,8 @@ public interface ActionCapability { void removePhaserRecord(Phaser phaser); + List listPhaserRecords(); + ActionCore.PhaserRecord getPhaserRecord(String tendency, String source); MetaAction loadMetaAction(@NonNull String actionKey); diff --git a/Partner-Main/src/main/java/work/slhaf/partner/core/action/ActionCore.java b/Partner-Main/src/main/java/work/slhaf/partner/core/action/ActionCore.java index da7a269d..9bcedec0 100644 --- a/Partner-Main/src/main/java/work/slhaf/partner/core/action/ActionCore.java +++ b/Partner-Main/src/main/java/work/slhaf/partner/core/action/ActionCore.java @@ -222,6 +222,11 @@ public class ActionCore extends PartnerCore { throw new MetaActionNotFoundException("未找到对应的行动程序信息" + actionKey); } + @CapabilityMethod + public List listPhaserRecords() { + return phaserRecords; + } + /** * 命中缓存且评估通过时 * diff --git a/Partner-Main/src/main/java/work/slhaf/partner/module/common/module/PostRunningModule.java b/Partner-Main/src/main/java/work/slhaf/partner/module/common/module/PostRunningModule.java index f3c5f01f..dacd83fb 100644 --- a/Partner-Main/src/main/java/work/slhaf/partner/module/common/module/PostRunningModule.java +++ b/Partner-Main/src/main/java/work/slhaf/partner/module/common/module/PostRunningModule.java @@ -8,7 +8,7 @@ public abstract class PostRunningModule extends AgentRunningModule getPromptDataMap(PartnerRunningFlowContext context) { + return null; } diff --git a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/evaluator/entity/EvaluatorInput.java b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/evaluator/entity/EvaluatorInput.java index 170138f6..9a80793d 100644 --- a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/evaluator/entity/EvaluatorInput.java +++ b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/evaluator/entity/EvaluatorInput.java @@ -1,7 +1,14 @@ package work.slhaf.partner.module.modules.action.identifier.evaluator.entity; import lombok.Data; +import work.slhaf.partner.api.chat.pojo.Message; +import work.slhaf.partner.core.memory.pojo.EvaluatedSlice; + +import java.util.List; @Data public class EvaluatorInput { + private String interventionTendency; + private List activatedSlices; + private List recentMessages; } diff --git a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/recognizer/InterventionRecognizer.java b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/recognizer/InterventionRecognizer.java index d68a2619..1420ec5b 100644 --- a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/recognizer/InterventionRecognizer.java +++ b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/recognizer/InterventionRecognizer.java @@ -1,18 +1,82 @@ package work.slhaf.partner.module.modules.action.identifier.recognizer; +import com.alibaba.fastjson2.JSONObject; +import lombok.extern.slf4j.Slf4j; +import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability; import work.slhaf.partner.api.agent.factory.module.annotation.AgentSubModule; import work.slhaf.partner.api.agent.runtime.interaction.flow.abstracts.ActivateModel; import work.slhaf.partner.api.agent.runtime.interaction.flow.abstracts.AgentRunningSubModule; -import work.slhaf.partner.module.modules.action.identifier.recognizer.entity.InterventionResult; +import work.slhaf.partner.api.chat.pojo.ChatResponse; +import work.slhaf.partner.core.action.ActionCapability; +import work.slhaf.partner.core.action.ActionCore; +import work.slhaf.partner.core.action.entity.ActionData; +import work.slhaf.partner.module.modules.action.identifier.recognizer.entity.MetaRecognizerResult; +import work.slhaf.partner.module.modules.action.identifier.recognizer.entity.RecognizerInput; +import work.slhaf.partner.module.modules.action.identifier.recognizer.entity.RecognizerResult; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; + +@Slf4j @AgentSubModule -public class InterventionRecognizer extends AgentRunningSubModule implements ActivateModel { +public class InterventionRecognizer extends AgentRunningSubModule implements ActivateModel { + + @InjectCapability + private ActionCapability actionCapability; @Override - public InterventionResult execute(String data) { + public RecognizerResult execute(RecognizerInput input) { //使用LLM进行快速意图识别 + ExecutorService executor = actionCapability.getExecutor(ActionCore.ExecutorType.VIRTUAL); + RecognizerResult recognizerResult = new RecognizerResult(); + Map resultInterventionMap = recognizerResult.getInterventions(); + List executingActions = input.getExecutingActions(); + CountDownLatch countDownLatch = new CountDownLatch(executingActions.size()); + for (ActionCore.PhaserRecord record : executingActions) { + executor.execute(() -> { + try { + String prompt = buildPrompt(record, input); + ChatResponse response = this.singleChat(prompt); + MetaRecognizerResult result = JSONObject.parseObject(response.getMessage(), MetaRecognizerResult.class); + if (result.isOk()) { + synchronized (resultInterventionMap) { + resultInterventionMap.put(result.getIntervention(), record); + } + } + } catch (Exception e) { + log.error("LLM干预意图提取出错", e); + } finally { + countDownLatch.countDown(); + } + }); + } + try { + countDownLatch.await(); + } catch (InterruptedException e) { + log.warn("CountDownLatch阻塞已中断"); + } + return recognizerResult; + } - return null; + private String buildPrompt(ActionCore.PhaserRecord record, RecognizerInput input) { + ActionData actionData = record.actionData(); + JSONObject json = new JSONObject(); + + JSONObject actionInfo = json.putObject("行动信息"); + actionInfo.put("行动倾向", actionData.getStatus()); + actionInfo.put("行动原因", actionData.getReason()); + actionInfo.put("行动描述", actionData.getDescription()); + actionInfo.put("行动状态", actionData.getStatus()); + actionInfo.put("行动来源", actionData.getSource()); + + JSONObject interactionInfo = json.putObject("交互信息"); + interactionInfo.put("用户输入", input.getInput()); + interactionInfo.put("当前对话", input.getRecentMessages()); + interactionInfo.put("近期对话", input.getUserDialogMapStr()); + + return json.toString(); } @Override diff --git a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/recognizer/entity/InterventionResult.java b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/recognizer/entity/MetaRecognizerResult.java similarity index 51% rename from Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/recognizer/entity/InterventionResult.java rename to Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/recognizer/entity/MetaRecognizerResult.java index 017e73ee..d3d963dd 100644 --- a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/recognizer/entity/InterventionResult.java +++ b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/recognizer/entity/MetaRecognizerResult.java @@ -3,7 +3,7 @@ package work.slhaf.partner.module.modules.action.identifier.recognizer.entity; import lombok.Data; @Data -public class InterventionResult { - private boolean result; - private String interventionTendency; +public class MetaRecognizerResult { + private boolean ok; + private String intervention; } diff --git a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/recognizer/entity/RecognizerInput.java b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/recognizer/entity/RecognizerInput.java new file mode 100644 index 00000000..f4216adf --- /dev/null +++ b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/recognizer/entity/RecognizerInput.java @@ -0,0 +1,21 @@ +package work.slhaf.partner.module.modules.action.identifier.recognizer.entity; + +import lombok.Data; +import work.slhaf.partner.api.chat.pojo.Message; +import work.slhaf.partner.core.action.ActionCore; + +import java.util.List; + +@Data +public class RecognizerInput { + private String input; + private List recentMessages; + /** + * 当前用户对应的近两日对话缓存 + */ + private String userDialogMapStr; + /** + * 正在执行的行动-Phaser记录列表,在Recognizer中结合本次输入并发评估(考虑到不同行动链之间对LLM的影响) + */ + private List executingActions; +} diff --git a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/recognizer/entity/RecognizerResult.java b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/recognizer/entity/RecognizerResult.java new file mode 100644 index 00000000..384d0ed0 --- /dev/null +++ b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/recognizer/entity/RecognizerResult.java @@ -0,0 +1,21 @@ +package work.slhaf.partner.module.modules.action.identifier.recognizer.entity; + +import lombok.Data; +import work.slhaf.partner.core.action.ActionCore; + +import java.util.HashMap; +import java.util.Map; + +@Data +public class RecognizerResult { + + private boolean ok; + + /** + * key: 干预倾向 + *
+ * value: 干预倾向将作用的 phaser 记录 + */ + private Map interventions = new HashMap<>(); + +} diff --git a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/planner/confirmer/ActionConfirmer.java b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/planner/confirmer/ActionConfirmer.java index 4dbb5a2b..30df6471 100644 --- a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/planner/confirmer/ActionConfirmer.java +++ b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/planner/confirmer/ActionConfirmer.java @@ -10,6 +10,7 @@ import work.slhaf.partner.module.modules.action.planner.confirmer.entity.Confirm public class ActionConfirmer extends AgentRunningSubModule implements ActivateModel { @Override public ConfirmerResult execute(ConfirmerInput data) { + //TODO 完善确认逻辑 return null; } diff --git a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/memory/updater/MemoryUpdater.java b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/memory/updater/MemoryUpdater.java index 4b5d1eef..616c1bf8 100644 --- a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/memory/updater/MemoryUpdater.java +++ b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/memory/updater/MemoryUpdater.java @@ -119,6 +119,11 @@ public class MemoryUpdater extends PostRunningModule { }); } + @Override + protected boolean relyOnMessage() { + return true; + } + private void updateMemory() { log.debug("[MemoryUpdater] 记忆更新流程开始..."); tempMessage = new ArrayList<>(cognationCapability.getChatMessages()); diff --git a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/perceive/updater/PerceiveUpdater.java b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/perceive/updater/PerceiveUpdater.java index 206d6798..649c71d9 100644 --- a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/perceive/updater/PerceiveUpdater.java +++ b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/perceive/updater/PerceiveUpdater.java @@ -70,6 +70,11 @@ public class PerceiveUpdater extends PostRunningModule { }); } + @Override + protected boolean relyOnMessage() { + return true; + } + private void runRelationExtractorAction(PartnerRunningFlowContext context, ReentrantLock userLock, User user) { RelationExtractResult relationExtractResult = relationExtractor.execute(context); userLock.lock(); diff --git a/Partner-Main/src/test/java/NetTest.java b/Partner-Main/src/test/java/NetTest.java new file mode 100644 index 00000000..0fbb87ea --- /dev/null +++ b/Partner-Main/src/test/java/NetTest.java @@ -0,0 +1,15 @@ +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import org.junit.jupiter.api.Test; + +public class NetTest { + @Test + void httpTest() { + HttpRequest request = HttpRequest.get("slhaf.work"); + request.setConnectionTimeout(2); + request.setReadTimeout(2); + HttpResponse execute = request.execute(); + System.out.println(execute.toString()); + execute.close(); + } +}