mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
- 将 userInput 的身份标志交给 PreprocessExecutor 生成
- 调整 CoreModel 对应的提示词 - 在 MemoryUpdater 中添加了自动更新线程,同时修复/调整了 memorySlice 对应的 userId 获取
This commit is contained in:
@@ -40,6 +40,7 @@ public class ModelConstant {
|
|||||||
- 回复必须完全基于text字段的核心语义生成
|
- 回复必须完全基于text字段的核心语义生成
|
||||||
- 禁止引用与当前text无关的历史内容
|
- 禁止引用与当前text无关的历史内容
|
||||||
- 若角色设定与当前对话无关,应自动忽略
|
- 若角色设定与当前对话无关,应自动忽略
|
||||||
|
- 上文中你的回应可能并没有符合这个格式,但那是经过裁剪后的结果,你需要严格确保本次回应的格式正确
|
||||||
- 输出格式严格为:
|
- 输出格式严格为:
|
||||||
{
|
{
|
||||||
"text": "响应内容"
|
"text": "响应内容"
|
||||||
|
|||||||
@@ -54,8 +54,7 @@ public class CoreModel extends Model implements InteractionModule {
|
|||||||
coreModel.getMessages().set(0, new Message(ChatConstant.Character.SYSTEM, ModelConstant.CORE_MODEL_PROMPT + "\r\n" + tempPrompt));
|
coreModel.getMessages().set(0, new Message(ChatConstant.Character.SYSTEM, ModelConstant.CORE_MODEL_PROMPT + "\r\n" + tempPrompt));
|
||||||
promptCache = tempPrompt;
|
promptCache = tempPrompt;
|
||||||
}
|
}
|
||||||
String user = "[" + interactionContext.getUserNickname() + "(" + interactionContext.getUserId() + ")]";
|
Message userMessage = new Message(ChatConstant.Character.USER, interactionContext.getCoreContext().toString());
|
||||||
Message userMessage = new Message(ChatConstant.Character.USER, user + " " + interactionContext.getCoreContext());
|
|
||||||
this.messages.add(userMessage);
|
this.messages.add(userMessage);
|
||||||
JSONObject response = null;
|
JSONObject response = null;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|||||||
@@ -31,12 +31,17 @@ public class MemoryUpdater implements InteractionModule {
|
|||||||
|
|
||||||
private static MemoryUpdater memoryUpdater;
|
private static MemoryUpdater memoryUpdater;
|
||||||
|
|
||||||
|
private static final String USERID_REGEX = "\\[.*\\(([^)]+)\\)\\]";
|
||||||
|
private static final long SCHEDULED_UPDATE_INTERVAL = 10 * 1000;
|
||||||
|
private static final long UPDATE_TRIGGER_INTERVAL = 30 * 60 * 1000;
|
||||||
|
|
||||||
private MemoryManager memoryManager;
|
private MemoryManager memoryManager;
|
||||||
private InteractionThreadPoolExecutor executor;
|
private InteractionThreadPoolExecutor executor;
|
||||||
private MemorySelectExtractor memorySelectExtractor;
|
private MemorySelectExtractor memorySelectExtractor;
|
||||||
private MemorySummarizer memorySummarizer;
|
private MemorySummarizer memorySummarizer;
|
||||||
private SessionManager sessionManager;
|
private SessionManager sessionManager;
|
||||||
private StaticMemoryExtractor staticMemoryExtractor;
|
private StaticMemoryExtractor staticMemoryExtractor;
|
||||||
|
private long lastUpdatedTime = 0;
|
||||||
|
|
||||||
private MemoryUpdater() {
|
private MemoryUpdater() {
|
||||||
}
|
}
|
||||||
@@ -50,10 +55,30 @@ public class MemoryUpdater implements InteractionModule {
|
|||||||
memoryUpdater.setSessionManager(SessionManager.getInstance());
|
memoryUpdater.setSessionManager(SessionManager.getInstance());
|
||||||
memoryUpdater.setStaticMemoryExtractor(StaticMemoryExtractor.getInstance());
|
memoryUpdater.setStaticMemoryExtractor(StaticMemoryExtractor.getInstance());
|
||||||
memoryUpdater.setExecutor(InteractionThreadPoolExecutor.getInstance());
|
memoryUpdater.setExecutor(InteractionThreadPoolExecutor.getInstance());
|
||||||
|
|
||||||
|
memoryUpdater.setScheduledUpdater();
|
||||||
}
|
}
|
||||||
return memoryUpdater;
|
return memoryUpdater;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setScheduledUpdater() {
|
||||||
|
executor.execute(() -> {
|
||||||
|
log.info("记忆自动更新线程启动");
|
||||||
|
while (!Thread.interrupted()) {
|
||||||
|
try {
|
||||||
|
long currentTime = System.currentTimeMillis();
|
||||||
|
if (lastUpdatedTime != 0 && currentTime - lastUpdatedTime > UPDATE_TRIGGER_INTERVAL) {
|
||||||
|
updateMemory();
|
||||||
|
}
|
||||||
|
Thread.sleep(SCHEDULED_UPDATE_INTERVAL);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("记忆自动更新线程出错: {}", e.getLocalizedMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.info("记忆自动更新线程结束");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(InteractionContext interactionContext) {
|
public void execute(InteractionContext interactionContext) {
|
||||||
if (interactionContext.isFinished()) {
|
if (interactionContext.isFinished()) {
|
||||||
@@ -63,14 +88,9 @@ public class MemoryUpdater implements InteractionModule {
|
|||||||
//如果token 大于阈值,则更新记忆
|
//如果token 大于阈值,则更新记忆
|
||||||
JSONObject moduleContext = interactionContext.getModuleContext();
|
JSONObject moduleContext = interactionContext.getModuleContext();
|
||||||
if (moduleContext.getIntValue("total_token") > 24000) {
|
if (moduleContext.getIntValue("total_token") > 24000) {
|
||||||
String userId = interactionContext.getUserId();
|
|
||||||
HashMap<String, String> singleMemorySummary = new HashMap<>();
|
|
||||||
try {
|
try {
|
||||||
//更新单聊记忆以及该场景中对应的确定性记忆,同时从chatMessages中去掉单聊记忆
|
updateMemory();
|
||||||
updateSingleChatSlices(userId, singleMemorySummary);
|
} catch (Exception e) {
|
||||||
//更新多人场景下的记忆及相关的确定性记忆
|
|
||||||
updateMultiChatSlices(userId, singleMemorySummary);
|
|
||||||
} catch (InterruptedException | IOException | ClassNotFoundException e) {
|
|
||||||
log.error("记忆更新线程出错: {}", e.getLocalizedMessage());
|
log.error("记忆更新线程出错: {}", e.getLocalizedMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -78,11 +98,25 @@ public class MemoryUpdater implements InteractionModule {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateMultiChatSlices(String userId, HashMap<String, String> singleMemorySummary) throws InterruptedException, IOException, ClassNotFoundException {
|
private void updateMemory() throws InterruptedException, IOException, ClassNotFoundException {
|
||||||
|
HashMap<String, String> singleMemorySummary = new HashMap<>();
|
||||||
|
//更新单聊记忆以及该场景中对应的确定性记忆,同时从chatMessages中去掉单聊记忆
|
||||||
|
updateSingleChatSlices(singleMemorySummary);
|
||||||
|
//更新多人场景下的记忆及相关的确定性记忆
|
||||||
|
updateMultiChatSlices(singleMemorySummary);
|
||||||
|
//更新最近更新时间
|
||||||
|
lastUpdatedTime = System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateMultiChatSlices(HashMap<String, String> singleMemorySummary) throws InterruptedException, IOException, ClassNotFoundException {
|
||||||
//此时chatMessages中不再包含单聊记录,直接执行摘要以及切片插入
|
//此时chatMessages中不再包含单聊记录,直接执行摘要以及切片插入
|
||||||
//对剩下的多人聊天记录进行进行摘要
|
//对剩下的多人聊天记录进行进行摘要
|
||||||
executor.execute(() -> {
|
executor.execute(() -> {
|
||||||
try {
|
try {
|
||||||
|
//以第一条user对应的id为发起用户
|
||||||
|
Pattern pattern = Pattern.compile(USERID_REGEX);
|
||||||
|
Matcher matcher = pattern.matcher(memoryManager.getChatMessages().getFirst().getContent());
|
||||||
|
String userId = matcher.group(1);
|
||||||
SummarizeResult summarizeResult = memorySummarizer.execute(new SummarizeInput(memoryManager.getChatMessages(), memoryManager.getTopicTree()));
|
SummarizeResult summarizeResult = memorySummarizer.execute(new SummarizeInput(memoryManager.getChatMessages(), memoryManager.getTopicTree()));
|
||||||
MemorySlice memorySlice = getMemorySlice(userId, summarizeResult, memoryManager.getChatMessages());
|
MemorySlice memorySlice = getMemorySlice(userId, summarizeResult, memoryManager.getChatMessages());
|
||||||
//设置involvedUserId
|
//设置involvedUserId
|
||||||
@@ -104,7 +138,7 @@ private void setInvolvedUserId(String startUserId, MemorySlice memorySlice, List
|
|||||||
}
|
}
|
||||||
//匹配userId
|
//匹配userId
|
||||||
String content = chatMessage.getContent();
|
String content = chatMessage.getContent();
|
||||||
Pattern pattern = Pattern.compile("\\[.*\\(([^)]+)\\)\\]");
|
Pattern pattern = Pattern.compile(USERID_REGEX);
|
||||||
Matcher matcher = pattern.matcher(content);
|
Matcher matcher = pattern.matcher(content);
|
||||||
if (!matcher.find()) {
|
if (!matcher.find()) {
|
||||||
continue;
|
continue;
|
||||||
@@ -118,7 +152,7 @@ private void setInvolvedUserId(String startUserId, MemorySlice memorySlice, List
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void updateSingleChatSlices(String interactionContext, HashMap<String, String> singleMemorySummary) throws InterruptedException {
|
private void updateSingleChatSlices(HashMap<String, String> singleMemorySummary) throws InterruptedException {
|
||||||
//更新单聊记忆,同时从chatMessages中去掉单聊记忆
|
//更新单聊记忆,同时从chatMessages中去掉单聊记忆
|
||||||
Set<String> userIdSet = new HashSet<>(sessionManager.getSingleMetaMessageMap().keySet());
|
Set<String> userIdSet = new HashSet<>(sessionManager.getSingleMetaMessageMap().keySet());
|
||||||
List<Callable<Void>> tasks = new ArrayList<>();
|
List<Callable<Void>> tasks = new ArrayList<>();
|
||||||
@@ -129,7 +163,7 @@ private void setInvolvedUserId(String startUserId, MemorySlice memorySlice, List
|
|||||||
try {
|
try {
|
||||||
//单聊记忆更新
|
//单聊记忆更新
|
||||||
SummarizeResult summarizeResult = memorySummarizer.execute(new SummarizeInput(messages, memoryManager.getTopicTree()));
|
SummarizeResult summarizeResult = memorySummarizer.execute(new SummarizeInput(messages, memoryManager.getTopicTree()));
|
||||||
MemorySlice memorySlice = getMemorySlice(interactionContext, summarizeResult, messages);
|
MemorySlice memorySlice = getMemorySlice(id, summarizeResult, messages);
|
||||||
//插入时userDialogMap已经进行更新
|
//插入时userDialogMap已经进行更新
|
||||||
memoryManager.insertSlice(memorySlice, summarizeResult.getTopicPath());
|
memoryManager.insertSlice(memorySlice, summarizeResult.getTopicPath());
|
||||||
//从chatMessages中移除单聊记录
|
//从chatMessages中移除单聊记录
|
||||||
|
|||||||
@@ -38,10 +38,12 @@ public class PreprocessExecutor {
|
|||||||
context.setDateTime(inputData.getLocalDateTime());
|
context.setDateTime(inputData.getLocalDateTime());
|
||||||
|
|
||||||
context.setFinished(false);
|
context.setFinished(false);
|
||||||
context.setInput(inputData.getContent());
|
String user = "[" + inputData.getUserNickName() + "(" + userId + ")]";
|
||||||
|
String input = user + " " + inputData.getContent();
|
||||||
|
context.setInput(input);
|
||||||
|
|
||||||
context.setCoreContext(new JSONObject());
|
context.setCoreContext(new JSONObject());
|
||||||
context.getCoreContext().put("text", inputData.getContent());
|
context.getCoreContext().put("text", input);
|
||||||
context.getCoreContext().put("datetime", LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
|
context.getCoreContext().put("datetime", LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
|
||||||
context.getCoreContext().put("character", memoryManager.getCharacter());
|
context.getCoreContext().put("character", memoryManager.getCharacter());
|
||||||
context.getCoreContext().put("user_nick", inputData.getUserNickName());
|
context.getCoreContext().put("user_nick", inputData.getUserNickName());
|
||||||
|
|||||||
Reference in New Issue
Block a user