mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
推进感知模块相关开发,这部分倒意外地简单,现在有些基础,可能以后会有改进
- 新增 PerceiveCore 中的 updateUser 方法 - 新增了 PerceiveSelector 用于获取用户信息,提供基础的身份建模信息 - 新增 PerceiveUpdater 类用于异步更新用户身份感知 - 抽取 MemoryUpdater 中的执行判定逻辑至新增的 PostprocessExecutor 类,判定逻辑适用于多个`后处理模块` - 重构 Model 类,改为抽象类,将modelKey定义为抽象方法,强制规范子类实现
This commit is contained in:
@@ -11,7 +11,7 @@ import work.slhaf.agent.core.interaction.data.context.InteractionContext;
|
|||||||
import work.slhaf.agent.core.interaction.module.InteractionModule;
|
import work.slhaf.agent.core.interaction.module.InteractionModule;
|
||||||
import work.slhaf.agent.core.interaction.module.InteractionModulesLoader;
|
import work.slhaf.agent.core.interaction.module.InteractionModulesLoader;
|
||||||
import work.slhaf.agent.module.modules.core.CoreModel;
|
import work.slhaf.agent.module.modules.core.CoreModel;
|
||||||
import work.slhaf.agent.module.modules.preprocess.PreprocessExecutor;
|
import work.slhaf.agent.module.modules.process.PreprocessExecutor;
|
||||||
import work.slhaf.agent.module.modules.task.TaskScheduler;
|
import work.slhaf.agent.module.modules.task.TaskScheduler;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -46,7 +46,6 @@ public class InteractionHub {
|
|||||||
public void call(InteractionInputData inputData) throws IOException, ClassNotFoundException {
|
public void call(InteractionInputData inputData) throws IOException, ClassNotFoundException {
|
||||||
InteractionContext interactionContext = PreprocessExecutor.getInstance().execute(inputData);
|
InteractionContext interactionContext = PreprocessExecutor.getInstance().execute(inputData);
|
||||||
try {
|
try {
|
||||||
//预处理
|
|
||||||
for (InteractionModule interactionModule : interactionModules) {
|
for (InteractionModule interactionModule : interactionModules) {
|
||||||
interactionModule.execute(interactionContext);
|
interactionModule.execute(interactionContext);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import work.slhaf.agent.core.cognation.submodule.memory.pojo.MemorySlice;
|
|||||||
import work.slhaf.agent.core.cognation.submodule.perceive.PerceiveCapability;
|
import work.slhaf.agent.core.cognation.submodule.perceive.PerceiveCapability;
|
||||||
import work.slhaf.agent.core.cognation.submodule.perceive.PerceiveCore;
|
import work.slhaf.agent.core.cognation.submodule.perceive.PerceiveCore;
|
||||||
import work.slhaf.agent.core.cognation.submodule.perceive.pojo.User;
|
import work.slhaf.agent.core.cognation.submodule.perceive.pojo.User;
|
||||||
|
import work.slhaf.agent.module.modules.perceive.updater.pojo.PerceiveChatResult;
|
||||||
import work.slhaf.agent.shared.memory.EvaluatedSlice;
|
import work.slhaf.agent.shared.memory.EvaluatedSlice;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -285,6 +286,11 @@ public class CognationManager extends PersistableObject implements CacheCapabili
|
|||||||
return perceiveCore.addUser(userInfo, platform, userNickName);
|
return perceiveCore.addUser(userInfo, platform, userNickName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateUser(PerceiveChatResult perceiveChatResult, String userId) {
|
||||||
|
perceiveCore.updateUser(perceiveChatResult, userId);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public HashMap<String, List<EvaluatedSlice>> getActivatedSlices() {
|
public HashMap<String, List<EvaluatedSlice>> getActivatedSlices() {
|
||||||
return activeData.getActivatedSlices();
|
return activeData.getActivatedSlices();
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package work.slhaf.agent.core.cognation.submodule.perceive;
|
package work.slhaf.agent.core.cognation.submodule.perceive;
|
||||||
|
|
||||||
import work.slhaf.agent.core.cognation.submodule.perceive.pojo.User;
|
import work.slhaf.agent.core.cognation.submodule.perceive.pojo.User;
|
||||||
|
import work.slhaf.agent.module.modules.perceive.updater.pojo.PerceiveChatResult;
|
||||||
|
|
||||||
public interface PerceiveCapability {
|
public interface PerceiveCapability {
|
||||||
User getUser(String userInfo, String client);
|
User getUser(String userInfo, String client);
|
||||||
User getUser(String id);
|
User getUser(String id);
|
||||||
User addUser(String userInfo, String platform, String userNickName);
|
User addUser(String userInfo, String platform, String userNickName);
|
||||||
|
void updateUser(PerceiveChatResult perceiveChatResult, String userId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import lombok.Data;
|
|||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import work.slhaf.agent.common.serialize.PersistableObject;
|
import work.slhaf.agent.common.serialize.PersistableObject;
|
||||||
import work.slhaf.agent.core.cognation.submodule.perceive.pojo.User;
|
import work.slhaf.agent.core.cognation.submodule.perceive.pojo.User;
|
||||||
|
import work.slhaf.agent.module.modules.perceive.updater.pojo.PerceiveChatResult;
|
||||||
|
|
||||||
import java.io.Serial;
|
import java.io.Serial;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -74,4 +75,14 @@ public class PerceiveCore extends PersistableObject {
|
|||||||
usersLock.unlock();
|
usersLock.unlock();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void updateUser(PerceiveChatResult perceiveChatResult, String userId) {
|
||||||
|
usersLock.lock();
|
||||||
|
User user = selectUser(userId);
|
||||||
|
user.setRelation(perceiveChatResult.getRelation());
|
||||||
|
user.setImpressions(perceiveChatResult.getImpressions());
|
||||||
|
user.setAttitude(perceiveChatResult.getAttitude());
|
||||||
|
user.setStaticMemory(perceiveChatResult.getStaticMemory());
|
||||||
|
usersLock.unlock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import lombok.EqualsAndHashCode;
|
|||||||
import work.slhaf.agent.common.serialize.PersistableObject;
|
import work.slhaf.agent.common.serialize.PersistableObject;
|
||||||
|
|
||||||
import java.io.Serial;
|
import java.io.Serial;
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -19,21 +18,21 @@ public class User extends PersistableObject {
|
|||||||
|
|
||||||
private String uuid;
|
private String uuid;
|
||||||
private String nickName;
|
private String nickName;
|
||||||
private HashMap<String/*platform*/,String> info = new HashMap<>();
|
private HashMap<String/*platform*/, String> info = new HashMap<>();
|
||||||
|
|
||||||
private String relation;
|
private String relation = Constant.Relation.STRANGER;
|
||||||
private HashMap<LocalDate, String> events = new HashMap<>();
|
// private HashMap<LocalDate, String> events = new HashMap<>();
|
||||||
private List<String> impressions = new ArrayList<>();
|
private List<String> impressions = new ArrayList<>();
|
||||||
private List<String> attitude = new ArrayList<>();
|
private List<String> attitude = new ArrayList<>();
|
||||||
private List<String> staticMemory = new ArrayList<>();
|
private List<String> staticMemory = new ArrayList<>();
|
||||||
|
|
||||||
public void addInfo(String platform,String userInfo) {
|
public void addInfo(String platform, String userInfo) {
|
||||||
this.info.put(platform, userInfo);
|
this.info.put(platform, userInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Constant {
|
public static class Constant {
|
||||||
public static class Relation {
|
public static class Relation {
|
||||||
public static final String STRANGER = "stranger";
|
public static final String STRANGER = "陌生";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class Model {
|
public abstract class Model {
|
||||||
protected ChatClient chatClient;
|
protected ChatClient chatClient;
|
||||||
protected List<Message> chatMessages;
|
protected List<Message> chatMessages;
|
||||||
protected List<Message> baseMessages;
|
protected List<Message> baseMessages;
|
||||||
@@ -41,4 +41,6 @@ public class Model {
|
|||||||
this.chatClient.setTemperature(0.4);
|
this.chatClient.setTemperature(0.4);
|
||||||
this.chatClient.setTop_p(0.8);
|
this.chatClient.setTop_p(0.8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected abstract String modelKey();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ public class ModelConstant {
|
|||||||
public static final String MEMORY = "memory";
|
public static final String MEMORY = "memory";
|
||||||
public static final String SCHEDULE = "schedule";
|
public static final String SCHEDULE = "schedule";
|
||||||
public static final String CORE = "core";
|
public static final String CORE = "core";
|
||||||
|
public static final String PERCEIVE = "perceive";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class CharacterPrefix {
|
public static class CharacterPrefix {
|
||||||
|
|||||||
27
src/main/java/work/slhaf/agent/module/common/PreModule.java
Normal file
27
src/main/java/work/slhaf/agent/module/common/PreModule.java
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package work.slhaf.agent.module.common;
|
||||||
|
|
||||||
|
import work.slhaf.agent.core.interaction.data.context.InteractionContext;
|
||||||
|
import work.slhaf.agent.core.interaction.module.InteractionModule;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 前置模块抽象类
|
||||||
|
*/
|
||||||
|
public abstract class PreModule implements InteractionModule {
|
||||||
|
protected void setAppendedPrompt(InteractionContext context) {
|
||||||
|
AppendPromptData data = new AppendPromptData();
|
||||||
|
data.setModuleName(moduleName());
|
||||||
|
HashMap<String, String> map = getPromptDataMap(context.getUserId());
|
||||||
|
data.setAppendedPrompt(map);
|
||||||
|
context.getModuleContext().getAppendedPrompt().add(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setActiveModule(InteractionContext context) {
|
||||||
|
context.getCoreContext().addActiveModule(moduleName());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract HashMap<String, String> getPromptDataMap(String userId);
|
||||||
|
|
||||||
|
protected abstract String moduleName();
|
||||||
|
}
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
package work.slhaf.agent.module.common;
|
|
||||||
|
|
||||||
import work.slhaf.agent.core.interaction.data.context.InteractionContext;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 用于在前置模块设置追加提示词
|
|
||||||
*/
|
|
||||||
public interface PreModuleActions {
|
|
||||||
void setAppendedPrompt(InteractionContext context);
|
|
||||||
void setActiveModule(InteractionContext context);
|
|
||||||
String getModuleName();
|
|
||||||
}
|
|
||||||
@@ -12,10 +12,8 @@ import work.slhaf.agent.core.cognation.submodule.cache.CacheCapability;
|
|||||||
import work.slhaf.agent.core.cognation.submodule.memory.MemoryCapability;
|
import work.slhaf.agent.core.cognation.submodule.memory.MemoryCapability;
|
||||||
import work.slhaf.agent.core.cognation.submodule.memory.pojo.MemorySlice;
|
import work.slhaf.agent.core.cognation.submodule.memory.pojo.MemorySlice;
|
||||||
import work.slhaf.agent.core.interaction.data.context.InteractionContext;
|
import work.slhaf.agent.core.interaction.data.context.InteractionContext;
|
||||||
import work.slhaf.agent.core.interaction.module.InteractionModule;
|
|
||||||
import work.slhaf.agent.core.session.SessionManager;
|
import work.slhaf.agent.core.session.SessionManager;
|
||||||
import work.slhaf.agent.module.common.AppendPromptData;
|
import work.slhaf.agent.module.common.PreModule;
|
||||||
import work.slhaf.agent.module.common.PreModuleActions;
|
|
||||||
import work.slhaf.agent.module.modules.memory.selector.evaluator.SliceSelectEvaluator;
|
import work.slhaf.agent.module.modules.memory.selector.evaluator.SliceSelectEvaluator;
|
||||||
import work.slhaf.agent.module.modules.memory.selector.evaluator.data.EvaluatorInput;
|
import work.slhaf.agent.module.modules.memory.selector.evaluator.data.EvaluatorInput;
|
||||||
import work.slhaf.agent.module.modules.memory.selector.extractor.MemorySelectExtractor;
|
import work.slhaf.agent.module.modules.memory.selector.extractor.MemorySelectExtractor;
|
||||||
@@ -32,7 +30,7 @@ import java.util.List;
|
|||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class MemorySelector implements InteractionModule, PreModuleActions {
|
public class MemorySelector extends PreModule {
|
||||||
|
|
||||||
private static volatile MemorySelector memorySelector;
|
private static volatile MemorySelector memorySelector;
|
||||||
|
|
||||||
@@ -152,26 +150,11 @@ public class MemorySelector implements InteractionModule, PreModuleActions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setAppendedPrompt(InteractionContext context) {
|
public String moduleName() {
|
||||||
String userId = context.getUserId();
|
|
||||||
HashMap<String, String> map = getPromptDataMap(userId);
|
|
||||||
AppendPromptData data = new AppendPromptData();
|
|
||||||
data.setModuleName(getModuleName());
|
|
||||||
data.setAppendedPrompt(map);
|
|
||||||
context.setAppendedPrompt(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setActiveModule(InteractionContext context) {
|
|
||||||
context.getCoreContext().addActiveModule(getModuleName());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getModuleName() {
|
|
||||||
return "[记忆模块]";
|
return "[记忆模块]";
|
||||||
}
|
}
|
||||||
|
|
||||||
private HashMap<String, String> getPromptDataMap(String userId) {
|
protected HashMap<String, String> getPromptDataMap(String userId) {
|
||||||
HashMap<String, String> map = new HashMap<>();
|
HashMap<String, String> map = new HashMap<>();
|
||||||
String dialogMapStr = cacheCapability.getDialogMapStr();
|
String dialogMapStr = cacheCapability.getDialogMapStr();
|
||||||
if (!dialogMapStr.isEmpty()) {
|
if (!dialogMapStr.isEmpty()) {
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ import static work.slhaf.agent.common.util.ExtractUtil.extractJson;
|
|||||||
@Data
|
@Data
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class SliceSelectEvaluator extends Model {
|
public class SliceSelectEvaluator extends Model {
|
||||||
public static final String MODEL_KEY = "slice_evaluator";
|
|
||||||
private static volatile SliceSelectEvaluator sliceSelectEvaluator;
|
private static volatile SliceSelectEvaluator sliceSelectEvaluator;
|
||||||
private InteractionThreadPoolExecutor executor;
|
private InteractionThreadPoolExecutor executor;
|
||||||
|
|
||||||
@@ -44,7 +43,7 @@ public class SliceSelectEvaluator extends Model {
|
|||||||
if (sliceSelectEvaluator == null) {
|
if (sliceSelectEvaluator == null) {
|
||||||
sliceSelectEvaluator = new SliceSelectEvaluator();
|
sliceSelectEvaluator = new SliceSelectEvaluator();
|
||||||
sliceSelectEvaluator.setExecutor(InteractionThreadPoolExecutor.getInstance());
|
sliceSelectEvaluator.setExecutor(InteractionThreadPoolExecutor.getInstance());
|
||||||
setModel(sliceSelectEvaluator, MODEL_KEY, ModelConstant.Prompt.MEMORY, false);
|
setModel(sliceSelectEvaluator, sliceSelectEvaluator.modelKey(), ModelConstant.Prompt.MEMORY, false);
|
||||||
log.info("SliceEvaluator注册完毕...");
|
log.info("SliceEvaluator注册完毕...");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -135,4 +134,8 @@ public class SliceSelectEvaluator extends Model {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String modelKey() {
|
||||||
|
return "slice_evaluator";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ import static work.slhaf.agent.common.util.ExtractUtil.fixTopicPath;
|
|||||||
@Data
|
@Data
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class MemorySelectExtractor extends Model {
|
public class MemorySelectExtractor extends Model {
|
||||||
public static final String MODEL_KEY = "topic_extractor";
|
|
||||||
private static volatile MemorySelectExtractor memorySelectExtractor;
|
private static volatile MemorySelectExtractor memorySelectExtractor;
|
||||||
|
|
||||||
private MemoryCapability memoryCapability;
|
private MemoryCapability memoryCapability;
|
||||||
@@ -50,7 +49,7 @@ public class MemorySelectExtractor extends Model {
|
|||||||
memorySelectExtractor.setMemoryCapability(CognationManager.getInstance());
|
memorySelectExtractor.setMemoryCapability(CognationManager.getInstance());
|
||||||
memorySelectExtractor.setCognationCapability(CognationManager.getInstance());
|
memorySelectExtractor.setCognationCapability(CognationManager.getInstance());
|
||||||
memorySelectExtractor.setSessionManager(SessionManager.getInstance());
|
memorySelectExtractor.setSessionManager(SessionManager.getInstance());
|
||||||
setModel(memorySelectExtractor, MODEL_KEY, ModelConstant.Prompt.MEMORY, false);
|
setModel(memorySelectExtractor, memorySelectExtractor.modelKey(), ModelConstant.Prompt.MEMORY, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -106,4 +105,8 @@ public class MemorySelectExtractor extends Model {
|
|||||||
return extractorResult;
|
return extractorResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String modelKey() {
|
||||||
|
return "topic_extractor";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,9 +36,6 @@ public class MemoryUpdater implements InteractionModule {
|
|||||||
|
|
||||||
private static final long SCHEDULED_UPDATE_INTERVAL = 10 * 1000;
|
private static final long SCHEDULED_UPDATE_INTERVAL = 10 * 1000;
|
||||||
private static final long UPDATE_TRIGGER_INTERVAL = 60 * 60 * 1000;
|
private static final long UPDATE_TRIGGER_INTERVAL = 60 * 60 * 1000;
|
||||||
// private static final int TRIGGER_TOKEN_LIMIT = 5 * 1000;
|
|
||||||
private static final int TOKEN_PER_RECALL = 230;
|
|
||||||
private static final int TRIGGER_ROLL_LIMIT = 36;
|
|
||||||
|
|
||||||
private CognationCapability cognationCapability;
|
private CognationCapability cognationCapability;
|
||||||
private MemoryCapability memoryCapability;
|
private MemoryCapability memoryCapability;
|
||||||
@@ -101,31 +98,32 @@ public class MemoryUpdater implements InteractionModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(InteractionContext interactionContext) {
|
public void execute(InteractionContext context) {
|
||||||
if (interactionContext.isFinished()) {
|
if (context.isFinished()) {
|
||||||
log.warn("[MemoryUpdater] 流程强制结束, 不触发记忆被动更新机制");
|
log.warn("[MemoryUpdater] 流程强制结束, 不触发记忆被动更新机制");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
executor.execute(() -> {
|
executor.execute(() -> {
|
||||||
//如果token 大于阈值,则更新记忆
|
//如果token 大于阈值,则更新记忆
|
||||||
JSONObject moduleContext = interactionContext.getModuleContext().getExtraContext();
|
JSONObject moduleContext = context.getModuleContext().getExtraContext();
|
||||||
boolean recall = moduleContext.getBoolean("recall");
|
boolean recall = moduleContext.getBoolean("recall");
|
||||||
if (recall) {
|
if (recall) {
|
||||||
log.debug("[MemoryUpdater] 存在回忆");
|
log.debug("[MemoryUpdater] 存在回忆");
|
||||||
int recallCount = moduleContext.getIntValue("recall_count");
|
int recallCount = moduleContext.getIntValue("recall_count");
|
||||||
log.debug("[MemoryUpdater] 记忆切片数量 [{}]", recallCount);
|
log.debug("[MemoryUpdater] 记忆切片数量 [{}]", recallCount);
|
||||||
}
|
}
|
||||||
int messageCount = cognationCapability.getChatMessages().size();
|
boolean trigger = context.getModuleContext().getExtraContext().getBoolean("post_process_trigger");
|
||||||
if (messageCount >= TRIGGER_ROLL_LIMIT) {
|
if (!trigger) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
log.debug("[MemoryUpdater] 记忆更新: 已达{}轮", TRIGGER_ROLL_LIMIT);
|
log.debug("[MemoryUpdater] 记忆更新触发");
|
||||||
updateMemory();
|
updateMemory();
|
||||||
//清空chatMessages
|
//清空chatMessages
|
||||||
clearChatMessages();
|
clearChatMessages();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("[MemoryUpdater] 记忆更新线程出错: ", e);
|
log.error("[MemoryUpdater] 记忆更新线程出错: ", e);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,9 +193,9 @@ public class MemoryUpdater implements InteractionModule {
|
|||||||
private void clearChatMessages() {
|
private void clearChatMessages() {
|
||||||
//不全部清空,保留一部分输入防止上下文割裂
|
//不全部清空,保留一部分输入防止上下文割裂
|
||||||
cognationCapability.getMessageLock().lock();
|
cognationCapability.getMessageLock().lock();
|
||||||
List<Message> temp = new ArrayList<>(tempMessage.subList(tempMessage.size() - TRIGGER_ROLL_LIMIT / 6, tempMessage.size()));
|
List<Message> temp = new ArrayList<>(tempMessage.subList(tempMessage.size() - tempMessage.size() / 6, tempMessage.size()));
|
||||||
cognationCapability.getChatMessages().clear();
|
cognationCapability.getChatMessages().removeAll(tempMessage);
|
||||||
cognationCapability.getChatMessages().addAll(temp);
|
cognationCapability.getChatMessages().addAll(0, temp);
|
||||||
cognationCapability.getMessageLock().unlock();
|
cognationCapability.getMessageLock().unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ import static work.slhaf.agent.common.util.ExtractUtil.fixTopicPath;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class MultiSummarizer extends Model {
|
public class MultiSummarizer extends Model {
|
||||||
|
|
||||||
public static final String MODEL_KEY = "multi_summarizer";
|
|
||||||
private static volatile MultiSummarizer multiSummarizer;
|
private static volatile MultiSummarizer multiSummarizer;
|
||||||
|
|
||||||
public static MultiSummarizer getInstance() {
|
public static MultiSummarizer getInstance() {
|
||||||
@@ -30,7 +29,7 @@ public class MultiSummarizer extends Model {
|
|||||||
synchronized (MultiSummarizer.class) {
|
synchronized (MultiSummarizer.class) {
|
||||||
if (multiSummarizer == null) {
|
if (multiSummarizer == null) {
|
||||||
multiSummarizer = new MultiSummarizer();
|
multiSummarizer = new MultiSummarizer();
|
||||||
setModel(multiSummarizer, MODEL_KEY, ModelConstant.Prompt.MEMORY, true);
|
setModel(multiSummarizer, multiSummarizer.modelKey(), ModelConstant.Prompt.MEMORY, true);
|
||||||
multiSummarizer.updateChatClientSettings();
|
multiSummarizer.updateChatClientSettings();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -61,4 +60,8 @@ public class MultiSummarizer extends Model {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String modelKey() {
|
||||||
|
return "multi_summarizer";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|||||||
@Data
|
@Data
|
||||||
public class SingleSummarizer extends Model {
|
public class SingleSummarizer extends Model {
|
||||||
|
|
||||||
public static final String MODEL_KEY = "single_summarizer";
|
|
||||||
private static volatile SingleSummarizer singleSummarizer;
|
private static volatile SingleSummarizer singleSummarizer;
|
||||||
|
|
||||||
private InteractionThreadPoolExecutor executor;
|
private InteractionThreadPoolExecutor executor;
|
||||||
@@ -33,7 +32,7 @@ public class SingleSummarizer extends Model {
|
|||||||
if (singleSummarizer == null) {
|
if (singleSummarizer == null) {
|
||||||
singleSummarizer = new SingleSummarizer();
|
singleSummarizer = new SingleSummarizer();
|
||||||
singleSummarizer.setExecutor(InteractionThreadPoolExecutor.getInstance());
|
singleSummarizer.setExecutor(InteractionThreadPoolExecutor.getInstance());
|
||||||
setModel(singleSummarizer, MODEL_KEY, ModelConstant.Prompt.MEMORY, false);
|
setModel(singleSummarizer, singleSummarizer.modelKey(), ModelConstant.Prompt.MEMORY, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -72,4 +71,8 @@ public class SingleSummarizer extends Model {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String modelKey() {
|
||||||
|
return "single_summarizer";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import static work.slhaf.agent.common.util.ExtractUtil.extractJson;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class TotalSummarizer extends Model {
|
public class TotalSummarizer extends Model {
|
||||||
|
|
||||||
public static final String MODEL_KEY = "total_summarizer";
|
|
||||||
private static volatile TotalSummarizer totalSummarizer;
|
private static volatile TotalSummarizer totalSummarizer;
|
||||||
|
|
||||||
public static TotalSummarizer getInstance() {
|
public static TotalSummarizer getInstance() {
|
||||||
@@ -26,7 +25,7 @@ public class TotalSummarizer extends Model {
|
|||||||
synchronized (TotalSummarizer.class) {
|
synchronized (TotalSummarizer.class) {
|
||||||
if (totalSummarizer == null) {
|
if (totalSummarizer == null) {
|
||||||
totalSummarizer = new TotalSummarizer();
|
totalSummarizer = new TotalSummarizer();
|
||||||
setModel(totalSummarizer, MODEL_KEY, ModelConstant.Prompt.MEMORY, true);
|
setModel(totalSummarizer, totalSummarizer.modelKey(), ModelConstant.Prompt.MEMORY, true);
|
||||||
totalSummarizer.updateChatClientSettings();
|
totalSummarizer.updateChatClientSettings();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -38,4 +37,9 @@ public class TotalSummarizer extends Model {
|
|||||||
ChatResponse response = this.singleChat(JSONUtil.toJsonPrettyStr(singleMemorySummary));
|
ChatResponse response = this.singleChat(JSONUtil.toJsonPrettyStr(singleMemorySummary));
|
||||||
return JSONObject.parseObject(extractJson(response.getMessage())).getString("content");
|
return JSONObject.parseObject(extractJson(response.getMessage())).getString("content");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String modelKey() {
|
||||||
|
return "total_summarizer";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,24 +2,29 @@ package work.slhaf.agent.module.modules.perceive.selector;
|
|||||||
|
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import work.slhaf.agent.core.cognation.CognationManager;
|
||||||
|
import work.slhaf.agent.core.cognation.submodule.perceive.PerceiveCapability;
|
||||||
|
import work.slhaf.agent.core.cognation.submodule.perceive.pojo.User;
|
||||||
import work.slhaf.agent.core.interaction.data.context.InteractionContext;
|
import work.slhaf.agent.core.interaction.data.context.InteractionContext;
|
||||||
import work.slhaf.agent.core.interaction.module.InteractionModule;
|
import work.slhaf.agent.module.common.PreModule;
|
||||||
import work.slhaf.agent.module.common.PreModuleActions;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Setter
|
@Setter
|
||||||
public class PerceiveSelector implements InteractionModule, PreModuleActions {
|
public class PerceiveSelector extends PreModule {
|
||||||
|
|
||||||
private static volatile PerceiveSelector perceiveSelector;
|
private static volatile PerceiveSelector perceiveSelector;
|
||||||
|
|
||||||
|
private PerceiveCapability perceiveCapability;
|
||||||
|
|
||||||
public static PerceiveSelector getInstance() throws IOException, ClassNotFoundException {
|
public static PerceiveSelector getInstance() throws IOException, ClassNotFoundException {
|
||||||
if (perceiveSelector == null) {
|
if (perceiveSelector == null) {
|
||||||
synchronized (PerceiveSelector.class) {
|
synchronized (PerceiveSelector.class) {
|
||||||
if (perceiveSelector == null) {
|
if (perceiveSelector == null) {
|
||||||
perceiveSelector = new PerceiveSelector();
|
perceiveSelector = new PerceiveSelector();
|
||||||
|
perceiveSelector.setPerceiveCapability(CognationManager.getInstance());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -28,21 +33,26 @@ public class PerceiveSelector implements InteractionModule, PreModuleActions {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(InteractionContext context) throws IOException, ClassNotFoundException {
|
public void execute(InteractionContext context) throws IOException, ClassNotFoundException {
|
||||||
|
log.debug("[PerceiveSelector] 感知模块处理流程开始...");
|
||||||
|
//处理思路: 根据用户id,查询用户相关身份感知数据,直接添加到appendPrompt中,这直接执行appendPrompt方法应该可以
|
||||||
|
setAppendedPrompt(context);
|
||||||
|
setActiveModule(context);
|
||||||
|
log.debug("[PerceiveSelector] 感知模块处理流程结束...");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setAppendedPrompt(InteractionContext context) {
|
protected HashMap<String, String> getPromptDataMap(String userId) {
|
||||||
|
HashMap<String, String> map = new HashMap<>();
|
||||||
|
User user = perceiveCapability.getUser(userId);
|
||||||
|
map.put("[关系] <你与最新聊天用户的关系>", user.getRelation());
|
||||||
|
map.put("[态度] <你对于最新聊天用户的态度>", user.getAttitude().toString());
|
||||||
|
map.put("[印象] <你对于最新聊天用户的印象>", user.getImpressions().toString());
|
||||||
|
map.put("[静态记忆] <你关于最新聊天用户的静态记忆>", user.getStaticMemory().toString());
|
||||||
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setActiveModule(InteractionContext context) {
|
public String moduleName() {
|
||||||
|
return "[感知模块]";
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getModuleName() {
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,94 @@
|
|||||||
|
package work.slhaf.agent.module.modules.perceive.updater;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import work.slhaf.agent.common.chat.pojo.ChatResponse;
|
||||||
|
import work.slhaf.agent.common.chat.pojo.Message;
|
||||||
|
import work.slhaf.agent.common.thread.InteractionThreadPoolExecutor;
|
||||||
|
import work.slhaf.agent.core.cognation.CognationCapability;
|
||||||
|
import work.slhaf.agent.core.cognation.CognationManager;
|
||||||
|
import work.slhaf.agent.core.cognation.submodule.perceive.PerceiveCapability;
|
||||||
|
import work.slhaf.agent.core.cognation.submodule.perceive.pojo.User;
|
||||||
|
import work.slhaf.agent.core.interaction.data.context.InteractionContext;
|
||||||
|
import work.slhaf.agent.core.interaction.module.InteractionModule;
|
||||||
|
import work.slhaf.agent.module.common.Model;
|
||||||
|
import work.slhaf.agent.module.common.ModelConstant;
|
||||||
|
import work.slhaf.agent.module.modules.perceive.updater.pojo.PerceiveChatInput;
|
||||||
|
import work.slhaf.agent.module.modules.perceive.updater.pojo.PerceiveChatResult;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 感知更新,异步
|
||||||
|
*/
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Slf4j
|
||||||
|
@Data
|
||||||
|
public class PerceiveUpdater extends Model implements InteractionModule {
|
||||||
|
|
||||||
|
private static volatile PerceiveUpdater perceiveUpdater;
|
||||||
|
private PerceiveCapability perceiveCapability;
|
||||||
|
private CognationCapability cognationCapability;
|
||||||
|
private InteractionThreadPoolExecutor executor;
|
||||||
|
|
||||||
|
private List<Message> tempMessages;
|
||||||
|
|
||||||
|
public static PerceiveUpdater getInstance() throws IOException, ClassNotFoundException {
|
||||||
|
if (perceiveUpdater == null) {
|
||||||
|
synchronized (PerceiveUpdater.class) {
|
||||||
|
if (perceiveUpdater == null) {
|
||||||
|
perceiveUpdater = new PerceiveUpdater();
|
||||||
|
perceiveUpdater.setPerceiveCapability(CognationManager.getInstance());
|
||||||
|
perceiveUpdater.setCognationCapability(CognationManager.getInstance());
|
||||||
|
perceiveUpdater.setExecutor(InteractionThreadPoolExecutor.getInstance());
|
||||||
|
setModel(perceiveUpdater, perceiveUpdater.modelKey(), ModelConstant.Prompt.PERCEIVE, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return perceiveUpdater;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(InteractionContext context) throws IOException, ClassNotFoundException {
|
||||||
|
executor.execute(() -> {
|
||||||
|
boolean trigger = context.getModuleContext().getExtraContext().getBoolean("perceive_updater");
|
||||||
|
if (!trigger){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
tempMessages = new ArrayList<>(cognationCapability.getChatMessages());
|
||||||
|
String userId = context.getUserId();
|
||||||
|
PerceiveChatInput input = getPerceiveInput(userId);
|
||||||
|
PerceiveChatResult perceiveChatResult = getPerceiveResult(input);
|
||||||
|
perceiveCapability.updateUser(perceiveChatResult,context.getUserId());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private PerceiveChatResult getPerceiveResult(PerceiveChatInput input) {
|
||||||
|
ChatResponse response = singleChat(JSONObject.toJSONString(input));
|
||||||
|
return JSONObject.parseObject(response.getMessage(), PerceiveChatResult.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private PerceiveChatInput getPerceiveInput(String userId) {
|
||||||
|
HashMap<String,String> map = new HashMap<>();
|
||||||
|
User user = perceiveCapability.getUser(userId);
|
||||||
|
map.put("[用户昵称] <用户的昵称信息>",user.getNickName());
|
||||||
|
map.put("[关系] <你与用户的关系>", user.getRelation());
|
||||||
|
map.put("[态度] <你对于用户的态度>", user.getAttitude().toString());
|
||||||
|
map.put("[印象] <你对于用户的印象>", user.getImpressions().toString());
|
||||||
|
map.put("[静态记忆] <你关于用户的静态记忆>", user.getStaticMemory().toString());
|
||||||
|
PerceiveChatInput input = new PerceiveChatInput();
|
||||||
|
input.setPrimaryUserPerceive(map);
|
||||||
|
input.setChatMessages(tempMessages);
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String modelKey() {
|
||||||
|
return "perceive_updater";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package work.slhaf.agent.module.modules.perceive.updater.pojo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import work.slhaf.agent.common.chat.pojo.Message;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class PerceiveChatInput {
|
||||||
|
private HashMap<String,String> primaryUserPerceive;
|
||||||
|
private List<Message> chatMessages;
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package work.slhaf.agent.module.modules.perceive.updater.pojo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class PerceiveChatResult {
|
||||||
|
private String relation;
|
||||||
|
private List<String> impressions;
|
||||||
|
private List<String> attitude;
|
||||||
|
private List<String> staticMemory;
|
||||||
|
}
|
||||||
@@ -18,15 +18,13 @@ public class StaticPerceiveExtractor extends Model {
|
|||||||
|
|
||||||
private static volatile StaticPerceiveExtractor staticPerceiveExtractor;
|
private static volatile StaticPerceiveExtractor staticPerceiveExtractor;
|
||||||
|
|
||||||
public static final String MODEL_KEY = "static_extractor";
|
|
||||||
|
|
||||||
|
|
||||||
public static StaticPerceiveExtractor getInstance() {
|
public static StaticPerceiveExtractor getInstance() {
|
||||||
if (staticPerceiveExtractor == null) {
|
if (staticPerceiveExtractor == null) {
|
||||||
synchronized (StaticPerceiveExtractor.class) {
|
synchronized (StaticPerceiveExtractor.class) {
|
||||||
if (staticPerceiveExtractor == null) {
|
if (staticPerceiveExtractor == null) {
|
||||||
staticPerceiveExtractor = new StaticPerceiveExtractor();
|
staticPerceiveExtractor = new StaticPerceiveExtractor();
|
||||||
setModel(staticPerceiveExtractor, MODEL_KEY, ModelConstant.Prompt.MEMORY, true);
|
setModel(staticPerceiveExtractor, staticPerceiveExtractor.modelKey(), ModelConstant.Prompt.MEMORY, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -40,4 +38,9 @@ public class StaticPerceiveExtractor extends Model {
|
|||||||
jsonObject.forEach((k, v) -> result.put(k, (String) v));
|
jsonObject.forEach((k, v) -> result.put(k, (String) v));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String modelKey() {
|
||||||
|
return "static_extractor";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package work.slhaf.agent.module.modules.process;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import work.slhaf.agent.core.cognation.CognationCapability;
|
||||||
|
import work.slhaf.agent.core.cognation.CognationManager;
|
||||||
|
import work.slhaf.agent.core.interaction.data.context.InteractionContext;
|
||||||
|
import work.slhaf.agent.core.interaction.module.InteractionModule;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Data
|
||||||
|
public class PostprocessExecutor implements InteractionModule {
|
||||||
|
|
||||||
|
private static volatile PostprocessExecutor postprocessExecutor;
|
||||||
|
private static final int POST_PROCESS_TRIGGER_ROLL_LIMIT = 36;
|
||||||
|
|
||||||
|
private CognationCapability cognationCapability;
|
||||||
|
|
||||||
|
public static PostprocessExecutor getInstance() throws IOException, ClassNotFoundException {
|
||||||
|
if (postprocessExecutor == null) {
|
||||||
|
synchronized (PostprocessExecutor.class) {
|
||||||
|
if (postprocessExecutor == null) {
|
||||||
|
postprocessExecutor = new PostprocessExecutor();
|
||||||
|
postprocessExecutor.setCognationCapability(CognationManager.getInstance());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return postprocessExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(InteractionContext context) throws IOException, ClassNotFoundException {
|
||||||
|
boolean trigger = cognationCapability.getChatMessages().size() >= POST_PROCESS_TRIGGER_ROLL_LIMIT;
|
||||||
|
context.getModuleContext().getExtraContext().put("post_process_trigger", trigger);
|
||||||
|
log.debug("[PostprocessExecutor] 是否执行后处理: {}", trigger);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package work.slhaf.agent.module.modules.preprocess;
|
package work.slhaf.agent.module.modules.process;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
Reference in New Issue
Block a user