添加ai功能开关
This commit is contained in:
@@ -23,6 +23,7 @@ import org.springframework.ai.vectorstore.VectorStore;
|
|||||||
import org.springframework.ai.vectorstore.mariadb.MariaDBVectorStore;
|
import org.springframework.ai.vectorstore.mariadb.MariaDBVectorStore;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
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.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
@@ -40,6 +41,11 @@ public class ChatModelConfig {
|
|||||||
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ConditionalOnProperty(
|
||||||
|
name = "spring.ai.enable", // 配置项名称
|
||||||
|
havingValue = "true", // 期望的值
|
||||||
|
matchIfMissing = false // 配置不存在时是否匹配(false=不注册)
|
||||||
|
)
|
||||||
public ChatClient chatClient(OpenAiChatModel openAiChatModel, ChatMemory chatMemory, BasStockToolService toolService/*, ToolCallbackProvider toolCallbackProvider*/) {
|
public ChatClient chatClient(OpenAiChatModel openAiChatModel, ChatMemory chatMemory, BasStockToolService toolService/*, ToolCallbackProvider toolCallbackProvider*/) {
|
||||||
return ChatClient
|
return ChatClient
|
||||||
//使用的模型
|
//使用的模型
|
||||||
@@ -63,11 +69,21 @@ public class ChatModelConfig {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
|
@ConditionalOnProperty(
|
||||||
|
name = "spring.ai.enable", // 配置项名称
|
||||||
|
havingValue = "true", // 期望的值
|
||||||
|
matchIfMissing = false // 配置不存在时是否匹配(false=不注册)
|
||||||
|
)
|
||||||
public ChatMemory chatMemory() {
|
public ChatMemory chatMemory() {
|
||||||
return new InSqlMemory();
|
return new InSqlMemory();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ConditionalOnProperty(
|
||||||
|
name = "spring.ai.enable", // 配置项名称
|
||||||
|
havingValue = "true", // 期望的值
|
||||||
|
matchIfMissing = false // 配置不存在时是否匹配(false=不注册)
|
||||||
|
)
|
||||||
@ConfigurationProperties("spring.datasource.druid.slave")
|
@ConfigurationProperties("spring.datasource.druid.slave")
|
||||||
public MariaDbDataSource dataSource() throws SQLException {
|
public MariaDbDataSource dataSource() throws SQLException {
|
||||||
return DataSourceBuilder.create().username("root")
|
return DataSourceBuilder.create().username("root")
|
||||||
@@ -77,6 +93,11 @@ public class ChatModelConfig {
|
|||||||
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ConditionalOnProperty(
|
||||||
|
name = "spring.ai.enable", // 配置项名称
|
||||||
|
havingValue = "true", // 期望的值
|
||||||
|
matchIfMissing = false // 配置不存在时是否匹配(false=不注册)
|
||||||
|
)
|
||||||
public VectorStore vectorStore(MariaDbDataSource dataSource, EmbeddingModel embeddingModel) throws SQLException {
|
public VectorStore vectorStore(MariaDbDataSource dataSource, EmbeddingModel embeddingModel) throws SQLException {
|
||||||
JdbcTemplate customJdbcTemplate = new JdbcTemplate(dataSource);
|
JdbcTemplate customJdbcTemplate = new JdbcTemplate(dataSource);
|
||||||
return MariaDBVectorStore.builder(customJdbcTemplate, embeddingModel)
|
return MariaDBVectorStore.builder(customJdbcTemplate, embeddingModel)
|
||||||
|
|||||||
@@ -19,10 +19,10 @@ import java.util.List;
|
|||||||
@RequestMapping("/api/embedding")
|
@RequestMapping("/api/embedding")
|
||||||
public class EmbeddingDemoController {
|
public class EmbeddingDemoController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired(required = false)
|
||||||
private OpenAiEmbeddingModel embeddingModel;
|
private OpenAiEmbeddingModel embeddingModel;
|
||||||
|
|
||||||
@Autowired
|
@Autowired(required = false)
|
||||||
private VectorStore vectorStore;
|
private VectorStore vectorStore;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -28,17 +28,17 @@ import java.util.List;
|
|||||||
@Service
|
@Service
|
||||||
public class ChatServiceImpl implements ChatService {
|
public class ChatServiceImpl implements ChatService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired(required = false)
|
||||||
private ChatClient chatClient;
|
private ChatClient chatClient;
|
||||||
@Autowired
|
@Autowired(required = false)
|
||||||
private OpenAiChatModel openAiChatModel;
|
private OpenAiChatModel openAiChatModel;
|
||||||
@Autowired
|
@Autowired(required = false)
|
||||||
private ChatMemory chatMemory;
|
private ChatMemory chatMemory;
|
||||||
@Autowired
|
@Autowired(required = false)
|
||||||
private VectorStore vectorStore;
|
private VectorStore vectorStore;
|
||||||
@Autowired
|
@Autowired(required = false)
|
||||||
private IChatMessageService chatMessageService;
|
private IChatMessageService chatMessageService;
|
||||||
@Autowired
|
@Autowired(required = false)
|
||||||
private IConversationsService conversationsService;
|
private IConversationsService conversationsService;
|
||||||
|
|
||||||
// 定义问答模板
|
// 定义问答模板
|
||||||
@@ -110,6 +110,10 @@ public class ChatServiceImpl implements ChatService {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Flux<String> chatByUserContent(CustomMessage customMessage) {
|
public Flux<String> chatByUserContent(CustomMessage customMessage) {
|
||||||
|
if (chatClient == null){
|
||||||
|
return Flux.just("未开启聊天模型");
|
||||||
|
}
|
||||||
|
|
||||||
// 生成唯一会话ID
|
// 生成唯一会话ID
|
||||||
Long sessionId = IdUtil.getSnowflakeNextId();
|
Long sessionId = IdUtil.getSnowflakeNextId();
|
||||||
//修改会话
|
//修改会话
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ import java.util.List;
|
|||||||
@Service
|
@Service
|
||||||
public class DataVectorService {
|
public class DataVectorService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired(required = false)
|
||||||
private VectorStore vectorStore;
|
private VectorStore vectorStore;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -92,6 +92,7 @@ spring:
|
|||||||
max-wait: -1ms
|
max-wait: -1ms
|
||||||
|
|
||||||
ai:
|
ai:
|
||||||
|
enable: false
|
||||||
openai:
|
openai:
|
||||||
api-key: sk-dzftrybcpjujsjgehjgtsjzjuhqxciosldcncjtlazxvjams # 密钥key
|
api-key: sk-dzftrybcpjujsjgehjgtsjzjuhqxciosldcncjtlazxvjams # 密钥key
|
||||||
base-url: https://api.siliconflow.cn # 产商地址
|
base-url: https://api.siliconflow.cn # 产商地址
|
||||||
|
|||||||
Reference in New Issue
Block a user