mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-12 10:05:07 +08:00
【新增】AI:通过 AiClientFactory 提供 chatclient
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
package cn.iocoder.yudao.framework.ai.config;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.iocoder.yudao.framework.ai.core.factory.AiClientFactory;
|
||||
import cn.iocoder.yudao.framework.ai.core.factory.AiClientFactoryImpl;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenChatClient;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenChatModal;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenOptions;
|
||||
@ -36,17 +38,22 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ai 自动配置
|
||||
* 芋道 AI 自动配置
|
||||
*
|
||||
* @author fansili
|
||||
* @time 2024/4/12 16:29
|
||||
* @since 1.0
|
||||
*/
|
||||
@Slf4j
|
||||
@AutoConfiguration
|
||||
@EnableConfigurationProperties(YudaoAiProperties.class)
|
||||
@Slf4j
|
||||
public class YudaoAiAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public AiClientFactory aiClientFactory() {
|
||||
return new AiClientFactoryImpl();
|
||||
}
|
||||
|
||||
// ========== 各种 AI Client 创建 ==========
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(value = "yudao.ai.xinghuo.enable", havingValue = "true")
|
||||
public XingHuoChatClient xingHuoChatClient(YudaoAiProperties yudaoAiProperties) {
|
||||
@ -107,21 +114,6 @@ public class YudaoAiAutoConfiguration {
|
||||
);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(value = "yudao.ai.openAiImage.enable", havingValue = "true")
|
||||
public OpenAiImageClient openAiImageClient(YudaoAiProperties yudaoAiProperties) {
|
||||
YudaoAiProperties.OpenAiImageProperties openAiImageProperties = yudaoAiProperties.getOpenAiImage();
|
||||
OpenAiImageOptions openAiImageOptions = new OpenAiImageOptions();
|
||||
openAiImageOptions.setModel(openAiImageProperties.getModel().getModel());
|
||||
openAiImageOptions.setStyle(openAiImageProperties.getStyle().getStyle());
|
||||
openAiImageOptions.setResponseFormat("url"); // TODO 芋艿:OpenAiImageOptions.ResponseFormatEnum.URL.getValue()
|
||||
// 创建 client
|
||||
return new OpenAiImageClient(
|
||||
new OpenAiImageApi(openAiImageProperties.getApiKey()),
|
||||
openAiImageOptions,
|
||||
RetryUtils.DEFAULT_RETRY_TEMPLATE);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(value = MidjourneyMessageHandler.class)
|
||||
public MidjourneyMessageHandler defaultMidjourneyMessageHandler() {
|
||||
|
@ -0,0 +1,47 @@
|
||||
package cn.iocoder.yudao.framework.ai.core.factory;
|
||||
|
||||
import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum;
|
||||
import org.springframework.ai.chat.StreamingChatClient;
|
||||
import org.springframework.ai.chat.prompt.ChatOptions;
|
||||
|
||||
/**
|
||||
* AI 客户端工厂的接口类
|
||||
*
|
||||
* @author fansili
|
||||
*/
|
||||
public interface AiClientFactory {
|
||||
|
||||
/**
|
||||
* 基于指定配置,获得 StreamingChatClient 对象
|
||||
*
|
||||
* 如果不存在,则进行创建
|
||||
*
|
||||
* @param platform 平台
|
||||
* @param apiKey API KEY
|
||||
* @param url API URL
|
||||
* @return StreamingChatClient 对象
|
||||
*/
|
||||
StreamingChatClient getOrCreateStreamingChatClient(AiPlatformEnum platform, String apiKey, String url);
|
||||
|
||||
/**
|
||||
* 基于默认配置,获得 StreamingChatClient 对象
|
||||
*
|
||||
* 默认配置,指的是在 application.yaml 配置文件中的 spring.ai 相关的配置
|
||||
*
|
||||
* @param platform 平台
|
||||
* @return StreamingChatClient 对象
|
||||
*/
|
||||
StreamingChatClient getDefaultStreamingChatClient(AiPlatformEnum platform);
|
||||
|
||||
/**
|
||||
* 创建 Chat 参数
|
||||
*
|
||||
* @param platform 平台
|
||||
* @param model 模型
|
||||
* @param temperature 温度
|
||||
* @param maxTokens 生成的最大 Token
|
||||
* @return Chat 参数
|
||||
*/
|
||||
ChatOptions buildChatOptions(AiPlatformEnum platform, String model, Double temperature, Integer maxTokens);
|
||||
|
||||
}
|
@ -0,0 +1,167 @@
|
||||
package cn.iocoder.yudao.framework.ai.core.factory;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.lang.Singleton;
|
||||
import cn.hutool.core.lang.func.Func0;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.iocoder.yudao.framework.ai.config.YudaoAiAutoConfiguration;
|
||||
import cn.iocoder.yudao.framework.ai.config.YudaoAiProperties;
|
||||
import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenChatClient;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenChatModal;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenOptions;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.tongyi.api.QianWenApi;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoChatClient;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoChatModel;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoOptions;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.xinghuo.api.XingHuoApi;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.yiyan.YiYanChatClient;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.yiyan.YiYanChatOptions;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanApi;
|
||||
import org.springframework.ai.autoconfigure.ollama.OllamaAutoConfiguration;
|
||||
import org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration;
|
||||
import org.springframework.ai.chat.StreamingChatClient;
|
||||
import org.springframework.ai.chat.prompt.ChatOptions;
|
||||
import org.springframework.ai.ollama.OllamaChatClient;
|
||||
import org.springframework.ai.ollama.api.OllamaApi;
|
||||
import org.springframework.ai.ollama.api.OllamaOptions;
|
||||
import org.springframework.ai.openai.OpenAiChatClient;
|
||||
import org.springframework.ai.openai.OpenAiChatOptions;
|
||||
import org.springframework.ai.openai.api.ApiUtils;
|
||||
import org.springframework.ai.openai.api.OpenAiApi;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* AI 客户端工厂的实现类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public class AiClientFactoryImpl implements AiClientFactory {
|
||||
|
||||
@Override
|
||||
public StreamingChatClient getOrCreateStreamingChatClient(AiPlatformEnum platform, String apiKey, String url) {
|
||||
String cacheKey = buildClientCacheKey(StreamingChatClient.class, platform, apiKey, url);
|
||||
return Singleton.get(cacheKey, (Func0<StreamingChatClient>) () -> {
|
||||
//noinspection EnhancedSwitchMigration
|
||||
switch (platform) {
|
||||
case OPENAI:
|
||||
return buildOpenAiChatClient(apiKey, url);
|
||||
case OLLAMA:
|
||||
return buildOllamaChatClient(url);
|
||||
case YI_YAN:
|
||||
return buildYiYanChatClient(apiKey);
|
||||
case XING_HUO:
|
||||
return buildXingHuoChatClient(apiKey);
|
||||
case QIAN_WEN:
|
||||
return buildQianWenChatClient(apiKey);
|
||||
default:
|
||||
throw new IllegalArgumentException(StrUtil.format("未知平台({})", platform));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public StreamingChatClient getDefaultStreamingChatClient(AiPlatformEnum platform) {
|
||||
//noinspection EnhancedSwitchMigration
|
||||
switch (platform) {
|
||||
case OPENAI:
|
||||
return SpringUtil.getBean(OpenAiChatClient.class);
|
||||
case OLLAMA:
|
||||
return SpringUtil.getBean(OllamaChatClient.class);
|
||||
case YI_YAN:
|
||||
return SpringUtil.getBean(YiYanChatClient.class);
|
||||
case XING_HUO:
|
||||
return SpringUtil.getBean(XingHuoChatClient.class);
|
||||
case QIAN_WEN:
|
||||
return SpringUtil.getBean(QianWenChatClient.class);
|
||||
default:
|
||||
throw new IllegalArgumentException(StrUtil.format("未知平台({})", platform));
|
||||
}
|
||||
}
|
||||
|
||||
private static String buildClientCacheKey(Class<?> clazz, Object... params) {
|
||||
if (ArrayUtil.isEmpty(params)) {
|
||||
return clazz.getName();
|
||||
}
|
||||
return StrUtil.format("{}#{}", clazz.getName(), ArrayUtil.join(params, "_"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatOptions buildChatOptions(AiPlatformEnum platform, String model, Double temperature, Integer maxTokens) {
|
||||
Float temperatureF = temperature != null ? temperature.floatValue() : null;
|
||||
//noinspection EnhancedSwitchMigration
|
||||
switch (platform) {
|
||||
case OPENAI:
|
||||
return OpenAiChatOptions.builder().withModel(model).withTemperature(temperatureF).withMaxTokens(maxTokens).build();
|
||||
case OLLAMA:
|
||||
return OllamaOptions.create().withModel(model).withTemperature(temperatureF).withNumPredict(maxTokens);
|
||||
case YI_YAN:
|
||||
// TODO @fan:增加一个 model
|
||||
return new YiYanChatOptions().setTemperature(temperatureF).setMaxOutputTokens(maxTokens);
|
||||
case XING_HUO:
|
||||
return new XingHuoOptions().setChatModel(XingHuoChatModel.valueOfModel(model)).setTemperature(temperatureF)
|
||||
.setMaxTokens(maxTokens);
|
||||
case QIAN_WEN:
|
||||
// TODO @fan:增加 model、temperature 参数
|
||||
return new QianWenOptions().setMaxTokens(maxTokens);
|
||||
default:
|
||||
throw new IllegalArgumentException(StrUtil.format("未知平台({})", platform));
|
||||
}
|
||||
}
|
||||
|
||||
// ========== 各种创建 spring-ai 客户端的方法 ==========
|
||||
|
||||
/**
|
||||
* 可参考 {@link OpenAiAutoConfiguration}
|
||||
*/
|
||||
private static OpenAiChatClient buildOpenAiChatClient(String openAiToken, String url) {
|
||||
url = StrUtil.blankToDefault(url, ApiUtils.DEFAULT_BASE_URL);
|
||||
OpenAiApi openAiApi = new OpenAiApi(url, openAiToken);
|
||||
return new OpenAiChatClient(openAiApi);
|
||||
}
|
||||
|
||||
/**
|
||||
* 可参考 {@link OllamaAutoConfiguration}
|
||||
*/
|
||||
private static OllamaChatClient buildOllamaChatClient(String url) {
|
||||
OllamaApi ollamaApi = new OllamaApi(url);
|
||||
return new OllamaChatClient(ollamaApi);
|
||||
}
|
||||
|
||||
/**
|
||||
* 可参考 {@link YudaoAiAutoConfiguration#yiYanChatClient(YudaoAiProperties)}
|
||||
*/
|
||||
private static YiYanChatClient buildYiYanChatClient(String key) {
|
||||
List<String> keys = StrUtil.split(key, '|');
|
||||
Assert.equals(keys.size(), 2, "YiYanChatClient 的密钥需要 (appKey|secretKey) 格式");
|
||||
String appKey = keys.get(0);
|
||||
String secretKey = keys.get(1);
|
||||
YiYanApi yiYanApi = new YiYanApi(appKey, secretKey, YiYanApi.DEFAULT_CHAT_MODEL, 0);
|
||||
return new YiYanChatClient(yiYanApi);
|
||||
}
|
||||
|
||||
/**
|
||||
* 可参考 {@link YudaoAiAutoConfiguration#xingHuoChatClient(YudaoAiProperties)}
|
||||
*/
|
||||
private static XingHuoChatClient buildXingHuoChatClient(String key) {
|
||||
List<String> keys = StrUtil.split(key, '|');
|
||||
Assert.equals(keys.size(), 2, "XingHuoChatClient 的密钥需要 (appKey|secretKey) 格式");
|
||||
String appId = keys.get(0);
|
||||
String appKey = keys.get(1);
|
||||
String secretKey = keys.get(2);
|
||||
XingHuoApi xingHuoApi = new XingHuoApi(appId, appKey, secretKey);
|
||||
return new XingHuoChatClient(xingHuoApi);
|
||||
}
|
||||
|
||||
/**
|
||||
* 可参考 {@link YudaoAiAutoConfiguration#qianWenChatClient(YudaoAiProperties)}
|
||||
*/
|
||||
private static QianWenChatClient buildQianWenChatClient(String key) {
|
||||
QianWenApi qianWenApi = new QianWenApi(key, QianWenChatModal.QWEN_72B_CHAT);
|
||||
return new QianWenChatClient(qianWenApi);
|
||||
}
|
||||
|
||||
}
|
@ -6,6 +6,8 @@ import lombok.experimental.Accessors;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
// TODO @fan:增加一个 model 参数
|
||||
// TODO @fan:增加一个 Temperature 参数
|
||||
/**
|
||||
* 阿里云 千问 属性
|
||||
*
|
||||
|
@ -14,6 +14,7 @@ import lombok.experimental.Accessors;
|
||||
@Accessors(chain = true)
|
||||
public class XingHuoOptions implements ChatOptions {
|
||||
|
||||
// TODO @fan:这里 model 参数,然后使用 string
|
||||
/**
|
||||
* https://www.xfyun.cn/doc/spark/Web.html#_1-%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
|
||||
* <p>
|
||||
@ -43,7 +44,6 @@ public class XingHuoOptions implements ChatOptions {
|
||||
*/
|
||||
private String chatId;
|
||||
|
||||
|
||||
@Override
|
||||
public Float getTemperature() {
|
||||
return this.temperature;
|
||||
|
@ -6,6 +6,7 @@ import org.springframework.ai.chat.prompt.ChatOptions;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
// TODO @fan:增加一个 model
|
||||
// TODO @fan:字段命名,penalty_score 类似的,建议改成驼峰原则
|
||||
// TODO @fan:字段的注释,可以都删除掉,让用户 https://cloud.baidu.com/doc/WENXINWORKSHOP/s/clntwmv7t 即可
|
||||
/**
|
||||
|
@ -18,7 +18,7 @@ public class YiYanApi {
|
||||
|
||||
private static final String AUTH_2_TOKEN_URI = "/oauth/2.0/token";
|
||||
|
||||
public static final String DEFAULT_CHAT_MODEL = YiYanChatModel.ERNIE4_0.getModel();
|
||||
public static final YiYanChatModel DEFAULT_CHAT_MODEL = YiYanChatModel.ERNIE4_0;
|
||||
|
||||
private final String appKey;
|
||||
private final String secretKey;
|
||||
@ -39,6 +39,7 @@ public class YiYanApi {
|
||||
*/
|
||||
private final YiYanChatModel useChatModel;
|
||||
|
||||
// TODO fan:看看是不是去掉 refreshTokenSecondTime 字段
|
||||
public YiYanApi(String appKey, String secretKey, YiYanChatModel useChatModel, int refreshTokenSecondTime) {
|
||||
this.appKey = appKey;
|
||||
this.secretKey = secretKey;
|
||||
|
Reference in New Issue
Block a user