向量库可选,在pom里去掉向量库实现即可

This commit is contained in:
thinkgem
2025-05-23 19:01:15 +08:00
parent 4e5d776104
commit 6f05485e8f
4 changed files with 28 additions and 27 deletions

View File

@@ -37,6 +37,7 @@ import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.filter.FilterExpressionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Service;
import java.io.IOException;
@@ -51,6 +52,7 @@ import java.util.Set;
* @author ThinkGem
*/
@Service
@ConditionalOnBean(VectorStore.class)
public class ArticleVectorStoreImpl implements ArticleVectorStore {
protected Logger logger = LoggerFactory.getLogger(getClass());

View File

@@ -58,7 +58,7 @@ public class CmsAiChatService extends BaseService {
private ChatClient chatClient;
@Autowired
private ChatMemory chatMemory;
@Autowired
@Autowired(required = false)
private VectorStore vectorStore;
@Autowired
private CmsAiProperties properties;
@@ -126,20 +126,18 @@ public class CmsAiChatService extends BaseService {
* @author ThinkGem
*/
public Flux<ChatResponse> chatStream(String conversationId, String message, HttpServletRequest request) {
return chatClient.prompt()
.messages(
new UserMessage(StringUtils.replaceEach(message, USER_MESSAGE_SEARCH, USER_MESSAGE_REPLACE))
)
.advisors(
MessageChatMemoryAdvisor.builder(chatMemory)
ChatClient.ChatClientRequestSpec spec = chatClient.prompt()
.messages(new UserMessage(StringUtils.replaceEach(message, USER_MESSAGE_SEARCH, USER_MESSAGE_REPLACE)))
.advisors(MessageChatMemoryAdvisor.builder(chatMemory)
.conversationId(conversationId)
.build(),
QuestionAnswerAdvisor.builder(vectorStore)
.searchRequest(SearchRequest.builder().similarityThreshold(0.6F).topK(6).build())
.promptTemplate(new PromptTemplate(properties.getDefaultPromptTemplate()))
.build()
)
.stream()
.build());
if (vectorStore != null) {
spec.advisors(QuestionAnswerAdvisor.builder(vectorStore)
.searchRequest(SearchRequest.builder().similarityThreshold(0.6F).topK(6).build())
.promptTemplate(new PromptTemplate(properties.getDefaultPromptTemplate()))
.build());
}
return spec.stream()
.chatResponse()
.doOnNext(response -> {
if (response.getResult() != null && StringUtils.isNotBlank(response.getResult().getOutput().getText())) {
@@ -204,7 +202,8 @@ public class CmsAiChatService extends BaseService {
new UserMessage(StringUtils.replaceEach(message, USER_MESSAGE_SEARCH, USER_MESSAGE_REPLACE))
)
.call()
.responseEntity(new AbstractMessageOutputConverter<Map<String, Object>>(
.responseEntity(
new AbstractMessageOutputConverter<Map<String, Object>>(
new MappingJackson2MessageConverter(JsonMapper.getInstance())
) {
final MapOutputConverter mapOutputConverter = new MapOutputConverter();
@@ -228,18 +227,18 @@ public class CmsAiChatService extends BaseService {
public List<Area> chatArea(String message) {
List<Area> list = AreaUtils.getAreaAllList();
if (list.size() > 10) list = list.subList(0, 10);
return chatClient.prompt()
.messages(
ChatClient.ChatClientRequestSpec spec = chatClient.prompt()
.messages(
new SystemMessage(JsonMapper.toJson(list)),
new UserMessage(StringUtils.replaceEach(message, USER_MESSAGE_SEARCH, USER_MESSAGE_REPLACE))
)
.advisors(
QuestionAnswerAdvisor.builder(vectorStore)
.searchRequest(SearchRequest.builder().similarityThreshold(0.6F).topK(6).build())
.promptTemplate(new PromptTemplate(properties.getDefaultPromptTemplate()))
.build()
)
.call()
);
if (vectorStore != null) {
spec.advisors(QuestionAnswerAdvisor.builder(vectorStore)
.searchRequest(SearchRequest.builder().similarityThreshold(0.6F).topK(6).build())
.promptTemplate(new PromptTemplate(properties.getDefaultPromptTemplate()))
.build());
}
return spec.call()
.responseEntity(new BeanOutputConverter<>(new ParameterizedTypeReference<List<Area>>() {},
JsonMapper.getInstance()))
.getEntity();

View File

@@ -133,7 +133,7 @@ public class CategoryService extends TreeService<CategoryDao, Category> {
*/
public String rebuildVectorStore(Category category) {
if (articleVectorStore == null) {
return text("您好,系统未安装全文检索模块");
return text("您好,系统未配置向量数据库");
}
return articleVectorStore.rebuild(new Article(category));
}

View File

@@ -129,7 +129,7 @@ public class SiteService extends CrudService<SiteDao, Site> {
*/
public String rebuildVectorStore(Site site) {
if (articleVectorStore == null) {
return text("您好,系统未安装内容管理AI模块");
return text("您好,系统未配置向量数据库");
}
return articleVectorStore.rebuild(new Article(new Category(site)));
}