Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I implement a ChatMemoryStore to store data item by item? please help me #1027

Closed
LiuYuPeng1101 opened this issue Apr 26, 2024 · 4 comments

Comments

@LiuYuPeng1101
Copy link

`public class PersistentChatMemoryStore implements ChatMemoryStore {

@Resource
private SessionChatRecordService sessionChatRecordService;

@Override
public List<ChatMessage> getMessages(Object memoryId) {
    SessionChatRecord chatRecord = sessionChatRecordService.getOne(new LambdaQueryWrapper<SessionChatRecord>().eq(SessionChatRecord::getSessionId, Convert.toLong(memoryId)));
    if (ObjectUtil.isNull(chatRecord)) {
        return  new ArrayList<>();
    }
    return messagesFromJson(chatRecord.getContent());
}


@Override
public void updateMessages(Object memoryId, List<ChatMessage> messages) {
    String json = messagesToJson(messages);
    SessionChatRecord sessionChatRecord = SessionChatRecord.builder().sessionId(Convert.toLong(memoryId)).content(json).build();
    sessionChatRecordService.saveOrUpdate(sessionChatRecord,new LambdaUpdateWrapper<SessionChatRecord>()
            .eq(SessionChatRecord::getSessionId,Convert.toLong(memoryId)).set(SessionChatRecord::getContent,sessionChatRecord.getContent()));
}

@Override
public void deleteMessages(Object memoryId) {}

}`
As mentioned in my code above, I store all chat records in the 'content' field of a MySQL database. However, I now want to store chat records one by one. How can I implement this?

@langchain4j
Copy link
Owner

@LiuYuPeng1101 there is a similar messageToJson() method that converts each message to json. Then you can store each one as a separate record in the DB.

Please note that this is not a chat history, it is a chat memory, it does not store all the messages in the conversation, only those that fit into the configured message/token window.

@LiuYuPeng1101
Copy link
Author

@langchain4j Thank you for the reminder. I used the following code for the dialogue with the large model:
@GetMapping("/assistant") public String assistant(@RequestParam(value = "memoryId") Long memoryId, @RequestParam(value = "message") String message) { return assistant.chat(memoryId,message);
I plan to use MySQL to store chat records one by one. Are there any examples I can refer to? If not MySQL, are there any references for storing chat records in general?

@langchain4j
Copy link
Owner

@LiuYuPeng1101 may I ask you why do you want to store chat messages separately?

There is a CassandraChatMemoryStore as an example of storing messages separately, but honestly there is nothing LangChain4j-specific here, this is a "how do I store multiple related entries in a relational database" type of problem, there should be a lot of material on that online.

@langchain4j
Copy link
Owner

@LiuYuPeng1101 I am closing htis issue, please use discord or discussions in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants