欢迎光临
我们一直在努力

spring boot4+springAI 2加redis多轮对话存储

1、采用redis的方式进行存储,安装maven依赖

<!— 模型:OpenAI 兼容 —>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-chat-memory-repository-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2、yml配置

spring:
ai:
openai:
base-url: https://api.deepseek.com
api-key: xxxxxx
chat:
temperature: 0.3
model: deepseek-v4-flash

data:
redis:
host: localhost
port: 6379
password: xxxxxx
jedis:
pool:
max-active: 18
database: 0

3、AI配置类

package com.example.web_service.tj.ai.config;

import com.example.web_service.tj.ai.service.IProjectTagQueryService;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.memory.repository.redis.RedisChatMemoryRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.RedisClient;

import java.time.Duration;

@Configuration
public class AiConfig {

@Value("${spring.data.redis.host}")
private String redisHost;

@Value("${spring.data.redis.port}")
private int redisPort;

@Value("${spring.data.redis.password}")
private String redisPassword;

@Bean
public ChatMemory chatMemory() {
RedisClient redisClient=RedisClient.create(redisHost, redisPort,null,redisPassword);
RedisChatMemoryRepository repo=RedisChatMemoryRepository.builder()
.jedisClient(redisClient)
.keyPrefix("chat:memory:")
.indexName("chat-memory-idx")
.timeToLive(Duration.ofHours(2))
.initializeSchema(true)
.build();
return MessageWindowChatMemory.builder()
.chatMemoryRepository(repo)
.maxMessages(20)
.build();
}

@Bean
public ChatClient chatClient(ChatClient.Builder builder,ChatMemory chatMemory, IProjectTagQueryService projectTagQueryService) {
return builder
.defaultSystem("""
我是一个物联网数据查询助手,支持以下能力:

## 能力1:查询1个或者多个点位/变量的当前值
触发词:点位、变量、当前值
参数:
– tagCodes: 点位列表(List<String>),如 ["forward", "reverse", "start"]


## 能力2:查询点位/变量的历史数据,指定时间范围,从…..到……
触发词:点位、变量、历史数据、时间范围
参数:
– tag: 点位/变量,如"forward"
– startDateTime: 开始时间,格式 yyyy-MM-dd HH:mm:ss,必须包含年月日时分秒,如 2026-09-01 08:00:00,如果分钟或者秒不存在,则都写成0
– endDateTime: 结束时间,格式 yyyy-MM-dd HH:mm:ss,必须包含年月日时分秒,如 2026-09-01 08:00:00,如果分钟或者秒不存在,则都写成0

""")
.defaultTools(projectTagQueryService)//带入业务接口/类(对象)
.defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory).build())
.build();
}
}

其中IProjectTagQueryService projectTagQueryService是业务对象
4、控制器测试

@RestController
@RequestMapping("/businessAi")
public class BusinessAiController {
@Resource
@Lazy
private ChatClient chatClient;

@PostMapping("/chat")
public ResultMap chat(@RequestBody Map<String,String> params) {
String chatId="%s-%s".formatted(AuthUserUtils.getUser().getTenantId(),AuthUserUtils.getUser().getUserId());
return ResultMap.SUCCESS.setNewData(chatClient.prompt()
.advisors(v->v.param(ChatMemory.CONVERSATION_ID, chatId))
.user(params.get("message"))
.call()
.content());

}
}

赞(0)
未经允许不得转载:171主机测评 » spring boot4+springAI 2加redis多轮对话存储
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址