mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2025-01-31 09:30:05 +08:00
增加yudao ai client
This commit is contained in:
parent
652a8f9633
commit
97df2755f9
@ -36,6 +36,8 @@ public class QianWenChatClient implements ChatClient, StreamingChatClient {
|
|||||||
|
|
||||||
private QianWenOptions qianWenOptions;
|
private QianWenOptions qianWenOptions;
|
||||||
|
|
||||||
|
|
||||||
|
public QianWenChatClient() {}
|
||||||
public QianWenChatClient(QianWenApi qianWenApi) {
|
public QianWenChatClient(QianWenApi qianWenApi) {
|
||||||
this.qianWenApi = qianWenApi;
|
this.qianWenApi = qianWenApi;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,19 @@
|
|||||||
|
package cn.iocoder.yudao.framework.ai.config;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.ai.chat.ChatResponse;
|
||||||
|
import cn.iocoder.yudao.framework.ai.chat.prompt.Prompt;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ai client传入
|
||||||
|
*
|
||||||
|
* @author fansili
|
||||||
|
* @time 2024/4/14 10:27
|
||||||
|
* @since 1.0
|
||||||
|
*/
|
||||||
|
public interface AiClient {
|
||||||
|
|
||||||
|
ChatResponse call(Prompt prompt, String clientName);
|
||||||
|
|
||||||
|
Flux<ChatResponse> stream(Prompt prompt, String clientName);
|
||||||
|
}
|
@ -14,7 +14,6 @@ import cn.iocoder.yudao.framework.ai.chatyiyan.api.YiYanApi;
|
|||||||
import cn.iocoder.yudao.framework.ai.exception.AiException;
|
import cn.iocoder.yudao.framework.ai.exception.AiException;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
@ -23,6 +22,7 @@ import org.springframework.context.ApplicationContextAware;
|
|||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.support.GenericApplicationContext;
|
import org.springframework.context.support.GenericApplicationContext;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -36,11 +36,33 @@ import java.util.Map;
|
|||||||
@EnableConfigurationProperties(YudaoAiProperties.class)
|
@EnableConfigurationProperties(YudaoAiProperties.class)
|
||||||
public class YudaoAiAutoConfiguration {
|
public class YudaoAiAutoConfiguration {
|
||||||
|
|
||||||
// TODO @芋艿:我看sharding jdbc 差不多这么玩的
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean(value = InitChatClient.class)
|
@ConditionalOnMissingBean(value = AiClient.class)
|
||||||
public InitChatClient initChatClient(YudaoAiProperties yudaoAiProperties) {
|
public AiClient aiClient(YudaoAiProperties yudaoAiProperties) {
|
||||||
return new InitChatClient(yudaoAiProperties);
|
Map<String, Object> chatClientMap = buildChatClientMap(yudaoAiProperties);
|
||||||
|
return new YudaoAiClient(chatClientMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Object> buildChatClientMap(YudaoAiProperties yudaoAiProperties) {
|
||||||
|
Map<String, Object> chatMap = new HashMap<>();
|
||||||
|
for (Map.Entry<String, Map<String, Object>> properties : yudaoAiProperties.entrySet()) {
|
||||||
|
String beanName = properties.getKey();
|
||||||
|
Map<String, Object> aiPlatformMap = properties.getValue();
|
||||||
|
|
||||||
|
// 检查平台类型是否正确
|
||||||
|
String aiPlatform = String.valueOf(aiPlatformMap.get("aiPlatform"));
|
||||||
|
if (!AiPlatformEnum.mapValues.containsKey(aiPlatform)) {
|
||||||
|
throw new AiException("AI平台名称错误! 可以参考 AiPlatformEnum 类!");
|
||||||
|
}
|
||||||
|
// 获取平台类型
|
||||||
|
AiPlatformEnum aiPlatformEnum = AiPlatformEnum.mapValues.get(aiPlatform);
|
||||||
|
// 获取 chat properties
|
||||||
|
YudaoAiProperties.ChatProperties chatProperties = getChatProperties(aiPlatformEnum, aiPlatformMap);
|
||||||
|
// 创建客户端
|
||||||
|
Object chatClient = createChatClient(chatProperties);
|
||||||
|
chatMap.put(beanName, chatClient);
|
||||||
|
}
|
||||||
|
return chatMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class InitChatClient implements InitializingBean, ApplicationContextAware {
|
public static class InitChatClient implements InitializingBean, ApplicationContextAware {
|
||||||
@ -53,26 +75,8 @@ public class YudaoAiAutoConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() throws Exception {
|
public void afterPropertiesSet() {
|
||||||
for (Map.Entry<String, Map<String, Object>> properties : yudaoAiProperties.entrySet()) {
|
|
||||||
String beanName = properties.getKey();
|
|
||||||
Map<String, Object> aiPlatformMap = properties.getValue();
|
|
||||||
|
|
||||||
// 检查平台类型是否正确
|
|
||||||
String aiPlatform = String.valueOf(aiPlatformMap.get("aiPlatform"));
|
|
||||||
if (!AiPlatformEnum.mapValues.containsKey(aiPlatform)) {
|
|
||||||
throw new AiException("AI平台名称错误! 可以参考 AiPlatformEnum 类!");
|
|
||||||
}
|
|
||||||
// 获取平台类型
|
|
||||||
AiPlatformEnum aiPlatformEnum = AiPlatformEnum.mapValues.get(aiPlatform);
|
|
||||||
// 获取 chat properties
|
|
||||||
YudaoAiProperties.ChatProperties chatProperties = getChatProperties(aiPlatformEnum, aiPlatformMap);
|
|
||||||
// 创建客户端
|
|
||||||
registerChatClient(applicationContext, chatProperties, beanName);
|
|
||||||
// applicationContext.refresh();
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
System.err.println(applicationContext.getBean("qianWen"));
|
System.err.println(applicationContext.getBean("qianWen"));
|
||||||
System.err.println(applicationContext.getBean("yiYan"));
|
System.err.println(applicationContext.getBean("yiYan"));
|
||||||
@ -84,53 +88,41 @@ public class YudaoAiAutoConfiguration {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void registerChatClient(GenericApplicationContext applicationContext, YudaoAiProperties.ChatProperties chatProperties, String beanName) {
|
private static Object createChatClient(YudaoAiProperties.ChatProperties chatProperties) {
|
||||||
ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
|
|
||||||
Object wrapperBean = null;
|
|
||||||
if (AiPlatformEnum.XING_HUO == chatProperties.getAiPlatform()) {
|
if (AiPlatformEnum.XING_HUO == chatProperties.getAiPlatform()) {
|
||||||
YudaoAiProperties.XingHuoProperties xingHuoProperties = (YudaoAiProperties.XingHuoProperties) chatProperties;
|
YudaoAiProperties.XingHuoProperties xingHuoProperties = (YudaoAiProperties.XingHuoProperties) chatProperties;
|
||||||
wrapperBean = beanFactory.initializeBean(
|
return new XingHuoChatClient(
|
||||||
new XingHuoChatClient(
|
new XingHuoApi(
|
||||||
new XingHuoApi(
|
xingHuoProperties.getAppId(),
|
||||||
xingHuoProperties.getAppId(),
|
xingHuoProperties.getAppKey(),
|
||||||
xingHuoProperties.getAppKey(),
|
xingHuoProperties.getSecretKey()
|
||||||
xingHuoProperties.getSecretKey()
|
|
||||||
),
|
|
||||||
new XingHuoOptions().setChatModel(xingHuoProperties.getChatModel())
|
|
||||||
),
|
),
|
||||||
beanName
|
new XingHuoOptions().setChatModel(xingHuoProperties.getChatModel())
|
||||||
);
|
);
|
||||||
} else if (AiPlatformEnum.QIAN_WEN == chatProperties.getAiPlatform()) {
|
} else if (AiPlatformEnum.QIAN_WEN == chatProperties.getAiPlatform()) {
|
||||||
YudaoAiProperties.QianWenProperties qianWenProperties = (YudaoAiProperties.QianWenProperties) chatProperties;
|
YudaoAiProperties.QianWenProperties qianWenProperties = (YudaoAiProperties.QianWenProperties) chatProperties;
|
||||||
wrapperBean = beanFactory.initializeBean(new QianWenChatClient(
|
return new QianWenChatClient(
|
||||||
new QianWenApi(
|
new QianWenApi(
|
||||||
qianWenProperties.getAccessKeyId(),
|
qianWenProperties.getAccessKeyId(),
|
||||||
qianWenProperties.getAccessKeySecret(),
|
qianWenProperties.getAccessKeySecret(),
|
||||||
qianWenProperties.getAgentKey(),
|
qianWenProperties.getAgentKey(),
|
||||||
qianWenProperties.getEndpoint()
|
qianWenProperties.getEndpoint()
|
||||||
),
|
|
||||||
new QianWenOptions()
|
|
||||||
.setAppId(qianWenProperties.getAppId())
|
|
||||||
),
|
),
|
||||||
beanName
|
new QianWenOptions()
|
||||||
|
.setAppId(qianWenProperties.getAppId())
|
||||||
);
|
);
|
||||||
} else if (AiPlatformEnum.YI_YAN == chatProperties.getAiPlatform()) {
|
} else if (AiPlatformEnum.YI_YAN == chatProperties.getAiPlatform()) {
|
||||||
YudaoAiProperties.YiYanProperties yiYanProperties = (YudaoAiProperties.YiYanProperties) chatProperties;
|
YudaoAiProperties.YiYanProperties yiYanProperties = (YudaoAiProperties.YiYanProperties) chatProperties;
|
||||||
|
return new YiYanChatClient(
|
||||||
wrapperBean = beanFactory.initializeBean(new YiYanChatClient(
|
new YiYanApi(
|
||||||
new YiYanApi(
|
yiYanProperties.getAppKey(),
|
||||||
yiYanProperties.getAppKey(),
|
yiYanProperties.getSecretKey(),
|
||||||
yiYanProperties.getSecretKey(),
|
yiYanProperties.getChatModel(),
|
||||||
yiYanProperties.getChatModel(),
|
yiYanProperties.getRefreshTokenSecondTime()
|
||||||
yiYanProperties.getRefreshTokenSecondTime()
|
),
|
||||||
),
|
new YiYanOptions().setMax_output_tokens(2048));
|
||||||
new YiYanOptions().setMax_output_tokens(2048)),
|
|
||||||
beanName
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (wrapperBean != null) {
|
|
||||||
beanFactory.registerSingleton(beanName, wrapperBean);
|
|
||||||
}
|
}
|
||||||
|
throw new AiException("不支持的Ai类型!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
package cn.iocoder.yudao.framework.ai.config;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.ai.chat.ChatClient;
|
||||||
|
import cn.iocoder.yudao.framework.ai.chat.ChatResponse;
|
||||||
|
import cn.iocoder.yudao.framework.ai.chat.StreamingChatClient;
|
||||||
|
import cn.iocoder.yudao.framework.ai.chat.prompt.Prompt;
|
||||||
|
import cn.iocoder.yudao.framework.ai.exception.AiException;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* yudao ai client
|
||||||
|
*
|
||||||
|
* @author fansili
|
||||||
|
* @time 2024/4/14 10:27
|
||||||
|
* @since 1.0
|
||||||
|
*/
|
||||||
|
public class YudaoAiClient implements AiClient{
|
||||||
|
|
||||||
|
protected Map<String, Object> chatClientMap;
|
||||||
|
|
||||||
|
public YudaoAiClient(Map<String, Object> chatClientMap) {
|
||||||
|
this.chatClientMap = chatClientMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChatResponse call(Prompt prompt, String clientName) {
|
||||||
|
if (!chatClientMap.containsKey(clientName)) {
|
||||||
|
throw new AiException("clientName不存在!");
|
||||||
|
}
|
||||||
|
ChatClient chatClient = (ChatClient) chatClientMap.get(clientName);
|
||||||
|
return chatClient.call(prompt);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Flux<ChatResponse> stream(Prompt prompt, String clientName) {
|
||||||
|
if (!chatClientMap.containsKey(clientName)) {
|
||||||
|
throw new AiException("clientName不存在!");
|
||||||
|
}
|
||||||
|
StreamingChatClient streamingChatClient = (StreamingChatClient) chatClientMap.get(clientName);
|
||||||
|
return streamingChatClient.stream(prompt);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user