Commit b9e8495712de03b9c7e215b43f772c0c9b85ef0a
1 parent
20eb3013
【功能新增】AI:新增 AzureOpenAiEmbeddingModel、OpenAiEmbeddingModel 向量模型
Showing
1 changed file
with
36 additions
and
4 deletions
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/factory/AiModelFactoryImpl.java
| ... | ... | @@ -35,6 +35,7 @@ import lombok.SneakyThrows; |
| 35 | 35 | import org.springframework.ai.autoconfigure.azure.openai.AzureOpenAiAutoConfiguration; |
| 36 | 36 | import org.springframework.ai.autoconfigure.azure.openai.AzureOpenAiChatProperties; |
| 37 | 37 | import org.springframework.ai.autoconfigure.azure.openai.AzureOpenAiConnectionProperties; |
| 38 | +import org.springframework.ai.autoconfigure.azure.openai.AzureOpenAiEmbeddingProperties; | |
| 38 | 39 | import org.springframework.ai.autoconfigure.ollama.OllamaAutoConfiguration; |
| 39 | 40 | import org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration; |
| 40 | 41 | import org.springframework.ai.autoconfigure.qianfan.QianFanAutoConfiguration; |
| ... | ... | @@ -49,6 +50,7 @@ import org.springframework.ai.autoconfigure.vectorstore.redis.RedisVectorStorePr |
| 49 | 50 | import org.springframework.ai.autoconfigure.zhipuai.ZhiPuAiAutoConfiguration; |
| 50 | 51 | import org.springframework.ai.autoconfigure.zhipuai.ZhiPuAiConnectionProperties; |
| 51 | 52 | import org.springframework.ai.azure.openai.AzureOpenAiChatModel; |
| 53 | +import org.springframework.ai.azure.openai.AzureOpenAiEmbeddingModel; | |
| 52 | 54 | import org.springframework.ai.chat.model.ChatModel; |
| 53 | 55 | import org.springframework.ai.document.MetadataMode; |
| 54 | 56 | import org.springframework.ai.embedding.BatchingStrategy; |
| ... | ... | @@ -59,6 +61,8 @@ import org.springframework.ai.ollama.OllamaEmbeddingModel; |
| 59 | 61 | import org.springframework.ai.ollama.api.OllamaApi; |
| 60 | 62 | import org.springframework.ai.ollama.api.OllamaOptions; |
| 61 | 63 | import org.springframework.ai.openai.OpenAiChatModel; |
| 64 | +import org.springframework.ai.openai.OpenAiEmbeddingModel; | |
| 65 | +import org.springframework.ai.openai.OpenAiEmbeddingOptions; | |
| 62 | 66 | import org.springframework.ai.openai.OpenAiImageModel; |
| 63 | 67 | import org.springframework.ai.openai.api.OpenAiApi; |
| 64 | 68 | import org.springframework.ai.openai.api.OpenAiImageApi; |
| ... | ... | @@ -227,6 +231,7 @@ public class AiModelFactoryImpl implements AiModelFactory { |
| 227 | 231 | } |
| 228 | 232 | |
| 229 | 233 | @Override |
| 234 | + @SuppressWarnings("EnhancedSwitchMigration") | |
| 230 | 235 | public EmbeddingModel getOrCreateEmbeddingModel(AiPlatformEnum platform, String apiKey, String url, String model) { |
| 231 | 236 | String cacheKey = buildClientCacheKey(EmbeddingModel.class, platform, apiKey, url, model); |
| 232 | 237 | return Singleton.get(cacheKey, (Func0<EmbeddingModel>) () -> { |
| ... | ... | @@ -237,10 +242,10 @@ public class AiModelFactoryImpl implements AiModelFactory { |
| 237 | 242 | return buildYiYanEmbeddingModel(apiKey, model); |
| 238 | 243 | case ZHI_PU: |
| 239 | 244 | return buildZhiPuEmbeddingModel(apiKey, url, model); |
| 240 | -// case OPENAI: | |
| 241 | -// return buildOpenAiChatModel(apiKey, url); | |
| 242 | -// case AZURE_OPENAI: | |
| 243 | -// return buildAzureOpenAiChatModel(apiKey, url); | |
| 245 | + case OPENAI: | |
| 246 | + return buildOpenAiEmbeddingModel(apiKey, url, model); | |
| 247 | + case AZURE_OPENAI: | |
| 248 | + return buildAzureOpenAiEmbeddingModel(apiKey, url, model); | |
| 244 | 249 | case OLLAMA: |
| 245 | 250 | return buildOllamaEmbeddingModel(url, model); |
| 246 | 251 | default: |
| ... | ... | @@ -474,6 +479,33 @@ public class AiModelFactoryImpl implements AiModelFactory { |
| 474 | 479 | return OllamaEmbeddingModel.builder().ollamaApi(ollamaApi).defaultOptions(ollamaOptions).build(); |
| 475 | 480 | } |
| 476 | 481 | |
| 482 | + /** | |
| 483 | + * 可参考 {@link OpenAiAutoConfiguration} 的 openAiEmbeddingModel 方法 | |
| 484 | + */ | |
| 485 | + private OpenAiEmbeddingModel buildOpenAiEmbeddingModel(String openAiToken, String url, String model) { | |
| 486 | + url = StrUtil.blankToDefault(url, OpenAiApiConstants.DEFAULT_BASE_URL); | |
| 487 | + OpenAiApi openAiApi = OpenAiApi.builder().baseUrl(url).apiKey(openAiToken).build(); | |
| 488 | + OpenAiEmbeddingOptions openAiEmbeddingProperties = OpenAiEmbeddingOptions.builder().model(model).build(); | |
| 489 | + return new OpenAiEmbeddingModel(openAiApi, MetadataMode.EMBED, openAiEmbeddingProperties); | |
| 490 | + } | |
| 491 | + | |
| 492 | + // TODO @芋艿:手头暂时没密钥,使用建议再测试下 | |
| 493 | + /** | |
| 494 | + * 可参考 {@link AzureOpenAiAutoConfiguration} 的 azureOpenAiEmbeddingModel 方法 | |
| 495 | + */ | |
| 496 | + private AzureOpenAiEmbeddingModel buildAzureOpenAiEmbeddingModel(String apiKey, String url, String model) { | |
| 497 | + AzureOpenAiAutoConfiguration azureOpenAiAutoConfiguration = new AzureOpenAiAutoConfiguration(); | |
| 498 | + // 创建 OpenAIClient 对象 | |
| 499 | + AzureOpenAiConnectionProperties connectionProperties = new AzureOpenAiConnectionProperties(); | |
| 500 | + connectionProperties.setApiKey(apiKey); | |
| 501 | + connectionProperties.setEndpoint(url); | |
| 502 | + OpenAIClientBuilder openAIClient = azureOpenAiAutoConfiguration.openAIClientBuilder(connectionProperties, null); | |
| 503 | + // 获取 AzureOpenAiChatProperties 对象 | |
| 504 | + AzureOpenAiEmbeddingProperties embeddingProperties = SpringUtil.getBean(AzureOpenAiEmbeddingProperties.class); | |
| 505 | + return azureOpenAiAutoConfiguration.azureOpenAiEmbeddingModel(openAIClient, embeddingProperties, | |
| 506 | + null, null); | |
| 507 | + } | |
| 508 | + | |
| 477 | 509 | // ========== 各种创建 VectorStore 的方法 ========== |
| 478 | 510 | |
| 479 | 511 | /** | ... | ... |