mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
完成了本体与框架的适配工作,并修复了某些问题。需要进一步进行测试
- 修复了 CognationCapability 相关的注解使用错误 - 将前置模块中的 setAppendedPrompt 与 setActiveModule 方法抽取到 execute 模板方法中 - 完善了已有模块的适配工作, 并去除了不必要的单例配置
This commit is contained in:
@@ -6,15 +6,15 @@ import work.slhaf.partner.api.agent.factory.capability.annotation.CoordinateMana
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.Coordinated;
|
||||
import work.slhaf.partner.api.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.common.exception.callback.GlobalExceptionHandler;
|
||||
import work.slhaf.partner.runtime.exception.pojo.GlobalException;
|
||||
import work.slhaf.partner.core.cognation.CognationCore;
|
||||
import work.slhaf.partner.core.common.pojo.MemoryResult;
|
||||
import work.slhaf.partner.core.common.pojo.MemorySliceResult;
|
||||
import work.slhaf.partner.core.submodule.cache.CacheCore;
|
||||
import work.slhaf.partner.core.submodule.dispatch.DispatchCore;
|
||||
import work.slhaf.partner.core.submodule.memory.MemoryCore;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.MemoryResult;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.MemorySlice;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.MemorySliceResult;
|
||||
import work.slhaf.partner.core.submodule.perceive.PerceiveCore;
|
||||
import work.slhaf.partner.runtime.exception.pojo.GlobalException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serial;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package work.slhaf.partner.core.cognation;
|
||||
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.Capability;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.CapabilityMethod;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.ToCoordinated;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.EvaluatedSlice;
|
||||
@@ -13,37 +12,16 @@ import java.util.concurrent.locks.Lock;
|
||||
@Capability("cognation")
|
||||
public interface CognationCapability {
|
||||
|
||||
@CapabilityMethod
|
||||
List<Message> getChatMessages();
|
||||
|
||||
@CapabilityMethod
|
||||
void setChatMessages(List<Message> chatMessages);
|
||||
|
||||
@CapabilityMethod
|
||||
void cleanMessage(List<Message> messages);
|
||||
|
||||
@CapabilityMethod
|
||||
void updateActivatedSlices(String userId, List<EvaluatedSlice> memorySlices);
|
||||
|
||||
@CapabilityMethod
|
||||
String getActivatedSlicesStr(String userId);
|
||||
|
||||
@CapabilityMethod
|
||||
HashMap<String, List<EvaluatedSlice>> getActivatedSlices();
|
||||
|
||||
@CapabilityMethod
|
||||
void clearActivatedSlices(String userId);
|
||||
|
||||
@CapabilityMethod
|
||||
boolean hasActivatedSlices(String userId);
|
||||
|
||||
@CapabilityMethod
|
||||
int getActivatedSlicesSize(String userId);
|
||||
|
||||
@CapabilityMethod
|
||||
List<EvaluatedSlice> getActivatedSlices(String userId);
|
||||
|
||||
@CapabilityMethod
|
||||
Lock getMessageLock();
|
||||
|
||||
@ToCoordinated
|
||||
|
||||
@@ -5,6 +5,7 @@ import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.CapabilityCore;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.CapabilityMethod;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.api.common.entity.PersistableObject;
|
||||
import work.slhaf.partner.core.cognation.pojo.ActiveData;
|
||||
@@ -66,6 +67,16 @@ public class CognationCore extends PersistableObject {
|
||||
temp.setPerceiveCore(PerceiveCore.getInstance());
|
||||
}
|
||||
|
||||
@CapabilityMethod
|
||||
public List<Message> getChatMessages() {
|
||||
return chatMessages;
|
||||
}
|
||||
|
||||
@CapabilityMethod
|
||||
public void setChatMessages(List<Message> chatMessages) {
|
||||
this.chatMessages = chatMessages;
|
||||
}
|
||||
|
||||
private void setupHook(CognationCore temp) {
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
try {
|
||||
@@ -131,6 +142,7 @@ public class CognationCore extends PersistableObject {
|
||||
}
|
||||
}
|
||||
|
||||
@CapabilityMethod
|
||||
public void cleanMessage(List<Message> messages) {
|
||||
messageLock.lock();
|
||||
this.getChatMessages().removeAll(messages);
|
||||
@@ -138,31 +150,38 @@ public class CognationCore extends PersistableObject {
|
||||
|
||||
}
|
||||
|
||||
@CapabilityMethod
|
||||
public void updateActivatedSlices(String userId, List<EvaluatedSlice> memorySlices) {
|
||||
activeData.updateActivatedSlices(userId, memorySlices);
|
||||
log.debug("[CoordinatedManager] 已更新激活切片, userId: {}", userId);
|
||||
}
|
||||
|
||||
@CapabilityMethod
|
||||
public String getActivatedSlicesStr(String userId) {
|
||||
return activeData.getActivatedSlicesStr(userId);
|
||||
}
|
||||
|
||||
@CapabilityMethod
|
||||
public HashMap<String, List<EvaluatedSlice>> getActivatedSlices() {
|
||||
return activeData.getActivatedSlices();
|
||||
}
|
||||
|
||||
@CapabilityMethod
|
||||
public void clearActivatedSlices(String userId) {
|
||||
activeData.clearActivatedSlices(userId);
|
||||
}
|
||||
|
||||
@CapabilityMethod
|
||||
public boolean hasActivatedSlices(String userId) {
|
||||
return activeData.hasActivatedSlices(userId);
|
||||
}
|
||||
|
||||
@CapabilityMethod
|
||||
public int getActivatedSlicesSize(String userId) {
|
||||
return activeData.getActivatedSlices().get(userId).size();
|
||||
}
|
||||
|
||||
@CapabilityMethod
|
||||
public List<EvaluatedSlice> getActivatedSlices(String userId) {
|
||||
return activeData.getActivatedSlices().get(userId);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.CapabilityCore;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.CapabilityMethod;
|
||||
import work.slhaf.partner.api.common.entity.PersistableObject;
|
||||
import work.slhaf.partner.core.common.pojo.MemoryResult;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.MemoryResult;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.MemorySlice;
|
||||
|
||||
import java.io.Serial;
|
||||
|
||||
@@ -2,7 +2,7 @@ package work.slhaf.partner.core.submodule.memory;
|
||||
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.Capability;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.ToCoordinated;
|
||||
import work.slhaf.partner.core.common.pojo.MemoryResult;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.MemoryResult;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.MemorySlice;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -5,11 +5,11 @@ import lombok.EqualsAndHashCode;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.CapabilityCore;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.CapabilityMethod;
|
||||
import work.slhaf.partner.api.common.entity.PersistableObject;
|
||||
import work.slhaf.partner.core.common.pojo.MemoryResult;
|
||||
import work.slhaf.partner.core.common.pojo.MemorySliceResult;
|
||||
import work.slhaf.partner.core.submodule.memory.exception.UnExistedDateIndexException;
|
||||
import work.slhaf.partner.core.submodule.memory.exception.UnExistedTopicException;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.MemoryResult;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.MemorySlice;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.MemorySliceResult;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.node.MemoryNode;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.node.TopicNode;
|
||||
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package work.slhaf.partner.core.common.pojo;
|
||||
package work.slhaf.partner.core.submodule.memory.pojo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import work.slhaf.partner.api.common.entity.PersistableObject;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.MemorySlice;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.util.List;
|
||||
@@ -1,10 +1,9 @@
|
||||
package work.slhaf.partner.core.common.pojo;
|
||||
package work.slhaf.partner.core.submodule.memory.pojo;
|
||||
|
||||
import com.alibaba.fastjson2.annotation.JSONField;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import work.slhaf.partner.api.common.entity.PersistableObject;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.MemorySlice;
|
||||
|
||||
import java.io.Serial;
|
||||
|
||||
@@ -1,28 +1,45 @@
|
||||
package work.slhaf.partner.module.common.module;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.api.agent.factory.module.annotation.AgentModule;
|
||||
import work.slhaf.partner.api.agent.runtime.interaction.flow.abstracts.AgentRunningModule;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
import work.slhaf.partner.module.common.entity.AppendPromptData;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* 前置模块抽象类
|
||||
*/
|
||||
@Slf4j
|
||||
public abstract class PreRunningModule extends AgentRunningModule<PartnerRunningFlowContext> {
|
||||
protected void setAppendedPrompt(PartnerRunningFlowContext context) {
|
||||
private void setAppendedPrompt(PartnerRunningFlowContext context) {
|
||||
AppendPromptData data = new AppendPromptData();
|
||||
data.setModuleName(moduleName());
|
||||
HashMap<String, String> map = getPromptDataMap(context.getUserId());
|
||||
data.setAppendedPrompt(map);
|
||||
context.getModuleContext().getAppendedPrompt().add(data);
|
||||
context.setAppendedPrompt(data);
|
||||
}
|
||||
|
||||
protected void setActiveModule(PartnerRunningFlowContext context) {
|
||||
private void setActiveModule(PartnerRunningFlowContext context) {
|
||||
context.getCoreContext().addActiveModule(moduleName());
|
||||
}
|
||||
|
||||
protected abstract HashMap<String, String> getPromptDataMap(String userId);
|
||||
|
||||
protected abstract String moduleName();
|
||||
|
||||
@Override
|
||||
public final void execute(PartnerRunningFlowContext context) throws IOException, ClassNotFoundException {
|
||||
log.debug("[{}] 模块执行开始...", this.getClass().getAnnotation(AgentModule.class).name());
|
||||
doExecute(context); // 子类实现差异化逻辑
|
||||
setAppendedPrompt(context); // 通用逻辑
|
||||
setActiveModule(context); // 通用逻辑
|
||||
log.debug("[{}] 模块执行结束...", this.getClass().getAnnotation(AgentModule.class).name());
|
||||
}
|
||||
|
||||
protected abstract void doExecute(PartnerRunningFlowContext context) throws IOException, ClassNotFoundException;
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -13,13 +13,12 @@ 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;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
import work.slhaf.partner.runtime.session.SessionManager;
|
||||
import work.slhaf.partner.module.common.entity.AppendPromptData;
|
||||
import work.slhaf.partner.module.common.model.ModelConstant;
|
||||
import work.slhaf.partner.module.common.module.CoreRunningModule;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
import work.slhaf.partner.runtime.session.SessionManager;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
@@ -44,6 +43,8 @@ public class CoreModel extends CoreRunningModule implements ActivateModel {
|
||||
this.getModel().setChatMessages(chatMessages);
|
||||
this.appendedMessages = new ArrayList<>();
|
||||
this.sessionManager = SessionManager.getInstance();
|
||||
|
||||
updateChatClientSettings();
|
||||
log.info("[CoreModel] CoreModel注册完毕...");
|
||||
}
|
||||
|
||||
@@ -53,11 +54,6 @@ public class CoreModel extends CoreRunningModule implements ActivateModel {
|
||||
chatClient().setTop_p(0.7);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String modelKey() {
|
||||
return "core_model";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean withBasicPrompt() {
|
||||
return true;
|
||||
|
||||
@@ -5,24 +5,24 @@ import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.api.agent.factory.module.annotation.AfterExecute;
|
||||
import work.slhaf.partner.api.agent.factory.module.annotation.AgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.module.annotation.InjectModule;
|
||||
import work.slhaf.partner.core.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.common.pojo.MemoryResult;
|
||||
import work.slhaf.partner.core.submodule.cache.CacheCapability;
|
||||
import work.slhaf.partner.core.submodule.memory.MemoryCapability;
|
||||
import work.slhaf.partner.core.submodule.memory.exception.UnExistedDateIndexException;
|
||||
import work.slhaf.partner.core.submodule.memory.exception.UnExistedTopicException;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.EvaluatedSlice;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.MemoryResult;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.MemorySlice;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
import work.slhaf.partner.runtime.session.SessionManager;
|
||||
import work.slhaf.partner.module.common.module.PreRunningModule;
|
||||
import work.slhaf.partner.module.modules.memory.selector.evaluator.SliceSelectEvaluator;
|
||||
import work.slhaf.partner.module.modules.memory.selector.evaluator.data.EvaluatorInput;
|
||||
import work.slhaf.partner.module.modules.memory.selector.extractor.MemorySelectExtractor;
|
||||
import work.slhaf.partner.module.modules.memory.selector.extractor.data.ExtractorMatchData;
|
||||
import work.slhaf.partner.module.modules.memory.selector.extractor.data.ExtractorResult;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
@@ -50,8 +50,7 @@ public class MemorySelector extends PreRunningModule {
|
||||
private MemorySelectExtractor memorySelectExtractor;
|
||||
|
||||
@Override
|
||||
public void execute(PartnerRunningFlowContext runningFlowContext) throws IOException, ClassNotFoundException {
|
||||
log.debug("[MemorySelector] 记忆回溯流程开始...");
|
||||
public void doExecute(PartnerRunningFlowContext runningFlowContext) throws IOException, ClassNotFoundException {
|
||||
String userId = runningFlowContext.getUserId();
|
||||
//获取主题路径
|
||||
ExtractorResult extractorResult = memorySelectExtractor.execute(runningFlowContext);
|
||||
@@ -60,11 +59,6 @@ public class MemorySelector extends PreRunningModule {
|
||||
List<EvaluatedSlice> evaluatedSlices = selectAndEvaluateMemory(runningFlowContext, extractorResult);
|
||||
cognationCapability.updateActivatedSlices(userId, evaluatedSlices);
|
||||
}
|
||||
//设置追加提示词
|
||||
setAppendedPrompt(runningFlowContext);
|
||||
setModuleContextRecall(runningFlowContext);
|
||||
setActiveModule(runningFlowContext);
|
||||
log.debug("[MemorySelector] 记忆回溯完成...");
|
||||
}
|
||||
|
||||
private List<EvaluatedSlice> selectAndEvaluateMemory(PartnerRunningFlowContext runningFlowContext, ExtractorResult extractorResult) throws IOException, ClassNotFoundException {
|
||||
@@ -85,6 +79,7 @@ public class MemorySelector extends PreRunningModule {
|
||||
return memorySlices;
|
||||
}
|
||||
|
||||
@AfterExecute(order = 1)
|
||||
private void setModuleContextRecall(PartnerRunningFlowContext runningFlowContext) {
|
||||
String userId = runningFlowContext.getUserId();
|
||||
boolean recall = cognationCapability.hasActivatedSlices(userId);
|
||||
|
||||
@@ -11,16 +11,15 @@ import work.slhaf.partner.api.agent.factory.module.annotation.Init;
|
||||
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.common.thread.InteractionThreadPoolExecutor;
|
||||
import work.slhaf.partner.core.common.pojo.MemoryResult;
|
||||
import work.slhaf.partner.core.common.pojo.MemorySliceResult;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.EvaluatedSlice;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.MemoryResult;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.MemorySlice;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.MemorySliceResult;
|
||||
import work.slhaf.partner.module.modules.memory.selector.evaluator.data.EvaluatorBatchInput;
|
||||
import work.slhaf.partner.module.modules.memory.selector.evaluator.data.EvaluatorInput;
|
||||
import work.slhaf.partner.module.modules.memory.selector.evaluator.data.EvaluatorResult;
|
||||
import work.slhaf.partner.module.modules.memory.selector.evaluator.data.SliceSummary;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ConcurrentLinkedDeque;
|
||||
|
||||
@@ -3,7 +3,7 @@ package work.slhaf.partner.module.modules.memory.selector.evaluator.data;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.common.pojo.MemoryResult;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.MemoryResult;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@@ -12,15 +12,15 @@ import work.slhaf.partner.api.agent.runtime.interaction.flow.abstracts.AgentRunn
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.api.chat.pojo.MetaMessage;
|
||||
import work.slhaf.partner.common.exception.callback.GlobalExceptionHandler;
|
||||
import work.slhaf.partner.runtime.exception.pojo.GlobalException;
|
||||
import work.slhaf.partner.core.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.submodule.memory.MemoryCapability;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.EvaluatedSlice;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
import work.slhaf.partner.runtime.session.SessionManager;
|
||||
import work.slhaf.partner.module.modules.memory.selector.extractor.data.ExtractorInput;
|
||||
import work.slhaf.partner.module.modules.memory.selector.extractor.data.ExtractorMatchData;
|
||||
import work.slhaf.partner.module.modules.memory.selector.extractor.data.ExtractorResult;
|
||||
import work.slhaf.partner.runtime.exception.pojo.GlobalException;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
import work.slhaf.partner.runtime.session.SessionManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -16,15 +16,15 @@ import work.slhaf.partner.core.submodule.cache.CacheCapability;
|
||||
import work.slhaf.partner.core.submodule.memory.MemoryCapability;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.MemorySlice;
|
||||
import work.slhaf.partner.core.submodule.perceive.PerceiveCapability;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
import work.slhaf.partner.runtime.session.SessionManager;
|
||||
import work.slhaf.partner.module.common.module.PostRunningModule;
|
||||
import work.slhaf.partner.module.modules.memory.selector.extractor.MemorySelectExtractor;
|
||||
import work.slhaf.partner.module.modules.memory.updater.summarizer.MemorySummarizer;
|
||||
import work.slhaf.partner.module.modules.memory.updater.summarizer.MultiSummarizer;
|
||||
import work.slhaf.partner.module.modules.memory.updater.summarizer.SingleSummarizer;
|
||||
import work.slhaf.partner.module.modules.memory.updater.summarizer.TotalSummarizer;
|
||||
import work.slhaf.partner.module.modules.memory.updater.summarizer.data.SummarizeInput;
|
||||
import work.slhaf.partner.module.modules.memory.updater.summarizer.data.SummarizeResult;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
import work.slhaf.partner.runtime.session.SessionManager;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.Callable;
|
||||
@@ -35,11 +35,9 @@ import static work.slhaf.partner.common.util.ExtractUtil.extractUserId;
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@Slf4j
|
||||
@AgentModule(name="memory_updater",order=6)
|
||||
@AgentModule(name = "memory_updater", order = 7)
|
||||
public class MemoryUpdater extends PostRunningModule {
|
||||
|
||||
private static volatile MemoryUpdater memoryUpdater;
|
||||
|
||||
private static final long SCHEDULED_UPDATE_INTERVAL = 10 * 1000;
|
||||
private static final long UPDATE_TRIGGER_INTERVAL = 60 * 60 * 1000;
|
||||
|
||||
@@ -53,9 +51,11 @@ public class MemoryUpdater extends PostRunningModule {
|
||||
private PerceiveCapability perceiveCapability;
|
||||
|
||||
@InjectModule
|
||||
private MemorySelectExtractor memorySelectExtractor;
|
||||
private MultiSummarizer multiSummarizer;
|
||||
@InjectModule
|
||||
private MemorySummarizer memorySummarizer;
|
||||
private SingleSummarizer singleSummarizer;
|
||||
@InjectModule
|
||||
private TotalSummarizer totalSummarizer;
|
||||
|
||||
private SessionManager sessionManager;
|
||||
private InteractionThreadPoolExecutor executor;
|
||||
@@ -64,20 +64,6 @@ public class MemoryUpdater extends PostRunningModule {
|
||||
*/
|
||||
private List<Message> tempMessage;
|
||||
|
||||
public static MemoryUpdater getInstance() throws IOException, ClassNotFoundException {
|
||||
if (memoryUpdater == null) {
|
||||
synchronized (MemoryUpdater.class) {
|
||||
if (memoryUpdater == null) {
|
||||
memoryUpdater = new MemoryUpdater();
|
||||
memoryUpdater.setSessionManager(SessionManager.getInstance());
|
||||
memoryUpdater.setExecutor(InteractionThreadPoolExecutor.getInstance());
|
||||
memoryUpdater.setScheduledUpdater();
|
||||
}
|
||||
}
|
||||
}
|
||||
return memoryUpdater;
|
||||
}
|
||||
|
||||
@Init
|
||||
public void init() {
|
||||
executor = InteractionThreadPoolExecutor.getInstance();
|
||||
@@ -96,7 +82,7 @@ public class MemoryUpdater extends PostRunningModule {
|
||||
if (lastUpdatedTime != 0 && currentTime - lastUpdatedTime > UPDATE_TRIGGER_INTERVAL && chatCount > 1) {
|
||||
updateMemory();
|
||||
cognationCapability.getChatMessages().clear();
|
||||
//重置MemoryId
|
||||
// 重置MemoryId
|
||||
sessionManager.refreshMemoryId();
|
||||
log.info("[MemoryUpdater] 记忆更新: 自动触发");
|
||||
}
|
||||
@@ -116,7 +102,7 @@ public class MemoryUpdater extends PostRunningModule {
|
||||
return;
|
||||
}
|
||||
executor.execute(() -> {
|
||||
//如果token 大于阈值,则更新记忆
|
||||
// 如果token 大于阈值,则更新记忆
|
||||
JSONObject moduleContext = context.getModuleContext().getExtraContext();
|
||||
boolean recall = moduleContext.getBoolean("recall");
|
||||
if (recall) {
|
||||
@@ -128,14 +114,14 @@ public class MemoryUpdater extends PostRunningModule {
|
||||
if (!trigger) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
log.debug("[MemoryUpdater] 记忆更新触发");
|
||||
updateMemory();
|
||||
//清空chatMessages
|
||||
clearChatMessages();
|
||||
} catch (Exception e) {
|
||||
log.error("[MemoryUpdater] 记忆更新线程出错: ", e);
|
||||
}
|
||||
try {
|
||||
log.debug("[MemoryUpdater] 记忆更新触发");
|
||||
updateMemory();
|
||||
// 清空chatMessages
|
||||
clearChatMessages();
|
||||
} catch (Exception e) {
|
||||
log.error("[MemoryUpdater] 记忆更新线程出错: ", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -143,17 +129,17 @@ public class MemoryUpdater extends PostRunningModule {
|
||||
log.debug("[MemoryUpdater] 记忆更新流程开始...");
|
||||
tempMessage = new ArrayList<>(cognationCapability.getChatMessages());
|
||||
HashMap<String, String> singleMemorySummary = new HashMap<>();
|
||||
//更新单聊记忆,同时从chatMessages中去掉单聊记忆
|
||||
// 更新单聊记忆,同时从chatMessages中去掉单聊记忆
|
||||
updateSingleChatSlices(singleMemorySummary);
|
||||
//更新多人场景下的记忆及相关的确定性记忆
|
||||
// 更新多人场景下的记忆及相关的确定性记忆
|
||||
updateMultiChatSlices(singleMemorySummary);
|
||||
sessionManager.resetLastUpdatedTime();
|
||||
log.debug("[MemoryUpdater] 记忆更新流程结束...");
|
||||
}
|
||||
|
||||
private void updateMultiChatSlices(HashMap<String, String> singleMemorySummary) {
|
||||
//此时chatMessages中不再包含单聊记录,直接执行摘要以及切片插入
|
||||
//对剩下的多人聊天记录进行进行摘要
|
||||
// 此时chatMessages中不再包含单聊记录,直接执行摘要以及切片插入
|
||||
// 对剩下的多人聊天记录进行进行摘要
|
||||
Callable<Void> task = () -> {
|
||||
log.debug("[MemoryUpdater] 多人聊天记忆更新流程开始...");
|
||||
List<Message> chatMessages;
|
||||
@@ -163,17 +149,17 @@ public class MemoryUpdater extends PostRunningModule {
|
||||
cleanMessage(chatMessages);
|
||||
if (!chatMessages.isEmpty()) {
|
||||
log.debug("[MemoryUpdater] 存在多人聊天记录, 流程正常进行...");
|
||||
//以第一条user对应的id为发起用户
|
||||
// 以第一条user对应的id为发起用户
|
||||
String userId = extractUserId(chatMessages.getFirst().getContent());
|
||||
if (userId == null) {
|
||||
throw new RuntimeException("未匹配到 userId!");
|
||||
}
|
||||
SummarizeInput summarizeInput = new SummarizeInput(chatMessages, memoryCapability.getTopicTree());
|
||||
log.debug("[MemoryUpdater] 多人聊天记忆更新-总结流程-输入: {}", summarizeInput);
|
||||
SummarizeResult summarizeResult = memorySummarizer.execute(summarizeInput);
|
||||
SummarizeResult summarizeResult = summarize(summarizeInput);
|
||||
log.debug("[MemoryUpdater] 多人聊天记忆更新-总结流程-输出: {}", summarizeResult);
|
||||
MemorySlice memorySlice = getMemorySlice(userId, summarizeResult, chatMessages);
|
||||
//设置involvedUserId
|
||||
// 设置involvedUserId
|
||||
setInvolvedUserId(userId, memorySlice, chatMessages);
|
||||
memoryCapability.insertSlice(memorySlice, summarizeResult.getTopicPath());
|
||||
|
||||
@@ -181,7 +167,7 @@ public class MemoryUpdater extends PostRunningModule {
|
||||
|
||||
} else {
|
||||
log.debug("[MemoryUpdater] 不存在多人聊天记录, 将以单聊总结为对话缓存的主要输入: {}", singleMemorySummary);
|
||||
cacheCapability.updateDialogMap(LocalDateTime.now(), memorySummarizer.executeTotalSummary(singleMemorySummary));
|
||||
cacheCapability.updateDialogMap(LocalDateTime.now(), totalSummarizer.execute(singleMemorySummary));
|
||||
}
|
||||
log.debug("[MemoryUpdater] 对话缓存更新完毕");
|
||||
log.debug("[MemoryUpdater] 多人聊天记忆更新流程结束...");
|
||||
@@ -192,7 +178,7 @@ public class MemoryUpdater extends PostRunningModule {
|
||||
}
|
||||
|
||||
private void cleanMessage(List<Message> chatMessages) {
|
||||
//清理时间标识
|
||||
// 清理时间标识
|
||||
for (Message message : chatMessages) {
|
||||
if (message.getRole().equals(ChatConstant.Character.ASSISTANT)) {
|
||||
continue;
|
||||
@@ -203,9 +189,10 @@ public class MemoryUpdater extends PostRunningModule {
|
||||
}
|
||||
|
||||
private void clearChatMessages() {
|
||||
//不全部清空,保留一部分输入防止上下文割裂
|
||||
// 不全部清空,保留一部分输入防止上下文割裂
|
||||
cognationCapability.getMessageLock().lock();
|
||||
List<Message> temp = new ArrayList<>(tempMessage.subList(tempMessage.size() - tempMessage.size() / 6, tempMessage.size()));
|
||||
List<Message> temp = new ArrayList<>(
|
||||
tempMessage.subList(tempMessage.size() - tempMessage.size() / 6, tempMessage.size()));
|
||||
cognationCapability.getChatMessages().removeAll(tempMessage);
|
||||
cognationCapability.getChatMessages().addAll(0, temp);
|
||||
cognationCapability.getMessageLock().unlock();
|
||||
@@ -216,7 +203,7 @@ public class MemoryUpdater extends PostRunningModule {
|
||||
if (chatMessage.getRole().equals(ChatConstant.Character.ASSISTANT)) {
|
||||
continue;
|
||||
}
|
||||
//匹配userId
|
||||
// 匹配userId
|
||||
String userId = extractUserId(chatMessage.getContent());
|
||||
if (userId == null) {
|
||||
continue;
|
||||
@@ -229,13 +216,12 @@ public class MemoryUpdater extends PostRunningModule {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void updateSingleChatSlices(HashMap<String, String> singleMemorySummary) {
|
||||
log.debug("[MemoryUpdater] 单聊记忆更新流程开始...");
|
||||
//更新单聊记忆,同时从chatMessages中去掉单聊记忆
|
||||
// 更新单聊记忆,同时从chatMessages中去掉单聊记忆
|
||||
Set<String> userIdSet = new HashSet<>(sessionManager.getSingleMetaMessageMap().keySet());
|
||||
List<Callable<Void>> tasks = new ArrayList<>();
|
||||
//多人聊天?
|
||||
// 多人聊天?
|
||||
AtomicInteger count = new AtomicInteger(0);
|
||||
for (String id : userIdSet) {
|
||||
List<Message> messages = sessionManager.unpackAndClear(id);
|
||||
@@ -243,17 +229,17 @@ public class MemoryUpdater extends PostRunningModule {
|
||||
int thisCount = count.incrementAndGet();
|
||||
log.debug("[MemoryUpdater] 单聊记忆[{}]更新: {}", thisCount, id);
|
||||
try {
|
||||
//单聊记忆更新
|
||||
// 单聊记忆更新
|
||||
SummarizeInput summarizeInput = new SummarizeInput(messages, memoryCapability.getTopicTree());
|
||||
log.debug("[MemoryUpdater] 单聊记忆[{}]更新-总结流程-输入: {}", thisCount, JSONObject.toJSONString(summarizeInput));
|
||||
SummarizeResult summarizeResult = memorySummarizer.execute(summarizeInput);
|
||||
SummarizeResult summarizeResult = summarize(summarizeInput);
|
||||
log.debug("[MemoryUpdater] 单聊记忆[{}]更新-总结流程-输出: {}", thisCount, JSONObject.toJSONString(summarizeResult));
|
||||
MemorySlice memorySlice = getMemorySlice(id, summarizeResult, messages);
|
||||
//插入时userDialogMap已经进行更新
|
||||
// 插入时userDialogMap已经进行更新
|
||||
memoryCapability.insertSlice(memorySlice, summarizeResult.getTopicPath());
|
||||
//从chatMessages中移除单聊记录
|
||||
// 从chatMessages中移除单聊记录
|
||||
cognationCapability.cleanMessage(messages);
|
||||
//添加至singleMemorySummary
|
||||
// 添加至singleMemorySummary
|
||||
String key = perceiveCapability.getUser(id).getNickName() + "[" + id + "]";
|
||||
singleMemorySummary.put(key, summarizeResult.getSummary());
|
||||
log.debug("[MemoryUpdater] 单聊记忆[{}]更新成功: ", thisCount);
|
||||
@@ -268,13 +254,18 @@ public class MemoryUpdater extends PostRunningModule {
|
||||
log.debug("[MemoryUpdater] 单聊记忆更新结束...");
|
||||
}
|
||||
|
||||
private SummarizeResult summarize(SummarizeInput summarizeInput) {
|
||||
singleSummarizer.execute(summarizeInput.getChatMessages());
|
||||
return multiSummarizer.execute(summarizeInput);
|
||||
}
|
||||
|
||||
private MemorySlice getMemorySlice(String userId, SummarizeResult summarizeResult, List<Message> chatMessages) {
|
||||
MemorySlice memorySlice = new MemorySlice();
|
||||
//设置 memoryId,timestamp
|
||||
// 设置 memoryId,timestamp
|
||||
memorySlice.setMemoryId(sessionManager.getCurrentMemoryId());
|
||||
memorySlice.setTimestamp(System.currentTimeMillis());
|
||||
|
||||
//补充信息
|
||||
// 补充信息
|
||||
memorySlice.setPrivate(summarizeResult.isPrivate());
|
||||
memorySlice.setSummary(summarizeResult.getSummary());
|
||||
memorySlice.setChatMessages(chatMessages);
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
package work.slhaf.partner.module.modules.memory.updater.summarizer;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.api.agent.runtime.interaction.flow.abstracts.AgentRunningSubModule;
|
||||
import work.slhaf.partner.common.thread.InteractionThreadPoolExecutor;
|
||||
import work.slhaf.partner.module.modules.memory.updater.summarizer.data.SummarizeInput;
|
||||
import work.slhaf.partner.module.modules.memory.updater.summarizer.data.SummarizeResult;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@Slf4j
|
||||
public class MemorySummarizer extends AgentRunningSubModule<SummarizeInput,SummarizeResult> {
|
||||
|
||||
private static volatile MemorySummarizer memorySummarizer;
|
||||
public static final String MODEL_KEY = "memory_summarizer";
|
||||
|
||||
private InteractionThreadPoolExecutor executor;
|
||||
private SingleSummarizer singleSummarizer;
|
||||
private MultiSummarizer multiSummarizer;
|
||||
private TotalSummarizer totalSummarizer;
|
||||
|
||||
public static MemorySummarizer getInstance() {
|
||||
if (memorySummarizer == null) {
|
||||
synchronized (MemorySummarizer.class) {
|
||||
if (memorySummarizer == null) {
|
||||
memorySummarizer = new MemorySummarizer();
|
||||
memorySummarizer.setExecutor(InteractionThreadPoolExecutor.getInstance());
|
||||
memorySummarizer.setSingleSummarizer(SingleSummarizer.getInstance());
|
||||
memorySummarizer.setMultiSummarizer(MultiSummarizer.getInstance());
|
||||
memorySummarizer.setTotalSummarizer(TotalSummarizer.getInstance());
|
||||
}
|
||||
}
|
||||
}
|
||||
return memorySummarizer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SummarizeResult execute(SummarizeInput input) {
|
||||
//进行长文本批量摘要
|
||||
singleSummarizer.execute(input.getChatMessages());
|
||||
//进行整体摘要并返回结果
|
||||
return multiSummarizer.execute(input);
|
||||
}
|
||||
|
||||
public String executeTotalSummary(HashMap<String, String> singleMemorySummary) {
|
||||
return totalSummarizer.execute(singleMemorySummary);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.api.agent.factory.module.annotation.AgentSubModule;
|
||||
import work.slhaf.partner.api.agent.factory.module.annotation.Init;
|
||||
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.api.chat.pojo.ChatResponse;
|
||||
@@ -20,24 +22,16 @@ import static work.slhaf.partner.common.util.ExtractUtil.fixTopicPath;
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@Slf4j
|
||||
@AgentSubModule
|
||||
public class MultiSummarizer extends AgentRunningSubModule<SummarizeInput, SummarizeResult> implements ActivateModel {
|
||||
|
||||
private static volatile MultiSummarizer multiSummarizer;
|
||||
|
||||
private MultiSummarizer() {
|
||||
modelSettings();
|
||||
}
|
||||
|
||||
public static MultiSummarizer getInstance() {
|
||||
if (multiSummarizer == null) {
|
||||
synchronized (MultiSummarizer.class) {
|
||||
if (multiSummarizer == null) {
|
||||
multiSummarizer = new MultiSummarizer();
|
||||
multiSummarizer.updateChatClientSettings();
|
||||
}
|
||||
}
|
||||
}
|
||||
return multiSummarizer;
|
||||
@Init
|
||||
public void init() {
|
||||
updateChatClientSettings();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -4,6 +4,8 @@ import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.api.agent.factory.module.annotation.AgentSubModule;
|
||||
import work.slhaf.partner.api.agent.factory.module.annotation.Init;
|
||||
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.api.chat.constant.ChatConstant;
|
||||
@@ -20,26 +22,14 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Slf4j
|
||||
@Data
|
||||
@AgentSubModule
|
||||
public class SingleSummarizer extends AgentRunningSubModule<List<Message>,Void> implements ActivateModel {
|
||||
|
||||
private static volatile SingleSummarizer singleSummarizer;
|
||||
|
||||
private InteractionThreadPoolExecutor executor;
|
||||
|
||||
|
||||
private SingleSummarizer() {
|
||||
modelSettings();
|
||||
}
|
||||
public static SingleSummarizer getInstance() {
|
||||
if (singleSummarizer == null) {
|
||||
synchronized (SingleSummarizer.class) {
|
||||
if (singleSummarizer == null) {
|
||||
singleSummarizer = new SingleSummarizer();
|
||||
singleSummarizer.setExecutor(InteractionThreadPoolExecutor.getInstance());
|
||||
}
|
||||
}
|
||||
}
|
||||
return singleSummarizer;
|
||||
@Init
|
||||
public void init() {
|
||||
this.executor = InteractionThreadPoolExecutor.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,6 +5,8 @@ import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.api.agent.factory.module.annotation.AgentSubModule;
|
||||
import work.slhaf.partner.api.agent.factory.module.annotation.Init;
|
||||
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.api.chat.pojo.ChatResponse;
|
||||
@@ -16,25 +18,12 @@ import static work.slhaf.partner.common.util.ExtractUtil.extractJson;
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@Slf4j
|
||||
@AgentSubModule
|
||||
public class TotalSummarizer extends AgentRunningSubModule<HashMap<String, String>, String> implements ActivateModel {
|
||||
|
||||
private static volatile TotalSummarizer totalSummarizer;
|
||||
|
||||
|
||||
private TotalSummarizer() {
|
||||
modelSettings();
|
||||
}
|
||||
|
||||
public static TotalSummarizer getInstance() {
|
||||
if (totalSummarizer == null) {
|
||||
synchronized (TotalSummarizer.class) {
|
||||
if (totalSummarizer == null) {
|
||||
totalSummarizer = new TotalSummarizer();
|
||||
totalSummarizer.updateChatClientSettings();
|
||||
}
|
||||
}
|
||||
}
|
||||
return totalSummarizer;
|
||||
@Init
|
||||
public void init() {
|
||||
updateChatClientSettings();
|
||||
}
|
||||
|
||||
public String execute(HashMap<String, String> singleMemorySummary){
|
||||
|
||||
@@ -3,41 +3,24 @@ package work.slhaf.partner.module.modules.perceive.selector;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.api.agent.factory.module.annotation.AgentModule;
|
||||
import work.slhaf.partner.core.submodule.perceive.PerceiveCapability;
|
||||
import work.slhaf.partner.core.submodule.perceive.pojo.User;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
import work.slhaf.partner.module.common.module.PreRunningModule;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
@Slf4j
|
||||
@Setter
|
||||
@AgentModule(name = "perceive_selector",order = 2)
|
||||
public class PerceiveSelector extends PreRunningModule {
|
||||
|
||||
private static volatile PerceiveSelector perceiveSelector;
|
||||
|
||||
@InjectCapability
|
||||
private PerceiveCapability perceiveCapability;
|
||||
|
||||
public static PerceiveSelector getInstance() throws IOException, ClassNotFoundException {
|
||||
if (perceiveSelector == null) {
|
||||
synchronized (PerceiveSelector.class) {
|
||||
if (perceiveSelector == null) {
|
||||
perceiveSelector = new PerceiveSelector();
|
||||
}
|
||||
}
|
||||
}
|
||||
return perceiveSelector;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(PartnerRunningFlowContext context) throws IOException, ClassNotFoundException {
|
||||
log.debug("[PerceiveSelector] 感知模块处理流程开始...");
|
||||
//处理思路: 根据用户id,查询用户相关身份感知数据,直接添加到appendPrompt中,这直接执行appendPrompt方法应该可以
|
||||
setAppendedPrompt(context);
|
||||
setActiveModule(context);
|
||||
log.debug("[PerceiveSelector] 感知模块处理流程结束...");
|
||||
public void doExecute(PartnerRunningFlowContext context) {
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,16 +1,21 @@
|
||||
package work.slhaf.partner.module.modules.perceive.updater;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.api.agent.factory.module.annotation.AgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.module.annotation.Init;
|
||||
import work.slhaf.partner.api.agent.factory.module.annotation.InjectModule;
|
||||
import work.slhaf.partner.api.agent.runtime.interaction.flow.abstracts.AgentRunningModule;
|
||||
import work.slhaf.partner.common.thread.InteractionThreadPoolExecutor;
|
||||
import work.slhaf.partner.core.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.submodule.perceive.PerceiveCapability;
|
||||
import work.slhaf.partner.core.submodule.perceive.pojo.User;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
import work.slhaf.partner.module.modules.perceive.updater.relation_extractor.RelationExtractor;
|
||||
import work.slhaf.partner.module.modules.perceive.updater.relation_extractor.pojo.RelationExtractResult;
|
||||
import work.slhaf.partner.module.modules.perceive.updater.static_extractor.StaticMemoryExtractor;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
@@ -22,9 +27,11 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
/**
|
||||
* 感知更新,异步
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Slf4j
|
||||
@Data
|
||||
public class PerceiveUpdater {
|
||||
@AgentModule(name = "perceive_updater", order = 8)
|
||||
public class PerceiveUpdater extends AgentRunningModule<PartnerRunningFlowContext> {
|
||||
|
||||
private static volatile PerceiveUpdater perceiveUpdater;
|
||||
|
||||
@@ -32,23 +39,18 @@ public class PerceiveUpdater {
|
||||
private PerceiveCapability perceiveCapability;
|
||||
@InjectCapability
|
||||
private CognationCapability cognationCapability;
|
||||
private InteractionThreadPoolExecutor executor;
|
||||
|
||||
@InjectModule
|
||||
private RelationExtractor relationExtractor;
|
||||
@InjectModule
|
||||
private StaticMemoryExtractor staticMemoryExtractor;
|
||||
|
||||
private InteractionThreadPoolExecutor executor;
|
||||
|
||||
public static PerceiveUpdater getInstance() throws IOException, ClassNotFoundException {
|
||||
if (perceiveUpdater == null) {
|
||||
synchronized (PerceiveUpdater.class) {
|
||||
if (perceiveUpdater == null) {
|
||||
perceiveUpdater = new PerceiveUpdater();
|
||||
perceiveUpdater.setExecutor(InteractionThreadPoolExecutor.getInstance());
|
||||
perceiveUpdater.setRelationExtractor(RelationExtractor.getInstance());
|
||||
perceiveUpdater.setStaticMemoryExtractor(StaticMemoryExtractor.getInstance());
|
||||
}
|
||||
}
|
||||
}
|
||||
return perceiveUpdater;
|
||||
|
||||
@Init
|
||||
public void init() {
|
||||
this.executor = InteractionThreadPoolExecutor.getInstance();
|
||||
}
|
||||
|
||||
public void execute(PartnerRunningFlowContext context) throws IOException, ClassNotFoundException {
|
||||
|
||||
@@ -3,6 +3,8 @@ package work.slhaf.partner.module.modules.perceive.updater.relation_extractor;
|
||||
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.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.api.chat.pojo.ChatResponse;
|
||||
@@ -10,39 +12,26 @@ import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.submodule.perceive.PerceiveCapability;
|
||||
import work.slhaf.partner.core.submodule.perceive.pojo.User;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
import work.slhaf.partner.module.modules.perceive.updater.relation_extractor.pojo.RelationExtractInput;
|
||||
import work.slhaf.partner.module.modules.perceive.updater.relation_extractor.pojo.RelationExtractResult;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@AgentSubModule
|
||||
public class RelationExtractor extends AgentRunningSubModule<PartnerRunningFlowContext, RelationExtractResult> implements ActivateModel {
|
||||
|
||||
private static volatile RelationExtractor relationExtractor;
|
||||
|
||||
@InjectCapability
|
||||
private CognationCapability cognationCapability;
|
||||
@InjectCapability
|
||||
private PerceiveCapability perceiveCapability;
|
||||
|
||||
private List<Message> tempMessages;
|
||||
|
||||
private RelationExtractor(){
|
||||
modelSettings();
|
||||
}
|
||||
public static RelationExtractor getInstance() throws IOException, ClassNotFoundException {
|
||||
if (relationExtractor == null) {
|
||||
synchronized (RelationExtractor.class) {
|
||||
if (relationExtractor == null) {
|
||||
relationExtractor = new RelationExtractor();
|
||||
}
|
||||
}
|
||||
}
|
||||
return relationExtractor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RelationExtractResult execute(PartnerRunningFlowContext context){
|
||||
|
||||
@@ -5,19 +5,20 @@ 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.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.api.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.core.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.submodule.perceive.PerceiveCapability;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
import work.slhaf.partner.module.modules.perceive.updater.static_extractor.data.StaticMemoryExtractInput;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@AgentSubModule
|
||||
public class StaticMemoryExtractor extends AgentRunningSubModule<PartnerRunningFlowContext, HashMap<String, String>> implements ActivateModel {
|
||||
|
||||
private static volatile StaticMemoryExtractor staticMemoryExtractor;
|
||||
@@ -27,21 +28,6 @@ public class StaticMemoryExtractor extends AgentRunningSubModule<PartnerRunningF
|
||||
@InjectCapability
|
||||
private PerceiveCapability perceiveCapability;
|
||||
|
||||
private StaticMemoryExtractor() {
|
||||
modelSettings();
|
||||
}
|
||||
|
||||
public static StaticMemoryExtractor getInstance() throws IOException, ClassNotFoundException {
|
||||
if (staticMemoryExtractor == null) {
|
||||
synchronized (StaticMemoryExtractor.class) {
|
||||
if (staticMemoryExtractor == null) {
|
||||
staticMemoryExtractor = new StaticMemoryExtractor();
|
||||
}
|
||||
}
|
||||
}
|
||||
return staticMemoryExtractor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HashMap<String, String> execute(PartnerRunningFlowContext context) {
|
||||
StaticMemoryExtractInput input = StaticMemoryExtractInput.builder()
|
||||
|
||||
@@ -4,34 +4,24 @@ import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.api.agent.factory.module.annotation.AgentModule;
|
||||
import work.slhaf.partner.core.cognation.CognationCapability;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
import work.slhaf.partner.module.common.module.PostRunningModule;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Slf4j
|
||||
@Data
|
||||
@AgentModule(name = "postprocess_executor", order = 6)
|
||||
public class PostprocessExecutor extends PostRunningModule {
|
||||
|
||||
private static volatile PostprocessExecutor postprocessExecutor;
|
||||
private static final int POST_PROCESS_TRIGGER_ROLL_LIMIT = 36;
|
||||
|
||||
@InjectCapability
|
||||
private CognationCapability cognationCapability;
|
||||
|
||||
public static PostprocessExecutor getInstance() throws IOException, ClassNotFoundException {
|
||||
if (postprocessExecutor == null) {
|
||||
synchronized (PostprocessExecutor.class) {
|
||||
if (postprocessExecutor == null) {
|
||||
postprocessExecutor = new PostprocessExecutor();
|
||||
}
|
||||
}
|
||||
}
|
||||
return postprocessExecutor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(PartnerRunningFlowContext context) throws IOException, ClassNotFoundException {
|
||||
boolean trigger = cognationCapability.getChatMessages().size() >= POST_PROCESS_TRIGGER_ROLL_LIMIT;
|
||||
|
||||
@@ -4,15 +4,15 @@ import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.CapabilityHolder;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.api.agent.factory.module.annotation.Init;
|
||||
import work.slhaf.partner.core.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.submodule.perceive.PerceiveCapability;
|
||||
import work.slhaf.partner.core.submodule.perceive.pojo.User;
|
||||
import work.slhaf.partner.runtime.interaction.data.PartnerInputData;
|
||||
import work.slhaf.partner.module.common.module.PreRunningModule;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.subcontext.CoreContext;
|
||||
import work.slhaf.partner.runtime.session.SessionManager;
|
||||
import work.slhaf.partner.module.common.entity.AppendPromptData;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.HashMap;
|
||||
@@ -20,7 +20,7 @@ import java.util.HashMap;
|
||||
@Data
|
||||
@Slf4j
|
||||
@CapabilityHolder
|
||||
public class PreprocessExecutor {
|
||||
public class PreprocessExecutor extends PreRunningModule {
|
||||
|
||||
private static volatile PreprocessExecutor preprocessExecutor;
|
||||
|
||||
@@ -30,24 +30,15 @@ public class PreprocessExecutor {
|
||||
private PerceiveCapability perceiveCapability;
|
||||
private SessionManager sessionManager;
|
||||
|
||||
private PreprocessExecutor() {
|
||||
@Init
|
||||
public void init() {
|
||||
this.sessionManager = SessionManager.getInstance();
|
||||
}
|
||||
|
||||
public static PreprocessExecutor getInstance() throws IOException, ClassNotFoundException {
|
||||
if (preprocessExecutor == null) {
|
||||
synchronized (PreprocessExecutor.class) {
|
||||
if (preprocessExecutor == null) {
|
||||
preprocessExecutor = new PreprocessExecutor();
|
||||
preprocessExecutor.setSessionManager(SessionManager.getInstance());
|
||||
}
|
||||
}
|
||||
}
|
||||
return preprocessExecutor;
|
||||
}
|
||||
|
||||
public PartnerRunningFlowContext execute(PartnerInputData inputData) {
|
||||
@Override
|
||||
public void doExecute(PartnerRunningFlowContext context) {
|
||||
checkAndSetMemoryId();
|
||||
return getInteractionContext(inputData);
|
||||
getInteractionContext(context);
|
||||
}
|
||||
|
||||
private void checkAndSetMemoryId() {
|
||||
@@ -57,29 +48,25 @@ public class PreprocessExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
private PartnerRunningFlowContext getInteractionContext(PartnerInputData inputData) {
|
||||
log.debug("[PreprocessExecutor] 预处理原始输入: {}", inputData);
|
||||
PartnerRunningFlowContext context = new PartnerRunningFlowContext();
|
||||
|
||||
User user = perceiveCapability.getUser(inputData.getUserInfo(), inputData.getPlatform());
|
||||
private void getInteractionContext(PartnerRunningFlowContext context) {
|
||||
log.debug("[PreprocessExecutor] 预处理原始输入: {}", context);
|
||||
User user = perceiveCapability.getUser(context.getUserInfo(), context.getPlatform());
|
||||
if (user == null) {
|
||||
user = perceiveCapability.addUser(inputData.getUserInfo(), inputData.getPlatform(), inputData.getUserNickName());
|
||||
user = perceiveCapability.addUser(context.getUserInfo(), context.getPlatform(), context.getUserNickname());
|
||||
}
|
||||
String userId = user.getUuid();
|
||||
context.setUserId(userId);
|
||||
|
||||
String userStr = "[" + inputData.getUserNickName() + "(" + userId + ")]";
|
||||
String input = userStr + " " + inputData.getContent();
|
||||
String userStr = "[" + context.getUserNickname() + "(" + userId + ")]";
|
||||
String input = userStr + " " + context.getInput();
|
||||
context.setInput(input);
|
||||
|
||||
setAppendedPrompt(context);
|
||||
setCoreContext(inputData, context, input, userId);
|
||||
|
||||
setCoreContext(context);
|
||||
log.debug("[PreprocessExecutor] 预处理结果: {}", context);
|
||||
return context;
|
||||
}
|
||||
|
||||
private void setAppendedPrompt(PartnerRunningFlowContext context) {
|
||||
|
||||
@Override
|
||||
protected HashMap<String, String> getPromptDataMap(String userId) {
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
map.put("text", "这部分才是真正的用户输入内容, 就像你之前收到过的输入一样。但...不会是'同一个人'。");
|
||||
map.put("datetime", "本次用户输入对应的当前时间");
|
||||
@@ -87,16 +74,19 @@ public class PreprocessExecutor {
|
||||
map.put("user_id", "用户id, 与user_nick区分, 这是用户的唯一标识");
|
||||
map.put("active_modules", "已激活的模块, 为false时为激活但未活跃; 为true时为激活且活跃");
|
||||
map.put("其他", "历史对话中将在用户消息的最后一行标注时间");
|
||||
AppendPromptData data = new AppendPromptData();
|
||||
data.setModuleName("[基础模块]");
|
||||
data.setAppendedPrompt(map);
|
||||
context.setAppendedPrompt(data);
|
||||
return map;
|
||||
}
|
||||
|
||||
private void setCoreContext(PartnerInputData inputData, PartnerRunningFlowContext context, String input, String userId) {
|
||||
context.getCoreContext().setText(input);
|
||||
context.getCoreContext().setDateTime(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
|
||||
context.getCoreContext().setUserNick(inputData.getUserNickName());
|
||||
context.getCoreContext().setUserId(userId);
|
||||
@Override
|
||||
protected String moduleName() {
|
||||
return "[基础模块]";
|
||||
}
|
||||
|
||||
private void setCoreContext(PartnerRunningFlowContext context) {
|
||||
CoreContext coreContext = context.getCoreContext();
|
||||
coreContext.setText(context.getInput());
|
||||
coreContext.setDateTime(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
|
||||
coreContext.setUserNick(context.getUserNickname());
|
||||
coreContext.setUserId(context.getUserId());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,11 @@ package work.slhaf.partner.module.modules.task;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.api.agent.runtime.interaction.flow.abstracts.AgentRunningModule;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
|
||||
@Data
|
||||
@Slf4j
|
||||
public class TaskScheduler extends AgentRunningModule {
|
||||
public class TaskScheduler {
|
||||
private static TaskScheduler taskScheduler;
|
||||
|
||||
private TaskScheduler() {
|
||||
@@ -22,7 +21,6 @@ public class TaskScheduler extends AgentRunningModule {
|
||||
return taskScheduler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(PartnerRunningFlowContext runningFlowContext) {
|
||||
|
||||
}
|
||||
|
||||
@@ -3,9 +3,6 @@ package work.slhaf.partner.runtime.exception.pojo;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.core.cognation.CognationCore;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
import work.slhaf.partner.runtime.session.SessionManager;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Slf4j
|
||||
|
||||
@@ -23,6 +23,8 @@ public class PartnerInteractionAdapter extends AgentInteractionAdapter<PartnerIn
|
||||
context.setUserInfo(inputData.getUserInfo());
|
||||
context.setDateTime(inputData.getDateTime());
|
||||
context.setSingle(inputData.isSingle());
|
||||
context.setPlatform(inputData.getPlatform());
|
||||
context.setInput(inputData.getContent());
|
||||
return context;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,9 +4,9 @@ import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import work.slhaf.partner.api.agent.runtime.interaction.flow.entity.RunningFlowContext;
|
||||
import work.slhaf.partner.module.common.entity.AppendPromptData;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.subcontext.CoreContext;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.subcontext.ModuleContext;
|
||||
import work.slhaf.partner.module.common.entity.AppendPromptData;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -25,6 +25,7 @@ public class PartnerRunningFlowContext extends RunningFlowContext {
|
||||
protected String userId;
|
||||
protected String userNickname;
|
||||
protected String userInfo;
|
||||
protected String platform;
|
||||
protected LocalDateTime dateTime;
|
||||
protected boolean single;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import org.junit.jupiter.api.Test;
|
||||
import work.slhaf.partner.core.common.pojo.MemoryResult;
|
||||
import work.slhaf.partner.core.submodule.memory.MemoryCapability;
|
||||
import work.slhaf.partner.core.submodule.memory.pojo.MemoryResult;
|
||||
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.function.Function;
|
||||
|
||||
Reference in New Issue
Block a user