代码优化

This commit is contained in:
thinkgem
2025-06-23 13:59:02 +08:00
parent 32f4d7e0ae
commit 6faa39ebea

View File

@@ -37,7 +37,6 @@ 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;
@@ -52,12 +51,11 @@ 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());
@Autowired @Autowired(required = false)
private VectorStore vectorStore; private VectorStore vectorStore;
/** /**
@@ -66,6 +64,7 @@ public class ArticleVectorStoreImpl implements ArticleVectorStore {
*/ */
@Override @Override
public void save(Article article) { public void save(Article article) {
if (vectorStore == null) return;
Map<String, Object> metadata = MapUtils.newHashMap(); Map<String, Object> metadata = MapUtils.newHashMap();
metadata.put("id", article.getId()); metadata.put("id", article.getId());
metadata.put("siteCode", article.getCategory().getSite().getSiteCode()); metadata.put("siteCode", article.getCategory().getSite().getSiteCode());
@@ -184,6 +183,7 @@ public class ArticleVectorStoreImpl implements ArticleVectorStore {
*/ */
@Override @Override
public void delete(Article article) { public void delete(Article article) {
if (vectorStore == null) return;
if (StringUtils.isNotBlank(article.getId())) { if (StringUtils.isNotBlank(article.getId())) {
vectorStore.delete(new FilterExpressionBuilder().eq("id", article.getId()).build()); vectorStore.delete(new FilterExpressionBuilder().eq("id", article.getId()).build());
} }
@@ -194,6 +194,7 @@ public class ArticleVectorStoreImpl implements ArticleVectorStore {
* @author ThinkGem * @author ThinkGem
*/ */
public String rebuild(Article article) { public String rebuild(Article article) {
if (vectorStore == null) return null;
logger.debug("开始重建向量库。 siteCode: {}, categoryCode: {}", logger.debug("开始重建向量库。 siteCode: {}, categoryCode: {}",
article.getCategory().getSite().getSiteCode(), article.getCategory().getSite().getSiteCode(),
article.getCategory().getCategoryCode()); article.getCategory().getCategoryCode());