Skip to content

Commit

Permalink
spotless format
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangxiaojiawow committed Jul 21, 2023
1 parent 3601f97 commit 0329a02
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@
*/
public class JsonUtils {

private JsonUtils() {
}

private static final ObjectMapper OBJECT_MAPPER = createObjectMapper();

private static ObjectMapper createObjectMapper() {
Expand All @@ -53,7 +50,7 @@ public static String toJsonStringWithIndent(Object object, int indent) {
}

public static String toJsonStringWithIndent(Object object) {
return toJsonStringWithIndent(object, 4);
return toJsonStringWithIndent(object, 4);
}

public static <T> T convertFromJsonStr(String jsonStr, Class<T> clazz) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hw.langchain.memory.chat.message.histories.database;

import com.hw.langchain.schema.BaseMessage;

import java.util.List;

/**
Expand All @@ -11,15 +30,15 @@
**/
public interface ChatMessageRepository {

/**
* load all history chat message of given sessionId
*
* @param sessionId
* @return
*/
List<BaseMessage> loadMessage(String sessionId);
/**
* load all history chat message of given sessionId
*
* @param sessionId
* @return
*/
List<BaseMessage> loadMessage(String sessionId);

void saveMessage(String sessionId, BaseMessage baseMessage);
void saveMessage(String sessionId, BaseMessage baseMessage);

void clearSessionChatMessage(String sessionId);
void clearSessionChatMessage(String sessionId);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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;

/**
Expand All @@ -12,26 +31,26 @@
**/
public class DataBaseChatMessageHistory extends BaseChatMessageHistory {

private final String sessionId;
private final ChatMessageRepository chatMessageRepository;
private final String sessionId;
private final ChatMessageRepository chatMessageRepository;

public DataBaseChatMessageHistory(String sessionId, ChatMessageRepository chatMessageRepository) {
this.sessionId = sessionId;
this.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 addMessage(BaseMessage message) {
chatMessageRepository.saveMessage(sessionId, message);
}

@Override
public void clear() {
chatMessageRepository.clearSessionChatMessage(sessionId);
}
@Override
public void clear() {
chatMessageRepository.clearSessionChatMessage(sessionId);
}

@Override
public List<BaseMessage> getMessages() {
return chatMessageRepository.loadMessage(sessionId);
}
@Override
public List<BaseMessage> getMessages() {
return chatMessageRepository.loadMessage(sessionId);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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;

import java.util.List;

/**
* a simple wrapper for DataBaseChatMessageHistory with redisChatMessageRepository;
*
Expand All @@ -14,30 +34,30 @@
**/
public class RedisChatMessageHistory extends BaseChatMessageHistory {

private DataBaseChatMessageHistory dataBaseChatMessageHistory;
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, 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);
}
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 addMessage(BaseMessage message) {
dataBaseChatMessageHistory.addMessage(message);
}

@Override
public void clear() {
dataBaseChatMessageHistory.clear();
}
@Override
public void clear() {
dataBaseChatMessageHistory.clear();
}

@Override
public List<BaseMessage> getMessages() {
return dataBaseChatMessageHistory.getMessages();
}
@Override
public List<BaseMessage> getMessages() {
return dataBaseChatMessageHistory.getMessages();
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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 org.redisson.api.RQueue;
import org.redisson.api.RedissonClient;

import lombok.Builder;
import lombok.experimental.Tolerate;

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
Expand All @@ -20,57 +41,57 @@
@Builder
public class RedisChatMessageRepository implements ChatMessageRepository {

private RedissonClient redissonClient;
private String keyPrefix = "message_store";
private Integer ttlSeconds;
private RedissonClient redissonClient;
private String keyPrefix = "message_store";
private Integer ttlSeconds;

@Tolerate
public RedisChatMessageRepository(RedissonClient redissonClient) {
this.redissonClient = redissonClient;
}
@Tolerate
public RedisChatMessageRepository(RedissonClient redissonClient) {
this.redissonClient = redissonClient;
}

@Tolerate
public RedisChatMessageRepository(RedissonClient redissonClient, int ttlSeconds) {
this.redissonClient = redissonClient;
this.ttlSeconds = ttlSeconds;
}
@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;
}
/**
* 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 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 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();
}
@Override
public void clearSessionChatMessage(String sessionId) {
RQueue<String> messageQueue = redissonClient.getQueue(key(sessionId));
messageQueue.delete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.hw.langchain.chains.query.constructor.JsonUtils;
import com.hw.langchain.exception.LangChainException;

import lombok.Data;
import lombok.NoArgsConstructor;

Expand Down Expand Up @@ -56,8 +57,9 @@ public Map<String, Object> toMap() {
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
objectMapper.registerModule(module);
Map<String, Object> map = objectMapper.convertValue(this, new TypeReference<>() {});
return Map.of("type", type(), "data", map);
Map<String, Object> map = objectMapper.convertValue(this, new TypeReference<>() {
});
return Map.of("type", type(), "data", map);
}

public static BaseMessage fromMap(Map<String, Object> message) {
Expand Down

0 comments on commit 0329a02

Please sign in to comment.