mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
重构认知模块、着手感知模块相关开发
- 调整MemoryManager为CognationManager - 由于CognationManager方法数量过多,根据能力分类抽取为四个主要接口CognationCapability、CacheCapability、MemoryCapability、PerceiveCapability - 开始推进感知模块开发
This commit is contained in:
@@ -3,8 +3,8 @@ package work.slhaf.agent.common.exception_handler.pojo;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import work.slhaf.agent.core.cognation.CognationManager;
|
||||||
import work.slhaf.agent.core.interaction.data.context.InteractionContext;
|
import work.slhaf.agent.core.interaction.data.context.InteractionContext;
|
||||||
import work.slhaf.agent.core.memory.MemoryManager;
|
|
||||||
import work.slhaf.agent.core.session.SessionManager;
|
import work.slhaf.agent.core.session.SessionManager;
|
||||||
|
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
@@ -20,7 +20,7 @@ public class GlobalException extends RuntimeException {
|
|||||||
this.data = new GlobalExceptionData();
|
this.data = new GlobalExceptionData();
|
||||||
this.data.setExceptionTime(System.currentTimeMillis());
|
this.data.setExceptionTime(System.currentTimeMillis());
|
||||||
this.data.setSessionManager(SessionManager.getInstance());
|
this.data.setSessionManager(SessionManager.getInstance());
|
||||||
this.data.setMemoryManager(MemoryManager.getInstance());
|
this.data.setCognationManager(CognationManager.getInstance());
|
||||||
this.data.setContext(InteractionContext.getInstance());
|
this.data.setContext(InteractionContext.getInstance());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("[GlobalException] 捕获异常, 获取数据失败");
|
log.error("[GlobalException] 捕获异常, 获取数据失败");
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ package work.slhaf.agent.common.exception_handler.pojo;
|
|||||||
import lombok.Data;
|
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.CognationManager;
|
||||||
import work.slhaf.agent.core.interaction.data.context.InteractionContext;
|
import work.slhaf.agent.core.interaction.data.context.InteractionContext;
|
||||||
import work.slhaf.agent.core.memory.MemoryManager;
|
|
||||||
import work.slhaf.agent.core.session.SessionManager;
|
import work.slhaf.agent.core.session.SessionManager;
|
||||||
|
|
||||||
import java.io.Serial;
|
import java.io.Serial;
|
||||||
@@ -21,6 +21,6 @@ public class GlobalExceptionData extends PersistableObject {
|
|||||||
|
|
||||||
protected HashMap<String, InteractionContext> context;
|
protected HashMap<String, InteractionContext> context;
|
||||||
protected SessionManager sessionManager;
|
protected SessionManager sessionManager;
|
||||||
protected MemoryManager memoryManager;
|
protected CognationManager cognationManager;
|
||||||
protected Long exceptionTime;
|
protected Long exceptionTime;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package work.slhaf.agent.core.cognation;
|
||||||
|
|
||||||
|
import work.slhaf.agent.common.chat.pojo.Message;
|
||||||
|
import work.slhaf.agent.shared.memory.EvaluatedSlice;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
|
public interface CognationCapability {
|
||||||
|
List<Message> getChatMessages();
|
||||||
|
void setChatMessages(List<Message> chatMessages);
|
||||||
|
void cleanMessage(List<Message> messages);
|
||||||
|
void updateActivatedSlices(String userId, List<EvaluatedSlice> memorySlices);
|
||||||
|
String getActivatedSlicesStr(String userId);
|
||||||
|
HashMap<String, List<EvaluatedSlice>> getActivatedSlices();
|
||||||
|
void clearActivatedSlices(String userId);
|
||||||
|
boolean hasActivatedSlices(String userId);
|
||||||
|
int getActivatedSlicesSize(String userId);
|
||||||
|
List<EvaluatedSlice> getActivatedSlices(String userId);
|
||||||
|
boolean isSingleUser();
|
||||||
|
ReentrantLock getMessageLock();
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package work.slhaf.agent.core.memory;
|
package work.slhaf.agent.core.cognation;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
@@ -6,61 +6,62 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
import work.slhaf.agent.common.chat.pojo.Message;
|
import work.slhaf.agent.common.chat.pojo.Message;
|
||||||
import work.slhaf.agent.common.serialize.PersistableObject;
|
import work.slhaf.agent.common.serialize.PersistableObject;
|
||||||
import work.slhaf.agent.core.memory.submodule.cache.CacheCore;
|
import work.slhaf.agent.core.cognation.submodule.cache.CacheCore;
|
||||||
import work.slhaf.agent.core.memory.submodule.graph.GraphCore;
|
import work.slhaf.agent.core.cognation.submodule.memory.MemoryCore;
|
||||||
import work.slhaf.agent.core.memory.submodule.perceive.PerceiveCore;
|
import work.slhaf.agent.core.cognation.submodule.perceive.PerceiveCore;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.nio.file.StandardCopyOption;
|
import java.nio.file.StandardCopyOption;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
@Data
|
@Data
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class MemoryCore extends PersistableObject {
|
public class CognationCore extends PersistableObject {
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private static final String STORAGE_DIR = "./data/memory/";
|
private static final String STORAGE_DIR = "./data/memory/";
|
||||||
private static volatile MemoryCore memoryCore;
|
private static volatile CognationCore cognationCore;
|
||||||
|
|
||||||
private String id;
|
private String id;
|
||||||
private GraphCore graphCore = new GraphCore();
|
private MemoryCore memoryCore = new MemoryCore();
|
||||||
private CacheCore cacheCore = new CacheCore();
|
private CacheCore cacheCore = new CacheCore();
|
||||||
private PerceiveCore perceiveCore = new PerceiveCore();
|
private PerceiveCore perceiveCore = new PerceiveCore();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 主模型的聊天记录
|
* 主模型的聊天记录
|
||||||
*/
|
*/
|
||||||
private List<Message> chatMessages;
|
private List<Message> chatMessages = new ArrayList<>();
|
||||||
|
|
||||||
public MemoryCore(String id) {
|
public CognationCore(String id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MemoryCore getInstance(String id) throws IOException, ClassNotFoundException {
|
public static CognationCore getInstance(String id) throws IOException, ClassNotFoundException {
|
||||||
if (memoryCore == null) {
|
if (cognationCore == null) {
|
||||||
synchronized (MemoryCore.class) {
|
synchronized (CognationCore.class) {
|
||||||
// 检查存储目录是否存在,不存在则创建
|
// 检查存储目录是否存在,不存在则创建
|
||||||
if (memoryCore == null) {
|
if (cognationCore == null) {
|
||||||
createStorageDirectory();
|
createStorageDirectory();
|
||||||
Path filePath = getFilePath(id);
|
Path filePath = getFilePath(id);
|
||||||
if (Files.exists(filePath)) {
|
if (Files.exists(filePath)) {
|
||||||
memoryCore = deserialize(id);
|
cognationCore = deserialize(id);
|
||||||
} else {
|
} else {
|
||||||
FileUtils.createParentDirectories(filePath.toFile().getParentFile());
|
FileUtils.createParentDirectories(filePath.toFile().getParentFile());
|
||||||
memoryCore = new MemoryCore(id);
|
cognationCore = new CognationCore(id);
|
||||||
memoryCore.serialize();
|
cognationCore.serialize();
|
||||||
}
|
}
|
||||||
log.info("MemoryGraph注册完毕...");
|
log.info("CognationCore注册完毕...");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return memoryCore;
|
return cognationCore;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void serialize() throws IOException {
|
public void serialize() throws IOException {
|
||||||
@@ -73,19 +74,19 @@ public class MemoryCore extends PersistableObject {
|
|||||||
oos.close();
|
oos.close();
|
||||||
Path path = getFilePath(this.id);
|
Path path = getFilePath(this.id);
|
||||||
Files.move(filePath, path, StandardCopyOption.REPLACE_EXISTING);
|
Files.move(filePath, path, StandardCopyOption.REPLACE_EXISTING);
|
||||||
log.info("MemoryCore 已保存到: {}", path);
|
log.info("CognationCore 已保存到: {}", path);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Files.delete(filePath);
|
Files.delete(filePath);
|
||||||
log.error("序列化保存失败: {}", e.getMessage());
|
log.error("序列化保存失败: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static MemoryCore deserialize(String id) throws IOException, ClassNotFoundException {
|
private static CognationCore deserialize(String id) throws IOException, ClassNotFoundException {
|
||||||
Path filePath = getFilePath(id);
|
Path filePath = getFilePath(id);
|
||||||
try (ObjectInputStream ois = new ObjectInputStream(
|
try (ObjectInputStream ois = new ObjectInputStream(
|
||||||
new FileInputStream(filePath.toFile()))) {
|
new FileInputStream(filePath.toFile()))) {
|
||||||
MemoryCore graph = (MemoryCore) ois.readObject();
|
CognationCore graph = (CognationCore) ois.readObject();
|
||||||
log.info("MemoryCore 已从文件加载: {}", filePath);
|
log.info("CognationCore 已从文件加载: {}", filePath);
|
||||||
return graph;
|
return graph;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package work.slhaf.agent.core.memory;
|
package work.slhaf.agent.core.cognation;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
@@ -9,13 +9,18 @@ import work.slhaf.agent.common.config.Config;
|
|||||||
import work.slhaf.agent.common.exception_handler.GlobalExceptionHandler;
|
import work.slhaf.agent.common.exception_handler.GlobalExceptionHandler;
|
||||||
import work.slhaf.agent.common.exception_handler.pojo.GlobalException;
|
import work.slhaf.agent.common.exception_handler.pojo.GlobalException;
|
||||||
import work.slhaf.agent.common.serialize.PersistableObject;
|
import work.slhaf.agent.common.serialize.PersistableObject;
|
||||||
import work.slhaf.agent.core.memory.pojo.MemoryResult;
|
import work.slhaf.agent.core.cognation.common.exception.UserNotExistsException;
|
||||||
import work.slhaf.agent.core.memory.pojo.MemorySliceResult;
|
import work.slhaf.agent.core.cognation.common.pojo.ActiveData;
|
||||||
import work.slhaf.agent.core.memory.submodule.cache.CacheCore;
|
import work.slhaf.agent.core.cognation.common.pojo.MemoryResult;
|
||||||
import work.slhaf.agent.core.memory.submodule.graph.GraphCore;
|
import work.slhaf.agent.core.cognation.common.pojo.MemorySliceResult;
|
||||||
import work.slhaf.agent.core.memory.submodule.graph.pojo.MemorySlice;
|
import work.slhaf.agent.core.cognation.submodule.cache.CacheCapability;
|
||||||
import work.slhaf.agent.core.memory.submodule.perceive.PerceiveCore;
|
import work.slhaf.agent.core.cognation.submodule.cache.CacheCore;
|
||||||
import work.slhaf.agent.core.memory.submodule.perceive.pojo.User;
|
import work.slhaf.agent.core.cognation.submodule.memory.MemoryCapability;
|
||||||
|
import work.slhaf.agent.core.cognation.submodule.memory.MemoryCore;
|
||||||
|
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.PerceiveCore;
|
||||||
|
import work.slhaf.agent.core.cognation.submodule.perceive.pojo.User;
|
||||||
import work.slhaf.agent.shared.memory.EvaluatedSlice;
|
import work.slhaf.agent.shared.memory.EvaluatedSlice;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -29,65 +34,65 @@ import java.util.concurrent.locks.Lock;
|
|||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
import static work.slhaf.agent.common.util.ExtractUtil.extractUserId;
|
import static work.slhaf.agent.common.util.ExtractUtil.extractUserId;
|
||||||
|
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
@Data
|
@Data
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class MemoryManager extends PersistableObject {
|
public class CognationManager extends PersistableObject implements CacheCapability, MemoryCapability, PerceiveCapability, CognationCapability {
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private static volatile MemoryManager memoryManager;
|
private static volatile CognationManager cognationManager;
|
||||||
private final Lock sliceInsertLock = new ReentrantLock();
|
private final Lock sliceInsertLock = new ReentrantLock();
|
||||||
public final Lock messageLock = new ReentrantLock();
|
public final Lock messageLock = new ReentrantLock();
|
||||||
|
|
||||||
|
|
||||||
private MemoryCore memoryCore;
|
private CognationCore cognationCore;
|
||||||
private CacheCore cacheCore;
|
private CacheCore cacheCore;
|
||||||
private GraphCore graphCore;
|
private MemoryCore memoryCore;
|
||||||
private PerceiveCore perceiveCore;
|
private PerceiveCore perceiveCore;
|
||||||
|
|
||||||
private HashMap<String, List<EvaluatedSlice>> activatedSlices;
|
private ActiveData activeData;
|
||||||
|
|
||||||
private MemoryManager() {
|
private CognationManager() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static MemoryManager getInstance() throws IOException, ClassNotFoundException {
|
public static CognationManager getInstance() throws IOException, ClassNotFoundException {
|
||||||
if (memoryManager == null) {
|
if (cognationManager == null) {
|
||||||
synchronized (MemoryManager.class) {
|
synchronized (CognationManager.class) {
|
||||||
if (memoryManager == null) {
|
if (cognationManager == null) {
|
||||||
Config config = Config.getConfig();
|
Config config = Config.getConfig();
|
||||||
memoryManager = new MemoryManager();
|
cognationManager = new CognationManager();
|
||||||
memoryManager.setMemoryCore(MemoryCore.getInstance(config.getAgentId()));
|
cognationManager.setCognationCore(CognationCore.getInstance(config.getAgentId()));
|
||||||
memoryManager.setCores();
|
cognationManager.setCores();
|
||||||
memoryManager.setActivatedSlices(new HashMap<>());
|
cognationManager.setActiveData(new ActiveData());
|
||||||
memoryManager.setShutdownHook();
|
cognationManager.setShutdownHook();
|
||||||
log.info("[MemoryManager] MemoryManager注册完毕...");
|
log.info("[CognationManager] MemoryManager注册完毕...");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return memoryManager;
|
return cognationManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setCores() {
|
private void setCores() {
|
||||||
this.setCacheCore(this.getMemoryCore().getCacheCore());
|
this.setCacheCore(this.getCognationCore().getCacheCore());
|
||||||
this.setGraphCore(this.getMemoryCore().getGraphCore());
|
this.setMemoryCore(this.getCognationCore().getMemoryCore());
|
||||||
this.setPerceiveCore(this.getMemoryCore().getPerceiveCore());
|
this.setPerceiveCore(this.getCognationCore().getPerceiveCore());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setShutdownHook() {
|
private void setShutdownHook() {
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
memoryManager.save();
|
cognationManager.save();
|
||||||
log.info("[MemoryManager] MemoryGraph已保存");
|
log.info("[CognationManager] MemoryGraph已保存");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("[MemoryManager] 保存MemoryGraph失败: ", e);
|
log.error("[CognationManager] 保存MemoryGraph失败: ", e);
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public MemoryResult selectMemory(String topicPathStr) {
|
public MemoryResult selectMemory(String topicPathStr) {
|
||||||
MemoryResult memoryResult;
|
MemoryResult memoryResult;
|
||||||
List<String> topicPath = List.of(topicPathStr.split("->"));
|
List<String> topicPath = List.of(topicPathStr.split("->"));
|
||||||
@@ -101,13 +106,13 @@ public class MemoryManager extends PersistableObject {
|
|||||||
if ((memoryResult = cacheCore.selectCache(path)) != null) {
|
if ((memoryResult = cacheCore.selectCache(path)) != null) {
|
||||||
return memoryResult;
|
return memoryResult;
|
||||||
}
|
}
|
||||||
memoryResult = graphCore.selectMemory(path);
|
memoryResult = memoryCore.selectMemory(path);
|
||||||
//尝试更新缓存
|
//尝试更新缓存
|
||||||
cacheCore.updateCache(topicPath, memoryResult);
|
cacheCore.updateCache(topicPath, memoryResult);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("[MemoryManager] selectMemory error: ", e);
|
log.error("[CognationManager] selectMemory error: ", e);
|
||||||
log.error("[MemoryManager] 路径: {}", topicPathStr);
|
log.error("[CognationManager] 路径: {}", topicPathStr);
|
||||||
log.error("[MemoryManager] 主题树: {}", getTopicTree());
|
log.error("[CognationManager] 主题树: {}", getTopicTree());
|
||||||
memoryResult = new MemoryResult();
|
memoryResult = new MemoryResult();
|
||||||
memoryResult.setRelatedMemorySliceResult(new ArrayList<>());
|
memoryResult.setRelatedMemorySliceResult(new ArrayList<>());
|
||||||
memoryResult.setMemorySliceResult(new CopyOnWriteArrayList<>());
|
memoryResult.setMemorySliceResult(new CopyOnWriteArrayList<>());
|
||||||
@@ -116,8 +121,9 @@ public class MemoryManager extends PersistableObject {
|
|||||||
return cacheFilter(memoryResult);
|
return cacheFilter(memoryResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public MemoryResult selectMemory(LocalDate date) throws IOException, ClassNotFoundException {
|
public MemoryResult selectMemory(LocalDate date) throws IOException, ClassNotFoundException {
|
||||||
return cacheFilter(graphCore.selectMemory(date));
|
return cacheFilter(memoryCore.selectMemory(date));
|
||||||
}
|
}
|
||||||
|
|
||||||
private MemoryResult cacheFilter(MemoryResult memoryResult) {
|
private MemoryResult cacheFilter(MemoryResult memoryResult) {
|
||||||
@@ -131,55 +137,42 @@ public class MemoryManager extends PersistableObject {
|
|||||||
return memoryResult;
|
return memoryResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void cleanSelectedSliceFilter() {
|
public void cleanSelectedSliceFilter() {
|
||||||
graphCore.getSelectedSlices().clear();
|
memoryCore.getSelectedSlices().clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUserId(String userInfo, String nickName) {
|
@Override
|
||||||
String userId = null;
|
public User getUser(String userInfo, String client) {
|
||||||
for (User user : perceiveCore.getUsers()) {
|
return perceiveCore.selectUser(userInfo, client);
|
||||||
if (user.getInfo().contains(userInfo)) {
|
|
||||||
userId = user.getUuid();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (userId == null) {
|
|
||||||
User newUser = setNewUser(userInfo, nickName);
|
|
||||||
perceiveCore.getUsers().add(newUser);
|
|
||||||
userId = newUser.getUuid();
|
|
||||||
}
|
|
||||||
return userId;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public List<Message> getChatMessages() {
|
public List<Message> getChatMessages() {
|
||||||
return memoryCore.getChatMessages();
|
return cognationCore.getChatMessages();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void setChatMessages(List<Message> chatMessages) {
|
public void setChatMessages(List<Message> chatMessages) {
|
||||||
memoryCore.setChatMessages(chatMessages);
|
cognationCore.setChatMessages(chatMessages);
|
||||||
}
|
|
||||||
|
|
||||||
private static User setNewUser(String userInfo, String nickName) {
|
|
||||||
User newUser = new User();
|
|
||||||
newUser.setUuid(UUID.randomUUID().toString());
|
|
||||||
List<String> infoList = new ArrayList<>();
|
|
||||||
infoList.add(userInfo);
|
|
||||||
newUser.setInfo(infoList);
|
|
||||||
newUser.setNickName(nickName);
|
|
||||||
return newUser;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public String getTopicTree() {
|
public String getTopicTree() {
|
||||||
return graphCore.getTopicTree();
|
return memoryCore.getTopicTree();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public HashMap<LocalDateTime, String> getDialogMap() {
|
public HashMap<LocalDateTime, String> getDialogMap() {
|
||||||
return cacheCore.getDialogMap();
|
return cacheCore.getDialogMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public ConcurrentHashMap<LocalDateTime, String> getUserDialogMap(String userId) {
|
public ConcurrentHashMap<LocalDateTime, String> getUserDialogMap(String userId) {
|
||||||
return cacheCore.getUserDialogMap().get(userId);
|
return cacheCore.getUserDialogMap().get(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void insertSlice(MemorySlice memorySlice, String topicPath) {
|
public void insertSlice(MemorySlice memorySlice, String topicPath) {
|
||||||
sliceInsertLock.lock();
|
sliceInsertLock.lock();
|
||||||
List<String> topicPathList = Arrays.stream(topicPath.split("->")).toList();
|
List<String> topicPathList = Arrays.stream(topicPath.split("->")).toList();
|
||||||
@@ -189,57 +182,55 @@ public class MemoryManager extends PersistableObject {
|
|||||||
cacheCore.checkCacheDate();
|
cacheCore.checkCacheDate();
|
||||||
//如果topicPath在memorySliceCache中存在对应缓存,由于进行的插入操作,则需要移除该缓存,但不清除相关计数
|
//如果topicPath在memorySliceCache中存在对应缓存,由于进行的插入操作,则需要移除该缓存,但不清除相关计数
|
||||||
cacheCore.clearCacheByTopicPath(topicPathList);
|
cacheCore.clearCacheByTopicPath(topicPathList);
|
||||||
graphCore.insertMemory(topicPathList, memorySlice);
|
memoryCore.insertMemory(topicPathList, memorySlice);
|
||||||
if (!memorySlice.isPrivate()) {
|
if (!memorySlice.isPrivate()) {
|
||||||
cacheCore.updateUserDialogMap(memorySlice);
|
cacheCore.updateUserDialogMap(memorySlice);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("[MemoryManager] 插入记忆时出错: ", e);
|
log.error("[CognationManager] 插入记忆时出错: ", e);
|
||||||
GlobalExceptionHandler.writeExceptionState(new GlobalException("插入记忆时出错: " + e.getLocalizedMessage()));
|
GlobalExceptionHandler.writeExceptionState(new GlobalException("插入记忆时出错: " + e.getLocalizedMessage()));
|
||||||
}
|
}
|
||||||
log.debug("[MemoryManager] 插入切片: {}, 路径: {}", memorySlice, topicPath);
|
log.debug("[CognationManager] 插入切片: {}, 路径: {}", memorySlice, topicPath);
|
||||||
sliceInsertLock.unlock();
|
sliceInsertLock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void cleanMessage(List<Message> messages) {
|
public void cleanMessage(List<Message> messages) {
|
||||||
messageLock.lock();
|
messageLock.lock();
|
||||||
memoryCore.getChatMessages().removeAll(messages);
|
cognationCore.getChatMessages().removeAll(messages);
|
||||||
messageLock.unlock();
|
messageLock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void updateDialogMap(LocalDateTime dateTime, String newDialogCache) {
|
public void updateDialogMap(LocalDateTime dateTime, String newDialogCache) {
|
||||||
cacheCore.updateDialogMap(dateTime, newDialogCache);
|
cacheCore.updateDialogMap(dateTime, newDialogCache);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void save() throws IOException {
|
private void save() throws IOException {
|
||||||
memoryCore.serialize();
|
cognationCore.serialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void updateActivatedSlices(String userId, List<EvaluatedSlice> memorySlices) {
|
public void updateActivatedSlices(String userId, List<EvaluatedSlice> memorySlices) {
|
||||||
memoryManager.getActivatedSlices().put(userId, memorySlices);
|
activeData.updateActivatedSlices(userId, memorySlices);
|
||||||
log.debug("[MemoryManager] 已更新激活切片, userId: {}", userId);
|
log.debug("[CognationManager] 已更新激活切片, userId: {}", userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public User getUser(String id) {
|
public User getUser(String id) {
|
||||||
for (User user : perceiveCore.getUsers()) {
|
User user = perceiveCore.selectUser(id);
|
||||||
if (user.getUuid().equals(id)) {
|
if (user == null) {
|
||||||
return user;
|
throw new UserNotExistsException("[CognationManager] 用户不存在: " + id);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return null;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public String getActivatedSlicesStr(String userId) {
|
public String getActivatedSlicesStr(String userId) {
|
||||||
if (memoryManager.getActivatedSlices().containsKey(userId)) {
|
return activeData.getActivatedSlicesStr(userId);
|
||||||
StringBuilder str = new StringBuilder();
|
|
||||||
memoryManager.getActivatedSlices().get(userId).forEach(slice -> str.append("\n\n").append("[").append(slice.getDate()).append("]\n")
|
|
||||||
.append(slice.getSummary()));
|
|
||||||
return str.toString();
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public String getDialogMapStr() {
|
public String getDialogMapStr() {
|
||||||
StringBuilder str = new StringBuilder();
|
StringBuilder str = new StringBuilder();
|
||||||
cacheCore.getDialogMap().forEach((dateTime, dialog) -> str.append("\n\n").append("[").append(dateTime).append("]\n")
|
cacheCore.getDialogMap().forEach((dateTime, dialog) -> str.append("\n\n").append("[").append(dateTime).append("]\n")
|
||||||
@@ -247,6 +238,7 @@ public class MemoryManager extends PersistableObject {
|
|||||||
return str.toString();
|
return str.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public String getUserDialogMapStr(String userId) {
|
public String getUserDialogMapStr(String userId) {
|
||||||
if (cacheCore.getUserDialogMap().containsKey(userId)) {
|
if (cacheCore.getUserDialogMap().containsKey(userId)) {
|
||||||
StringBuilder str = new StringBuilder();
|
StringBuilder str = new StringBuilder();
|
||||||
@@ -268,13 +260,14 @@ public class MemoryManager extends PersistableObject {
|
|||||||
return cacheCore.getUserDialogMap().size() <= 1;
|
return cacheCore.getUserDialogMap().size() <= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public boolean isSingleUser() {
|
public boolean isSingleUser() {
|
||||||
return isCacheSingleUser() && isChatMessagesSingleUser();
|
return isCacheSingleUser() && isChatMessagesSingleUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isChatMessagesSingleUser() {
|
private boolean isChatMessagesSingleUser() {
|
||||||
Set<String> userIdSet = new HashSet<>();
|
Set<String> userIdSet = new HashSet<>();
|
||||||
memoryManager.getChatMessages().forEach(m -> {
|
cognationManager.getChatMessages().forEach(m -> {
|
||||||
if (m.getRole().equals(ChatConstant.Character.ASSISTANT)) {
|
if (m.getRole().equals(ChatConstant.Character.ASSISTANT)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -286,4 +279,34 @@ public class MemoryManager extends PersistableObject {
|
|||||||
});
|
});
|
||||||
return userIdSet.size() <= 1;
|
return userIdSet.size() <= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public User addUser(String userInfo, String platform, String userNickName) {
|
||||||
|
return perceiveCore.addUser(userInfo, platform, userNickName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public HashMap<String, List<EvaluatedSlice>> getActivatedSlices() {
|
||||||
|
return activeData.getActivatedSlices();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clearActivatedSlices(String userId) {
|
||||||
|
activeData.clearActivatedSlices(userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasActivatedSlices(String userId) {
|
||||||
|
return activeData.hasActivatedSlices(userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getActivatedSlicesSize(String userId) {
|
||||||
|
return activeData.getActivatedSlices().get(userId).size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<EvaluatedSlice> getActivatedSlices(String userId) {
|
||||||
|
return activeData.getActivatedSlices().get(userId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package work.slhaf.agent.core.memory.exception;
|
package work.slhaf.agent.core.cognation.common.exception;
|
||||||
|
|
||||||
public class NullSliceListException extends RuntimeException {
|
public class NullSliceListException extends RuntimeException {
|
||||||
public NullSliceListException(String message) {
|
public NullSliceListException(String message) {
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package work.slhaf.agent.core.memory.exception;
|
package work.slhaf.agent.core.cognation.common.exception;
|
||||||
|
|
||||||
public class UnExistedDateIndexException extends RuntimeException {
|
public class UnExistedDateIndexException extends RuntimeException {
|
||||||
public UnExistedDateIndexException(String message) {
|
public UnExistedDateIndexException(String message) {
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package work.slhaf.agent.core.memory.exception;
|
package work.slhaf.agent.core.cognation.common.exception;
|
||||||
|
|
||||||
public class UnExistedTopicException extends RuntimeException {
|
public class UnExistedTopicException extends RuntimeException {
|
||||||
public UnExistedTopicException(String message) {
|
public UnExistedTopicException(String message) {
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package work.slhaf.agent.core.cognation.common.exception;
|
||||||
|
|
||||||
|
public class UserNotExistsException extends RuntimeException {
|
||||||
|
public UserNotExistsException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package work.slhaf.agent.core.cognation.common.pojo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import work.slhaf.agent.shared.memory.EvaluatedSlice;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class ActiveData {
|
||||||
|
private HashMap<String, List<EvaluatedSlice>> activatedSlices;
|
||||||
|
|
||||||
|
public void updateActivatedSlices(String userId, List<EvaluatedSlice> memorySlices) {
|
||||||
|
activatedSlices.put(userId, memorySlices);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getActivatedSlicesStr(String userId) {
|
||||||
|
if (activatedSlices.containsKey(userId)) {
|
||||||
|
StringBuilder str = new StringBuilder();
|
||||||
|
activatedSlices.get(userId).forEach(slice -> str.append("\n\n").append("[").append(slice.getDate()).append("]\n")
|
||||||
|
.append(slice.getSummary()));
|
||||||
|
return str.toString();
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clearActivatedSlices(String userId) {
|
||||||
|
activatedSlices.remove(userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasActivatedSlices(String userId) {
|
||||||
|
if (!activatedSlices.containsKey(userId)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return !activatedSlices.get(userId).isEmpty();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
package work.slhaf.agent.core.memory.pojo;
|
package work.slhaf.agent.core.cognation.common.pojo;
|
||||||
|
|
||||||
import lombok.Data;
|
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.memory.submodule.graph.pojo.MemorySlice;
|
import work.slhaf.agent.core.cognation.submodule.memory.pojo.MemorySlice;
|
||||||
|
|
||||||
import java.io.Serial;
|
import java.io.Serial;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
package work.slhaf.agent.core.memory.pojo;
|
package work.slhaf.agent.core.cognation.common.pojo;
|
||||||
|
|
||||||
import com.alibaba.fastjson2.annotation.JSONField;
|
import com.alibaba.fastjson2.annotation.JSONField;
|
||||||
import lombok.Data;
|
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.memory.submodule.graph.pojo.MemorySlice;
|
import work.slhaf.agent.core.cognation.submodule.memory.pojo.MemorySlice;
|
||||||
|
|
||||||
import java.io.Serial;
|
import java.io.Serial;
|
||||||
|
|
||||||
13
src/main/java/work/slhaf/agent/core/cognation/submodule/cache/CacheCapability.java
vendored
Normal file
13
src/main/java/work/slhaf/agent/core/cognation/submodule/cache/CacheCapability.java
vendored
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package work.slhaf.agent.core.cognation.submodule.cache;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
public interface CacheCapability {
|
||||||
|
HashMap<LocalDateTime, String> getDialogMap();
|
||||||
|
ConcurrentHashMap<LocalDateTime, String> getUserDialogMap(String userId);
|
||||||
|
void updateDialogMap(LocalDateTime dateTime, String newDialogCache);
|
||||||
|
String getDialogMapStr();
|
||||||
|
String getUserDialogMapStr(String userId);
|
||||||
|
}
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
package work.slhaf.agent.core.memory.submodule.cache;
|
package work.slhaf.agent.core.cognation.submodule.cache;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import work.slhaf.agent.common.serialize.PersistableObject;
|
import work.slhaf.agent.common.serialize.PersistableObject;
|
||||||
import work.slhaf.agent.core.memory.pojo.MemoryResult;
|
import work.slhaf.agent.core.cognation.common.pojo.MemoryResult;
|
||||||
import work.slhaf.agent.core.memory.submodule.graph.pojo.MemorySlice;
|
import work.slhaf.agent.core.cognation.submodule.memory.pojo.MemorySlice;
|
||||||
|
|
||||||
import java.io.Serial;
|
import java.io.Serial;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package work.slhaf.agent.core.cognation.submodule.memory;
|
||||||
|
|
||||||
|
import work.slhaf.agent.core.cognation.common.pojo.MemoryResult;
|
||||||
|
import work.slhaf.agent.core.cognation.submodule.memory.pojo.MemorySlice;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
public interface MemoryCapability {
|
||||||
|
MemoryResult selectMemory(String topicPathStr);
|
||||||
|
MemoryResult selectMemory(LocalDate date) throws IOException, ClassNotFoundException;
|
||||||
|
void insertSlice(MemorySlice memorySlice, String topicPath);
|
||||||
|
void cleanSelectedSliceFilter();
|
||||||
|
String getTopicTree();
|
||||||
|
}
|
||||||
@@ -1,15 +1,15 @@
|
|||||||
package work.slhaf.agent.core.memory.submodule.graph;
|
package work.slhaf.agent.core.cognation.submodule.memory;
|
||||||
|
|
||||||
import lombok.Data;
|
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.memory.exception.UnExistedDateIndexException;
|
import work.slhaf.agent.core.cognation.common.exception.UnExistedDateIndexException;
|
||||||
import work.slhaf.agent.core.memory.exception.UnExistedTopicException;
|
import work.slhaf.agent.core.cognation.common.exception.UnExistedTopicException;
|
||||||
import work.slhaf.agent.core.memory.pojo.MemoryResult;
|
import work.slhaf.agent.core.cognation.common.pojo.MemoryResult;
|
||||||
import work.slhaf.agent.core.memory.pojo.MemorySliceResult;
|
import work.slhaf.agent.core.cognation.common.pojo.MemorySliceResult;
|
||||||
import work.slhaf.agent.core.memory.submodule.graph.pojo.MemorySlice;
|
import work.slhaf.agent.core.cognation.submodule.memory.pojo.MemorySlice;
|
||||||
import work.slhaf.agent.core.memory.submodule.graph.pojo.node.MemoryNode;
|
import work.slhaf.agent.core.cognation.submodule.memory.pojo.node.MemoryNode;
|
||||||
import work.slhaf.agent.core.memory.submodule.graph.pojo.node.TopicNode;
|
import work.slhaf.agent.core.cognation.submodule.memory.pojo.node.TopicNode;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Serial;
|
import java.io.Serial;
|
||||||
@@ -20,7 +20,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
|||||||
|
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
@Data
|
@Data
|
||||||
public class GraphCore extends PersistableObject {
|
public class MemoryCore extends PersistableObject {
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
@@ -77,9 +77,9 @@ public class GraphCore extends PersistableObject {
|
|||||||
throw new UnExistedDateIndexException("不存在的日期索引: " + date);
|
throw new UnExistedDateIndexException("不存在的日期索引: " + date);
|
||||||
}
|
}
|
||||||
List<List<MemorySlice>> list = new ArrayList<>();
|
List<List<MemorySlice>> list = new ArrayList<>();
|
||||||
for (String memoryId : dateIndex.get(date)) {
|
for (String memoryNodeId : dateIndex.get(date)) {
|
||||||
MemoryNode memoryNode = new MemoryNode();
|
MemoryNode memoryNode = new MemoryNode();
|
||||||
memoryNode.setMemoryNodeId(memoryId);
|
memoryNode.setMemoryNodeId(memoryNodeId);
|
||||||
list.add(memoryNode.loadMemorySliceList());
|
list.add(memoryNode.loadMemorySliceList());
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
@@ -202,7 +202,6 @@ public class GraphCore extends PersistableObject {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void updateDateIndex(MemorySlice slice) {
|
private void updateDateIndex(MemorySlice slice) {
|
||||||
String memoryId = slice.getMemoryId();
|
String memoryId = slice.getMemoryId();
|
||||||
LocalDate date = LocalDate.now();
|
LocalDate date = LocalDate.now();
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package work.slhaf.agent.core.memory.submodule.graph.pojo;
|
package work.slhaf.agent.core.cognation.submodule.memory.pojo;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
package work.slhaf.agent.core.memory.submodule.graph.pojo.node;
|
package work.slhaf.agent.core.cognation.submodule.memory.pojo.node;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import work.slhaf.agent.common.serialize.PersistableObject;
|
import work.slhaf.agent.common.serialize.PersistableObject;
|
||||||
import work.slhaf.agent.core.memory.exception.NullSliceListException;
|
import work.slhaf.agent.core.cognation.common.exception.NullSliceListException;
|
||||||
import work.slhaf.agent.core.memory.submodule.graph.pojo.MemorySlice;
|
import work.slhaf.agent.core.cognation.submodule.memory.pojo.MemorySlice;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package work.slhaf.agent.core.memory.submodule.graph.pojo.node;
|
package work.slhaf.agent.core.cognation.submodule.memory.pojo.node;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package work.slhaf.agent.core.cognation.submodule.perceive;
|
||||||
|
|
||||||
|
import work.slhaf.agent.core.cognation.submodule.perceive.pojo.User;
|
||||||
|
|
||||||
|
public interface PerceiveCapability {
|
||||||
|
User getUser(String userInfo, String client);
|
||||||
|
User getUser(String id);
|
||||||
|
User addUser(String userInfo, String platform, String userNickName);
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
package work.slhaf.agent.core.cognation.submodule.perceive;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import work.slhaf.agent.common.serialize.PersistableObject;
|
||||||
|
import work.slhaf.agent.core.cognation.submodule.perceive.pojo.User;
|
||||||
|
|
||||||
|
import java.io.Serial;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
public class PerceiveCore extends PersistableObject {
|
||||||
|
|
||||||
|
@Serial
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
private static volatile PerceiveCore perceiveCore = new PerceiveCore();
|
||||||
|
private static final ReentrantLock usersLock = new ReentrantLock();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户列表
|
||||||
|
*/
|
||||||
|
private List<User> users = new ArrayList<>();
|
||||||
|
|
||||||
|
public static PerceiveCore getInstance() {
|
||||||
|
if (perceiveCore == null) {
|
||||||
|
synchronized (PerceiveCore.class) {
|
||||||
|
if (perceiveCore == null) {
|
||||||
|
perceiveCore = new PerceiveCore();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return perceiveCore;
|
||||||
|
}
|
||||||
|
|
||||||
|
public User selectUser(String userInfo, String platform) {
|
||||||
|
User resultUser = null;
|
||||||
|
usersLock.lock();
|
||||||
|
for (User user : users) {
|
||||||
|
HashMap<String, String> info = user.getInfo();
|
||||||
|
if (info.containsKey(platform)) {
|
||||||
|
if (info.get(platform).equals(userInfo)) {
|
||||||
|
resultUser = user;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
usersLock.unlock();
|
||||||
|
return resultUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
public User addUser(String userInfo, String platform, String userNickName) {
|
||||||
|
User user = new User();
|
||||||
|
user.addInfo(platform, userInfo);
|
||||||
|
user.setNickName(userNickName);
|
||||||
|
user.setUuid(UUID.randomUUID().toString());
|
||||||
|
|
||||||
|
usersLock.lock();
|
||||||
|
users.add(user);
|
||||||
|
usersLock.unlock();
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public User selectUser(String id) {
|
||||||
|
usersLock.lock();
|
||||||
|
for (User user : users) {
|
||||||
|
if (user.getUuid().equals(id)) {
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
usersLock.unlock();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package work.slhaf.agent.core.cognation.submodule.perceive.pojo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import work.slhaf.agent.common.serialize.PersistableObject;
|
||||||
|
|
||||||
|
import java.io.Serial;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
public class User extends PersistableObject {
|
||||||
|
|
||||||
|
@Serial
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private String uuid;
|
||||||
|
private String nickName;
|
||||||
|
private HashMap<String/*platform*/,String> info = new HashMap<>();
|
||||||
|
|
||||||
|
private String relation;
|
||||||
|
private HashMap<LocalDate, String> events = new HashMap<>();
|
||||||
|
private List<String> impressions = new ArrayList<>();
|
||||||
|
private List<String> attitude = new ArrayList<>();
|
||||||
|
private List<String> staticMemory = new ArrayList<>();
|
||||||
|
|
||||||
|
public void addInfo(String platform,String userInfo) {
|
||||||
|
this.info.put(platform, userInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Constant {
|
||||||
|
public static class Relation {
|
||||||
|
public static final String STRANGER = "stranger";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
package work.slhaf.agent.core.memory.submodule.perceive;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.EqualsAndHashCode;
|
|
||||||
import work.slhaf.agent.common.serialize.PersistableObject;
|
|
||||||
import work.slhaf.agent.core.memory.submodule.perceive.pojo.User;
|
|
||||||
|
|
||||||
import java.io.Serial;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@EqualsAndHashCode(callSuper = true)
|
|
||||||
@Data
|
|
||||||
public class PerceiveCore extends PersistableObject {
|
|
||||||
|
|
||||||
@Serial
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 用户列表
|
|
||||||
*/
|
|
||||||
private List<User> users = new ArrayList<>();
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
package work.slhaf.agent.core.memory.submodule.perceive.pojo;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.EqualsAndHashCode;
|
|
||||||
import work.slhaf.agent.common.serialize.PersistableObject;
|
|
||||||
|
|
||||||
import java.io.Serial;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@EqualsAndHashCode(callSuper = true)
|
|
||||||
@Data
|
|
||||||
public class User extends PersistableObject {
|
|
||||||
|
|
||||||
@Serial
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
|
|
||||||
private String uuid;
|
|
||||||
private List<String> info;
|
|
||||||
private String nickName;
|
|
||||||
}
|
|
||||||
@@ -8,4 +8,5 @@ import work.slhaf.agent.core.interaction.data.context.InteractionContext;
|
|||||||
public interface PreModuleActions {
|
public interface PreModuleActions {
|
||||||
void setAppendedPrompt(InteractionContext context);
|
void setAppendedPrompt(InteractionContext context);
|
||||||
void setActiveModule(InteractionContext context);
|
void setActiveModule(InteractionContext context);
|
||||||
|
String getModuleName();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,9 +8,10 @@ import work.slhaf.agent.common.chat.constant.ChatConstant;
|
|||||||
import work.slhaf.agent.common.chat.pojo.ChatResponse;
|
import work.slhaf.agent.common.chat.pojo.ChatResponse;
|
||||||
import work.slhaf.agent.common.chat.pojo.Message;
|
import work.slhaf.agent.common.chat.pojo.Message;
|
||||||
import work.slhaf.agent.common.chat.pojo.MetaMessage;
|
import work.slhaf.agent.common.chat.pojo.MetaMessage;
|
||||||
|
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.data.context.InteractionContext;
|
||||||
import work.slhaf.agent.core.interaction.module.InteractionModule;
|
import work.slhaf.agent.core.interaction.module.InteractionModule;
|
||||||
import work.slhaf.agent.core.memory.MemoryManager;
|
|
||||||
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.AppendPromptData;
|
||||||
import work.slhaf.agent.module.common.Model;
|
import work.slhaf.agent.module.common.Model;
|
||||||
@@ -20,7 +21,6 @@ import java.io.IOException;
|
|||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static work.slhaf.agent.common.util.ExtractUtil.extractJson;
|
import static work.slhaf.agent.common.util.ExtractUtil.extractJson;
|
||||||
@@ -33,7 +33,7 @@ public class CoreModel extends Model implements InteractionModule {
|
|||||||
public static final String MODEL_KEY = "core_model";
|
public static final String MODEL_KEY = "core_model";
|
||||||
private static volatile CoreModel coreModel;
|
private static volatile CoreModel coreModel;
|
||||||
|
|
||||||
private MemoryManager memoryManager;
|
private CognationCapability cognationCapability;
|
||||||
private SessionManager sessionManager;
|
private SessionManager sessionManager;
|
||||||
private List<Message> appendedMessages;
|
private List<Message> appendedMessages;
|
||||||
|
|
||||||
@@ -45,8 +45,8 @@ public class CoreModel extends Model implements InteractionModule {
|
|||||||
synchronized (CoreModel.class) {
|
synchronized (CoreModel.class) {
|
||||||
if (coreModel == null) {
|
if (coreModel == null) {
|
||||||
coreModel = new CoreModel();
|
coreModel = new CoreModel();
|
||||||
coreModel.memoryManager = MemoryManager.getInstance();
|
coreModel.cognationCapability = CognationManager.getInstance();
|
||||||
coreModel.chatMessages = coreModel.memoryManager.getChatMessages();
|
coreModel.chatMessages = coreModel.cognationCapability.getChatMessages();
|
||||||
coreModel.appendedMessages = new ArrayList<>();
|
coreModel.appendedMessages = new ArrayList<>();
|
||||||
coreModel.sessionManager = SessionManager.getInstance();
|
coreModel.sessionManager = SessionManager.getInstance();
|
||||||
setModel(coreModel, MODEL_KEY, ModelConstant.Prompt.CORE, true);
|
setModel(coreModel, MODEL_KEY, ModelConstant.Prompt.CORE, true);
|
||||||
@@ -145,7 +145,7 @@ public class CoreModel extends Model implements InteractionModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void updateModuleContextAndChatMessages(InteractionContext interactionContext, String response, ChatResponse chatResponse) {
|
private void updateModuleContextAndChatMessages(InteractionContext interactionContext, String response, ChatResponse chatResponse) {
|
||||||
memoryManager.getMessageLock().lock();
|
cognationCapability.getMessageLock().lock();
|
||||||
this.chatMessages.removeIf(m -> {
|
this.chatMessages.removeIf(m -> {
|
||||||
if (m.getRole().equals(ChatConstant.Character.ASSISTANT)) {
|
if (m.getRole().equals(ChatConstant.Character.ASSISTANT)) {
|
||||||
return false;
|
return false;
|
||||||
@@ -163,7 +163,7 @@ public class CoreModel extends Model implements InteractionModule {
|
|||||||
this.chatMessages.add(primaryUserMessage);
|
this.chatMessages.add(primaryUserMessage);
|
||||||
Message assistantMessage = new Message(ChatConstant.Character.ASSISTANT, response);
|
Message assistantMessage = new Message(ChatConstant.Character.ASSISTANT, response);
|
||||||
this.chatMessages.add(assistantMessage);
|
this.chatMessages.add(assistantMessage);
|
||||||
memoryManager.getMessageLock().unlock();
|
cognationCapability.getMessageLock().unlock();
|
||||||
//设置上下文
|
//设置上下文
|
||||||
interactionContext.getModuleContext().getExtraContext().put("total_token", chatResponse.getUsageBean().getTotal_tokens());
|
interactionContext.getModuleContext().getExtraContext().put("total_token", chatResponse.getUsageBean().getTotal_tokens());
|
||||||
//区分单人聊天场景
|
//区分单人聊天场景
|
||||||
|
|||||||
@@ -3,13 +3,16 @@ package work.slhaf.agent.module.modules.memory.selector;
|
|||||||
import com.alibaba.fastjson2.JSONObject;
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import work.slhaf.agent.core.cognation.CognationCapability;
|
||||||
|
import work.slhaf.agent.core.cognation.CognationManager;
|
||||||
|
import work.slhaf.agent.core.cognation.common.exception.UnExistedDateIndexException;
|
||||||
|
import work.slhaf.agent.core.cognation.common.exception.UnExistedTopicException;
|
||||||
|
import work.slhaf.agent.core.cognation.common.pojo.MemoryResult;
|
||||||
|
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.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.interaction.module.InteractionModule;
|
||||||
import work.slhaf.agent.core.memory.MemoryManager;
|
|
||||||
import work.slhaf.agent.core.memory.exception.UnExistedDateIndexException;
|
|
||||||
import work.slhaf.agent.core.memory.exception.UnExistedTopicException;
|
|
||||||
import work.slhaf.agent.core.memory.pojo.MemoryResult;
|
|
||||||
import work.slhaf.agent.core.memory.submodule.graph.pojo.MemorySlice;
|
|
||||||
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.AppendPromptData;
|
||||||
import work.slhaf.agent.module.common.PreModuleActions;
|
import work.slhaf.agent.module.common.PreModuleActions;
|
||||||
@@ -32,9 +35,10 @@ import java.util.List;
|
|||||||
public class MemorySelector implements InteractionModule, PreModuleActions {
|
public class MemorySelector implements InteractionModule, PreModuleActions {
|
||||||
|
|
||||||
private static volatile MemorySelector memorySelector;
|
private static volatile MemorySelector memorySelector;
|
||||||
private static final String MODULE_NAME = "[记忆模块]";
|
|
||||||
|
|
||||||
private MemoryManager memoryManager;
|
private CacheCapability cacheCapability;
|
||||||
|
private MemoryCapability memoryCapability;
|
||||||
|
private CognationCapability cognationCapability;
|
||||||
private SliceSelectEvaluator sliceSelectEvaluator;
|
private SliceSelectEvaluator sliceSelectEvaluator;
|
||||||
private MemorySelectExtractor memorySelectExtractor;
|
private MemorySelectExtractor memorySelectExtractor;
|
||||||
private SessionManager sessionManager;
|
private SessionManager sessionManager;
|
||||||
@@ -47,7 +51,9 @@ public class MemorySelector implements InteractionModule, PreModuleActions {
|
|||||||
synchronized (MemorySelector.class) {
|
synchronized (MemorySelector.class) {
|
||||||
if (memorySelector == null) {
|
if (memorySelector == null) {
|
||||||
memorySelector = new MemorySelector();
|
memorySelector = new MemorySelector();
|
||||||
memorySelector.setMemoryManager(MemoryManager.getInstance());
|
memorySelector.setCacheCapability(CognationManager.getInstance());
|
||||||
|
memorySelector.setMemoryCapability(CognationManager.getInstance());
|
||||||
|
memorySelector.setCognationCapability(CognationManager.getInstance());
|
||||||
memorySelector.setSliceSelectEvaluator(SliceSelectEvaluator.getInstance());
|
memorySelector.setSliceSelectEvaluator(SliceSelectEvaluator.getInstance());
|
||||||
memorySelector.setMemorySelectExtractor(MemorySelectExtractor.getInstance());
|
memorySelector.setMemorySelectExtractor(MemorySelectExtractor.getInstance());
|
||||||
memorySelector.setSessionManager(SessionManager.getInstance());
|
memorySelector.setSessionManager(SessionManager.getInstance());
|
||||||
@@ -64,11 +70,9 @@ public class MemorySelector implements InteractionModule, PreModuleActions {
|
|||||||
//获取主题路径
|
//获取主题路径
|
||||||
ExtractorResult extractorResult = memorySelectExtractor.execute(interactionContext);
|
ExtractorResult extractorResult = memorySelectExtractor.execute(interactionContext);
|
||||||
if (extractorResult.isRecall() || !extractorResult.getMatches().isEmpty()) {
|
if (extractorResult.isRecall() || !extractorResult.getMatches().isEmpty()) {
|
||||||
if (memoryManager.getActivatedSlices().get(userId) != null) {
|
cognationCapability.clearActivatedSlices(userId);
|
||||||
memoryManager.getActivatedSlices().get(userId).clear();
|
|
||||||
}
|
|
||||||
List<EvaluatedSlice> evaluatedSlices = selectAndEvaluateMemory(interactionContext, extractorResult);
|
List<EvaluatedSlice> evaluatedSlices = selectAndEvaluateMemory(interactionContext, extractorResult);
|
||||||
memoryManager.updateActivatedSlices(userId, evaluatedSlices);
|
cognationCapability.updateActivatedSlices(userId, evaluatedSlices);
|
||||||
}
|
}
|
||||||
//设置追加提示词
|
//设置追加提示词
|
||||||
setAppendedPrompt(interactionContext);
|
setAppendedPrompt(interactionContext);
|
||||||
@@ -87,7 +91,7 @@ public class MemorySelector implements InteractionModule, PreModuleActions {
|
|||||||
EvaluatorInput evaluatorInput = EvaluatorInput.builder()
|
EvaluatorInput evaluatorInput = EvaluatorInput.builder()
|
||||||
.input(interactionContext.getInput())
|
.input(interactionContext.getInput())
|
||||||
.memoryResults(memoryResultList)
|
.memoryResults(memoryResultList)
|
||||||
.messages(memoryManager.getChatMessages())
|
.messages(cognationCapability.getChatMessages())
|
||||||
.build();
|
.build();
|
||||||
log.debug("[MemorySelector] 切片评估输入: {}", JSONObject.toJSONString(evaluatorInput));
|
log.debug("[MemorySelector] 切片评估输入: {}", JSONObject.toJSONString(evaluatorInput));
|
||||||
List<EvaluatedSlice> memorySlices = sliceSelectEvaluator.execute(evaluatorInput);
|
List<EvaluatedSlice> memorySlices = sliceSelectEvaluator.execute(evaluatorInput);
|
||||||
@@ -97,15 +101,10 @@ public class MemorySelector implements InteractionModule, PreModuleActions {
|
|||||||
|
|
||||||
private void setModuleContextRecall(InteractionContext interactionContext) {
|
private void setModuleContextRecall(InteractionContext interactionContext) {
|
||||||
String userId = interactionContext.getUserId();
|
String userId = interactionContext.getUserId();
|
||||||
boolean recall;
|
boolean recall = cognationCapability.hasActivatedSlices(userId);
|
||||||
if (memoryManager.getActivatedSlices().get(userId) == null) {
|
|
||||||
recall = false;
|
|
||||||
} else {
|
|
||||||
recall = !memoryManager.getActivatedSlices().get(userId).isEmpty();
|
|
||||||
}
|
|
||||||
interactionContext.getModuleContext().getExtraContext().put("recall", recall);
|
interactionContext.getModuleContext().getExtraContext().put("recall", recall);
|
||||||
if (recall) {
|
if (recall) {
|
||||||
interactionContext.getModuleContext().getExtraContext().put("recall_count", memoryManager.getActivatedSlices().get(userId).size());
|
interactionContext.getModuleContext().getExtraContext().put("recall_count", cognationCapability.getActivatedSlicesSize(userId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,9 +113,9 @@ public class MemorySelector implements InteractionModule, PreModuleActions {
|
|||||||
for (ExtractorMatchData match : matches) {
|
for (ExtractorMatchData match : matches) {
|
||||||
try {
|
try {
|
||||||
MemoryResult memoryResult = switch (match.getType()) {
|
MemoryResult memoryResult = switch (match.getType()) {
|
||||||
case ExtractorMatchData.Constant.TOPIC -> memoryManager.selectMemory(match.getText());
|
case ExtractorMatchData.Constant.TOPIC -> memoryCapability.selectMemory(match.getText());
|
||||||
case ExtractorMatchData.Constant.DATE ->
|
case ExtractorMatchData.Constant.DATE ->
|
||||||
memoryManager.selectMemory(LocalDate.parse(match.getText()));
|
memoryCapability.selectMemory(LocalDate.parse(match.getText()));
|
||||||
default -> null;
|
default -> null;
|
||||||
};
|
};
|
||||||
if (memoryResult == null || memoryResult.isEmpty()) continue;
|
if (memoryResult == null || memoryResult.isEmpty()) continue;
|
||||||
@@ -128,7 +127,7 @@ public class MemorySelector implements InteractionModule, PreModuleActions {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//清理切片记录
|
//清理切片记录
|
||||||
memoryManager.cleanSelectedSliceFilter();
|
memoryCapability.cleanSelectedSliceFilter();
|
||||||
|
|
||||||
//根据userInfo过滤是否为私人记忆
|
//根据userInfo过滤是否为私人记忆
|
||||||
for (MemoryResult memoryResult : memoryResultList) {
|
for (MemoryResult memoryResult : memoryResultList) {
|
||||||
@@ -140,7 +139,7 @@ public class MemorySelector implements InteractionModule, PreModuleActions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void removeDuplicateSlice(MemoryResult memoryResult) {
|
private void removeDuplicateSlice(MemoryResult memoryResult) {
|
||||||
Collection<String> values = memoryManager.getDialogMap().values();
|
Collection<String> values = cacheCapability.getDialogMap().values();
|
||||||
memoryResult.getRelatedMemorySliceResult().removeIf(m -> values.contains(m.getSummary()));
|
memoryResult.getRelatedMemorySliceResult().removeIf(m -> values.contains(m.getSummary()));
|
||||||
memoryResult.getMemorySliceResult().removeIf(m -> values.contains(m.getMemorySlice().getSummary()));
|
memoryResult.getMemorySliceResult().removeIf(m -> values.contains(m.getMemorySlice().getSummary()));
|
||||||
}
|
}
|
||||||
@@ -157,29 +156,34 @@ public class MemorySelector implements InteractionModule, PreModuleActions {
|
|||||||
String userId = context.getUserId();
|
String userId = context.getUserId();
|
||||||
HashMap<String, String> map = getPromptDataMap(userId);
|
HashMap<String, String> map = getPromptDataMap(userId);
|
||||||
AppendPromptData data = new AppendPromptData();
|
AppendPromptData data = new AppendPromptData();
|
||||||
data.setModuleName(MODULE_NAME);
|
data.setModuleName(getModuleName());
|
||||||
data.setAppendedPrompt(map);
|
data.setAppendedPrompt(map);
|
||||||
context.setAppendedPrompt(data);
|
context.setAppendedPrompt(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setActiveModule(InteractionContext context) {
|
public void setActiveModule(InteractionContext context) {
|
||||||
context.getCoreContext().addActiveModule(MODULE_NAME);
|
context.getCoreContext().addActiveModule(getModuleName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getModuleName() {
|
||||||
|
return "[记忆模块]";
|
||||||
}
|
}
|
||||||
|
|
||||||
private HashMap<String, String> getPromptDataMap(String userId) {
|
private HashMap<String, String> getPromptDataMap(String userId) {
|
||||||
HashMap<String, String> map = new HashMap<>();
|
HashMap<String, String> map = new HashMap<>();
|
||||||
String dialogMapStr = memoryManager.getDialogMapStr();
|
String dialogMapStr = cacheCapability.getDialogMapStr();
|
||||||
if (!dialogMapStr.isEmpty()) {
|
if (!dialogMapStr.isEmpty()) {
|
||||||
map.put("[记忆缓存] <你最近两日和所有聊天者的对话记忆印象>", dialogMapStr);
|
map.put("[记忆缓存] <你最近两日和所有聊天者的对话记忆印象>", dialogMapStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
String userDialogMapStr = memoryManager.getUserDialogMapStr(userId);
|
String userDialogMapStr = cacheCapability.getUserDialogMapStr(userId);
|
||||||
if (userDialogMapStr != null && !userDialogMapStr.isEmpty() && !memoryManager.isSingleUser()) {
|
if (userDialogMapStr != null && !userDialogMapStr.isEmpty() && !cognationCapability.isSingleUser()) {
|
||||||
map.put("[用户记忆缓存] <与最新一条消息的发送者的近两天对话记忆印象, 可能与[记忆缓存]稍有重复>", userDialogMapStr);
|
map.put("[用户记忆缓存] <与最新一条消息的发送者的近两天对话记忆印象, 可能与[记忆缓存]稍有重复>", userDialogMapStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
String sliceStr = memoryManager.getActivatedSlicesStr(userId);
|
String sliceStr = cognationCapability.getActivatedSlicesStr(userId);
|
||||||
if (sliceStr != null && !sliceStr.isEmpty()) {
|
if (sliceStr != null && !sliceStr.isEmpty()) {
|
||||||
map.put("[记忆切片] <你与最新一条消息的发送者的相关回忆, 不会与[记忆缓存]重复, 如果有重复你也可以指出来()>", sliceStr);
|
map.put("[记忆切片] <你与最新一条消息的发送者的相关回忆, 不会与[记忆缓存]重复, 如果有重复你也可以指出来()>", sliceStr);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,10 +7,9 @@ import lombok.Data;
|
|||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import work.slhaf.agent.common.thread.InteractionThreadPoolExecutor;
|
import work.slhaf.agent.common.thread.InteractionThreadPoolExecutor;
|
||||||
import work.slhaf.agent.core.memory.MemoryManager;
|
import work.slhaf.agent.core.cognation.common.pojo.MemoryResult;
|
||||||
import work.slhaf.agent.core.memory.pojo.MemoryResult;
|
import work.slhaf.agent.core.cognation.common.pojo.MemorySliceResult;
|
||||||
import work.slhaf.agent.core.memory.pojo.MemorySliceResult;
|
import work.slhaf.agent.core.cognation.submodule.memory.pojo.MemorySlice;
|
||||||
import work.slhaf.agent.core.memory.submodule.graph.pojo.MemorySlice;
|
|
||||||
import work.slhaf.agent.module.common.Model;
|
import work.slhaf.agent.module.common.Model;
|
||||||
import work.slhaf.agent.module.common.ModelConstant;
|
import work.slhaf.agent.module.common.ModelConstant;
|
||||||
import work.slhaf.agent.module.modules.memory.selector.evaluator.data.EvaluatorBatchInput;
|
import work.slhaf.agent.module.modules.memory.selector.evaluator.data.EvaluatorBatchInput;
|
||||||
@@ -34,7 +33,6 @@ import static work.slhaf.agent.common.util.ExtractUtil.extractJson;
|
|||||||
public class SliceSelectEvaluator extends Model {
|
public class SliceSelectEvaluator extends Model {
|
||||||
public static final String MODEL_KEY = "slice_evaluator";
|
public static final String MODEL_KEY = "slice_evaluator";
|
||||||
private static volatile SliceSelectEvaluator sliceSelectEvaluator;
|
private static volatile SliceSelectEvaluator sliceSelectEvaluator;
|
||||||
private MemoryManager memoryManager;
|
|
||||||
private InteractionThreadPoolExecutor executor;
|
private InteractionThreadPoolExecutor executor;
|
||||||
|
|
||||||
private SliceSelectEvaluator() {
|
private SliceSelectEvaluator() {
|
||||||
@@ -45,7 +43,6 @@ public class SliceSelectEvaluator extends Model {
|
|||||||
synchronized (SliceSelectEvaluator.class) {
|
synchronized (SliceSelectEvaluator.class) {
|
||||||
if (sliceSelectEvaluator == null) {
|
if (sliceSelectEvaluator == null) {
|
||||||
sliceSelectEvaluator = new SliceSelectEvaluator();
|
sliceSelectEvaluator = new SliceSelectEvaluator();
|
||||||
sliceSelectEvaluator.setMemoryManager(MemoryManager.getInstance());
|
|
||||||
sliceSelectEvaluator.setExecutor(InteractionThreadPoolExecutor.getInstance());
|
sliceSelectEvaluator.setExecutor(InteractionThreadPoolExecutor.getInstance());
|
||||||
setModel(sliceSelectEvaluator, MODEL_KEY, ModelConstant.Prompt.MEMORY, false);
|
setModel(sliceSelectEvaluator, MODEL_KEY, ModelConstant.Prompt.MEMORY, false);
|
||||||
log.info("SliceEvaluator注册完毕...");
|
log.info("SliceEvaluator注册完毕...");
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package work.slhaf.agent.module.modules.memory.selector.evaluator.data;
|
|||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import work.slhaf.agent.common.chat.pojo.Message;
|
import work.slhaf.agent.common.chat.pojo.Message;
|
||||||
import work.slhaf.agent.core.memory.pojo.MemoryResult;
|
import work.slhaf.agent.core.cognation.common.pojo.MemoryResult;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|||||||
@@ -9,8 +9,10 @@ import work.slhaf.agent.common.chat.pojo.Message;
|
|||||||
import work.slhaf.agent.common.chat.pojo.MetaMessage;
|
import work.slhaf.agent.common.chat.pojo.MetaMessage;
|
||||||
import work.slhaf.agent.common.exception_handler.GlobalExceptionHandler;
|
import work.slhaf.agent.common.exception_handler.GlobalExceptionHandler;
|
||||||
import work.slhaf.agent.common.exception_handler.pojo.GlobalException;
|
import work.slhaf.agent.common.exception_handler.pojo.GlobalException;
|
||||||
|
import work.slhaf.agent.core.cognation.CognationCapability;
|
||||||
|
import work.slhaf.agent.core.cognation.CognationManager;
|
||||||
|
import work.slhaf.agent.core.cognation.submodule.memory.MemoryCapability;
|
||||||
import work.slhaf.agent.core.interaction.data.context.InteractionContext;
|
import work.slhaf.agent.core.interaction.data.context.InteractionContext;
|
||||||
import work.slhaf.agent.core.memory.MemoryManager;
|
|
||||||
import work.slhaf.agent.core.session.SessionManager;
|
import work.slhaf.agent.core.session.SessionManager;
|
||||||
import work.slhaf.agent.module.common.Model;
|
import work.slhaf.agent.module.common.Model;
|
||||||
import work.slhaf.agent.module.common.ModelConstant;
|
import work.slhaf.agent.module.common.ModelConstant;
|
||||||
@@ -33,7 +35,8 @@ public class MemorySelectExtractor extends Model {
|
|||||||
public static final String MODEL_KEY = "topic_extractor";
|
public static final String MODEL_KEY = "topic_extractor";
|
||||||
private static volatile MemorySelectExtractor memorySelectExtractor;
|
private static volatile MemorySelectExtractor memorySelectExtractor;
|
||||||
|
|
||||||
private MemoryManager memoryManager;
|
private MemoryCapability memoryCapability;
|
||||||
|
private CognationCapability cognationCapability;
|
||||||
private SessionManager sessionManager;
|
private SessionManager sessionManager;
|
||||||
|
|
||||||
private MemorySelectExtractor() {
|
private MemorySelectExtractor() {
|
||||||
@@ -44,7 +47,8 @@ public class MemorySelectExtractor extends Model {
|
|||||||
synchronized (MemorySelectExtractor.class) {
|
synchronized (MemorySelectExtractor.class) {
|
||||||
if (memorySelectExtractor == null) {
|
if (memorySelectExtractor == null) {
|
||||||
memorySelectExtractor = new MemorySelectExtractor();
|
memorySelectExtractor = new MemorySelectExtractor();
|
||||||
memorySelectExtractor.setMemoryManager(MemoryManager.getInstance());
|
memorySelectExtractor.setMemoryCapability(CognationManager.getInstance());
|
||||||
|
memorySelectExtractor.setCognationCapability(CognationManager.getInstance());
|
||||||
memorySelectExtractor.setSessionManager(SessionManager.getInstance());
|
memorySelectExtractor.setSessionManager(SessionManager.getInstance());
|
||||||
setModel(memorySelectExtractor, MODEL_KEY, ModelConstant.Prompt.MEMORY, false);
|
setModel(memorySelectExtractor, MODEL_KEY, ModelConstant.Prompt.MEMORY, false);
|
||||||
}
|
}
|
||||||
@@ -69,12 +73,12 @@ public class MemorySelectExtractor extends Model {
|
|||||||
|
|
||||||
ExtractorResult extractorResult;
|
ExtractorResult extractorResult;
|
||||||
try {
|
try {
|
||||||
List<EvaluatedSlice> activatedMemorySlices = memoryManager.getActivatedSlices().get(context.getUserId());
|
List<EvaluatedSlice> activatedMemorySlices = cognationCapability.getActivatedSlices(context.getUserId());
|
||||||
ExtractorInput extractorInput = ExtractorInput.builder()
|
ExtractorInput extractorInput = ExtractorInput.builder()
|
||||||
.text(context.getInput())
|
.text(context.getInput())
|
||||||
.date(context.getDateTime().toLocalDate())
|
.date(context.getDateTime().toLocalDate())
|
||||||
.history(chatMessages)
|
.history(chatMessages)
|
||||||
.topic_tree(memoryManager.getTopicTree())
|
.topic_tree(memoryCapability.getTopicTree())
|
||||||
.activatedMemorySlices(activatedMemorySlices)
|
.activatedMemorySlices(activatedMemorySlices)
|
||||||
.build();
|
.build();
|
||||||
log.debug("[MemorySelectExtractor] 主题提取输入: {}", JSONObject.toJSONString(extractorInput));
|
log.debug("[MemorySelectExtractor] 主题提取输入: {}", JSONObject.toJSONString(extractorInput));
|
||||||
|
|||||||
@@ -6,10 +6,14 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import work.slhaf.agent.common.chat.constant.ChatConstant;
|
import work.slhaf.agent.common.chat.constant.ChatConstant;
|
||||||
import work.slhaf.agent.common.chat.pojo.Message;
|
import work.slhaf.agent.common.chat.pojo.Message;
|
||||||
import work.slhaf.agent.common.thread.InteractionThreadPoolExecutor;
|
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.cache.CacheCapability;
|
||||||
|
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.perceive.PerceiveCapability;
|
||||||
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.interaction.module.InteractionModule;
|
||||||
import work.slhaf.agent.core.memory.MemoryManager;
|
|
||||||
import work.slhaf.agent.core.memory.submodule.graph.pojo.MemorySlice;
|
|
||||||
import work.slhaf.agent.core.session.SessionManager;
|
import work.slhaf.agent.core.session.SessionManager;
|
||||||
import work.slhaf.agent.module.modules.memory.selector.extractor.MemorySelectExtractor;
|
import work.slhaf.agent.module.modules.memory.selector.extractor.MemorySelectExtractor;
|
||||||
import work.slhaf.agent.module.modules.memory.updater.summarizer.MemorySummarizer;
|
import work.slhaf.agent.module.modules.memory.updater.summarizer.MemorySummarizer;
|
||||||
@@ -36,7 +40,10 @@ public class MemoryUpdater implements InteractionModule {
|
|||||||
private static final int TOKEN_PER_RECALL = 230;
|
private static final int TOKEN_PER_RECALL = 230;
|
||||||
private static final int TRIGGER_ROLL_LIMIT = 36;
|
private static final int TRIGGER_ROLL_LIMIT = 36;
|
||||||
|
|
||||||
private MemoryManager memoryManager;
|
private CognationCapability cognationCapability;
|
||||||
|
private MemoryCapability memoryCapability;
|
||||||
|
private CacheCapability cacheCapability;
|
||||||
|
private PerceiveCapability perceiveCapability;
|
||||||
private InteractionThreadPoolExecutor executor;
|
private InteractionThreadPoolExecutor executor;
|
||||||
private MemorySelectExtractor memorySelectExtractor;
|
private MemorySelectExtractor memorySelectExtractor;
|
||||||
private MemorySummarizer memorySummarizer;
|
private MemorySummarizer memorySummarizer;
|
||||||
@@ -54,7 +61,10 @@ public class MemoryUpdater implements InteractionModule {
|
|||||||
synchronized (MemoryUpdater.class) {
|
synchronized (MemoryUpdater.class) {
|
||||||
if (memoryUpdater == null) {
|
if (memoryUpdater == null) {
|
||||||
memoryUpdater = new MemoryUpdater();
|
memoryUpdater = new MemoryUpdater();
|
||||||
memoryUpdater.setMemoryManager(MemoryManager.getInstance());
|
memoryUpdater.setCognationCapability(CognationManager.getInstance());
|
||||||
|
memoryUpdater.setMemoryCapability(CognationManager.getInstance());
|
||||||
|
memoryUpdater.setCacheCapability(CognationManager.getInstance());
|
||||||
|
memoryUpdater.setPerceiveCapability(CognationManager.getInstance());
|
||||||
memoryUpdater.setMemorySelectExtractor(MemorySelectExtractor.getInstance());
|
memoryUpdater.setMemorySelectExtractor(MemorySelectExtractor.getInstance());
|
||||||
memoryUpdater.setMemorySummarizer(MemorySummarizer.getInstance());
|
memoryUpdater.setMemorySummarizer(MemorySummarizer.getInstance());
|
||||||
memoryUpdater.setSessionManager(SessionManager.getInstance());
|
memoryUpdater.setSessionManager(SessionManager.getInstance());
|
||||||
@@ -73,10 +83,10 @@ public class MemoryUpdater implements InteractionModule {
|
|||||||
try {
|
try {
|
||||||
long currentTime = System.currentTimeMillis();
|
long currentTime = System.currentTimeMillis();
|
||||||
long lastUpdatedTime = sessionManager.getLastUpdatedTime();
|
long lastUpdatedTime = sessionManager.getLastUpdatedTime();
|
||||||
int chatCount = memoryManager.getChatMessages().size();
|
int chatCount = cognationCapability.getChatMessages().size();
|
||||||
if (lastUpdatedTime != 0 && currentTime - lastUpdatedTime > UPDATE_TRIGGER_INTERVAL && chatCount > 1) {
|
if (lastUpdatedTime != 0 && currentTime - lastUpdatedTime > UPDATE_TRIGGER_INTERVAL && chatCount > 1) {
|
||||||
updateMemory();
|
updateMemory();
|
||||||
memoryManager.getChatMessages().clear();
|
cognationCapability.getChatMessages().clear();
|
||||||
//重置MemoryId
|
//重置MemoryId
|
||||||
sessionManager.refreshMemoryId();
|
sessionManager.refreshMemoryId();
|
||||||
log.info("[MemoryUpdater] 记忆更新: 自动触发");
|
log.info("[MemoryUpdater] 记忆更新: 自动触发");
|
||||||
@@ -105,7 +115,7 @@ public class MemoryUpdater implements InteractionModule {
|
|||||||
int recallCount = moduleContext.getIntValue("recall_count");
|
int recallCount = moduleContext.getIntValue("recall_count");
|
||||||
log.debug("[MemoryUpdater] 记忆切片数量 [{}]", recallCount);
|
log.debug("[MemoryUpdater] 记忆切片数量 [{}]", recallCount);
|
||||||
}
|
}
|
||||||
int messageCount = memoryManager.getChatMessages().size();
|
int messageCount = cognationCapability.getChatMessages().size();
|
||||||
if (messageCount >= TRIGGER_ROLL_LIMIT) {
|
if (messageCount >= TRIGGER_ROLL_LIMIT) {
|
||||||
try {
|
try {
|
||||||
log.debug("[MemoryUpdater] 记忆更新: 已达{}轮", TRIGGER_ROLL_LIMIT);
|
log.debug("[MemoryUpdater] 记忆更新: 已达{}轮", TRIGGER_ROLL_LIMIT);
|
||||||
@@ -121,7 +131,7 @@ public class MemoryUpdater implements InteractionModule {
|
|||||||
|
|
||||||
private void updateMemory() {
|
private void updateMemory() {
|
||||||
log.debug("[MemoryUpdater] 记忆更新流程开始...");
|
log.debug("[MemoryUpdater] 记忆更新流程开始...");
|
||||||
tempMessage = new ArrayList<>(memoryManager.getChatMessages());
|
tempMessage = new ArrayList<>(cognationCapability.getChatMessages());
|
||||||
HashMap<String, String> singleMemorySummary = new HashMap<>();
|
HashMap<String, String> singleMemorySummary = new HashMap<>();
|
||||||
//更新单聊记忆,同时从chatMessages中去掉单聊记忆
|
//更新单聊记忆,同时从chatMessages中去掉单聊记忆
|
||||||
updateSingleChatSlices(singleMemorySummary);
|
updateSingleChatSlices(singleMemorySummary);
|
||||||
@@ -137,9 +147,9 @@ public class MemoryUpdater implements InteractionModule {
|
|||||||
Callable<Void> task = () -> {
|
Callable<Void> task = () -> {
|
||||||
log.debug("[MemoryUpdater] 多人聊天记忆更新流程开始...");
|
log.debug("[MemoryUpdater] 多人聊天记忆更新流程开始...");
|
||||||
List<Message> chatMessages;
|
List<Message> chatMessages;
|
||||||
memoryManager.getMessageLock().lock();
|
cognationCapability.getMessageLock().lock();
|
||||||
chatMessages = new ArrayList<>(memoryManager.getChatMessages());
|
chatMessages = new ArrayList<>(cognationCapability.getChatMessages());
|
||||||
memoryManager.getMessageLock().unlock();
|
cognationCapability.getMessageLock().unlock();
|
||||||
cleanMessage(chatMessages);
|
cleanMessage(chatMessages);
|
||||||
if (!chatMessages.isEmpty()) {
|
if (!chatMessages.isEmpty()) {
|
||||||
log.debug("[MemoryUpdater] 存在多人聊天记录, 流程正常进行...");
|
log.debug("[MemoryUpdater] 存在多人聊天记录, 流程正常进行...");
|
||||||
@@ -148,20 +158,20 @@ public class MemoryUpdater implements InteractionModule {
|
|||||||
if (userId == null) {
|
if (userId == null) {
|
||||||
throw new RuntimeException("未匹配到 userId!");
|
throw new RuntimeException("未匹配到 userId!");
|
||||||
}
|
}
|
||||||
SummarizeInput summarizeInput = new SummarizeInput(chatMessages, memoryManager.getTopicTree());
|
SummarizeInput summarizeInput = new SummarizeInput(chatMessages, memoryCapability.getTopicTree());
|
||||||
log.debug("[MemoryUpdater] 多人聊天记忆更新-总结流程-输入: {}", summarizeInput);
|
log.debug("[MemoryUpdater] 多人聊天记忆更新-总结流程-输入: {}", summarizeInput);
|
||||||
SummarizeResult summarizeResult = memorySummarizer.execute(summarizeInput);
|
SummarizeResult summarizeResult = memorySummarizer.execute(summarizeInput);
|
||||||
log.debug("[MemoryUpdater] 多人聊天记忆更新-总结流程-输出: {}", summarizeResult);
|
log.debug("[MemoryUpdater] 多人聊天记忆更新-总结流程-输出: {}", summarizeResult);
|
||||||
MemorySlice memorySlice = getMemorySlice(userId, summarizeResult, chatMessages);
|
MemorySlice memorySlice = getMemorySlice(userId, summarizeResult, chatMessages);
|
||||||
//设置involvedUserId
|
//设置involvedUserId
|
||||||
setInvolvedUserId(userId, memorySlice, chatMessages);
|
setInvolvedUserId(userId, memorySlice, chatMessages);
|
||||||
memoryManager.insertSlice(memorySlice, summarizeResult.getTopicPath());
|
memoryCapability.insertSlice(memorySlice, summarizeResult.getTopicPath());
|
||||||
|
|
||||||
memoryManager.updateDialogMap(LocalDateTime.now(), summarizeResult.getSummary());
|
cacheCapability.updateDialogMap(LocalDateTime.now(), summarizeResult.getSummary());
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
log.debug("[MemoryUpdater] 不存在多人聊天记录, 将以单聊总结为对话缓存的主要输入: {}", singleMemorySummary);
|
log.debug("[MemoryUpdater] 不存在多人聊天记录, 将以单聊总结为对话缓存的主要输入: {}", singleMemorySummary);
|
||||||
memoryManager.updateDialogMap(LocalDateTime.now(), memorySummarizer.executeTotalSummary(singleMemorySummary));
|
cacheCapability.updateDialogMap(LocalDateTime.now(), memorySummarizer.executeTotalSummary(singleMemorySummary));
|
||||||
}
|
}
|
||||||
log.debug("[MemoryUpdater] 对话缓存更新完毕");
|
log.debug("[MemoryUpdater] 对话缓存更新完毕");
|
||||||
log.debug("[MemoryUpdater] 多人聊天记忆更新流程结束...");
|
log.debug("[MemoryUpdater] 多人聊天记忆更新流程结束...");
|
||||||
@@ -184,11 +194,11 @@ public class MemoryUpdater implements InteractionModule {
|
|||||||
|
|
||||||
private void clearChatMessages() {
|
private void clearChatMessages() {
|
||||||
//不全部清空,保留一部分输入防止上下文割裂
|
//不全部清空,保留一部分输入防止上下文割裂
|
||||||
memoryManager.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() - TRIGGER_ROLL_LIMIT / 6, tempMessage.size()));
|
||||||
memoryManager.getChatMessages().clear();
|
cognationCapability.getChatMessages().clear();
|
||||||
memoryManager.getChatMessages().addAll(temp);
|
cognationCapability.getChatMessages().addAll(temp);
|
||||||
memoryManager.getMessageLock().unlock();
|
cognationCapability.getMessageLock().unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setInvolvedUserId(String startUserId, MemorySlice memorySlice, List<Message> chatMessages) {
|
private void setInvolvedUserId(String startUserId, MemorySlice memorySlice, List<Message> chatMessages) {
|
||||||
@@ -224,17 +234,17 @@ public class MemoryUpdater implements InteractionModule {
|
|||||||
log.debug("[MemoryUpdater] 单聊记忆[{}]更新: {}", thisCount, id);
|
log.debug("[MemoryUpdater] 单聊记忆[{}]更新: {}", thisCount, id);
|
||||||
try {
|
try {
|
||||||
//单聊记忆更新
|
//单聊记忆更新
|
||||||
SummarizeInput summarizeInput = new SummarizeInput(messages, memoryManager.getTopicTree());
|
SummarizeInput summarizeInput = new SummarizeInput(messages, memoryCapability.getTopicTree());
|
||||||
log.debug("[MemoryUpdater] 单聊记忆[{}]更新-总结流程-输入: {}", thisCount, JSONObject.toJSONString(summarizeInput));
|
log.debug("[MemoryUpdater] 单聊记忆[{}]更新-总结流程-输入: {}", thisCount, JSONObject.toJSONString(summarizeInput));
|
||||||
SummarizeResult summarizeResult = memorySummarizer.execute(summarizeInput);
|
SummarizeResult summarizeResult = memorySummarizer.execute(summarizeInput);
|
||||||
log.debug("[MemoryUpdater] 单聊记忆[{}]更新-总结流程-输出: {}", thisCount, JSONObject.toJSONString(summarizeResult));
|
log.debug("[MemoryUpdater] 单聊记忆[{}]更新-总结流程-输出: {}", thisCount, JSONObject.toJSONString(summarizeResult));
|
||||||
MemorySlice memorySlice = getMemorySlice(id, summarizeResult, messages);
|
MemorySlice memorySlice = getMemorySlice(id, summarizeResult, messages);
|
||||||
//插入时userDialogMap已经进行更新
|
//插入时userDialogMap已经进行更新
|
||||||
memoryManager.insertSlice(memorySlice, summarizeResult.getTopicPath());
|
memoryCapability.insertSlice(memorySlice, summarizeResult.getTopicPath());
|
||||||
//从chatMessages中移除单聊记录
|
//从chatMessages中移除单聊记录
|
||||||
memoryManager.cleanMessage(messages);
|
cognationCapability.cleanMessage(messages);
|
||||||
//添加至singleMemorySummary
|
//添加至singleMemorySummary
|
||||||
String key = memoryManager.getUser(id).getNickName() + "[" + id + "]";
|
String key = perceiveCapability.getUser(id).getNickName() + "[" + id + "]";
|
||||||
singleMemorySummary.put(key, summarizeResult.getSummary());
|
singleMemorySummary.put(key, summarizeResult.getSummary());
|
||||||
log.debug("[MemoryUpdater] 单聊记忆[{}]更新成功: ", thisCount);
|
log.debug("[MemoryUpdater] 单聊记忆[{}]更新成功: ", thisCount);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
@@ -1,19 +1,28 @@
|
|||||||
package work.slhaf.agent.module.modules.perceive.selector;
|
package work.slhaf.agent.module.modules.perceive.selector;
|
||||||
|
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
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.interaction.module.InteractionModule;
|
||||||
|
import work.slhaf.agent.module.common.PreModuleActions;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class PerceiveSelector implements InteractionModule {
|
@Slf4j
|
||||||
|
@Setter
|
||||||
|
public class PerceiveSelector implements InteractionModule, PreModuleActions {
|
||||||
|
|
||||||
|
private static volatile PerceiveSelector perceiveSelector;
|
||||||
|
|
||||||
private static PerceiveSelector perceiveSelector;
|
|
||||||
|
|
||||||
public static PerceiveSelector getInstance() throws IOException, ClassNotFoundException {
|
public static PerceiveSelector getInstance() throws IOException, ClassNotFoundException {
|
||||||
if (perceiveSelector == null) {
|
if (perceiveSelector == null) {
|
||||||
perceiveSelector = new PerceiveSelector();
|
synchronized (PerceiveSelector.class) {
|
||||||
|
if (perceiveSelector == null) {
|
||||||
|
perceiveSelector = new PerceiveSelector();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return perceiveSelector;
|
return perceiveSelector;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,4 +30,19 @@ public class PerceiveSelector implements InteractionModule {
|
|||||||
public void execute(InteractionContext context) throws IOException, ClassNotFoundException {
|
public void execute(InteractionContext context) throws IOException, ClassNotFoundException {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setAppendedPrompt(InteractionContext context) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setActiveModule(InteractionContext context) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getModuleName() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,12 @@ package work.slhaf.agent.module.modules.preprocess;
|
|||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
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.InteractionInputData;
|
import work.slhaf.agent.core.interaction.data.InteractionInputData;
|
||||||
import work.slhaf.agent.core.interaction.data.context.InteractionContext;
|
import work.slhaf.agent.core.interaction.data.context.InteractionContext;
|
||||||
import work.slhaf.agent.core.memory.MemoryManager;
|
|
||||||
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.AppendPromptData;
|
||||||
|
|
||||||
@@ -19,7 +22,8 @@ public class PreprocessExecutor {
|
|||||||
|
|
||||||
private static volatile PreprocessExecutor preprocessExecutor;
|
private static volatile PreprocessExecutor preprocessExecutor;
|
||||||
|
|
||||||
private MemoryManager memoryManager;
|
private CognationCapability cognationCapability;
|
||||||
|
private PerceiveCapability perceiveCapability;
|
||||||
private SessionManager sessionManager;
|
private SessionManager sessionManager;
|
||||||
|
|
||||||
private PreprocessExecutor() {
|
private PreprocessExecutor() {
|
||||||
@@ -30,7 +34,8 @@ public class PreprocessExecutor {
|
|||||||
synchronized (PreprocessExecutor.class) {
|
synchronized (PreprocessExecutor.class) {
|
||||||
if (preprocessExecutor == null) {
|
if (preprocessExecutor == null) {
|
||||||
preprocessExecutor = new PreprocessExecutor();
|
preprocessExecutor = new PreprocessExecutor();
|
||||||
preprocessExecutor.setMemoryManager(MemoryManager.getInstance());
|
preprocessExecutor.setCognationCapability(CognationManager.getInstance());
|
||||||
|
preprocessExecutor.setPerceiveCapability(CognationManager.getInstance());
|
||||||
preprocessExecutor.setSessionManager(SessionManager.getInstance());
|
preprocessExecutor.setSessionManager(SessionManager.getInstance());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -45,7 +50,7 @@ public class PreprocessExecutor {
|
|||||||
|
|
||||||
private void checkAndSetMemoryId() {
|
private void checkAndSetMemoryId() {
|
||||||
String currentMemoryId = sessionManager.getCurrentMemoryId();
|
String currentMemoryId = sessionManager.getCurrentMemoryId();
|
||||||
if (currentMemoryId == null || memoryManager.getChatMessages().isEmpty()) {
|
if (currentMemoryId == null || cognationCapability.getChatMessages().isEmpty()) {
|
||||||
sessionManager.refreshMemoryId();
|
sessionManager.refreshMemoryId();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -54,15 +59,19 @@ public class PreprocessExecutor {
|
|||||||
log.debug("[PreprocessExecutor] 预处理原始输入: {}", inputData);
|
log.debug("[PreprocessExecutor] 预处理原始输入: {}", inputData);
|
||||||
InteractionContext context = new InteractionContext();
|
InteractionContext context = new InteractionContext();
|
||||||
|
|
||||||
String userId = memoryManager.getUserId(inputData.getUserInfo(), inputData.getUserNickName());
|
User user = perceiveCapability.getUser(inputData.getUserInfo(), inputData.getPlatform());
|
||||||
|
if (user == null) {
|
||||||
|
user = perceiveCapability.addUser(inputData.getUserInfo(), inputData.getPlatform(), inputData.getUserNickName());
|
||||||
|
}
|
||||||
|
String userId = user.getUuid();
|
||||||
context.setUserId(userId);
|
context.setUserId(userId);
|
||||||
context.setUserNickname(inputData.getUserNickName());
|
context.setUserNickname(inputData.getUserNickName());
|
||||||
context.setUserInfo(inputData.getUserInfo());
|
context.setUserInfo(inputData.getUserInfo());
|
||||||
context.setDateTime(inputData.getLocalDateTime());
|
context.setDateTime(inputData.getLocalDateTime());
|
||||||
context.setSingle(inputData.isSingle());
|
context.setSingle(inputData.isSingle());
|
||||||
|
|
||||||
String user = "[" + inputData.getUserNickName() + "(" + userId + ")]";
|
String userStr = "[" + inputData.getUserNickName() + "(" + userId + ")]";
|
||||||
String input = user + " " + inputData.getContent();
|
String input = userStr + " " + inputData.getContent();
|
||||||
context.setInput(input);
|
context.setInput(input);
|
||||||
|
|
||||||
setAppendedPrompt(context);
|
setAppendedPrompt(context);
|
||||||
@@ -78,7 +87,7 @@ public class PreprocessExecutor {
|
|||||||
map.put("datetime", "本次用户输入对应的当前时间");
|
map.put("datetime", "本次用户输入对应的当前时间");
|
||||||
map.put("user_nick", "用户昵称");
|
map.put("user_nick", "用户昵称");
|
||||||
map.put("user_id", "用户id, 与user_nick区分, 这是用户的唯一标识");
|
map.put("user_id", "用户id, 与user_nick区分, 这是用户的唯一标识");
|
||||||
map.put("active_modules","已激活的模块, 为false时为激活但未活跃; 为true时为激活且活跃");
|
map.put("active_modules", "已激活的模块, 为false时为激活但未活跃; 为true时为激活且活跃");
|
||||||
map.put("其他", "历史对话中将在用户消息的最后一行标注时间");
|
map.put("其他", "历史对话中将在用户消息的最后一行标注时间");
|
||||||
AppendPromptData data = new AppendPromptData();
|
AppendPromptData data = new AppendPromptData();
|
||||||
data.setModuleName("[基础模块]");
|
data.setModuleName("[基础模块]");
|
||||||
|
|||||||
Reference in New Issue
Block a user