向量库可选,在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.VectorStore;
import org.springframework.ai.vectorstore.filter.FilterExpressionBuilder; import org.springframework.ai.vectorstore.filter.FilterExpressionBuilder;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException; import java.io.IOException;
@@ -51,6 +52,7 @@ import java.util.Set;
* @author ThinkGem * @author ThinkGem
*/ */
@Service @Service
@ConditionalOnBean(VectorStore.class)
public class ArticleVectorStoreImpl implements ArticleVectorStore { public class ArticleVectorStoreImpl implements ArticleVectorStore {
protected Logger logger = LoggerFactory.getLogger(getClass()); protected Logger logger = LoggerFactory.getLogger(getClass());

View File

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

View File

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

View File

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