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 | 36 | @Operation(summary = "获取知识库分页") |
| 37 | 37 | @PreAuthorize("@ss.hasPermission('ai:knowledge:query')") |
| 38 | 38 | public CommonResult<PageResult<AiKnowledgeRespVO>> getKnowledgePage(@Valid AiKnowledgePageReqVO pageReqVO) { |
| 39 | - | |
| 40 | 39 | PageResult<AiKnowledgeDO> pageResult = knowledgeService.getKnowledgePage(pageReqVO); |
| 41 | 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 | 163 | |
| 164 | 164 | // 创建或获取 VectorStore 对象 |
| 165 | 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 | 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 | 77 | import org.springframework.ai.vectorstore.qdrant.QdrantVectorStore; |
| 78 | 78 | import org.springframework.ai.vectorstore.redis.RedisVectorStore; |
| 79 | 79 | import org.springframework.ai.zhipuai.ZhiPuAiChatModel; |
| 80 | +import org.springframework.ai.zhipuai.ZhiPuAiEmbeddingModel; | |
| 80 | 81 | import org.springframework.ai.zhipuai.ZhiPuAiImageModel; |
| 81 | 82 | import org.springframework.ai.zhipuai.api.ZhiPuAiApi; |
| 82 | 83 | import org.springframework.ai.zhipuai.api.ZhiPuAiImageApi; |
| ... | ... | @@ -231,7 +232,9 @@ public class AiModelFactoryImpl implements AiModelFactory { |
| 231 | 232 | return buildTongYiEmbeddingModel(apiKey, model); |
| 232 | 233 | case OLLAMA: |
| 233 | 234 | return buildOllamaEmbeddingModel(url, model); |
| 234 | - // TODO @芋艿:各个平台的向量化能力; | |
| 235 | + // TODO @芋艿:yiyan | |
| 236 | + case ZHI_PU: | |
| 237 | + return buildZhiPuEmbeddingModel(apiKey, url, model); | |
| 235 | 238 | default: |
| 236 | 239 | throw new IllegalArgumentException(StrUtil.format("未知平台({})", platform)); |
| 237 | 240 | } |
| ... | ... | @@ -240,8 +243,8 @@ public class AiModelFactoryImpl implements AiModelFactory { |
| 240 | 243 | |
| 241 | 244 | @Override |
| 242 | 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 | 248 | String cacheKey = buildClientCacheKey(VectorStore.class, embeddingModel, type); |
| 246 | 249 | return Singleton.get(cacheKey, (Func0<VectorStore>) () -> { |
| 247 | 250 | if (type == SimpleVectorStore.class) { |
| ... | ... | @@ -425,17 +428,24 @@ public class AiModelFactoryImpl implements AiModelFactory { |
| 425 | 428 | |
| 426 | 429 | // ========== 各种创建 EmbeddingModel 的方法 ========== |
| 427 | 430 | |
| 428 | - // TODO @芋艿:需要测试下 | |
| 429 | 431 | /** |
| 430 | 432 | * 可参考 {@link DashScopeAutoConfiguration} 的 dashscopeEmbeddingModel 方法 |
| 431 | 433 | */ |
| 432 | 434 | private DashScopeEmbeddingModel buildTongYiEmbeddingModel(String apiKey, String model) { |
| 433 | 435 | DashScopeApi dashScopeApi = new DashScopeApi(apiKey); |
| 434 | - DashScopeEmbeddingOptions dashScopeEmbeddingOptions = DashScopeEmbeddingOptions.builder().withModel(model) | |
| 435 | - .build(); | |
| 436 | + DashScopeEmbeddingOptions dashScopeEmbeddingOptions = DashScopeEmbeddingOptions.builder().withModel(model).build(); | |
| 436 | 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 | 449 | private OllamaEmbeddingModel buildOllamaEmbeddingModel(String url, String model) { |
| 440 | 450 | OllamaApi ollamaApi = new OllamaApi(url); |
| 441 | 451 | OllamaOptions ollamaOptions = OllamaOptions.builder().model(model).build(); | ... | ... |