在 MessageChatMemoryAdvisor 组件的before方法中,方法如下: `@Override public ChatClientRequest before(ChatClientRequest chatClientRequest, AdvisorChain advisorChain) { String conversationId = getConversationId(chatClientRequest.context(), this.defaultConversationId);
// 1. Retrieve the chat memory for the current conversation.
List<Message> memoryMessages = this.chatMemory.get(conversationId);
// 2. Advise the request messages list.
List<Message> processedMessages = new ArrayList<>(memoryMessages);
processedMessages.addAll(chatClientRequest.prompt().getInstructions());
// 3. Create a new request with the advised messages.
ChatClientRequest processedChatClientRequest = chatClientRequest.mutate()
.prompt(chatClientRequest.prompt().mutate().messages(processedMessages).build())
.build();
// 4. Add the new user message to the conversation memory.
UserMessage userMessage = processedChatClientRequest.prompt().getUserMessage();
this.chatMemory.add(conversationId, userMessage);
return processedChatClientRequest;
}`
方法入参的ChatClientRequest chatClientRequest中,是本次会话的用户信息。
当执行 List
记录信息如下:
Comment From: eeaters
UserMessage userMessage = processedChatClientRequest.prompt().getUserMessage();
In the code, getUserMessage() returns the last user message and does not save duplicates. I see that your table structure is not the default ChatMemory. You can debug it.
Comment From: chenlinks
UserMessage userMessage = processedChatClientRequest.prompt().getUserMessage();
In the code, getUserMessage() returns the last user message and does not save duplicates. I see that your table structure is not the default ChatMemory. You can debug it.
好的,我测试了一下,确实没有问题。debug 看了一下,发现执行写入时先删除了数据,如下:
`@Override
public void saveAll(String conversationId, List
this.transactionTemplate.execute(status -> {
deleteByConversationId(conversationId);
this.jdbcTemplate.batchUpdate(this.dialect.getInsertMessageSql(),
new AddBatchPreparedStatement(conversationId, messages));
return null;
});
}`
关键点在于:deleteByConversationId(conversationId);否则还是会存在重复记录。这样做不知道是否合理
Comment From: chenlinks
UserMessage userMessage = processedChatClientRequest.prompt().getUserMessage();
In the code, getUserMessage() returns the last user message and does not save duplicates. I see that your table structure is not the default ChatMemory. You can debug it.
也就是说,这个会话记忆里面,始终记录的是最后一次会话的内容。 谢谢你的回答。