From 3f510a94ab5b982a39c962444e1757777a615af6 Mon Sep 17 00:00:00 2001 From: zhangxiaojia002 <479174937@qq.com> Date: Fri, 21 Jul 2023 10:57:32 +0800 Subject: [PATCH] add redisChatMessageHistory --- .../redis/RedisChatMessageHistory.java | 43 +++++++++++++++++++ .../RedisChatMessageRepository.java | 3 +- 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 langchain-core/src/main/java/com/hw/langchain/memory/chat/message/histories/database/redis/RedisChatMessageHistory.java rename langchain-core/src/main/java/com/hw/langchain/memory/chat/message/histories/database/{ => redis}/RedisChatMessageRepository.java (92%) diff --git a/langchain-core/src/main/java/com/hw/langchain/memory/chat/message/histories/database/redis/RedisChatMessageHistory.java b/langchain-core/src/main/java/com/hw/langchain/memory/chat/message/histories/database/redis/RedisChatMessageHistory.java new file mode 100644 index 000000000..9fc135635 --- /dev/null +++ b/langchain-core/src/main/java/com/hw/langchain/memory/chat/message/histories/database/redis/RedisChatMessageHistory.java @@ -0,0 +1,43 @@ +package com.hw.langchain.memory.chat.message.histories.database.redis; + +import com.hw.langchain.memory.chat.message.histories.database.DataBaseChatMessageHistory; +import com.hw.langchain.schema.BaseChatMessageHistory; +import com.hw.langchain.schema.BaseMessage; +import java.util.List; +import org.redisson.api.RedissonClient; + +/** + * a simple wrapper for DataBaseChatMessageHistory with redisChatMessageRepository; + * + * @author zhangxiaojia002 + * @date 2023/7/21 10:49 上午 + **/ +public class RedisChatMessageHistory extends BaseChatMessageHistory { + + private DataBaseChatMessageHistory dataBaseChatMessageHistory; + + public RedisChatMessageHistory(String sessionId, RedissonClient redissonClient, int ttl) { + RedisChatMessageRepository redisChatMessageRepository = new RedisChatMessageRepository(redissonClient, ttl); + dataBaseChatMessageHistory = new DataBaseChatMessageHistory(sessionId, redisChatMessageRepository); + } + + public RedisChatMessageHistory(String sessionId, RedissonClient redissonClient) { + RedisChatMessageRepository redisChatMessageRepository = new RedisChatMessageRepository(redissonClient); + dataBaseChatMessageHistory = new DataBaseChatMessageHistory(sessionId, redisChatMessageRepository); + } + + @Override + public void addMessage(BaseMessage message) { + dataBaseChatMessageHistory.addMessage(message); + } + + @Override + public void clear() { + dataBaseChatMessageHistory.clear(); + } + + @Override + public List getMessages() { + return dataBaseChatMessageHistory.getMessages(); + } +} diff --git a/langchain-core/src/main/java/com/hw/langchain/memory/chat/message/histories/database/RedisChatMessageRepository.java b/langchain-core/src/main/java/com/hw/langchain/memory/chat/message/histories/database/redis/RedisChatMessageRepository.java similarity index 92% rename from langchain-core/src/main/java/com/hw/langchain/memory/chat/message/histories/database/RedisChatMessageRepository.java rename to langchain-core/src/main/java/com/hw/langchain/memory/chat/message/histories/database/redis/RedisChatMessageRepository.java index 08939f847..a132bbb9d 100644 --- a/langchain-core/src/main/java/com/hw/langchain/memory/chat/message/histories/database/RedisChatMessageRepository.java +++ b/langchain-core/src/main/java/com/hw/langchain/memory/chat/message/histories/database/redis/RedisChatMessageRepository.java @@ -1,7 +1,8 @@ -package com.hw.langchain.memory.chat.message.histories.database; +package com.hw.langchain.memory.chat.message.histories.database.redis; import com.fasterxml.jackson.core.type.TypeReference; import com.hw.langchain.chains.query.constructor.JsonUtils; +import com.hw.langchain.memory.chat.message.histories.database.ChatMessageRepository; import com.hw.langchain.schema.BaseMessage; import java.time.Duration; import java.time.temporal.ChronoUnit;