mirror of
https://gitcode.com/ageerle/ruoyi-ai.git
synced 2026-04-16 13:23:42 +00:00
refactor(vector-store): 移除VectorStoreStrategy接口并简化策略模式实现
移除VectorStoreStrategy接口,直接使用VectorStoreService作为策略接口 简化VectorStoreStrategyFactory实现,移除冗余方法 更新相关实现类以适配新的接口结构
This commit is contained in:
@@ -2,16 +2,13 @@ package org.ruoyi.service.impl;
|
|||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.ruoyi.common.core.service.ConfigService;
|
|
||||||
import org.ruoyi.domain.bo.QueryVectorBo;
|
import org.ruoyi.domain.bo.QueryVectorBo;
|
||||||
import org.ruoyi.domain.bo.StoreEmbeddingBo;
|
import org.ruoyi.domain.bo.StoreEmbeddingBo;
|
||||||
import org.ruoyi.service.VectorStoreService;
|
import org.ruoyi.service.VectorStoreService;
|
||||||
import org.ruoyi.service.strategy.VectorStoreStrategy;
|
|
||||||
import org.ruoyi.service.strategy.VectorStoreStrategyFactory;
|
import org.ruoyi.service.strategy.VectorStoreStrategyFactory;
|
||||||
import org.springframework.context.annotation.Primary;
|
import org.springframework.context.annotation.Primary;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 向量库管理
|
* 向量库管理
|
||||||
@@ -30,14 +27,14 @@ public class VectorStoreServiceImpl implements VectorStoreService {
|
|||||||
/**
|
/**
|
||||||
* 获取当前配置的向量库策略
|
* 获取当前配置的向量库策略
|
||||||
*/
|
*/
|
||||||
private VectorStoreStrategy getCurrentStrategy() {
|
private VectorStoreService getCurrentStrategy() {
|
||||||
return strategyFactory.getStrategy();
|
return strategyFactory.getStrategy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createSchema(String vectorModelName, String kid) {
|
public void createSchema(String vectorModelName, String kid) {
|
||||||
log.info("创建向量库schema: vectorModelName={}, kid={}, modelName={}", vectorModelName, kid);
|
log.info("创建向量库schema: vectorModelName={}, kid={}, modelName={}", vectorModelName, kid);
|
||||||
VectorStoreStrategy strategy = getCurrentStrategy();
|
VectorStoreService strategy = getCurrentStrategy();
|
||||||
strategy.createSchema(vectorModelName, kid);
|
strategy.createSchema(vectorModelName, kid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,7 +42,7 @@ public class VectorStoreServiceImpl implements VectorStoreService {
|
|||||||
public void storeEmbeddings(StoreEmbeddingBo storeEmbeddingBo) {
|
public void storeEmbeddings(StoreEmbeddingBo storeEmbeddingBo) {
|
||||||
log.info("存储向量数据: kid={}, docId={}, 数据条数={}",
|
log.info("存储向量数据: kid={}, docId={}, 数据条数={}",
|
||||||
storeEmbeddingBo.getKid(), storeEmbeddingBo.getDocId(), storeEmbeddingBo.getChunkList().size());
|
storeEmbeddingBo.getKid(), storeEmbeddingBo.getDocId(), storeEmbeddingBo.getChunkList().size());
|
||||||
VectorStoreStrategy strategy = getCurrentStrategy();
|
VectorStoreService strategy = getCurrentStrategy();
|
||||||
strategy.storeEmbeddings(storeEmbeddingBo);
|
strategy.storeEmbeddings(storeEmbeddingBo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,28 +50,28 @@ public class VectorStoreServiceImpl implements VectorStoreService {
|
|||||||
public List<String> getQueryVector(QueryVectorBo queryVectorBo) {
|
public List<String> getQueryVector(QueryVectorBo queryVectorBo) {
|
||||||
log.info("查询向量数据: kid={}, query={}, maxResults={}",
|
log.info("查询向量数据: kid={}, query={}, maxResults={}",
|
||||||
queryVectorBo.getKid(), queryVectorBo.getQuery(), queryVectorBo.getMaxResults());
|
queryVectorBo.getKid(), queryVectorBo.getQuery(), queryVectorBo.getMaxResults());
|
||||||
VectorStoreStrategy strategy = getCurrentStrategy();
|
VectorStoreService strategy = getCurrentStrategy();
|
||||||
return strategy.getQueryVector(queryVectorBo);
|
return strategy.getQueryVector(queryVectorBo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeById(String id, String modelName) {
|
public void removeById(String id, String modelName) {
|
||||||
log.info("根据ID删除向量数据: id={}, modelName={}", id, modelName);
|
log.info("根据ID删除向量数据: id={}, modelName={}", id, modelName);
|
||||||
VectorStoreStrategy strategy = getCurrentStrategy();
|
VectorStoreService strategy = getCurrentStrategy();
|
||||||
strategy.removeById(id, modelName);
|
strategy.removeById(id, modelName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeByDocId(String docId, String kid) {
|
public void removeByDocId(String docId, String kid) {
|
||||||
log.info("根据docId删除向量数据: docId={}, kid={}", docId, kid);
|
log.info("根据docId删除向量数据: docId={}, kid={}", docId, kid);
|
||||||
VectorStoreStrategy strategy = getCurrentStrategy();
|
VectorStoreService strategy = getCurrentStrategy();
|
||||||
strategy.removeByDocId(docId, kid);
|
strategy.removeByDocId(docId, kid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeByFid(String fid, String kid) {
|
public void removeByFid(String fid, String kid) {
|
||||||
log.info("根据fid删除向量数据: fid={}, kid={}", fid, kid);
|
log.info("根据fid删除向量数据: fid={}, kid={}", fid, kid);
|
||||||
VectorStoreStrategy strategy = getCurrentStrategy();
|
VectorStoreService strategy = getCurrentStrategy();
|
||||||
strategy.removeByFid(fid, kid);
|
strategy.removeByFid(fid, kid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import lombok.SneakyThrows;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.ruoyi.common.core.config.VectorStoreProperties;
|
import org.ruoyi.common.core.config.VectorStoreProperties;
|
||||||
import org.ruoyi.common.core.utils.StringUtils;
|
import org.ruoyi.common.core.utils.StringUtils;
|
||||||
|
import org.ruoyi.service.VectorStoreService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 向量库策略抽象基类
|
* 向量库策略抽象基类
|
||||||
@@ -18,7 +19,7 @@ import org.ruoyi.common.core.utils.StringUtils;
|
|||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public abstract class AbstractVectorStoreStrategy implements VectorStoreStrategy {
|
public abstract class AbstractVectorStoreStrategy implements VectorStoreService {
|
||||||
|
|
||||||
protected final VectorStoreProperties vectorStoreProperties;
|
protected final VectorStoreProperties vectorStoreProperties;
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
package org.ruoyi.service.strategy;
|
|
||||||
|
|
||||||
import org.ruoyi.service.VectorStoreService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 向量库策略接口
|
|
||||||
* 继承VectorStoreService以避免重复定义相同的方法
|
|
||||||
*
|
|
||||||
* @author Yzm
|
|
||||||
*/
|
|
||||||
public interface VectorStoreStrategy extends VectorStoreService {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取向量库类型标识
|
|
||||||
* @return 向量库类型(如:weaviate, milvus)
|
|
||||||
*/
|
|
||||||
String getVectorStoreType();
|
|
||||||
}
|
|
||||||
@@ -6,6 +6,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.ruoyi.common.core.config.VectorStoreProperties;
|
import org.ruoyi.common.core.config.VectorStoreProperties;
|
||||||
import org.ruoyi.service.strategy.impl.MilvusVectorStoreStrategy;
|
import org.ruoyi.service.strategy.impl.MilvusVectorStoreStrategy;
|
||||||
import org.ruoyi.service.strategy.impl.WeaviateVectorStoreStrategy;
|
import org.ruoyi.service.strategy.impl.WeaviateVectorStoreStrategy;
|
||||||
|
import org.ruoyi.service.VectorStoreService;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -26,7 +27,7 @@ public class VectorStoreStrategyFactory {
|
|||||||
private final WeaviateVectorStoreStrategy weaviateStrategy;
|
private final WeaviateVectorStoreStrategy weaviateStrategy;
|
||||||
private final MilvusVectorStoreStrategy milvusStrategy;
|
private final MilvusVectorStoreStrategy milvusStrategy;
|
||||||
|
|
||||||
private Map<String, VectorStoreStrategy> strategies;
|
private Map<String, VectorStoreService> strategies;
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() {
|
public void init() {
|
||||||
@@ -39,36 +40,18 @@ public class VectorStoreStrategyFactory {
|
|||||||
/**
|
/**
|
||||||
* 获取当前配置的向量库策略
|
* 获取当前配置的向量库策略
|
||||||
*/
|
*/
|
||||||
public VectorStoreStrategy getStrategy() {
|
public VectorStoreService getStrategy() {
|
||||||
String vectorStoreType = vectorStoreProperties.getType();
|
String vectorStoreType = vectorStoreProperties.getType();
|
||||||
if (vectorStoreType == null || vectorStoreType.trim().isEmpty()) {
|
if (vectorStoreType == null || vectorStoreType.trim().isEmpty()) {
|
||||||
vectorStoreType = "weaviate"; // 默认使用weaviate
|
vectorStoreType = "weaviate"; // 默认使用weaviate
|
||||||
}
|
}
|
||||||
|
VectorStoreService strategy = strategies.get(vectorStoreType.toLowerCase());
|
||||||
VectorStoreStrategy strategy = strategies.get(vectorStoreType.toLowerCase());
|
|
||||||
if (strategy == null) {
|
if (strategy == null) {
|
||||||
log.warn("未找到向量库策略: {}, 使用默认策略: weaviate", vectorStoreType);
|
log.warn("未找到向量库策略: {}, 使用默认策略: weaviate", vectorStoreType);
|
||||||
strategy = strategies.get("weaviate");
|
strategy = strategies.get("weaviate");
|
||||||
}
|
}
|
||||||
|
|
||||||
log.debug("使用向量库策略: {}", vectorStoreType);
|
log.debug("使用向量库策略: {}", vectorStoreType);
|
||||||
return strategy;
|
return strategy;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据类型获取向量库策略
|
|
||||||
*/
|
|
||||||
public VectorStoreStrategy getStrategy(String type) {
|
|
||||||
if (type == null || type.trim().isEmpty()) {
|
|
||||||
return getStrategy();
|
|
||||||
}
|
|
||||||
|
|
||||||
VectorStoreStrategy strategy = strategies.get(type.toLowerCase());
|
|
||||||
if (strategy == null) {
|
|
||||||
log.warn("未找到向量库策略: {}, 使用默认策略", type);
|
|
||||||
return getStrategy();
|
|
||||||
}
|
|
||||||
|
|
||||||
return strategy;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -46,7 +46,6 @@ public class MilvusVectorStoreStrategy extends AbstractVectorStoreStrategy {
|
|||||||
public void createSchema(String vectorModelName, String kid) {
|
public void createSchema(String vectorModelName, String kid) {
|
||||||
String url = vectorStoreProperties.getMilvus().getUrl();
|
String url = vectorStoreProperties.getMilvus().getUrl();
|
||||||
String collectionName = vectorStoreProperties.getMilvus().getCollectionname() + kid;
|
String collectionName = vectorStoreProperties.getMilvus().getCollectionname() + kid;
|
||||||
// 使用 LangChain4j 的 MilvusEmbeddingStore 来确保集合存在(按需创建)
|
|
||||||
MilvusEmbeddingStore store = MilvusEmbeddingStore.builder()
|
MilvusEmbeddingStore store = MilvusEmbeddingStore.builder()
|
||||||
.uri(url)
|
.uri(url)
|
||||||
.collectionName(collectionName)
|
.collectionName(collectionName)
|
||||||
|
|||||||
Reference in New Issue
Block a user