forked from HamaWhiteGG/langchain-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add database based chat history message memory
- Loading branch information
1 parent
3a267e8
commit 24be2ec
Showing
6 changed files
with
185 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...n/java/com/hw/langchain/memory/chat/message/histories/database/ChatMessageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.hw.langchain.memory.chat.message.histories.database; | ||
|
||
import com.hw.langchain.schema.BaseMessage; | ||
import java.util.List; | ||
|
||
/** | ||
* interface for database supported chat message repository; | ||
* @author zhangxiaojia002 | ||
* @date 2023/7/20 9:50 下午 | ||
**/ | ||
public interface ChatMessageRepository { | ||
|
||
/** | ||
* load all history chat message of given sessionId | ||
* @param sessionId | ||
* @return | ||
*/ | ||
List<BaseMessage> loadMessage(String sessionId); | ||
|
||
void saveMessage(String sessionId, BaseMessage baseMessage); | ||
|
||
void clearSessionChatMessage(String sessionId); | ||
} |
35 changes: 35 additions & 0 deletions
35
...a/com/hw/langchain/memory/chat/message/histories/database/DataBaseChatMessageHistory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.hw.langchain.memory.chat.message.histories.database; | ||
|
||
import com.hw.langchain.schema.BaseChatMessageHistory; | ||
import com.hw.langchain.schema.BaseMessage; | ||
import java.util.List; | ||
|
||
/** | ||
* database based chat message history; | ||
* @author zhangxiaojia002 | ||
* @date 2023/7/20 9:53 下午 | ||
**/ | ||
public class DataBaseChatMessageHistory extends BaseChatMessageHistory { | ||
private final String sessionId; | ||
private final ChatMessageRepository chatMessageRepository; | ||
|
||
public DataBaseChatMessageHistory(String sessionId, ChatMessageRepository chatMessageRepository) { | ||
this.sessionId = sessionId; | ||
this.chatMessageRepository = chatMessageRepository; | ||
} | ||
|
||
@Override | ||
public void addMessage(BaseMessage message) { | ||
chatMessageRepository.saveMessage(sessionId, message); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
chatMessageRepository.clearSessionChatMessage(sessionId); | ||
} | ||
|
||
@Override | ||
public List<BaseMessage> getMessages() { | ||
return chatMessageRepository.loadMessage(sessionId); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...a/com/hw/langchain/memory/chat/message/histories/database/RedisChatMessageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.hw.langchain.memory.chat.message.histories.database; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.hw.langchain.chains.query.constructor.JsonUtils; | ||
import com.hw.langchain.schema.BaseMessage; | ||
import java.time.Duration; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.List; | ||
import java.util.Map; | ||
import lombok.Builder; | ||
import lombok.experimental.Tolerate; | ||
import org.redisson.api.RQueue; | ||
import org.redisson.api.RedissonClient; | ||
|
||
/** | ||
* @author zhangxiaojia002 | ||
* @date 2023/7/20 9:59 下午 | ||
**/ | ||
@Builder | ||
public class RedisChatMessageRepository implements ChatMessageRepository { | ||
|
||
private RedissonClient redissonClient; | ||
private String keyPrefix = "message_store"; | ||
private Integer ttlSeconds; | ||
|
||
@Tolerate | ||
public RedisChatMessageRepository(RedissonClient redissonClient) { | ||
this.redissonClient = redissonClient; | ||
} | ||
|
||
@Tolerate | ||
public RedisChatMessageRepository(RedissonClient redissonClient, int ttlSeconds) { | ||
this.redissonClient = redissonClient; | ||
this.ttlSeconds = ttlSeconds; | ||
} | ||
|
||
/** | ||
* Construct the record key to use | ||
* | ||
* @return | ||
*/ | ||
private String key(String sessionId) { | ||
if (this.keyPrefix == null) { | ||
return sessionId; | ||
} | ||
return this.keyPrefix + sessionId; | ||
} | ||
|
||
@Override | ||
public List<BaseMessage> loadMessage(String sessionId) { | ||
RQueue<String> messageQueue = redissonClient.getQueue(key(sessionId)); | ||
List<String> messageJSonStrList = messageQueue.readAll(); | ||
return messageJSonStrList.stream().map(x -> { | ||
Map<String, Object> data = | ||
JsonUtils.convertFromJsonStr(x, new TypeReference<>() { | ||
}); | ||
return BaseMessage.fromMap(data); | ||
}).toList(); | ||
} | ||
|
||
@Override | ||
public void saveMessage(String sessionId, BaseMessage baseMessage) { | ||
RQueue<String> messageQueue = redissonClient.getQueue(key(sessionId)); | ||
messageQueue.add(JsonUtils.toJsonStringWithIndent(baseMessage.toMap())); | ||
if (this.ttlSeconds != null) { | ||
messageQueue.expire(Duration.of(ttlSeconds, ChronoUnit.SECONDS)); | ||
} | ||
} | ||
|
||
@Override | ||
public void clearSessionChatMessage(String sessionId) { | ||
RQueue<String> messageQueue = redissonClient.getQueue(key(sessionId)); | ||
messageQueue.delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters