将向量库从es切换到mariadb
This commit is contained in:
parent
480f5a1456
commit
ffc78462c8
|
|
@ -41,11 +41,11 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.ai</groupId>
|
<groupId>org.springframework.ai</groupId>
|
||||||
<artifactId>spring-ai-elasticsearch-store-spring-boot-starter</artifactId>
|
<artifactId>spring-ai-starter-model-ollama</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.ai</groupId>
|
<groupId>org.springframework.ai</groupId>
|
||||||
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
|
<artifactId>spring-ai-starter-vector-store-mariadb</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.ai</groupId>
|
<groupId>org.springframework.ai</groupId>
|
||||||
|
|
@ -71,6 +71,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mariadb.jdbc</groupId>
|
<groupId>org.mariadb.jdbc</groupId>
|
||||||
<artifactId>mariadb-java-client</artifactId>
|
<artifactId>mariadb-java-client</artifactId>
|
||||||
|
<version>3.5.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.electromagnetic.data</groupId>
|
<groupId>com.electromagnetic.data</groupId>
|
||||||
|
|
|
||||||
|
|
@ -1,24 +1,13 @@
|
||||||
package com.electromagnetic.industry.software.manage.config;
|
package com.electromagnetic.industry.software.manage.config;
|
||||||
|
|
||||||
import jakarta.annotation.Resource;
|
|
||||||
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
|
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
|
||||||
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
|
|
||||||
import org.springframework.ai.chat.memory.InMemoryChatMemory;
|
import org.springframework.ai.chat.memory.InMemoryChatMemory;
|
||||||
import org.springframework.ai.vectorstore.VectorStore;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class AiAdvisor {
|
public class AiAdvisor {
|
||||||
|
|
||||||
@Resource
|
|
||||||
private VectorStore vectorStore;
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public QuestionAnswerAdvisor questionAnswerAdvisor() {
|
|
||||||
return new QuestionAnswerAdvisor(vectorStore);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public MessageChatMemoryAdvisor messageChatMemoryAdvisor() {
|
public MessageChatMemoryAdvisor messageChatMemoryAdvisor() {
|
||||||
return new MessageChatMemoryAdvisor(new InMemoryChatMemory());
|
return new MessageChatMemoryAdvisor(new InMemoryChatMemory());
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,6 @@ import jakarta.annotation.Resource;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.ai.chat.client.ChatClient;
|
import org.springframework.ai.chat.client.ChatClient;
|
||||||
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
|
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
|
||||||
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
|
|
||||||
import org.springframework.ai.chat.messages.UserMessage;
|
import org.springframework.ai.chat.messages.UserMessage;
|
||||||
import org.springframework.ai.chat.model.ChatResponse;
|
import org.springframework.ai.chat.model.ChatResponse;
|
||||||
import org.springframework.ai.chat.prompt.Prompt;
|
import org.springframework.ai.chat.prompt.Prompt;
|
||||||
|
|
@ -56,29 +55,24 @@ public class ChatService {
|
||||||
@Resource
|
@Resource
|
||||||
private MessageChatMemoryAdvisor messageChatMemoryAdvisor;
|
private MessageChatMemoryAdvisor messageChatMemoryAdvisor;
|
||||||
@Resource
|
@Resource
|
||||||
private QuestionAnswerAdvisor questionAnswerAdvisor;
|
|
||||||
@Resource
|
|
||||||
private AiFileUploadRecordMapper aiFileUploadRecordMapper;
|
private AiFileUploadRecordMapper aiFileUploadRecordMapper;
|
||||||
@Resource
|
@Resource
|
||||||
private AiQuestionRecordMapper aiQuestionRecordMapper;
|
private AiQuestionRecordMapper aiQuestionRecordMapper;
|
||||||
@Resource
|
@Resource
|
||||||
private ElePropertyConfig elePropertyConfig;
|
private ElePropertyConfig elePropertyConfig;
|
||||||
|
|
||||||
public ElectromagneticResult<?> deleteDocument(List<String> ids) {
|
// public ElectromagneticResult<?> deleteDocument(List<String> ids) {
|
||||||
List<String> vectorIds = aiFileUploadRecordMapper.selectList(Wrappers.lambdaQuery(AiFileUploadRecord.class)
|
// List<String> vectorIds = aiFileUploadRecordMapper.selectList(Wrappers.lambdaQuery(AiFileUploadRecord.class)
|
||||||
.in(AiFileUploadRecord::getId, ids))
|
// .in(AiFileUploadRecord::getId, ids))
|
||||||
.stream()
|
// .stream()
|
||||||
.map(AiFileUploadRecord::getVectorId)
|
// .map(AiFileUploadRecord::getVectorId)
|
||||||
.toList();
|
// .toList();
|
||||||
Optional<Boolean> delete = vectorStore.delete(vectorIds);
|
// vectorStore.delete(vectorIds);
|
||||||
if (null != delete && delete.isPresent() && delete.get()) {
|
// aiFileUploadRecordMapper.update(Wrappers.lambdaUpdate(AiFileUploadRecord.class)
|
||||||
aiFileUploadRecordMapper.update(Wrappers.lambdaUpdate(AiFileUploadRecord.class)
|
// .in(AiFileUploadRecord::getId, ids)
|
||||||
.in(AiFileUploadRecord::getId, ids)
|
// .set(AiFileUploadRecord::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code));
|
||||||
.set(AiFileUploadRecord::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code));
|
// return ElectromagneticResultUtil.success(true);
|
||||||
return ElectromagneticResultUtil.success(true);
|
// }
|
||||||
}
|
|
||||||
return ElectromagneticResultUtil.fail("-1", "删除失败。");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public ElectromagneticResult<?> addFromUpload(MultipartFile file) throws Exception {
|
public ElectromagneticResult<?> addFromUpload(MultipartFile file) throws Exception {
|
||||||
|
|
@ -172,15 +166,17 @@ public class ChatService {
|
||||||
UserThreadLocal.set(userLoginInfo);
|
UserThreadLocal.set(userLoginInfo);
|
||||||
aiQuestionRecordMapper.insert(record);
|
aiQuestionRecordMapper.insert(record);
|
||||||
UserThreadLocal.remove();
|
UserThreadLocal.remove();
|
||||||
|
List<Document> documents = vectorStore.similaritySearch(queryDTO.getMsg());
|
||||||
|
String augmentedPrompt = "基于以下信息回答:" + documents + "\n\n" + queryDTO.getMsg();
|
||||||
return ChatClient.builder(model)
|
return ChatClient.builder(model)
|
||||||
.defaultSystem("必须用中文回答")
|
.defaultSystem("必须用中文回答")
|
||||||
.defaultAdvisors(messageChatMemoryAdvisor, questionAnswerAdvisor)
|
.defaultAdvisors(messageChatMemoryAdvisor)
|
||||||
.defaultOptions(OllamaOptions
|
.defaultOptions(OllamaOptions
|
||||||
.builder()
|
.builder()
|
||||||
.temperature(0.3)
|
.temperature(0.3)
|
||||||
.build())
|
.build())
|
||||||
.build()
|
.build()
|
||||||
.prompt(queryDTO.getMsg())
|
.prompt(augmentedPrompt)
|
||||||
.stream()
|
.stream()
|
||||||
.content();
|
.content();
|
||||||
}
|
}
|
||||||
|
|
@ -192,15 +188,17 @@ public class ChatService {
|
||||||
UserThreadLocal.set(userLoginInfo);
|
UserThreadLocal.set(userLoginInfo);
|
||||||
aiQuestionRecordMapper.insert(record);
|
aiQuestionRecordMapper.insert(record);
|
||||||
UserThreadLocal.remove();
|
UserThreadLocal.remove();
|
||||||
|
List<Document> documents = vectorStore.similaritySearch(queryDTO.getMsg());
|
||||||
|
String augmentedPrompt = "基于以下信息回答:" + documents + "\n\n" + queryDTO.getMsg();
|
||||||
return ChatClient.builder(model)
|
return ChatClient.builder(model)
|
||||||
.defaultSystem("必须用中文回答")
|
.defaultSystem("必须用中文回答")
|
||||||
.defaultAdvisors(messageChatMemoryAdvisor, questionAnswerAdvisor)
|
.defaultAdvisors(messageChatMemoryAdvisor)
|
||||||
.defaultOptions(OllamaOptions
|
.defaultOptions(OllamaOptions
|
||||||
.builder()
|
.builder()
|
||||||
.temperature(0.3)
|
.temperature(0.3)
|
||||||
.build())
|
.build())
|
||||||
.build()
|
.build()
|
||||||
.prompt(new Prompt(new UserMessage(queryDTO.getMsg())))
|
.prompt(new Prompt(new UserMessage(augmentedPrompt)))
|
||||||
.stream()
|
.stream()
|
||||||
.chatResponse();
|
.chatResponse();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
pom.xml
2
pom.xml
|
|
@ -20,7 +20,7 @@
|
||||||
<maven.compiler.target>17</maven.compiler.target>
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<spring.boot.version>3.3.8</spring.boot.version>
|
<spring.boot.version>3.3.8</spring.boot.version>
|
||||||
<spring.ai.version>1.0.0-M5</spring.ai.version>
|
<spring.ai.version>1.0.0-M7</spring.ai.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue