| | |
| | | import cn.hutool.extra.spring.SpringUtil; |
| | | import cn.iocoder.yudao.module.ai.enums.model.AiPlatformEnum; |
| | | import cn.iocoder.yudao.module.ai.framework.ai.config.AiAutoConfiguration; |
| | | import cn.iocoder.yudao.module.ai.framework.ai.config.YudaoAiProperties; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.ai.chat.model.ChatModel; |
| | | import org.springframework.ai.embedding.BatchingStrategy; |
| | | import org.springframework.ai.embedding.EmbeddingModel; |
| | | import org.springframework.ai.embedding.TokenCountBatchingStrategy; |
| | | import org.springframework.ai.openai.OpenAiChatModel; |
| | | import org.springframework.ai.vectorstore.VectorStore; |
| | | import org.springframework.ai.vectorstore.milvus.MilvusVectorStore; |
| | | import org.springframework.ai.vectorstore.milvus.autoconfigure.MilvusServiceClientProperties; |
| | | import org.springframework.ai.vectorstore.milvus.autoconfigure.MilvusVectorStoreProperties; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * AI Model 模型工厂实现类 |
| | | * |
| | | * 使用 OpenAI 兼容接口对接通义千问 + Milvus 向量存储 |
| | | * 使用 OpenAI 兼容接口对接通义千问 + 可配置向量存储(Milvus) |
| | | */ |
| | | @Slf4j |
| | | public class AiModelFactoryImpl implements AiModelFactory { |
| | |
| | | public VectorStore getOrCreateVectorStore(Class<? extends VectorStore> type, |
| | | EmbeddingModel embeddingModel, |
| | | Map<String, Class<?>> metadataFields) { |
| | | // metadataFields 参与缓存 key,确保不同知识库使用不同配置时不会复用 |
| | | String cacheKey = buildCacheKey(VectorStore.class, embeddingModel, type, metadataFields.hashCode()); |
| | | return Singleton.get(cacheKey, (Func0<VectorStore>) () -> { |
| | | if (type == MilvusVectorStore.class) { |
| | |
| | | } |
| | | |
| | | private MilvusVectorStore buildMilvusVectorStore(EmbeddingModel embeddingModel) { |
| | | MilvusVectorStoreProperties serverProperties = SpringUtil.getBean(MilvusVectorStoreProperties.class); |
| | | MilvusServiceClientProperties clientProperties = SpringUtil.getBean(MilvusServiceClientProperties.class); |
| | | YudaoAiProperties aiProperties = SpringUtil.getBean(YudaoAiProperties.class); |
| | | YudaoAiProperties.VectorStore.Milvus milvusConfig = aiProperties.getVectorStore().getMilvus(); |
| | | |
| | | var connectParam = io.milvus.param.ConnectParam.newBuilder() |
| | | .withHost(clientProperties.getHost()) |
| | | .withPort(clientProperties.getPort()) |
| | | .withDatabaseName(serverProperties.getDatabaseName()) |
| | | .withHost(milvusConfig.getHost()) |
| | | .withPort(milvusConfig.getPort()) |
| | | .withDatabaseName(milvusConfig.getDatabaseName()) |
| | | .build(); |
| | | var milvusClient = new io.milvus.client.MilvusServiceClient(connectParam); |
| | | |
| | | MilvusVectorStore vectorStore = MilvusVectorStore.builder(milvusClient, embeddingModel) |
| | | .databaseName(serverProperties.getDatabaseName()) |
| | | .collectionName(serverProperties.getCollectionName()) |
| | | .initializeSchema(serverProperties.isInitializeSchema()) |
| | | .databaseName(milvusConfig.getDatabaseName()) |
| | | .collectionName(milvusConfig.getCollectionName()) |
| | | .initializeSchema(milvusConfig.isInitializeSchema()) |
| | | .batchingStrategy(new TokenCountBatchingStrategy()) |
| | | .build(); |
| | | try { |