mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
refactor(chat): replace custom client with OpenAI runtime and remove file-based module prompt loading logic, prompt will be provided by each module
This commit is contained in:
@@ -4,9 +4,13 @@ import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.val;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
|
||||
import work.slhaf.partner.api.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.module.modules.action.executor.entity.CorrectorInput;
|
||||
import work.slhaf.partner.module.modules.action.executor.entity.CorrectorResult;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 负责在单组行动执行后,根据行动意图与结果检查后续行动是否符合目的,必要时直接调整行动链,或发起自对话请求进行干预
|
||||
*/
|
||||
@@ -14,8 +18,7 @@ public class ActionCorrector extends AbstractAgentModule.Sub<CorrectorInput, Cor
|
||||
@Override
|
||||
public CorrectorResult execute(CorrectorInput input) {
|
||||
val prompt = buildPrompt(input);
|
||||
val chatResponse = singleChat(prompt);
|
||||
return JSONObject.parseObject(chatResponse.getMessage(), CorrectorResult.class);
|
||||
return formattedChat(List.of(new Message(ChatConstant.Character.USER, prompt)), CorrectorResult.class);
|
||||
}
|
||||
|
||||
private String buildPrompt(CorrectorInput input) {
|
||||
@@ -37,9 +40,4 @@ public class ActionCorrector extends AbstractAgentModule.Sub<CorrectorInput, Cor
|
||||
public String modelKey() {
|
||||
return "action_corrector";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean withBasicPrompt() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,8 @@ import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentMod
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
|
||||
import work.slhaf.partner.api.agent.factory.component.annotation.Init;
|
||||
import work.slhaf.partner.api.agent.factory.component.annotation.InjectModule;
|
||||
import work.slhaf.partner.api.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.api.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.action.ActionCapability;
|
||||
import work.slhaf.partner.core.action.ActionCore.ExecutorType;
|
||||
import work.slhaf.partner.core.action.entity.MetaAction;
|
||||
@@ -61,8 +62,10 @@ public class ActionRepairer extends AbstractAgentModule.Sub<RepairerInput, Repai
|
||||
RepairerResult result;
|
||||
try {
|
||||
String prompt = assemblyHelper.buildPrompt(data, null);
|
||||
ChatResponse response = this.singleChat(prompt);
|
||||
RepairerData repairerData = JSONObject.parseObject(response.getMessage(), RepairerData.class);
|
||||
RepairerData repairerData = formattedChat(
|
||||
List.of(new Message(ChatConstant.Character.USER, prompt)),
|
||||
RepairerData.class
|
||||
);
|
||||
result = switch (repairerData.getRepairerType()) {
|
||||
case ACTION_GENERATION ->
|
||||
handleActionGeneration(JSONObject.parseObject(repairerData.getData(), GeneratorInput.class));
|
||||
@@ -75,8 +78,10 @@ public class ActionRepairer extends AbstractAgentModule.Sub<RepairerInput, Repai
|
||||
&& result.getStatus().equals(RepairerResult.RepairerStatus.FAILED)) {
|
||||
log.warn("常规行动修复失败,将尝试自对话通道");
|
||||
prompt = assemblyHelper.buildPrompt(data, "常规行动修复失败,请尝试通过自对话通道获取必要的信息以完成行动参数的修复");
|
||||
response = this.singleChat(prompt);
|
||||
repairerData = JSONObject.parseObject(response.getMessage(), RepairerData.class);
|
||||
repairerData = formattedChat(
|
||||
List.of(new Message(ChatConstant.Character.USER, prompt)),
|
||||
RepairerData.class
|
||||
);
|
||||
handleUserInteraction(repairerData.getData());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -165,20 +170,14 @@ public class ActionRepairer extends AbstractAgentModule.Sub<RepairerInput, Repai
|
||||
return "action_repairer";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean withBasicPrompt() {
|
||||
return false;
|
||||
}
|
||||
|
||||
private enum RepairerType {
|
||||
ACTION_GENERATION,
|
||||
ACTION_INVOCATION,
|
||||
USER_INTERACTION
|
||||
}
|
||||
|
||||
@SuppressWarnings("InnerClassMayBeStatic")
|
||||
@Data
|
||||
private class RepairerData {
|
||||
private static class RepairerData {
|
||||
private RepairerType repairerType;
|
||||
private String data;
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapabili
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
|
||||
import work.slhaf.partner.api.agent.factory.component.annotation.Init;
|
||||
import work.slhaf.partner.api.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.common.util.ExtractUtil;
|
||||
import work.slhaf.partner.api.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.action.ActionCapability;
|
||||
import work.slhaf.partner.core.action.entity.GeneratedData;
|
||||
import work.slhaf.partner.core.action.entity.MetaAction;
|
||||
@@ -15,6 +15,8 @@ import work.slhaf.partner.core.action.runner.RunnerClient;
|
||||
import work.slhaf.partner.module.modules.action.executor.entity.GeneratorInput;
|
||||
import work.slhaf.partner.module.modules.action.executor.entity.GeneratorResult;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 负责依据输入内容生成可执行的动态行动单元,并选择是否持久化至 SandboxRunner 容器内
|
||||
*/
|
||||
@@ -37,9 +39,10 @@ public class DynamicActionGenerator extends AbstractAgentModule.Sub<GeneratorInp
|
||||
// 所以此处的输入内容也只需要指定输入参数、临时key、是否持久化即可,路径将按照指定规则统一构建,不可交给LLM生成
|
||||
String prompt = buildPrompt(input);
|
||||
// 响应结果需要包含几个特殊数据: 依赖项、代码内容、是否序列化、响应数据释义
|
||||
ChatResponse response = this.singleChat(prompt);
|
||||
GeneratedData generatorData = JSONObject
|
||||
.parseObject(ExtractUtil.extractJson(response.getMessage()), GeneratedData.class);
|
||||
GeneratedData generatorData = formattedChat(
|
||||
List.of(new Message(ChatConstant.Character.USER, prompt)),
|
||||
GeneratedData.class
|
||||
);
|
||||
val location = runnerClient.buildTmpPath(input.getActionName(), generatorData.getCodeType());
|
||||
MetaAction tempAction = new MetaAction(
|
||||
input.getActionName(),
|
||||
@@ -77,9 +80,4 @@ public class DynamicActionGenerator extends AbstractAgentModule.Sub<GeneratorInp
|
||||
public String modelKey() {
|
||||
return "dynamic_generator";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean withBasicPrompt() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,8 @@ import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
|
||||
import work.slhaf.partner.api.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.api.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.action.entity.MetaActionInfo;
|
||||
import work.slhaf.partner.module.modules.action.executor.entity.ExtractorInput;
|
||||
import work.slhaf.partner.module.modules.action.executor.entity.ExtractorResult;
|
||||
@@ -20,12 +21,11 @@ public class ParamsExtractor extends AbstractAgentModule.Sub<ExtractorInput, Ext
|
||||
@Override
|
||||
public ExtractorResult execute(ExtractorInput input) {
|
||||
String prompt = buildPrompt(input);
|
||||
ChatResponse response = this.singleChat(prompt);
|
||||
ExtractorResult result;
|
||||
try {
|
||||
result = JSONObject.parseObject(response.getMessage(), ExtractorResult.class);
|
||||
result = formattedChat(List.of(new Message(ChatConstant.Character.USER, prompt)), ExtractorResult.class);
|
||||
} catch (Exception e) {
|
||||
log.error("ParamsExtractor解析结果失败,返回内容:{}", response.getMessage(), e);
|
||||
log.error("ParamsExtractor解析结果失败", e);
|
||||
result = new ExtractorResult();
|
||||
result.setOk(false);
|
||||
result.setParams(new HashMap<>());
|
||||
@@ -57,9 +57,4 @@ public class ParamsExtractor extends AbstractAgentModule.Sub<ExtractorInput, Ext
|
||||
public String modelKey() {
|
||||
return "params_extractor";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean withBasicPrompt() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,11 +134,6 @@ public class ActionInterventor extends PreRunningAbstractAgentModuleAbstract imp
|
||||
return "action_identifier";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean withBasicPrompt() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, String> getPromptDataMap(PartnerRunningFlowContext context) {
|
||||
return interventionPrompt.remove(context.getInfo().getUuid());
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.alibaba.fastjson2.JSONObject;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
|
||||
import work.slhaf.partner.api.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.api.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.action.ActionCapability;
|
||||
import work.slhaf.partner.core.action.ActionCore.ExecutorType;
|
||||
@@ -53,8 +53,8 @@ public class InterventionEvaluator extends AbstractAgentModule.Sub<EvaluatorInpu
|
||||
interventionMap.forEach((tendency, actionData) -> executor.execute(() -> {
|
||||
try {
|
||||
String prompt = buildPrompt(input.getRecentMessages(), input.getActivatedSlices(), actionData, tendency);
|
||||
ChatResponse response = this.singleChat(prompt);
|
||||
EvaluatedInterventionData evaluatedData = JSONObject.parseObject(response.getMessage(),
|
||||
EvaluatedInterventionData evaluatedData = formattedChat(
|
||||
List.of(new Message(ChatConstant.Character.USER, prompt)),
|
||||
EvaluatedInterventionData.class);
|
||||
synchronized (evaluatedDataList) {
|
||||
evaluatedDataList.add(evaluatedData);
|
||||
@@ -81,9 +81,4 @@ public class InterventionEvaluator extends AbstractAgentModule.Sub<EvaluatorInpu
|
||||
public String modelKey() {
|
||||
return "intervention_evaluator";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean withBasicPrompt() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,8 @@ import com.alibaba.fastjson2.JSONObject;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
|
||||
import work.slhaf.partner.api.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.api.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.action.ActionCapability;
|
||||
import work.slhaf.partner.core.action.ActionCore;
|
||||
import work.slhaf.partner.core.action.entity.ExecutableAction;
|
||||
@@ -48,8 +49,10 @@ public class InterventionRecognizer extends AbstractAgentModule.Sub<RecognizerIn
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
String prompt = buildPrompt(data, input);
|
||||
ChatResponse response = this.singleChat(prompt);
|
||||
MetaRecognizerResult result = JSONObject.parseObject(response.getMessage(), MetaRecognizerResult.class);
|
||||
MetaRecognizerResult result = formattedChat(
|
||||
List.of(new Message(ChatConstant.Character.USER, prompt)),
|
||||
MetaRecognizerResult.class
|
||||
);
|
||||
if (result.isOk()) {
|
||||
synchronized (interventionsMap) {
|
||||
interventionsMap.put(result.getIntervention(), data);
|
||||
@@ -83,9 +86,4 @@ public class InterventionRecognizer extends AbstractAgentModule.Sub<RecognizerIn
|
||||
public String modelKey() {
|
||||
return "intervention_recognizer";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean withBasicPrompt() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.alibaba.fastjson2.JSONObject;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
|
||||
import work.slhaf.partner.api.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.api.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.action.ActionCapability;
|
||||
import work.slhaf.partner.core.action.ActionCore;
|
||||
@@ -19,8 +19,6 @@ import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
||||
import static work.slhaf.partner.common.util.ExtractUtil.extractJson;
|
||||
|
||||
public class ActionConfirmer extends AbstractAgentModule.Sub<ConfirmerInput, ConfirmerResult> implements ActivateModel {
|
||||
@InjectCapability
|
||||
private ActionCapability actionCapability;
|
||||
@@ -40,10 +38,12 @@ public class ActionConfirmer extends AbstractAgentModule.Sub<ConfirmerInput, Con
|
||||
try {
|
||||
ExecutableAction executableAction = pendingAction.getExecutableAction();
|
||||
String prompt = buildPrompt(executableAction, data.getInput(), data.getRecentMessages());
|
||||
ChatResponse response = this.singleChat(prompt);
|
||||
JSONObject tempResult = JSONObject.parseObject(extractJson(response.getMessage()));
|
||||
DecisionResponse tempResult = formattedChat(
|
||||
List.of(new Message(ChatConstant.Character.USER, prompt)),
|
||||
DecisionResponse.class
|
||||
);
|
||||
PendingActionRecord.Decision decision = parseDecision(tempResult);
|
||||
String reason = tempResult == null ? null : tempResult.getString("reason");
|
||||
String reason = tempResult.getReason();
|
||||
synchronized (decisions) {
|
||||
decisions.add(new PendingDecisionItem(pendingAction.getPendingId(), decision, reason));
|
||||
}
|
||||
@@ -68,11 +68,11 @@ public class ActionConfirmer extends AbstractAgentModule.Sub<ConfirmerInput, Con
|
||||
return result;
|
||||
}
|
||||
|
||||
private PendingActionRecord.Decision parseDecision(JSONObject tempResult) {
|
||||
private PendingActionRecord.Decision parseDecision(DecisionResponse tempResult) {
|
||||
if (tempResult == null) {
|
||||
return PendingActionRecord.Decision.HOLD;
|
||||
}
|
||||
String decisionText = tempResult.getString("decision");
|
||||
String decisionText = tempResult.getDecision();
|
||||
if (decisionText != null) {
|
||||
String upperDecision = decisionText.toUpperCase();
|
||||
if (upperDecision.contains("CONFIRM")) {
|
||||
@@ -85,7 +85,7 @@ public class ActionConfirmer extends AbstractAgentModule.Sub<ConfirmerInput, Con
|
||||
return PendingActionRecord.Decision.HOLD;
|
||||
}
|
||||
}
|
||||
Boolean confirmed = tempResult.getBoolean("confirmed");
|
||||
Boolean confirmed = tempResult.getConfirmed();
|
||||
if (Boolean.TRUE.equals(confirmed)) {
|
||||
return PendingActionRecord.Decision.CONFIRM;
|
||||
}
|
||||
@@ -116,8 +116,10 @@ public class ActionConfirmer extends AbstractAgentModule.Sub<ConfirmerInput, Con
|
||||
return "action-confirmer";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean withBasicPrompt() {
|
||||
return false;
|
||||
@lombok.Data
|
||||
private static class DecisionResponse {
|
||||
private String decision;
|
||||
private String reason;
|
||||
private Boolean confirmed;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,8 @@ import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapabili
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
|
||||
import work.slhaf.partner.api.agent.factory.component.annotation.Init;
|
||||
import work.slhaf.partner.api.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.api.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.common.thread.InteractionThreadPoolExecutor;
|
||||
import work.slhaf.partner.core.action.ActionCapability;
|
||||
import work.slhaf.partner.core.memory.pojo.EvaluatedSlice;
|
||||
@@ -48,8 +49,10 @@ public class ActionEvaluator extends AbstractAgentModule.Sub<EvaluatorInput, Lis
|
||||
List<Callable<EvaluatorResult>> list = new ArrayList<>();
|
||||
for (EvaluatorBatchInput batchInput : batchInputs) {
|
||||
list.add(() -> {
|
||||
ChatResponse response = this.singleChat(buildPrompt(batchInput));
|
||||
EvaluatorResult evaluatorResult = JSONObject.parseObject(response.getMessage(), EvaluatorResult.class);
|
||||
EvaluatorResult evaluatorResult = formattedChat(
|
||||
List.of(new Message(ChatConstant.Character.USER, buildPrompt(batchInput))),
|
||||
EvaluatorResult.class
|
||||
);
|
||||
evaluatorResult.setTendency(batchInput.getTendency());
|
||||
return evaluatorResult;
|
||||
});
|
||||
@@ -89,9 +92,4 @@ public class ActionEvaluator extends AbstractAgentModule.Sub<EvaluatorInput, Lis
|
||||
public String modelKey() {
|
||||
return "action_evaluator";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean withBasicPrompt() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,8 @@ import com.alibaba.fastjson2.JSONObject;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
|
||||
import work.slhaf.partner.api.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.api.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.action.ActionCapability;
|
||||
import work.slhaf.partner.module.modules.action.planner.extractor.entity.ExtractorInput;
|
||||
import work.slhaf.partner.module.modules.action.planner.extractor.entity.ExtractorResult;
|
||||
@@ -25,8 +26,10 @@ public class ActionExtractor extends AbstractAgentModule.Sub<ExtractorInput, Ext
|
||||
}
|
||||
for (int i = 0; i < 3; i++) {
|
||||
try {
|
||||
ChatResponse response = this.singleChat(JSONObject.toJSONString(data));
|
||||
return JSONObject.parseObject(response.getMessage(), ExtractorResult.class);
|
||||
return formattedChat(
|
||||
List.of(new Message(ChatConstant.Character.USER, JSONObject.toJSONString(data))),
|
||||
ExtractorResult.class
|
||||
);
|
||||
} catch (Exception e) {
|
||||
log.error("[ActionExtractor] 提取信息出错", e);
|
||||
}
|
||||
@@ -38,9 +41,4 @@ public class ActionExtractor extends AbstractAgentModule.Sub<ExtractorInput, Ext
|
||||
public String modelKey() {
|
||||
return "action_extractor";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean withBasicPrompt() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,9 +8,7 @@ import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapabili
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
|
||||
import work.slhaf.partner.api.agent.factory.component.annotation.Init;
|
||||
import work.slhaf.partner.api.chat.ChatClient;
|
||||
import work.slhaf.partner.api.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.api.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.api.chat.pojo.MetaMessage;
|
||||
import work.slhaf.partner.core.cognation.CognationCapability;
|
||||
@@ -31,31 +29,23 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
|
||||
|
||||
@InjectCapability
|
||||
private CognationCapability cognationCapability;
|
||||
private List<Message> appendedMessages = new ArrayList<>();
|
||||
private final List<Message> appendedMessages = new ArrayList<>();
|
||||
private final List<Message> chatMessages = new ArrayList<>();
|
||||
|
||||
@Init
|
||||
public void init() {
|
||||
List<Message> chatMessages = this.cognationCapability.getChatMessages();
|
||||
this.getModel().getChatMessages().addAll(chatMessages);
|
||||
|
||||
updateChatClientSettings();
|
||||
this.chatMessages.clear();
|
||||
this.chatMessages.addAll(this.cognationCapability.getChatMessages());
|
||||
log.info("CommunicationProducer 注册完毕...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateChatClientSettings() {
|
||||
ChatClient chatClient = getModel().getChatClient();
|
||||
chatClient.setTemperature(0.3);
|
||||
chatClient.setTop_p(0.7);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String modelKey() {
|
||||
return "communication_producer";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean withBasicPrompt() {
|
||||
public boolean useStreaming() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -73,7 +63,7 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
|
||||
activateModule(runningFlowContext);
|
||||
setMessageCount(runningFlowContext);
|
||||
|
||||
log.debug("[CommunicationProducer] 当前消息列表大小: {}", getModel().getChatMessages().size());
|
||||
log.debug("[CommunicationProducer] 当前消息列表大小: {}", chatMessages.size());
|
||||
log.debug("[CommunicationProducer] 当前核心prompt内容: {}", runningFlowContext.getCoreContext().toString());
|
||||
|
||||
setMessage(runningFlowContext.getCoreContext().toString());
|
||||
@@ -94,28 +84,28 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
|
||||
int count = 0;
|
||||
while (true) {
|
||||
try {
|
||||
ChatResponse chatResponse = this.chat();
|
||||
String chatResponse = this.chat(buildChatMessages());
|
||||
try {
|
||||
response.putAll(JSONObject.parse(extractJson(chatResponse.getMessage())));
|
||||
response.putAll(JSONObject.parse(extractJson(chatResponse)));
|
||||
} catch (Exception e) {
|
||||
log.warn("主模型回复格式出错, 将直接作为消息返回, 建议尝试更换主模型...");
|
||||
handleExceptionResponse(response, chatResponse.getMessage());
|
||||
handleExceptionResponse(response, chatResponse);
|
||||
}
|
||||
log.debug("[CommunicationProducer] CommunicationProducer 响应内容: {}", response);
|
||||
updateModuleContextAndChatMessages(runningFlowContext, response.getString("text"), chatResponse);
|
||||
updateModuleContextAndChatMessages(runningFlowContext, response.getString("text"));
|
||||
break;
|
||||
} catch (Exception e) {
|
||||
count++;
|
||||
log.error("[CommunicationProducer] CoreModel执行异常: {}", e.getLocalizedMessage());
|
||||
if (count > 3) {
|
||||
handleExceptionResponse(response, "主模型交互出错: " + e.getLocalizedMessage());
|
||||
getModel().getChatMessages().removeLast();
|
||||
chatMessages.removeLast();
|
||||
break;
|
||||
}
|
||||
} finally {
|
||||
updateCoreResponse(runningFlowContext, response);
|
||||
resetAppendedMessages();
|
||||
log.debug("[CommunicationProducer] 消息列表更新大小: {}", getModel().getChatMessages().size());
|
||||
log.debug("[CommunicationProducer] 消息列表更新大小: {}", chatMessages.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -143,20 +133,15 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
|
||||
this.appendedMessages.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ChatResponse chat() {
|
||||
List<Message> baseMessages = getModel().getBaseMessages();
|
||||
List<Message> chatMessages = getModel().getChatMessages();
|
||||
List<Message> temp = new ArrayList<>(baseMessages.subList(0, baseMessages.size() - 2));
|
||||
private List<Message> buildChatMessages() {
|
||||
List<Message> temp = new ArrayList<>(appendedMessages.size() + chatMessages.size());
|
||||
temp.addAll(appendedMessages);
|
||||
temp.addAll(baseMessages.subList(baseMessages.size() - 2, baseMessages.size()));
|
||||
temp.addAll(chatMessages);
|
||||
return getModel().getChatClient().runChat(temp);
|
||||
return temp;
|
||||
}
|
||||
|
||||
private void updateModuleContextAndChatMessages(PartnerRunningFlowContext runningFlowContext, String response, ChatResponse chatResponse) {
|
||||
private void updateModuleContextAndChatMessages(PartnerRunningFlowContext runningFlowContext, String response) {
|
||||
cognationCapability.getMessageLock().lock();
|
||||
List<Message> chatMessages = getModel().getChatMessages();
|
||||
chatMessages.removeIf(m -> {
|
||||
if (m.getRole().equals(ChatConstant.Character.ASSISTANT)) {
|
||||
return false;
|
||||
@@ -176,8 +161,6 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
|
||||
Message assistantMessage = new Message(ChatConstant.Character.ASSISTANT, response);
|
||||
chatMessages.add(assistantMessage);
|
||||
cognationCapability.getMessageLock().unlock();
|
||||
//设置上下文
|
||||
runningFlowContext.getModuleContext().getExtraContext().put("total_token", chatResponse.getUsageBean().getTotal_tokens());
|
||||
//区分单人聊天场景
|
||||
// if (runningFlowContext.isSingle()) {
|
||||
MetaMessage metaMessage = new MetaMessage(primaryUserMessage, assistantMessage);
|
||||
@@ -187,7 +170,7 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
|
||||
|
||||
private void setMessage(String coreContextStr) {
|
||||
Message userMessage = new Message(ChatConstant.Character.USER, coreContextStr);
|
||||
getModel().getChatMessages().add(userMessage);
|
||||
chatMessages.add(userMessage);
|
||||
}
|
||||
|
||||
private void handleExceptionResponse(JSONObject response, String chatResponse) {
|
||||
@@ -196,7 +179,7 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
|
||||
}
|
||||
|
||||
private void setMessageCount(PartnerRunningFlowContext runningFlowContext) {
|
||||
runningFlowContext.getModuleContext().getExtraContext().put("message_count", getModel().getChatMessages().size());
|
||||
runningFlowContext.getModuleContext().getExtraContext().put("message_count", chatMessages.size());
|
||||
}
|
||||
|
||||
private void setAppendedPromptMessage(List<AppendPromptData> appendPrompt) {
|
||||
|
||||
@@ -8,6 +8,8 @@ import lombok.EqualsAndHashCode;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
|
||||
import work.slhaf.partner.api.agent.factory.component.annotation.Init;
|
||||
import work.slhaf.partner.api.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.common.thread.InteractionThreadPoolExecutor;
|
||||
import work.slhaf.partner.core.memory.pojo.EvaluatedSlice;
|
||||
import work.slhaf.partner.core.memory.pojo.MemoryResult;
|
||||
@@ -24,8 +26,6 @@ import java.util.concurrent.ConcurrentLinkedDeque;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static work.slhaf.partner.common.util.ExtractUtil.extractJson;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class SliceSelectEvaluator extends AbstractAgentModule.Sub<EvaluatorInput, List<EvaluatedSlice>> implements ActivateModel {
|
||||
@@ -61,7 +61,10 @@ public class SliceSelectEvaluator extends AbstractAgentModule.Sub<EvaluatorInput
|
||||
.history(evaluatorInput.getMessages())
|
||||
.build();
|
||||
log.debug("[SliceSelectEvaluator] 评估[{}]输入: {}", thisCount, JSONObject.toJSONString(batchInput));
|
||||
EvaluatorResult evaluatorResult = JSONObject.parseObject(extractJson(singleChat(JSONUtil.toJsonStr(batchInput)).getMessage()), EvaluatorResult.class);
|
||||
EvaluatorResult evaluatorResult = formattedChat(
|
||||
List.of(new Message(ChatConstant.Character.USER, JSONUtil.toJsonStr(batchInput))),
|
||||
EvaluatorResult.class
|
||||
);
|
||||
log.debug("[SliceSelectEvaluator] 评估[{}]结果: {}", thisCount, JSONObject.toJSONString(evaluatorResult));
|
||||
for (Long result : evaluatorResult.getResults()) {
|
||||
SliceSummary sliceSummary = map.get(result);
|
||||
@@ -117,9 +120,4 @@ public class SliceSelectEvaluator extends AbstractAgentModule.Sub<EvaluatorInput
|
||||
public String modelKey() {
|
||||
return "slice_evaluator";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean withBasicPrompt() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package work.slhaf.partner.module.modules.memory.selector.extractor;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
|
||||
import work.slhaf.partner.api.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.api.chat.pojo.MetaMessage;
|
||||
import work.slhaf.partner.core.cognation.CognationCapability;
|
||||
@@ -20,7 +20,6 @@ import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowCon
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static work.slhaf.partner.common.util.ExtractUtil.extractJson;
|
||||
import static work.slhaf.partner.common.util.ExtractUtil.fixTopicPath;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@@ -52,9 +51,11 @@ public class MemorySelectExtractor extends AbstractAgentModule.Sub<PartnerRunnin
|
||||
.topic_tree(memoryCapability.getTopicTree())
|
||||
.activatedMemorySlices(activatedMemorySlices)
|
||||
.build();
|
||||
log.debug("[MemorySelectExtractor] 主题提取输入: {}", JSONObject.toJSONString(extractorInput));
|
||||
String responseStr = extractJson(singleChat(JSONUtil.toJsonPrettyStr(extractorInput)).getMessage());
|
||||
extractorResult = JSONObject.parseObject(responseStr, ExtractorResult.class);
|
||||
log.debug("[MemorySelectExtractor] 主题提取输入: {}", JSONUtil.toJsonStr(extractorInput));
|
||||
extractorResult = formattedChat(
|
||||
List.of(new Message(ChatConstant.Character.USER, JSONUtil.toJsonPrettyStr(extractorInput))),
|
||||
ExtractorResult.class
|
||||
);
|
||||
log.debug("[MemorySelectExtractor] 主题提取结果: {}", extractorResult);
|
||||
} catch (Exception e) {
|
||||
log.error("[MemorySelectExtractor] 主题提取出错: ", e);
|
||||
@@ -83,9 +84,4 @@ public class MemorySelectExtractor extends AbstractAgentModule.Sub<PartnerRunnin
|
||||
public String modelKey() {
|
||||
return "topic_extractor";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean withBasicPrompt() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,31 +6,27 @@ import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
|
||||
import work.slhaf.partner.api.agent.factory.component.annotation.Init;
|
||||
import work.slhaf.partner.api.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.api.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.module.modules.memory.updater.summarizer.entity.SummarizeInput;
|
||||
import work.slhaf.partner.module.modules.memory.updater.summarizer.entity.SummarizeResult;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static work.slhaf.partner.common.util.ExtractUtil.extractJson;
|
||||
import static work.slhaf.partner.common.util.ExtractUtil.fixTopicPath;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class MultiSummarizer extends AbstractAgentModule.Sub<SummarizeInput, SummarizeResult> implements ActivateModel {
|
||||
@Init
|
||||
public void init() {
|
||||
updateChatClientSettings();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SummarizeResult execute(SummarizeInput input) {
|
||||
log.debug("[MemorySummarizer] 整体摘要开始...");
|
||||
ChatResponse response = this.singleChat(JSONUtil.toJsonPrettyStr(input));
|
||||
log.debug("[MemorySummarizer] 整体摘要结果: {}", JSONObject.toJSONString(response));
|
||||
SummarizeResult result = JSONObject.parseObject(extractJson(response.getMessage()), SummarizeResult.class);
|
||||
SummarizeResult result = formattedChat(
|
||||
List.of(new Message(ChatConstant.Character.USER, JSONUtil.toJsonPrettyStr(input))),
|
||||
SummarizeResult.class
|
||||
);
|
||||
log.debug("[MemorySummarizer] 整体摘要结果: {}", JSONObject.toJSONString(result));
|
||||
return fix(result);
|
||||
}
|
||||
|
||||
@@ -52,9 +48,4 @@ public class MultiSummarizer extends AbstractAgentModule.Sub<SummarizeInput, Sum
|
||||
public String modelKey() {
|
||||
return "multi_summarizer";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean withBasicPrompt() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentMod
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
|
||||
import work.slhaf.partner.api.agent.factory.component.annotation.Init;
|
||||
import work.slhaf.partner.api.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.api.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.common.thread.InteractionThreadPoolExecutor;
|
||||
|
||||
@@ -56,8 +55,7 @@ public class SingleSummarizer extends AbstractAgentModule.Sub<List<Message>, Voi
|
||||
|
||||
private String singleExecute(String primaryContent) {
|
||||
try {
|
||||
ChatResponse response = this.singleChat(primaryContent);
|
||||
return response.getMessage();
|
||||
return chat(List.of(new Message(ChatConstant.Character.USER, primaryContent)));
|
||||
} catch (Exception e) {
|
||||
log.error("[SingleSummarizer] 单消息总结出错: ", e);
|
||||
return primaryContent;
|
||||
@@ -68,9 +66,4 @@ public class SingleSummarizer extends AbstractAgentModule.Sub<List<Message>, Voi
|
||||
public String modelKey() {
|
||||
return "single_summarizer";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean withBasicPrompt() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +1,24 @@
|
||||
package work.slhaf.partner.module.modules.memory.updater.summarizer;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
|
||||
import work.slhaf.partner.api.agent.factory.component.annotation.Init;
|
||||
import work.slhaf.partner.api.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.api.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import static work.slhaf.partner.common.util.ExtractUtil.extractJson;
|
||||
import java.util.List;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class TotalSummarizer extends AbstractAgentModule.Sub<HashMap<String, String>, String> implements ActivateModel {
|
||||
@Init
|
||||
public void init() {
|
||||
updateChatClientSettings();
|
||||
}
|
||||
|
||||
public String execute(HashMap<String, String> singleMemorySummary) {
|
||||
ChatResponse response = this.singleChat(JSONUtil.toJsonPrettyStr(singleMemorySummary));
|
||||
return JSONObject.parseObject(extractJson(response.getMessage())).getString("content");
|
||||
return formattedChat(
|
||||
List.of(new Message(ChatConstant.Character.USER, JSONUtil.toJsonPrettyStr(singleMemorySummary))),
|
||||
SummaryContent.class
|
||||
).getContent();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -31,8 +26,8 @@ public class TotalSummarizer extends AbstractAgentModule.Sub<HashMap<String, Str
|
||||
return "total_summarizer";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean withBasicPrompt() {
|
||||
return true;
|
||||
@lombok.Data
|
||||
private static class SummaryContent {
|
||||
private String content;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import lombok.EqualsAndHashCode;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
|
||||
import work.slhaf.partner.api.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.api.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.perceive.PerceiveCapability;
|
||||
@@ -49,8 +49,10 @@ public class RelationExtractor extends AbstractAgentModule.Sub<PartnerRunningFlo
|
||||
}
|
||||
|
||||
private RelationExtractResult getRelationResult(RelationExtractInput input) {
|
||||
ChatResponse response = singleChat(JSONObject.toJSONString(input));
|
||||
return JSONObject.parseObject(response.getMessage(), RelationExtractResult.class);
|
||||
return formattedChat(
|
||||
List.of(new Message(ChatConstant.Character.USER, JSONObject.toJSONString(input))),
|
||||
RelationExtractResult.class
|
||||
);
|
||||
}
|
||||
|
||||
private RelationExtractInput getRelationInput(String userId) {
|
||||
@@ -71,9 +73,4 @@ public class RelationExtractor extends AbstractAgentModule.Sub<PartnerRunningFlo
|
||||
public String modelKey() {
|
||||
return "relation_extractor";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean withBasicPrompt() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,13 +7,15 @@ import lombok.EqualsAndHashCode;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
|
||||
import work.slhaf.partner.api.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.api.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.perceive.PerceiveCapability;
|
||||
import work.slhaf.partner.module.modules.perceive.updater.static_extractor.entity.StaticMemoryExtractInput;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@@ -30,8 +32,8 @@ public class StaticMemoryExtractor extends AbstractAgentModule.Sub<PartnerRunnin
|
||||
.messages(cognationCapability.getChatMessages())
|
||||
.existedStaticMap(perceiveCapability.getUser(context.getSource()).getStaticMemory())
|
||||
.build();
|
||||
ChatResponse response = singleChat(JSONUtil.toJsonPrettyStr(input));
|
||||
JSONObject jsonObject = JSONObject.parseObject(response.getMessage());
|
||||
String response = chat(List.of(new Message(ChatConstant.Character.USER, JSONUtil.toJsonPrettyStr(input))));
|
||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
HashMap<String, String> result = new HashMap<>();
|
||||
jsonObject.forEach((k, v) -> result.put(k, (String) v));
|
||||
return result;
|
||||
@@ -41,9 +43,4 @@ public class StaticMemoryExtractor extends AbstractAgentModule.Sub<PartnerRunnin
|
||||
public String modelKey() {
|
||||
return "static_extractor";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean withBasicPrompt() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user