添加ai功能开关

This commit is contained in:
liuhanqing
2025-11-04 08:57:29 +08:00
parent 31cf33c94f
commit 5e14889503
5 changed files with 35 additions and 9 deletions

View File

@@ -23,6 +23,7 @@ import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.mariadb.MariaDBVectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
@@ -40,6 +41,11 @@ public class ChatModelConfig {
@Bean
@ConditionalOnProperty(
name = "spring.ai.enable", // 配置项名称
havingValue = "true", // 期望的值
matchIfMissing = false // 配置不存在时是否匹配(false=不注册)
)
public ChatClient chatClient(OpenAiChatModel openAiChatModel, ChatMemory chatMemory, BasStockToolService toolService/*, ToolCallbackProvider toolCallbackProvider*/) {
return ChatClient
//使用的模型
@@ -63,11 +69,21 @@ public class ChatModelConfig {
* @return
*/
@Bean
@ConditionalOnProperty(
name = "spring.ai.enable", // 配置项名称
havingValue = "true", // 期望的值
matchIfMissing = false // 配置不存在时是否匹配(false=不注册)
)
public ChatMemory chatMemory() {
return new InSqlMemory();
}
@Bean
@ConditionalOnProperty(
name = "spring.ai.enable", // 配置项名称
havingValue = "true", // 期望的值
matchIfMissing = false // 配置不存在时是否匹配(false=不注册)
)
@ConfigurationProperties("spring.datasource.druid.slave")
public MariaDbDataSource dataSource() throws SQLException {
return DataSourceBuilder.create().username("root")
@@ -77,6 +93,11 @@ public class ChatModelConfig {
@Bean
@ConditionalOnProperty(
name = "spring.ai.enable", // 配置项名称
havingValue = "true", // 期望的值
matchIfMissing = false // 配置不存在时是否匹配(false=不注册)
)
public VectorStore vectorStore(MariaDbDataSource dataSource, EmbeddingModel embeddingModel) throws SQLException {
JdbcTemplate customJdbcTemplate = new JdbcTemplate(dataSource);
return MariaDBVectorStore.builder(customJdbcTemplate, embeddingModel)

View File

@@ -19,10 +19,10 @@ import java.util.List;
@RequestMapping("/api/embedding")
public class EmbeddingDemoController {
@Autowired
@Autowired(required = false)
private OpenAiEmbeddingModel embeddingModel;
@Autowired
@Autowired(required = false)
private VectorStore vectorStore;
/**

View File

@@ -28,17 +28,17 @@ import java.util.List;
@Service
public class ChatServiceImpl implements ChatService {
@Autowired
@Autowired(required = false)
private ChatClient chatClient;
@Autowired
@Autowired(required = false)
private OpenAiChatModel openAiChatModel;
@Autowired
@Autowired(required = false)
private ChatMemory chatMemory;
@Autowired
@Autowired(required = false)
private VectorStore vectorStore;
@Autowired
@Autowired(required = false)
private IChatMessageService chatMessageService;
@Autowired
@Autowired(required = false)
private IConversationsService conversationsService;
// 定义问答模板
@@ -110,6 +110,10 @@ public class ChatServiceImpl implements ChatService {
*/
@Override
public Flux<String> chatByUserContent(CustomMessage customMessage) {
if (chatClient == null){
return Flux.just("未开启聊天模型");
}
// 生成唯一会话ID
Long sessionId = IdUtil.getSnowflakeNextId();
//修改会话

View File

@@ -24,7 +24,7 @@ import java.util.List;
@Service
public class DataVectorService {
@Autowired
@Autowired(required = false)
private VectorStore vectorStore;
/**

View File

@@ -92,6 +92,7 @@ spring:
max-wait: -1ms
ai:
enable: false
openai:
api-key: sk-dzftrybcpjujsjgehjgtsjzjuhqxciosldcncjtlazxvjams # 密钥key
base-url: https://api.siliconflow.cn # 产商地址