springAi的上下文存储
环境:
1-springboot:3.4.3
2-jdk:17
3-spring-ai-alibaba:1.0.0-M6.1
问题:
在调用大模型聊天的时候,springAi使用`InMemoryChatMemory`在内存存储上下文,重启项目丢失
解决:实现`implements ChatMemory`接口方法,写出redis存储
2步走:
1-实现接口
2-在模型调用的时候,配置和使用一下
RedisUtil 工具
@Component
public class RedisUtil {
private static RedisTemplate<String, Object> redisTemplate;
private static final String redisPrefix = REDIS_PREFIX_BASIC;
/**
* 注入Redis
*
* @param redisTemplate Redis对象
* @author fzr
*/
@Resource
public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
RedisUtil.redisTemplate = redisTemplate;
}
/**
* 对象句柄
*
* @return RedisTemplate
* @author fzr
*/
public static RedisTemplate<String, Object> handler() {
return redisTemplate;
}
/**
* 指定缓存失效时间
*
* @param key 键
* @param second 时间(秒)
* @author fzr
*/
public static void expire(String key, Long second) {
key = redisPrefix + key;
redisTemplate.expire(key, second, TimeUnit.SECONDS);
}
/**
* 指定缓存失效时间
*
* @param key 键
* @param millisecond 时间(毫秒)
* @author fzr
*/
public static void pExpire(String key, Long millisecond) {
key = redisPrefix + key;
redisTemplate.expire(key, millisecond, TimeUnit.MILLISECONDS);
}
/**
* 指定缓存永久有效
*
* @param key 键
* @author fzr
*/
public static void persist(String key) {
key = redisPrefix + key;
redisTemplate.persist(key);
}
/**
* 根据key获取过期时间
*
* @param key 键不能为null
* @return 返回0代表为永久有效(秒)
* @author fzr
*/
public static Long ttl(String key) {
key = redisPrefix + key;
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
public static Long ttlWithoutPrefix(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 根据key获取过期时间
*
* @param key 键不能为null
* @return 返回0代表为永久有效(毫秒)
* @author fzr
*/
public static Long pTtl(String key) {
key = redisPrefix + key;
return redisTemplate.getExpire(key, TimeUnit.MILLISECONDS);
}
/**
* 判断key是否存在
*
* @param key 键
* @return true=存在,false=不存在
* @author fzr
*/
public static Boolean exists(String key) {
key = redisPrefix + key;
return redisTemplate.hasKey(key);
}
/**
* 删除1个或多个键
*
* @param key 键(一个或多个)
* @author fzr
*/
@SuppressWarnings("unchecked")
public static void del(String... key) {
if (key.length == 1) {
key[0] = redisPrefix + key[0];
redisTemplate.delete(key[0]);
} else {
for (int i = 0; key.length > i; i++) {
key[i] = redisPrefix + key[i];
}
redisTemplate.delete((Collection<String>) CollectionUtils.arrayToList(key));
}
}
/**
* 给key赋值一个新的key名
*
* @param oldKey 旧的key
* @param newKey 新的key
* @author fzr
*/
public static void rename(String oldKey, String newKey) {
oldKey = redisPrefix + oldKey;
newKey = redisPrefix + newKey;
redisTemplate.rename(oldKey, newKey);
}
/**
* 将当前数据库的key移动到给定的数据库db当中
*
* @param key 键
* @param db 库
* @return Boolean
* @author fzr
*/
public static Boolean move(String key, int db) {
key = redisPrefix + key;
return redisTemplate.move(key, db);
}
/**
* 获取匹配的key值
*
* @param pattern 通配符(*, ?, [])
* @return Set
* @author fzr
* @author fzr
*/
public static Set<String> keys(String pattern) {
return redisTemplate.keys(pattern);
}
/**
* 随机返回一个key
*
* @return String
* @author fzr
* @author fzr
*/
public static String randomKey() {
return redisTemplate.randomKey();
}
/* ***************** common end *************** */
/**
* 按匹配获取或有KEY
*
* @param pattern 规则
* @return Set<String>
* @author fzr
*/
public static Set<String> matchSet(String pattern) {
Set<String> keys = new LinkedHashSet<>();
RedisUtil.handler()
.execute(
(RedisConnection connection) -> {
try (Cursor<byte[]> cursor =
connection.scan(
ScanOptions.scanOptions()
.count(Long.MAX_VALUE)
.match(pattern)
.build())) {
cursor.forEachRemaining(
item -> {
keys.add(RedisSerializer.string().deserialize(item));
});
return null;
} catch (Exception e) {
throw new RuntimeException(e);
}
});
return keys;
}
/**
* 获取key的值
*
* @param key 键
* @return Object
* @author fzr
*/
public static Object get(String key) {
key = redisPrefix + key;
return redisTemplate.opsForValue().get(key);
}
/**
* 获取旧值并设置新值
*
* @param key 键
* @param newVal 新值
* @return Object
* @author fzr
*/
public static Object getSet(String key, Object newVal) {
key = redisPrefix + key;
return redisTemplate.opsForValue().getAndSet(key, newVal);
}
/**
* 设置键值对
*
* @param key 键
* @param value 值
* @author fzr
*/
public static void set(String key, Object value) {
key = redisPrefix + key;
redisTemplate.opsForValue().set(key, value);
}
/**
* 设置键值对并设置时间
*
* @param key 键
* @param value 值
* @param time time要大于0 如果time小于等于0 将设置无限期
* @author fzr
*/
public static void set(String key, Object value, long time) {
key = redisPrefix + key;
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
}
/**
* 递增
*
* @param key 键
* @param delta 要增加几(大于0)
* @return Long
* @author fzr
*/
public static Long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
key = redisPrefix + key;
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 递减
*
* @param key 键
* @param delta 要减少几(小于0)
* @return Long
* @author fzr
*/
public static Long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
key = redisPrefix + key;
return redisTemplate.opsForValue().increment(key, -delta);
}
/* ***************** String end *************** */
/**
* 获取key中field域的值
*
* @param key 键 不能为null
* @param field 项 不能为null
* @return 值
* @author fzr
*/
public static Object hGet(String key, String field) {
key = redisPrefix + key;
return redisTemplate.opsForHash().get(key, field);
}
/**
* 判断key中有没有field域名
*
* @param key 键
* @param field 字段
* @return Boolean
* @author fzr
*/
public static Boolean hExists(String key, Object field) {
key = redisPrefix + key;
return redisTemplate.opsForHash().hasKey(key, field);
}
/**
* 获取hashKey对应的所有键值
*
* @param key 键
* @return 对应的多个键值
* @author fzr
*/
public Map<Object, Object> hmGet(String key) {
key = redisPrefix + key;
return redisTemplate.opsForHash().entries(key);
}
/**
* 设置field1->N个域,对应的值是value1->N
*
* @param key 键
* @param map 对应多个键值
* @author fzr
*/
public static void hmSet(String key, Map<String, Object> map) {
key = redisPrefix + key;
redisTemplate.opsForHash().putAll(key, map);
}
/**
* HashSet 并设置时间
*
* @param key 键
* @param map 对应多个键值
* @param time 时间(秒)
* @author fzr
*/
public static void hmSet(String key, Map<String, Object> map, long time) {
key = redisPrefix + key;
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key 键
* @param item 项
* @param value 值
* @author fzr
*/
public static void hSet(String key, String item, Object value) {
key = redisPrefix + key;
redisTemplate.opsForHash().put(key, item, value);
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key 键
* @param item 项
* @param value 值
* @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
* @return true 成功 false失败
* @author fzr
*/
public static boolean hSet(String key, String item, Object value, long time) {
key = redisPrefix + key;
redisTemplate.opsForHash().put(key, item, value);
if (time > 0) {
expire(key, time);
}
return true;
}
/**
* 删除hash表中的值
*
* @param key 键 不能为null
* @param item 项 可以使多个 不能为null
* @author fzr
*/
public static void hDel(String key, Object... item) {
key = redisPrefix + key;
redisTemplate.opsForHash().delete(key, item);
}
/**
* 判断hash表中是否有该项的值
*
* @param key 键 不能为null
* @param item 项 不能为null
* @return true 存在 false不存在
* @author fzr
*/
public static boolean hHasKey(String key, String item) {
key = redisPrefix + key;
return redisTemplate.opsForHash().hasKey(key, item);
}
/**
* hash递增 如果不存在,就会创建一个并把新增后的值返回
*
* @param key 键
* @param item 项
* @param by 要增加几(大于0)
* @return double
* @author fzr
*/
public static double hIncr(String key, String item, long by) {
key = redisPrefix + key;
return redisTemplate.opsForHash().increment(key, item, by);
}
/**
* hash递减
*
* @param key 键
* @param item 项
* @param by 要减少记(小于0)
* @return double
* @author fzr
*/
public static double hDecr(String key, String item, long by) {
key = redisPrefix + key;
return redisTemplate.opsForHash().increment(key, item, -by);
}
/* ***************** Map end *************** */
/**
* 根据key获取Set中的所有值
*
* @param key 键
* @return Set
* @author fzr
*/
public static Set<Object> sGet(String key) {
key = redisPrefix + key;
return redisTemplate.opsForSet().members(key);
}
/**
* 根据value从一个set中查询,是否存在
*
* @param key 键
* @param value 值
* @return true 存在 false不存在
* @author fzr
*/
public Boolean sHasKey(String key, Object value) {
key = redisPrefix + key;
return redisTemplate.opsForSet().isMember(key, value);
}
/**
* 将数据放入set缓存
*
* @param key 键
* @param values 值 可以是多个
* @return 成功个数
* @author fzr
*/
public static Long sSet(String key, Object... values) {
key = redisPrefix + key;
return redisTemplate.opsForSet().add(key, values);
}
/**
* 将set数据放入缓存
*
* @param key 键
* @param time 时间(秒)
* @param values 值 可以是多个
* @return 成功个数
* @author fzr
*/
public Long sSetAndTime(String key, long time, Object... values) {
key = redisPrefix + key;
return redisTemplate.opsForSet().add(key, values);
}
/**
* 获取set缓存的长度
*
* @param key 键
* @return Long
* @author fzr
*/
public Long sGetSetSize(String key) {
key = redisPrefix + key;
return redisTemplate.opsForSet().size(key);
}
/**
* 移除值为value的
*
* @param key 键
* @param values 值 可以是多个
* @return 移除的个数
* @author fzr
*/
public Long setRemove(String key, Object... values) {
key = redisPrefix + key;
return redisTemplate.opsForSet().remove(key, values);
}
/* ***************** Set end *************** */
/**
* 获取list缓存的内容
*
* @param key 键
* @param start 开始
* @param end 结束 0 到 -1代表所有值
* @return List
* @author fzr
*/
public List<Object> lGet(String key, long start, long end) {
key = redisPrefix + key;
return redisTemplate.opsForList().range(key, start, end);
}
/**
* 获取list缓存的长度
*
* @param key 键
* @return Long
* @author fzr
*/
public Long lGetListSize(String key) {
key = redisPrefix + key;
return redisTemplate.opsForList().size(key);
}
/**
* 通过索引获取list中的值
*
* @param key 键
* @param index 索引 index>=0时,0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
* @return Object
* @author fzr
*/
public Object lGetIndex(String key, long index) {
key = redisPrefix + key;
return redisTemplate.opsForList().index(key, index);
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @return boolean
* @author fzr
*/
public boolean lSet(String key, Object value) {
key = redisPrefix + key;
redisTemplate.opsForList().rightPush(key, value);
return true;
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @param second 时间(秒)
* @return boolean
* @author fzr
*/
public boolean lSet(String key, Object value, long second) {
key = redisPrefix + key;
redisTemplate.opsForList().rightPush(key, value);
// if (second > 0) expire(key, second);
if (second > 0) {
// 显式设置过期时间,xxx秒
redisTemplate.expire(key, second, TimeUnit.SECONDS);
}
return true;
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @return boolean
* @author fzr
*/
public boolean lSet(String key, List<Object> value) {
key = redisPrefix + key;
redisTemplate.opsForList().rightPushAll(key, value);
return true;
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @param time 时间(秒)
* @return boolean
* @author fzr
*/
public boolean lSet(String key, List<Object> value, Long time) {
key = redisPrefix + key;
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0) expire(key, time);
return true;
}
/**
* 根据索引修改list中的某条数据
*
* @param key 键
* @param index 索引
* @param value 值
* @return boolean
* @author fzr
*/
public boolean lUpdateIndex(String key, Long index, Object value) {
key = redisPrefix + key;
redisTemplate.opsForList().set(key, index, value);
return true;
}
/**
* 移除N个值为value
*
* @param key 键
* @param count 移除多少个
* @param value 值
* @return 移除的个数
* @author fzr
*/
public Long lRemove(String key, Long count, Object value) {
key = redisPrefix + key;
return redisTemplate.opsForList().remove(key, count, value);
}
}
Msg对springAi中的messages进行处理
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Msg implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
MessageType messageType;
String text;
Map<String, Object> metadata;
List<AssistantMessage.ToolCall> toolCalls;
public Msg(Message message) {
this.messageType = message.getMessageType();
this.text = message.getText();
this.metadata = message.getMetadata();
if (message instanceof AssistantMessage am) {
this.toolCalls = am.getToolCalls();
}
}
public Message toMessage() {
if (messageType == null) {
throw new IllegalArgumentException("Message type cannot be null(自定义信息)");
}
return switch (messageType) {
case SYSTEM -> new SystemMessage(text);
case USER -> new UserMessage(text, List.of(), metadata);
case ASSISTANT -> new AssistantMessage(text, metadata, toolCalls, List.of());
default -> throw new IllegalArgumentException("Unsupported message type: " + messageType);
};
}
}
redisConfig配置
@Configuration
public class RedisConfig {
@Bean(name = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// key值采用StringRedisSerializer序列化
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
GenericJackson2JsonRedisSerializer jsonRedisSerializer =
new GenericJackson2JsonRedisSerializer();
redisTemplate.setValueSerializer(jsonRedisSerializer);
redisTemplate.setHashValueSerializer(jsonRedisSerializer);
return redisTemplate;
}
@Bean(name = "rateLimiterScript")
public RedisScript<Long> rateLimiterScript() {
DefaultRedisScript<Long> script = new DefaultRedisScript<>();
script.setScriptSource(
new ResourceScriptSource(new ClassPathResource("lua/RateLimiterScript.lua")));
script.setResultType(Long.class);
return script;
}
}
1-实现这个接口:
@Slf4j
@Component
public class InRedisChatMemory implements ChatMemory {
private static final String KEY_PREFIX = "chat:history:";
private static final long DEFAULT_EXPIRE_TIME = 2 * 60 * 60; // 2小时(会话过期时间)
@Autowired
private RedisUtil redisUtil;
@Override
public void add(String conversationId, List<Message> messages) {
String key = KEY_PREFIX + conversationId;
// (1-我提问的,2-ai回复的)逐个添加消息到 Redis List
for (Message message : messages) {
Msg msg = new Msg(message);
redisUtil.lSet(key, msg,DEFAULT_EXPIRE_TIME);
}
}
@Override
public List<Message> get(String conversationId, int lastN) {
String key = KEY_PREFIX + conversationId;
// 获取最后N条,如果lastN<=0则获取全部
long start = lastN > 0 ? -lastN : 0;
List<Object> objects = redisUtil.lGet(key, start, -1);
return objects.stream()
.filter(obj -> obj instanceof Msg)
.map(obj -> ((Msg) obj).toMessage())
.collect(Collectors.toList());
}
@Override
public void clear(String conversationId) {
String key = KEY_PREFIX + conversationId;
RedisUtil.del(key);
}
}
2、在大模型中使用
@Configuration
public class CommonConfiguration {
//配置(上下文)记忆存储器(本地内存的)
@Bean
public ChatMemory chatMemory() {
return new InMemoryChatMemory();
}
//配置(上下文)记忆存储器(redis存储)
@Bean
public ChatMemory redisChatMemory() {
return new InRedisChatMemory();
}
//这个地方的MessageChatMemoryAdvisor配置一些即可使用
@Bean
public ChatClient chatBaiLianClient(DashScopeChatModel model) {
return ChatClient.builder(model)
.defaultSystem("陈嘻嘻集团’的AI助手—嘻哈")
.defaultAdvisors(
new SimpleLoggerAdvisor(), //配置日志Advisor
new MessageChatMemoryAdvisor(redisChatMemory()) //配置redis上下文记忆Advisor
)
.build();
}
}
3-写接口调用
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class ChatController {
//存储会话id,后期根据(会话id,查询本次会话的聊天记录)
private final ChatHistoryRepository inRedisMemoryChatHistoryRecordRepository;
//使用百炼平台的模型
private final ChatClient chatBaiLianClient;
// 流式调用“阿里百炼平台”模型(有上下文)
@PostMapping(value = "/chat", produces = "text/html;charset=utf-8")
public Flux<String> streamChat(@RequestParam("prompt") String prompt, @RequestParam("chatId") String chatId) {
// 1.保存会话id
inRedisMemoryChatHistoryRecordRepository.save("chat", chatId);
//2.调用模型
return chatBaiLianClient.prompt().user(prompt)
.advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId))
.stream()
.content();
}
}
4、存储会话的id(选看)
public interface ChatHistoryRepository {
/**
* 保存会话记录
* @param type 业务类型,如:chat、service、pdf
* @param chatId 会话ID
*/
void save(String type, String chatId);
/**
* 获取会话ID列表
* @param type 业务类型,如:chat、service、pdf
* @return 会话ID列表
*/
List<String> getChatIds(String type);
}
@Slf4j
@Component
@RequiredArgsConstructor
public class InRedisMemoryChatHistoryRecordRepository implements ChatHistoryRepository {
private static final String KEY_PREFIX = "record:";
private static final long DEFAULT_EXPIRE_TIME = 2 * 60 * 60; // 2小时(会话过期时间)
@Autowired
private RedisUtil redisUtil;
@Override
public void save(String type, String chatId) {
String redisKey = KEY_PREFIX + type;
List<Object> existingRecords = redisUtil.lGet(redisKey, 0, -1);
if (existingRecords == null || existingRecords.isEmpty()) {
redisUtil.lSet(redisKey, chatId, DEFAULT_EXPIRE_TIME);
return;
}
boolean exists = existingRecords.stream()
.anyMatch(record -> record != null && record.toString().equals(chatId));
if (!exists) {
redisUtil.lSet(redisKey, chatId, DEFAULT_EXPIRE_TIME);
}
}
@Override
public List<String> getChatIds(String type) {
String redisKey = KEY_PREFIX + type;
List<Object> records = redisUtil.lGet(redisKey, 0, -1);
return Optional.ofNullable(records)
.orElse(Collections.emptyList())
.stream()
.filter(Objects::nonNull)
.map(Object::toString)
.distinct() // 去重(如果Redis列表允许重复)
.collect(Collectors.toList());
}
}
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/history")
public class ChatHistoryController {
//redis,存储数据['1003','1004','1005']
private final ChatHistoryRepository inRedisMemoryChatHistoryRecordRepository;
//redis,存储数据某个id的对话记录List结果[{user:你好,assistant:你好},{user:你好,assistant:你好}]
private final ChatMemory inRedisChatMemory;
//todo,根据 [类型] 获取ids:['1003','1004','1005']
//因为我这个项目是根据类型,你们可以替换成用户id,然后用户id的 [聊天会话ids]
@GetMapping("/{type}")
public List<String> getChatIds(@PathVariable("type") String type) {
return inRedisMemoryChatHistoryRecordRepository.getChatIds(type);
}
//todo,根据 [类型] [会话id] 获取:该会话id的聊天内容
//比如: 会话id=1003:[{user:你好,assistant:你好},{user:你好,assistant:你好}]
@GetMapping("/{type}/{chatId}")
public List<MessageVO> getChatHistory(@PathVariable("type") String type, @PathVariable("chatId") String chatId) {
List<Message> messages = inRedisChatMemory.get(chatId,0);
if (messages == null) {
return List.of();
}
return messages.stream().map(MessageVO::new).toList();
}
}
可以修改为:
用户id --> 聊天的ids['1003','1004','1005']
根据聊天的ids['1003','1004','1005'],获取对应的会话内容
//会话内容的存储,在上面有代码(1-根据会话id,存储会话内容、2-根据(会话id)获取(会话内容))
总结:
1-根据 [会话id] ,存储会话内容
2-根据 [会话id] ,获取会话内容
3-存储该用户的, [会话id]
4-获取该用户的, [会话ids] -List
//然后通过-{某个会话id},调:2-根据 [会话id] ,获取会话内容方法