Commit 3a3607e1cbd63999422f6e732e395446ec8dbf4e
1 parent
d7e801c4
【功能新增】AI:新增 ZhiPuAiEmbeddingModel 向量模型
Showing
3 changed files
with
19 additions
and
10 deletions
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/knowledge/AiKnowledgeController.java
| @@ -36,7 +36,6 @@ public class AiKnowledgeController { | @@ -36,7 +36,6 @@ public class AiKnowledgeController { | ||
| 36 | @Operation(summary = "获取知识库分页") | 36 | @Operation(summary = "获取知识库分页") |
| 37 | @PreAuthorize("@ss.hasPermission('ai:knowledge:query')") | 37 | @PreAuthorize("@ss.hasPermission('ai:knowledge:query')") |
| 38 | public CommonResult<PageResult<AiKnowledgeRespVO>> getKnowledgePage(@Valid AiKnowledgePageReqVO pageReqVO) { | 38 | public CommonResult<PageResult<AiKnowledgeRespVO>> getKnowledgePage(@Valid AiKnowledgePageReqVO pageReqVO) { |
| 39 | - | ||
| 40 | PageResult<AiKnowledgeDO> pageResult = knowledgeService.getKnowledgePage(pageReqVO); | 39 | PageResult<AiKnowledgeDO> pageResult = knowledgeService.getKnowledgePage(pageReqVO); |
| 41 | return success(BeanUtils.toBean(pageResult, AiKnowledgeRespVO.class)); | 40 | return success(BeanUtils.toBean(pageResult, AiKnowledgeRespVO.class)); |
| 42 | } | 41 | } |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/model/AiModelServiceImpl.java
| @@ -163,9 +163,9 @@ public class AiModelServiceImpl implements AiModelService { | @@ -163,9 +163,9 @@ public class AiModelServiceImpl implements AiModelService { | ||
| 163 | 163 | ||
| 164 | // 创建或获取 VectorStore 对象 | 164 | // 创建或获取 VectorStore 对象 |
| 165 | return modelFactory.getOrCreateVectorStore(SimpleVectorStore.class, embeddingModel, metadataFields); | 165 | return modelFactory.getOrCreateVectorStore(SimpleVectorStore.class, embeddingModel, metadataFields); |
| 166 | - // return modelFactory.getOrCreateVectorStore(QdrantVectorStore.class, embeddingModel, metadataFields); | ||
| 167 | - // return modelFactory.getOrCreateVectorStore(RedisVectorStore.class, embeddingModel, metadataFields); | ||
| 168 | -// return modelFactory.getOrCreateVectorStore(MilvusVectorStore.class, embeddingModel, metadataFields); | 166 | +// return modelFactory.getOrCreateVectorStore(QdrantVectorStore.class, embeddingModel, metadataFields); |
| 167 | +// return modelFactory.getOrCreateVectorStore(RedisVectorStore.class, embeddingModel, metadataFields); | ||
| 168 | +// return modelFactory.getOrCreateVectorStore(MilvusVectorStore.class, embeddingModel, metadataFields); | ||
| 169 | } | 169 | } |
| 170 | 170 | ||
| 171 | } | 171 | } |
| 172 | \ No newline at end of file | 172 | \ No newline at end of file |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/factory/AiModelFactoryImpl.java
| @@ -77,6 +77,7 @@ import org.springframework.ai.vectorstore.observation.VectorStoreObservationConv | @@ -77,6 +77,7 @@ import org.springframework.ai.vectorstore.observation.VectorStoreObservationConv | ||
| 77 | import org.springframework.ai.vectorstore.qdrant.QdrantVectorStore; | 77 | import org.springframework.ai.vectorstore.qdrant.QdrantVectorStore; |
| 78 | import org.springframework.ai.vectorstore.redis.RedisVectorStore; | 78 | import org.springframework.ai.vectorstore.redis.RedisVectorStore; |
| 79 | import org.springframework.ai.zhipuai.ZhiPuAiChatModel; | 79 | import org.springframework.ai.zhipuai.ZhiPuAiChatModel; |
| 80 | +import org.springframework.ai.zhipuai.ZhiPuAiEmbeddingModel; | ||
| 80 | import org.springframework.ai.zhipuai.ZhiPuAiImageModel; | 81 | import org.springframework.ai.zhipuai.ZhiPuAiImageModel; |
| 81 | import org.springframework.ai.zhipuai.api.ZhiPuAiApi; | 82 | import org.springframework.ai.zhipuai.api.ZhiPuAiApi; |
| 82 | import org.springframework.ai.zhipuai.api.ZhiPuAiImageApi; | 83 | import org.springframework.ai.zhipuai.api.ZhiPuAiImageApi; |
| @@ -231,7 +232,9 @@ public class AiModelFactoryImpl implements AiModelFactory { | @@ -231,7 +232,9 @@ public class AiModelFactoryImpl implements AiModelFactory { | ||
| 231 | return buildTongYiEmbeddingModel(apiKey, model); | 232 | return buildTongYiEmbeddingModel(apiKey, model); |
| 232 | case OLLAMA: | 233 | case OLLAMA: |
| 233 | return buildOllamaEmbeddingModel(url, model); | 234 | return buildOllamaEmbeddingModel(url, model); |
| 234 | - // TODO @芋艿:各个平台的向量化能力; | 235 | + // TODO @芋艿:yiyan |
| 236 | + case ZHI_PU: | ||
| 237 | + return buildZhiPuEmbeddingModel(apiKey, url, model); | ||
| 235 | default: | 238 | default: |
| 236 | throw new IllegalArgumentException(StrUtil.format("未知平台({})", platform)); | 239 | throw new IllegalArgumentException(StrUtil.format("未知平台({})", platform)); |
| 237 | } | 240 | } |
| @@ -240,8 +243,8 @@ public class AiModelFactoryImpl implements AiModelFactory { | @@ -240,8 +243,8 @@ public class AiModelFactoryImpl implements AiModelFactory { | ||
| 240 | 243 | ||
| 241 | @Override | 244 | @Override |
| 242 | public VectorStore getOrCreateVectorStore(Class<? extends VectorStore> type, | 245 | public VectorStore getOrCreateVectorStore(Class<? extends VectorStore> type, |
| 243 | - EmbeddingModel embeddingModel, | ||
| 244 | - Map<String, Class<?>> metadataFields) { | 246 | + EmbeddingModel embeddingModel, |
| 247 | + Map<String, Class<?>> metadataFields) { | ||
| 245 | String cacheKey = buildClientCacheKey(VectorStore.class, embeddingModel, type); | 248 | String cacheKey = buildClientCacheKey(VectorStore.class, embeddingModel, type); |
| 246 | return Singleton.get(cacheKey, (Func0<VectorStore>) () -> { | 249 | return Singleton.get(cacheKey, (Func0<VectorStore>) () -> { |
| 247 | if (type == SimpleVectorStore.class) { | 250 | if (type == SimpleVectorStore.class) { |
| @@ -425,17 +428,24 @@ public class AiModelFactoryImpl implements AiModelFactory { | @@ -425,17 +428,24 @@ public class AiModelFactoryImpl implements AiModelFactory { | ||
| 425 | 428 | ||
| 426 | // ========== 各种创建 EmbeddingModel 的方法 ========== | 429 | // ========== 各种创建 EmbeddingModel 的方法 ========== |
| 427 | 430 | ||
| 428 | - // TODO @芋艿:需要测试下 | ||
| 429 | /** | 431 | /** |
| 430 | * 可参考 {@link DashScopeAutoConfiguration} 的 dashscopeEmbeddingModel 方法 | 432 | * 可参考 {@link DashScopeAutoConfiguration} 的 dashscopeEmbeddingModel 方法 |
| 431 | */ | 433 | */ |
| 432 | private DashScopeEmbeddingModel buildTongYiEmbeddingModel(String apiKey, String model) { | 434 | private DashScopeEmbeddingModel buildTongYiEmbeddingModel(String apiKey, String model) { |
| 433 | DashScopeApi dashScopeApi = new DashScopeApi(apiKey); | 435 | DashScopeApi dashScopeApi = new DashScopeApi(apiKey); |
| 434 | - DashScopeEmbeddingOptions dashScopeEmbeddingOptions = DashScopeEmbeddingOptions.builder().withModel(model) | ||
| 435 | - .build(); | 436 | + DashScopeEmbeddingOptions dashScopeEmbeddingOptions = DashScopeEmbeddingOptions.builder().withModel(model).build(); |
| 436 | return new DashScopeEmbeddingModel(dashScopeApi, MetadataMode.EMBED, dashScopeEmbeddingOptions); | 437 | return new DashScopeEmbeddingModel(dashScopeApi, MetadataMode.EMBED, dashScopeEmbeddingOptions); |
| 437 | } | 438 | } |
| 438 | 439 | ||
| 440 | + /** | ||
| 441 | + * 可参考 {@link ZhiPuAiAutoConfiguration} 的 zhiPuAiEmbeddingModel 方法 | ||
| 442 | + */ | ||
| 443 | + private ZhiPuAiEmbeddingModel buildZhiPuEmbeddingModel(String apiKey, String url, String model) { | ||
| 444 | + url = StrUtil.blankToDefault(url, ZhiPuAiConnectionProperties.DEFAULT_BASE_URL); | ||
| 445 | + ZhiPuAiApi zhiPuAiApi = new ZhiPuAiApi(url, apiKey); | ||
| 446 | + return new ZhiPuAiEmbeddingModel(zhiPuAiApi); | ||
| 447 | + } | ||
| 448 | + | ||
| 439 | private OllamaEmbeddingModel buildOllamaEmbeddingModel(String url, String model) { | 449 | private OllamaEmbeddingModel buildOllamaEmbeddingModel(String url, String model) { |
| 440 | OllamaApi ollamaApi = new OllamaApi(url); | 450 | OllamaApi ollamaApi = new OllamaApi(url); |
| 441 | OllamaOptions ollamaOptions = OllamaOptions.builder().model(model).build(); | 451 | OllamaOptions ollamaOptions = OllamaOptions.builder().model(model).build(); |