@@ -8,11 +8,11 @@ import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.RuntimeUtil ;
import cn.hutool.core.util.StrUtil ;
import cn.hutool.extra.spring.SpringUtil ;
import cn.iocoder.yudao.framework.common.util.spring.SpringUtils ;
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 cn.iocoder.yudao.module.ai.enums.model.AiPlatformEnum ;
import cn.iocoder.yudao.module.ai.framework.ai.core.model.baichuan.BaiChuanChatModel ;
import cn.iocoder.yudao.module.ai.framework.ai.core.model.deepseek.DeepSeekChatModel ;
import cn.iocoder.yudao.module.ai.framework.ai.core.model.doubao.DouBaoChatModel ;
import cn.iocoder.yudao.module.ai.framework.ai.core.model.hunyuan.HunYuanChatModel ;
import cn.iocoder.yudao.module.ai.framework.ai.core.model.midjourney.api.MidjourneyApi ;
@@ -22,8 +22,9 @@ import cn.iocoder.yudao.module.ai.framework.ai.core.model.siliconflow.SiliconFlo
import cn.iocoder.yudao.module.ai.framework.ai.core.model.siliconflow.SiliconFlowImageModel ;
import cn.iocoder.yudao.module.ai.framework.ai.core.model.suno.api.SunoApi ;
import cn.iocoder.yudao.module.ai.framework.ai.core.model.xinghuo.XingHuoChatModel ;
import cn.iocoder.yudao.framework.common.util.spring.SpringUtils ;
import com.alibaba.cloud.ai.autoconfigure.dashscope.DashScopeAutoConfiguration ;
import com.alibaba.cloud.ai.autoconfigure.dashscope.DashScopeChatAutoConfiguration ;
import com.alibaba.cloud.ai.autoconfigure.dashscope.DashScopeEmbedding AutoConfiguration ;
import com.alibaba.cloud.ai.autoconfigure.dashscope.DashScopeImageAutoConfiguration ;
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi ;
import com.alibaba.cloud.ai.dashscope.api.DashScopeImageApi ;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel ;
@@ -32,47 +33,55 @@ import com.alibaba.cloud.ai.dashscope.embedding.DashScopeEmbeddingModel;
import com.alibaba.cloud.ai.dashscope.embedding.DashScopeEmbeddingOptions ;
import com.alibaba.cloud.ai.dashscope.image.DashScopeImageModel ;
import com.azure.ai.openai.OpenAIClientBuilder ;
import com.azure.core.credential.KeyCredential ;
import io.micrometer.observation.ObservationRegistry ;
import io.milvus.client.MilvusServiceClient ;
import io.qdrant.client.QdrantClient ;
import io.qdrant.client.QdrantGrpcClient ;
import lombok.SneakyThrows ;
import org.springframework.ai.autoconfigure.azure.openai.AzureOpenAiAutoConfiguration ;
import org.springframework.ai.autoconfigure.azure.openai.AzureOpenAiChatPropertie s ;
import org.springframework.ai.autoconfigure.azure.openai.AzureOpenAiConnectionProperties ;
import org.springframework.ai.autoconfigure.azure.openai.AzureOpenAiEmbeddingProperties ;
import org.springframework.ai.autoconfigure.minimax.MiniMaxAutoConfiguration ;
import org.springframework.ai.autoconfigure.moonshot.MoonshotAutoConfiguration ;
import org.springframework.ai.autoconfigure.ollama.OllamaAutoConfigura tion ;
import org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration ;
import org.springframework.ai.autoconfigure .qianfan.QianFanAutoConfiguration ;
import org.springframework.ai.autoconfigure.stabilityai.StabilityAiImageAutoConfiguration ;
import org.springframework.ai.autoconfigure.vectorstore.milvus.MilvusServiceClientConnectionDetails ;
import org.springframework.ai.autoconfigure.vectorstore.milvus.MilvusServiceClientProperties ;
import org.springframework.ai.autoconfigure.vectorstore.milvus.MilvusVectorStoreAutoConfiguration ;
import org.springframework.ai.autoconfigure.vectorstore.milvus.MilvusVectorStoreProperties ;
import org.springframework.ai.autoconfigure.vectorstore.qdrant.QdrantVectorStoreAutoConfiguration ;
import org.springframework.ai.autoconfigure.vectorstore.qdrant.QdrantVectorStoreProperties ;
import org.springframework.ai.autoconfigure.vectorstore.redis.RedisVectorStoreAutoConfiguration ;
import org.springframework.ai.autoconfigure.vectorstore.redis.RedisVectorStoreProperties ;
import org.springframework.ai.autoconfigure.zhipuai.ZhiPuAiAutoConfiguration ;
import org.springaicommunity.moonshot.MoonshotChatModel ;
import org.springaicommunity.moonshot.MoonshotChatOption s ;
import org.springaicommunity.moonshot.api.MoonshotApi ;
import org.springaicommunity.moonshot.autoconfigure.MoonshotChatAutoConfiguration ;
import org.springaicommunity.qianfan.QianFanChatModel ;
import org.springaicommunity.qianfan.QianFanEmbeddingModel ;
import org.springaicommunity.qianfan.QianFanEmbeddingOp tions ;
import org.springaicommunity.qianfan.QianFanImageModel ;
import org.springaicommunity .qianfan.api. QianFanApi ;
import org.springaicommunity.qianfan.api.QianFanImageApi ;
import org.springaicommunity.qianfan.autoconfigure.QianFanChatAutoConfiguration ;
import org.springaicommunity.qianfan.autoconfigure.QianFanEmbeddingAutoConfiguration ;
import org.springframework.ai.azure.openai.AzureOpenAiChatModel ;
import org.springframework.ai.azure.openai.AzureOpenAiEmbeddingModel ;
import org.springframework.ai.chat.model.ChatModel ;
import org.springframework.ai.deepseek.DeepSeekChatModel ;
import org.springframework.ai.deepseek.DeepSeekChatOptions ;
import org.springframework.ai.deepseek.api.DeepSeekApi ;
import org.springframework.ai.document.MetadataMode ;
import org.springframework.ai.embedding.BatchingStrategy ;
import org.springframework.ai.embedding.EmbeddingModel ;
import org.springframework.ai.embedding.observation.EmbeddingModelObservationConvention ;
import org.springframework.ai.image.ImageModel ;
import org.springframework.ai.minimax.MiniMaxChatModel ;
import org.springframework.ai.minimax.MiniMaxChatOptions ;
import org.springframework.ai.minimax.MiniMaxEmbeddingModel ;
import org.springframework.ai.minimax.MiniMaxEmbeddingOptions ;
import org.springframework.ai.minimax.api.MiniMaxApi ;
import org.springframework.ai.model.function.FunctionCallbackResolver ;
import org.springframework.ai.model.azure.openai.autoconfigure.AzureOpenAiChatAutoConfiguration ;
import org.springframework.ai.model.azure.openai.autoconfigure.AzureOpenAiEmbeddingAutoConfiguration ;
import org.springframework.ai.model.azure.openai.autoconfigure.AzureOpenAiEmbeddingProperties ;
import org.springframework.ai.model.deepseek.autoconfigure.DeepSeekChatAutoConfiguration ;
import org.springframework.ai.model.minimax.autoconfigure.MiniMaxChatAutoConfiguration ;
import org.springframework.ai.model.minimax.autoconfigure.MiniMaxEmbeddingAutoConfiguration ;
import org.springframework.ai.model.ollama.autoconfigure.OllamaChatAutoConfiguration ;
import org.springframework.ai.model.openai.autoconfigure.OpenAiChatAutoConfiguration ;
import org.springframework.ai.model.openai.autoconfigure.OpenAiEmbeddingAutoConfiguration ;
import org.springframework.ai.model.openai.autoconfigure.OpenAiImageAutoConfiguration ;
import org.springframework.ai.model.stabilityai.autoconfigure.StabilityAiImageAutoConfiguration ;
import org.springframework.ai.model.tool.ToolCallingManager ;
import org.springframework.ai.moonshot.MoonshotChatModel ;
import org.springframework.ai.moonshot.MoonshotChatOp tions ;
import org.springframework.ai.moonshot.api.MoonshotApi ;
import org.springframework.ai.model.zhipuai.autoconfigure.ZhiPuAiChatAutoConfiguration ;
import org.springframework.ai.model.zhipuai.autoconfigure.ZhiPuAiEmbeddingAutoConfigura tion ;
import org.springframework.ai.model.zhipuai.autoconfigure.ZhiPuAiImageAutoConfiguration ;
import org.springframework.ai.ollama.OllamaChatModel ;
import org.springframework.ai.ollama.OllamaEmbeddingModel ;
import org.springframework.ai.ollama.api.OllamaApi ;
@@ -84,21 +93,23 @@ import org.springframework.ai.openai.OpenAiImageModel;
import org.springframework.ai.openai.api.OpenAiApi ;
import org.springframework.ai.openai.api.OpenAiImageApi ;
import org.springframework.ai.openai.api.common.OpenAiApiConstants ;
import org.springframework.ai.qianfan.QianFanChatModel ;
import org.springframework.ai.qianfan.QianFanEmbeddingModel ;
import org.springframework.ai.qianfan.QianFanEmbeddingOptions ;
import org.springframework.ai.qianfan.QianFanImageModel ;
import org.springframework.ai.qianfan.api.QianFanApi ;
import org.springframework.ai.qianfan.api.QianFanImageApi ;
import org.springframework.ai.stabilityai.StabilityAiImageModel ;
import org.springframework.ai.stabilityai.api.StabilityAiApi ;
import org.springframework.ai.vectorstore.SimpleVectorStore ;
import org.springframework.ai.vectorstore.VectorStore ;
import org.springframework.ai.vectorstore.milvus.MilvusVectorStore ;
import org.springframework.ai.vectorstore.milvus.autoconfigure.MilvusServiceClientConnectionDetails ;
import org.springframework.ai.vectorstore.milvus.autoconfigure.MilvusServiceClientProperties ;
import org.springframework.ai.vectorstore.milvus.autoconfigure.MilvusVectorStoreAutoConfiguration ;
import org.springframework.ai.vectorstore.milvus.autoconfigure.MilvusVectorStoreProperties ;
import org.springframework.ai.vectorstore.observation.DefaultVectorStoreObservationConvention ;
import org.springframework.ai.vectorstore.observation.VectorStoreObservationConvention ;
import org.springframework.ai.vectorstore.qdrant.QdrantVectorStore ;
import org.springframework.ai.vectorstore.qdrant.autoconfigure.QdrantVectorStoreAutoConfiguration ;
import org.springframework.ai.vectorstore.qdrant.autoconfigure.QdrantVectorStoreProperties ;
import org.springframework.ai.vectorstore.redis.RedisVectorStore ;
import org.springframework.ai.vectorstore.redis.autoconfigure.RedisVectorStoreAutoConfiguration ;
import org.springframework.ai.vectorstore.redis.autoconfigure.RedisVectorStoreProperties ;
import org.springframework.ai.zhipuai.* ;
import org.springframework.ai.zhipuai.api.ZhiPuAiApi ;
import org.springframework.ai.zhipuai.api.ZhiPuAiImageApi ;
@@ -190,7 +201,7 @@ public class AiModelFactoryImpl implements AiModelFactory {
case XING_HUO :
return SpringUtil . getBean ( XingHuoChatModel . class ) ;
case BAI_CHUAN :
return SpringUtil . getBean ( AzureOpenAi ChatModel. class ) ;
return SpringUtil . getBean ( BaiChuan ChatModel. class ) ;
case OPENAI :
return SpringUtil . getBean ( OpenAiChatModel . class ) ;
case AZURE_OPENAI :
@@ -319,27 +330,34 @@ public class AiModelFactoryImpl implements AiModelFactory {
// ========== 各种创建 spring-ai 客户端的方法 ==========
/**
* 可参考 {@link DashScopeAutoConfiguration} 的 dashscopeChatModel 方法
* 可参考 {@link DashScopeChat AutoConfiguration} 的 dashscopeChatModel 方法
*/
private static DashScopeChatModel buildTongYiChatModel ( String key ) {
DashScopeApi dashScopeApi = new DashScopeApi ( key ) ;
DashScopeApi dashScopeApi = DashScopeApi . builder ( ) . apiKey ( key ) . build ( ) ;
DashScopeChatOptions options = DashScopeChatOptions . builder ( ) . withModel ( DashScopeApi . DEFAULT_CHAT_MODEL )
. withTemperature ( 0 . 7 ) . build ( ) ;
return new DashScopeChatModel ( dashScopeApi , options , getFunctionCallbackResolver ( ) , DEFAULT_RETRY_TEMPLATE ) ;
return DashScopeChatModel . builder ( )
. dashScopeApi ( dashScopeApi )
. defaultOptions ( options )
. toolCallingManager ( getToolCallingManager ( ) )
. build ( ) ;
}
/**
* 可参考 {@link DashScopeAutoConfiguration} 的 dashScopeImageModel 方法
* 可参考 {@link DashScopeImage AutoConfiguration} 的 dashScopeImageModel 方法
*/
private static DashScopeImageModel buildTongYiImagesModel ( String key ) {
DashScopeImageApi dashScopeImageApi = new DashScopeImageApi ( key ) ;
return new DashScopeImageModel ( dashScopeImageApi ) ;
return DashScopeImageModel . builder ( )
. dashScopeApi ( dashScopeImageApi )
. build ( ) ;
}
/**
* 可参考 {@link QianFanAutoConfiguration} 的 qianFanChatModel 方法
* 可参考 {@link QianFanChat AutoConfiguration} 的 qianFanChatModel 方法
*/
private static QianFanChatModel buildYiYanChatModel ( String key ) {
// TODO @芋艿:未测试
List < String > keys = StrUtil . split ( key , '|' ) ;
Assert . equals ( keys . size ( ) , 2 , " YiYanChatClient 的密钥需要 (appKey|secretKey) 格式 " ) ;
String appKey = keys . get ( 0 ) ;
@@ -349,9 +367,10 @@ public class AiModelFactoryImpl implements AiModelFactory {
}
/**
* 可参考 {@link QianFanAutoConfiguration} 的 qianFanImageModel 方法
* 可参考 {@link QianFanEmbedding AutoConfiguration} 的 qianFanImageModel 方法
*/
private QianFanImageModel buildQianFanImageModel ( String key ) {
// TODO @芋艿:未测试
List < String > keys = StrUtil . split ( key , '|' ) ;
Assert . equals ( keys . size ( ) , 2 , " YiYanChatClient 的密钥需要 (appKey|secretKey) 格式 " ) ;
String appKey = keys . get ( 0 ) ;
@@ -361,12 +380,17 @@ public class AiModelFactoryImpl implements AiModelFactory {
}
/**
* 可参考 {@link Ai AutoConfiguration# deepSeekChatModel(YudaoAiProperties)}
* 可参考 {@link DeepSeekChat AutoConfiguration} 的 deepSeekChatModel 方法
*/
private static DeepSeekChatModel buildDeepSeekChatModel ( String apiKey ) {
YudaoAiProperties . DeepSeekProperties properties = new YudaoAiProperties . DeepSeekProperties ( )
. setApiKey ( apiKey ) ;
return new AiAutoConfiguration ( ) . buildDeepSeekChatModel ( properties ) ;
DeepSeekApi deepSeekApi = DeepSeekApi . builder ( ) . apiKey ( apiKey ) . build ( ) ;
DeepSeekChatOptions options = DeepSeekChatOptions . builder ( ) . model ( DeepSeekApi . DEFAULT_CHAT_MODEL )
. temperature ( 0 . 7 ) . build ( ) ;
return DeepSeekChatModel . builder ( )
. deepSeekApi ( deepSeekApi )
. defaultOptions ( options )
. toolCallingManager ( getToolCallingManager ( ) )
. build ( ) ;
}
/**
@@ -397,17 +421,18 @@ public class AiModelFactoryImpl implements AiModelFactory {
}
/**
* 可参考 {@link ZhiPuAiAutoConfiguration} 的 zhiPuAiChatModel 方法
* 可参考 {@link ZhiPuAiChat AutoConfiguration} 的 zhiPuAiChatModel 方法
*/
private ZhiPuAiChatModel buildZhiPuChatModel ( String apiKey , String url ) {
ZhiPuAiApi zhiPuAiApi = StrUtil . isEmpty ( url ) ? new ZhiPuAiApi ( apiKey )
: new ZhiPuAiApi ( url , apiKey ) ;
ZhiPuAiChatOptions options = ZhiPuAiChatOptions . builder ( ) . model ( ZhiPuAiApi . DEFAULT_CHAT_MODEL ) . temperature ( 0 . 7 ) . build ( ) ;
return new ZhiPuAiChatModel ( zhiPuAiApi , options , getFunctionCallbackResolv er ( ) , DEFAULT_RETRY_TEMPLATE ) ;
return new ZhiPuAiChatModel ( zhiPuAiApi , options , getToolCallingManag er ( ) , DEFAULT_RETRY_TEMPLATE ,
getObservationRegistry ( ) . getIfAvailable ( ) ) ;
}
/**
* 可参考 {@link ZhiPuAiAutoConfiguration} 的 zhiPuAiImageModel 方法
* 可参考 {@link ZhiPuAiImage AutoConfiguration} 的 zhiPuAiImageModel 方法
*/
private ZhiPuAiImageModel buildZhiPuAiImageModel ( String apiKey , String url ) {
ZhiPuAiImageApi zhiPuAiApi = StrUtil . isEmpty ( url ) ? new ZhiPuAiImageApi ( apiKey )
@@ -416,23 +441,30 @@ public class AiModelFactoryImpl implements AiModelFactory {
}
/**
* 可参考 {@link MiniMaxAutoConfiguration} 的 miniMaxChatModel 方法
* 可参考 {@link MiniMaxChat AutoConfiguration} 的 miniMaxChatModel 方法
*/
private MiniMaxChatModel buildMiniMaxChatModel ( String apiKey , String url ) {
MiniMaxApi miniMaxApi = StrUtil . isEmpty ( url ) ? new MiniMaxApi ( apiKey )
: new MiniMaxApi ( url , apiKey ) ;
MiniMaxChatOptions options = MiniMaxChatOptions . builder ( ) . model ( MiniMaxApi . DEFAULT_CHAT_MODEL ) . temperature ( 0 . 7 ) . build ( ) ;
return new MiniMaxChatModel ( miniMaxApi , options , getFunctionCallbackResolv er ( ) , DEFAULT_RETRY_TEMPLATE ) ;
return new MiniMaxChatModel ( miniMaxApi , options , getToolCallingManag er ( ) , DEFAULT_RETRY_TEMPLATE ) ;
}
/**
* 可参考 {@link MoonshotAutoConfiguration} 的 moonshotChatModel 方法
* 可参考 {@link MoonshotChat AutoConfiguration} 的 moonshotChatModel 方法
*/
private MoonshotChatModel buildMoonshotChatModel ( String apiKey , String url ) {
MoonshotApi moonshotApi = StrUtil . isEmpty ( url ) ? new MoonshotApi ( apiKey )
: new MoonshotApi ( url , apiKey ) ;
MoonshotApi . Builder moonshotApiBuilder = MoonshotApi . builder ( )
. apiKey ( apiKey ) ;
if ( StrUtil . isNotEmpty ( url ) ) {
moonshotApiBuilder . baseUrl ( url ) ;
}
MoonshotChatOptions options = MoonshotChatOptions . builder ( ) . model ( MoonshotApi . DEFAULT_CHAT_MODEL ) . build ( ) ;
return new MoonshotChatModel ( moonshotApi , options , getFunctionCallbackResolver ( ) , DEFAULT_RETRY_TEMPLATE ) ;
return MoonshotChatModel . builder ( )
. moonshotApi ( moonshotApiBuilder . build ( ) )
. defaultOptions ( options )
. toolCallingManager ( getToolCallingManager ( ) )
. build ( ) ;
}
/**
@@ -456,33 +488,32 @@ public class AiModelFactoryImpl implements AiModelFactory {
}
/**
* 可参考 {@link OpenAiAutoConfiguration} 的 openAiChatModel 方法
* 可参考 {@link OpenAiChat AutoConfiguration} 的 openAiChatModel 方法
*/
private static OpenAiChatModel buildOpenAiChatModel ( String openAiToken , String url ) {
url = StrUtil . blankToDefault ( url , OpenAiApiConstants . DEFAULT_BASE_URL ) ;
OpenAiApi openAiApi = OpenAiApi . builder ( ) . baseUrl ( url ) . apiKey ( openAiToken ) . build ( ) ;
return OpenAiChatModel . builder ( ) . openAiApi ( openAiApi ) . toolCallingManager ( getToolCallingManager ( ) ) . build ( ) ;
return OpenAiChatModel . builder ( )
. openAiApi ( openAiApi )
. toolCallingManager ( getToolCallingManager ( ) )
. build ( ) ;
}
// TODO @芋艿:手头暂时没密钥,使用建议再测试下
/**
* 可参考 {@link AzureOpenAiAutoConfiguration}
* 可参考 {@link AzureOpenAiChat AutoConfiguration}
*/
private static AzureOpenAiChatModel buildAzureOpenAiChatModel ( String apiKey , String url ) {
AzureOpenAiAutoConfiguration azureOpenAiAutoConfiguration = new AzureOpenAiAutoConfiguration ( ) ;
// 创建 OpenAIClient 对象
AzureOpenAiConnectionProperties connectionProperties = new AzureOpenAiConnectionProperties ( ) ;
connectionProperties . setApiKey ( apiKey ) ;
connectionProperties . setEndpoint ( url ) ;
OpenAIClientBuilder openAIClient = azureOpenAiAutoConfiguration . openAIClientBuilder ( connectionProperties , null ) ;
// 获取 AzureOpenAiChatProperties 对象
AzureOpenAiChatProperties chatProperties = SpringUtil . getBean ( AzureOpenAiChatProperties . class ) ;
return azureOpenAiAutoConfiguration . azureOpenAiChatModel ( openAIClient , chatProperties ,
getToolCallingManager ( ) , null , null ) ;
// TODO @芋艿:使用前,请测试,暂时没密钥!!!
OpenAIClientBuilder openAIClientBuilder = new OpenAIClientBuilder ( )
. endpoint ( url ) . credential ( new KeyCredential ( apiKey ) ) ;
return AzureOpenAiChatModel . builder ( )
. openAIClientBuilder ( openAIClientBuilder )
. toolCallingManager ( getToolCallingManager ( ) )
. build ( ) ;
}
/**
* 可参考 {@link OpenAiAutoConfiguration} 的 openAiImageModel 方法
* 可参考 {@link OpenAiImage AutoConfiguration} 的 openAiImageModel 方法
*/
private OpenAiImageModel buildOpenAiImageModel ( String openAiToken , String url ) {
url = StrUtil . blankToDefault ( url , OpenAiApiConstants . DEFAULT_BASE_URL ) ;
@@ -494,16 +525,18 @@ public class AiModelFactoryImpl implements AiModelFactory {
* 创建 SiliconFlowImageModel 对象
*/
private SiliconFlowImageModel buildSiliconFlowImageModel ( String apiToken , String url ) {
// TODO @芋艿:未测试
url = StrUtil . blankToDefault ( url , SiliconFlowApiConstants . DEFAULT_BASE_URL ) ;
SiliconFlowImageApi openAiApi = new SiliconFlowImageApi ( url , apiToken ) ;
return new SiliconFlowImageModel ( openAiApi ) ;
}
/**
* 可参考 {@link OllamaAutoConfiguration} 的 ollamaApi 方法
* 可参考 {@link OllamaChat AutoConfiguration} 的 ollamaChatModel 方法
*/
private static OllamaChatModel buildOllamaChatModel ( String url ) {
OllamaApi ollamaApi = new OllamaApi ( url ) ;
// TODO @芋艿:未测试
OllamaApi ollamaApi = OllamaApi . builder ( ) . baseUrl ( url ) . build ( ) ;
return OllamaChatModel . builder ( ) . ollamaApi ( ollamaApi ) . toolCallingManager ( getToolCallingManager ( ) ) . build ( ) ;
}
@@ -519,16 +552,16 @@ public class AiModelFactoryImpl implements AiModelFactory {
// ========== 各种创建 EmbeddingModel 的方法 ==========
/**
* 可参考 {@link DashScopeAutoConfiguration} 的 dashscopeEmbeddingModel 方法
* 可参考 {@link DashScopeEmbedding AutoConfiguration} 的 dashscopeEmbeddingModel 方法
*/
private DashScopeEmbeddingModel buildTongYiEmbeddingModel ( String apiKey , String model ) {
DashScopeApi dashScopeApi = new DashScopeApi ( apiKey ) ;
DashScopeApi dashScopeApi = DashScopeApi . builder ( ) . apiKey ( apiKey ) . build ( ) ;
DashScopeEmbeddingOptions dashScopeEmbeddingOptions = DashScopeEmbeddingOptions . builder ( ) . withModel ( model ) . build ( ) ;
return new DashScopeEmbeddingModel ( dashScopeApi , MetadataMode . EMBED , dashScopeEmbeddingOptions ) ;
}
/**
* 可参考 {@link ZhiPuAiAutoConfiguration} 的 zhiPuAiEmbeddingModel 方法
* 可参考 {@link ZhiPuAiEmbedding AutoConfiguration} 的 zhiPuAiEmbeddingModel 方法
*/
private ZhiPuAiEmbeddingModel buildZhiPuEmbeddingModel ( String apiKey , String url , String model ) {
ZhiPuAiApi zhiPuAiApi = StrUtil . isEmpty ( url ) ? new ZhiPuAiApi ( apiKey )
@@ -538,7 +571,7 @@ public class AiModelFactoryImpl implements AiModelFactory {
}
/**
* 可参考 {@link MiniMaxAutoConfiguration} 的 miniMaxEmbeddingModel 方法
* 可参考 {@link MiniMaxEmbedding AutoConfiguration} 的 miniMaxEmbeddingModel 方法
*/
private EmbeddingModel buildMiniMaxEmbeddingModel ( String apiKey , String url , String model ) {
MiniMaxApi miniMaxApi = StrUtil . isEmpty ( url ) ? new MiniMaxApi ( apiKey )
@@ -548,7 +581,7 @@ public class AiModelFactoryImpl implements AiModelFactory {
}
/**
* 可参考 {@link QianFanAutoConfiguration} 的 qianFanEmbeddingModel 方法
* 可参考 {@link QianFanEmbedding AutoConfiguration} 的 qianFanEmbeddingModel 方法
*/
private QianFanEmbeddingModel buildYiYanEmbeddingModel ( String key , String model ) {
List < String > keys = StrUtil . split ( key , '|' ) ;
@@ -561,13 +594,13 @@ public class AiModelFactoryImpl implements AiModelFactory {
}
private OllamaEmbeddingModel buildOllamaEmbeddingModel ( String url , String model ) {
OllamaApi ollamaApi = new OllamaApi ( url ) ;
OllamaApi ollamaApi = OllamaApi . builder ( ) . baseUrl ( url ) . build ( ) ;
OllamaOptions ollamaOptions = OllamaOptions . builder ( ) . model ( model ) . build ( ) ;
return OllamaEmbeddingModel . builder ( ) . ollamaApi ( ollamaApi ) . defaultOptions ( ollamaOptions ) . build ( ) ;
}
/**
* 可参考 {@link OpenAiAutoConfiguration} 的 openAiEmbeddingModel 方法
* 可参考 {@link OpenAiEmbedding AutoConfiguration} 的 openAiEmbeddingModel 方法
*/
private OpenAiEmbeddingModel buildOpenAiEmbeddingModel ( String openAiToken , String url , String model ) {
url = StrUtil . blankToDefault ( url , OpenAiApiConstants . DEFAULT_BASE_URL ) ;
@@ -576,21 +609,19 @@ public class AiModelFactoryImpl implements AiModelFactory {
return new OpenAiEmbeddingModel ( openAiApi , MetadataMode . EMBED , openAiEmbeddingProperties ) ;
}
// TODO @芋艿:手头暂时没密钥,使用建议再测试下
/**
* 可参考 {@link AzureOpenAiAutoConfiguration} 的 azureOpenAiEmbeddingModel 方法
* 可参考 {@link AzureOpenAiEmbedding AutoConfiguration} 的 azureOpenAiEmbeddingModel 方法
*/
private AzureOpenAiEmbeddingModel buildAzureOpenAiEmbeddingModel ( String apiKey , String url , String model ) {
AzureOpenAiAutoConfiguration azureOpenAiAutoConfiguration = new AzureOpenAiAutoConfiguration ( ) ;
// 创建 OpenAIClient 对象
AzureOpenAiConnectionProperties connectionProperties = new AzureOpenAiConnectionProperties ( ) ;
connectionProperties . setApiKey ( apiKey ) ;
connectionProperties . setEndpoint ( url ) ;
OpenAIClientBuilder openAIClient = azureOpenAiAutoConfiguration . openAIClientBuilder ( connectionProperties , null ) ;
// TODO @芋艿:手头暂时没密钥,使用建议再测试下
AzureOpenAiEmbeddingAutoConfiguration azureOpenAiAutoConfiguration = new AzureOpenAiEmbeddingAutoConfiguration ( ) ;
// 创建 OpenAIClientBuilder 对象
OpenAIClientBuilder openAIClientBuilder = new OpenAIClientBuilder ( )
. endpoint ( url ) . credential ( new KeyCredential ( apiKey ) ) ;
// 获取 AzureOpenAiChatProperties 对象
AzureOpenAiEmbeddingProperties embeddingProperties = SpringUtil . getBean ( AzureOpenAiEmbeddingProperties . class ) ;
return azureOpenAiAutoConfiguration . azureOpenAiEmbeddingModel ( openAIClient , embeddingProperties ,
null , null ) ;
return azureOpenAiAutoConfiguration . azureOpenAiEmbeddingModel ( openAIClientBuilder , embeddingProperties ,
getObservationRegistry ( ) , getEmbeddingModelObservationConvention ( ) ) ;
}
// ========== 各种创建 VectorStore 的方法 ==========
@@ -657,10 +688,11 @@ public class AiModelFactoryImpl implements AiModelFactory {
RedisProperties redisProperties = SpringUtils . getBean ( RedisProperties . class ) ;
JedisPooled jedisPooled = new JedisPooled ( redisProperties . getHost ( ) , redisProperties . getPort ( ) ) ;
// 创建 RedisVectorStoreProperties 对象
// TODO @芋艿: index-name 可能影响索引名;
RedisVectorStoreAutoConfiguration configuration = new RedisVectorStoreAutoConfiguration ( ) ;
RedisVectorStoreProperties properties = SpringUtil . getBean ( RedisVectorStoreProperties . class ) ;
RedisVectorStore redisVectorStore = RedisVectorStore . builder ( jedisPooled , embeddingModel )
. indexName ( properties . getIndex ( ) ) . prefix ( properties . getPrefix ( ) )
. indexName ( properties . getIndexName ( ) ) . prefix ( properties . getPrefix ( ) )
. initializeSchema ( properties . isInitializeSchema ( ) )
. metadataFields ( convertList ( metadataFields . entrySet ( ) , entry - > {
String fieldName = entry . getKey ( ) ;
@@ -730,10 +762,12 @@ public class AiModelFactoryImpl implements AiModelFactory {
private static ObjectProvider < VectorStoreObservationConvention > getCustomObservationConvention ( ) {
return new ObjectProvider < > ( ) {
@Override
public VectorStoreObservationConvention getObject ( ) throws BeansException {
return new DefaultVectorStoreObservationConvention ( ) ;
}
} ;
}
@@ -745,8 +779,15 @@ public class AiModelFactoryImpl implements AiModelFactory {
return SpringUtil . getBean ( ToolCallingManager . class ) ;
}
private static FunctionCallbackResolver getFunctionCallbackResolver ( ) {
return SpringUtil . getBean ( FunctionCallbackResolver . class ) ;
private static ObjectProvider < EmbeddingModelObservationConvention > getEmbeddingModelObservationConvention ( ) {
return new ObjectProvider < > ( ) {
@Override
public EmbeddingModelObservationConvention getObject ( ) throws BeansException {
return SpringUtil . getBean ( EmbeddingModelObservationConvention . class ) ;
}
} ;
}
}