From 04021ce0682c9eea39cb2c06ac081d3c5c615db6 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 18 May 2024 09:34:17 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BB=A3=E7=A0=81=E8=AF=84=E5=AE=A1?= =?UTF-8?q?=E3=80=91AI=EF=BC=9A=E6=96=87=E5=BF=83=E4=B8=80=E8=A8=80?= =?UTF-8?q?=E7=9A=84=E6=8E=A5=E5=85=A5=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/ai/config/AiChatClientFactory.java | 6 +- .../AiChatConversationRespVO.java | 8 +- .../module/ai/dal/vo/AiChatModalConfigVO.java | 2 +- .../ai/service/impl/AiChatServiceImpl.java | 6 +- .../service/model/AiChatRoleServiceImpl.java | 5 +- .../yudao-spring-boot-starter-ai/pom.xml | 19 +-- .../ai/config/YudaoAiAutoConfiguration.java | 8 +- .../ai/config/YudaoAiImageProperties.java | 2 +- .../ai/config/YudaoAiProperties.java | 2 +- .../ai/core/enums/AiPlatformEnum.java | 2 +- .../framework/ai/core/model/package-info.java | 12 ++ .../ai/core/model/yiyan/YiYanChatClient.java | 154 +++++++++++++++++ .../core/model/yiyan/YiYanChatOptions.java} | 42 ++--- .../ai/core/model}/yiyan/api/YiYanApi.java | 75 +++++---- .../model/yiyan/api/YiYanAuthResponse.java} | 10 +- .../yiyan/api/YiYanChatCompletionRequest.java | 12 +- .../api/YiYanChatCompletionResponse.java} | 11 +- .../core/model/yiyan/api}/YiYanChatModel.java | 28 ++-- .../yiyan/exception/YiYanApiException.java | 2 +- .../ai/models/tongyi/QianWenChatClient.java | 2 +- .../ai/models/yiyan/YiYanChatClient.java | 158 ------------------ .../yiyan/api/YiYanChatCompletionMessage.java | 8 - .../framework/ai/chat/YiYanChatTests.java | 10 +- .../src/main/resources/application-local.yaml | 11 -- .../src/main/resources/application.yaml | 13 ++ 25 files changed, 300 insertions(+), 308 deletions(-) create mode 100644 yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/package-info.java create mode 100644 yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/YiYanChatClient.java rename yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/{org/springframework/ai/models/yiyan/YiYanOptions.java => cn/iocoder/yudao/framework/ai/core/model/yiyan/YiYanChatOptions.java} (82%) rename yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/{org/springframework/ai/models => cn/iocoder/yudao/framework/ai/core/model}/yiyan/api/YiYanApi.java (57%) rename yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/{org/springframework/ai/models/yiyan/api/YiYanAuthRes.java => cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanAuthResponse.java} (78%) rename yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/{org/springframework/ai/models => cn/iocoder/yudao/framework/ai/core/model}/yiyan/api/YiYanChatCompletionRequest.java (95%) rename yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/{org/springframework/ai/models/yiyan/api/YiYanChatCompletion.java => cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanChatCompletionResponse.java} (93%) rename yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/{org/springframework/ai/models/yiyan => cn/iocoder/yudao/framework/ai/core/model/yiyan/api}/YiYanChatModel.java (65%) rename yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/{org/springframework/ai/models => cn/iocoder/yudao/framework/ai/core/model}/yiyan/exception/YiYanApiException.java (79%) delete mode 100644 yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/YiYanChatClient.java delete mode 100644 yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/api/YiYanChatCompletionMessage.java diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/config/AiChatClientFactory.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/config/AiChatClientFactory.java index f93cdc9c7..d5863dbc2 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/config/AiChatClientFactory.java +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/config/AiChatClientFactory.java @@ -5,7 +5,7 @@ import org.springframework.ai.chat.ChatClient; import org.springframework.ai.chat.StreamingChatClient; import org.springframework.ai.models.tongyi.QianWenChatClient; import org.springframework.ai.models.xinghuo.XingHuoChatClient; -import org.springframework.ai.models.yiyan.YiYanChatClient; +import cn.iocoder.yudao.framework.ai.core.model.yiyan.YiYanChatClient; import org.springframework.ai.ollama.OllamaChatClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; @@ -27,7 +27,7 @@ public class AiChatClientFactory { public ChatClient getChatClient(AiPlatformEnum platformEnum) { if (AiPlatformEnum.QIAN_WEN == platformEnum) { return applicationContext.getBean(QianWenChatClient.class); - } else if (AiPlatformEnum.YI_YAN == platformEnum) { + } else if (AiPlatformEnum.YIYAN == platformEnum) { return applicationContext.getBean(YiYanChatClient.class); } else if (AiPlatformEnum.XING_HUO == platformEnum) { return applicationContext.getBean(XingHuoChatClient.class); @@ -42,7 +42,7 @@ public class AiChatClientFactory { // } if (AiPlatformEnum.QIAN_WEN == platformEnum) { return applicationContext.getBean(QianWenChatClient.class); - } else if (AiPlatformEnum.YI_YAN == platformEnum) { + } else if (AiPlatformEnum.YIYAN == platformEnum) { return applicationContext.getBean(YiYanChatClient.class); } else if (AiPlatformEnum.XING_HUO == platformEnum) { return applicationContext.getBean(XingHuoChatClient.class); diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/chat/vo/conversation/AiChatConversationRespVO.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/chat/vo/conversation/AiChatConversationRespVO.java index 78cd71365..e670ef9f9 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/chat/vo/conversation/AiChatConversationRespVO.java +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/chat/vo/conversation/AiChatConversationRespVO.java @@ -1,20 +1,14 @@ package cn.iocoder.yudao.module.ai.controller.admin.chat.vo.conversation; -import cn.iocoder.yudao.module.ai.controller.admin.model.vo.chatModel.AiChatModelRespVO; import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatModelDO; import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatRoleDO; -import com.baomidou.mybatisplus.annotation.FieldFill; -import com.baomidou.mybatisplus.annotation.TableField; import com.fhs.core.trans.anno.Trans; import com.fhs.core.trans.constant.TransType; import com.fhs.core.trans.vo.VO; import io.swagger.v3.oas.annotations.media.Schema; -import jakarta.validation.constraints.NotNull; import lombok.Data; -import lombok.experimental.Accessors; import java.time.LocalDateTime; -import java.time.LocalTime; @Schema(description = "管理后台 - AI 聊天会话 Response VO") @Data @@ -58,7 +52,7 @@ public class AiChatConversationRespVO implements VO { @Schema(description = "上下文的最大 Message 数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "10") private Integer maxContexts; - @Schema(description = "最后更新时间", requiredMode = Schema.RequiredMode.REQUIRED, example = "2024-05-16") + @Schema(description = "最后更新时间", requiredMode = Schema.RequiredMode.REQUIRED) private LocalDateTime updateTime; // ========== 关联 role 信息 ========== diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/dal/vo/AiChatModalConfigVO.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/dal/vo/AiChatModalConfigVO.java index ed5ecf217..723e5338f 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/dal/vo/AiChatModalConfigVO.java +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/dal/vo/AiChatModalConfigVO.java @@ -4,7 +4,7 @@ import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum; import lombok.Data; import lombok.experimental.Accessors; import org.springframework.ai.models.xinghuo.XingHuoChatModel; -import org.springframework.ai.models.yiyan.YiYanChatModel; +import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanChatModel; /** * modal config diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiChatServiceImpl.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiChatServiceImpl.java index 335c20789..b56b16648 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiChatServiceImpl.java +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiChatServiceImpl.java @@ -150,9 +150,9 @@ public class AiChatServiceImpl implements AiChatService { // 1.3 user message 新发送消息 chatMessages.add(new UserMessage(sendReqVO.getContent())); - // 2. 构建 ChatOptions 对象 - ChatOptions chatOptions = ChatOptionsBuilder.builder().withTemperature(conversation.getTemperature().floatValue()).build(); - return new Prompt(chatMessages, chatOptions); + // 2. 构建 ChatOptions 对象 TODO 芋艿:临时注释掉;等文心一言兼容了; +// ChatOptions chatOptions = ChatOptionsBuilder.builder().withTemperature(conversation.getTemperature().floatValue()).build(); + return new Prompt(chatMessages, null); } private AiChatMessageDO createChatMessage(Long conversationId, AiChatModelDO model, diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/model/AiChatRoleServiceImpl.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/model/AiChatRoleServiceImpl.java index 4372003d0..cf6bcec96 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/model/AiChatRoleServiceImpl.java +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/model/AiChatRoleServiceImpl.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.module.ai.service.model; import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; @@ -15,6 +16,8 @@ import org.springframework.stereotype.Service; import java.util.List; import java.util.Objects; +import java.util.function.Function; +import java.util.function.Predicate; import java.util.stream.Collectors; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; @@ -134,7 +137,7 @@ public class AiChatRoleServiceImpl implements AiChatRoleService { @Override public List getChatRoleCategoryList() { List list = chatRoleMapper.selectListGroupByCategory(CommonStatusEnum.ENABLE.getStatus()); - return convertList(list.stream().filter(Objects::nonNull).collect(Collectors.toList()), AiChatRoleDO::getCategory); + return convertList(list, AiChatRoleDO::getCategory, role -> StrUtil.isNotBlank(role.getCategory())); } } diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/pom.xml b/yudao-module-ai/yudao-spring-boot-starter-ai/pom.xml index 5efa91126..37813feb7 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/pom.xml +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/pom.xml @@ -10,32 +10,25 @@ yudao-spring-boot-starter-ai - - - io.springboot.ai - spring-ai-core - 1.0.3 - - - io.springboot.ai - spring-ai-openai - 1.0.3 - - io.springboot.ai spring-ai-ollama-spring-boot-starter 1.0.3 + + io.springboot.ai + spring-ai-openai-spring-boot-starter + 1.0.3 + cn.iocoder.boot yudao-common - + com.alibaba dashscope-sdk-java diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/config/YudaoAiAutoConfiguration.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/config/YudaoAiAutoConfiguration.java index b37938d4a..67226ec60 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/config/YudaoAiAutoConfiguration.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/config/YudaoAiAutoConfiguration.java @@ -8,9 +8,9 @@ import org.springframework.ai.models.tongyi.api.QianWenApi; import org.springframework.ai.models.xinghuo.XingHuoChatClient; import org.springframework.ai.models.xinghuo.XingHuoOptions; import org.springframework.ai.models.xinghuo.api.XingHuoApi; -import org.springframework.ai.models.yiyan.YiYanChatClient; -import org.springframework.ai.models.yiyan.YiYanOptions; -import org.springframework.ai.models.yiyan.api.YiYanApi; +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.models.midjourney.MidjourneyConfig; import org.springframework.ai.models.midjourney.MidjourneyMessage; import org.springframework.ai.models.midjourney.api.MidjourneyInteractionsApi; @@ -91,7 +91,7 @@ public class YudaoAiAutoConfiguration { public YiYanChatClient yiYanChatClient(YudaoAiProperties yudaoAiProperties) { YudaoAiProperties.YiYanProperties yiYanProperties = yudaoAiProperties.getYiyan(); // 转换配置 - YiYanOptions yiYanOptions = new YiYanOptions(); + YiYanChatOptions yiYanOptions = new YiYanChatOptions(); // yiYanOptions.setTopK(yiYanProperties.getTopK()); TODO 芋艿:后续弄 yiYanOptions.setTopP(yiYanProperties.getTopP()); yiYanOptions.setTemperature(yiYanProperties.getTemperature()); diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/config/YudaoAiImageProperties.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/config/YudaoAiImageProperties.java index 1f17a1203..1e6d9d9ec 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/config/YudaoAiImageProperties.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/config/YudaoAiImageProperties.java @@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.ai.config; import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum; import org.springframework.ai.models.xinghuo.XingHuoChatModel; import org.springframework.ai.models.xinghuo.XingHuoOptions; -import org.springframework.ai.models.yiyan.YiYanChatModel; +import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanChatModel; import lombok.Data; import lombok.experimental.Accessors; diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/config/YudaoAiProperties.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/config/YudaoAiProperties.java index a228a0045..62bd1318d 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/config/YudaoAiProperties.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/config/YudaoAiProperties.java @@ -2,7 +2,7 @@ package cn.iocoder.yudao.framework.ai.config; import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum; import org.springframework.ai.models.xinghuo.XingHuoChatModel; -import org.springframework.ai.models.yiyan.YiYanChatModel; +import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanChatModel; import cn.iocoder.yudao.framework.ai.core.enums.OpenAiImageModelEnum; import cn.iocoder.yudao.framework.ai.core.enums.OpenAiImageStyleEnum; import lombok.Data; diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/enums/AiPlatformEnum.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/enums/AiPlatformEnum.java index 66228ff22..cd60cc990 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/enums/AiPlatformEnum.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/enums/AiPlatformEnum.java @@ -16,8 +16,8 @@ public enum AiPlatformEnum { OPENAI("OpenAI", "OpenAI"), OLLAMA("Ollama", "Ollama"), + YIYAN("YiYan", "文心一言"), - YI_YAN("yiyan", "一言"), QIAN_WEN("qianwen", "千问"), XING_HUO("xinghuo", "星火"), OPEN_AI_DALL("dall", "dall"), diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/package-info.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/package-info.java new file mode 100644 index 000000000..8a193f7df --- /dev/null +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/package-info.java @@ -0,0 +1,12 @@ +/** + * model 包,接入各种大模型,对标 https://github.com/spring-projects/spring-ai/tree/main/models + * + * 1. yiyan 包:【百度】文心一言 + * 2. TODO 芋艿: + * tongyi 包:【阿里】通义千问,对标 spring-cloud-alibaba 提供的 ai 包 + * 2.2 + * 2.3 xinghuo 包:【讯飞】星火,自己实现 + * 2.4 openai 包:【OpenAI】ChatGPT,拷贝 spring-ai 提供的 models/openai 包 + * 2.5 midjourney 包:Midjourney,参考 https://github.com/novicezk/midjourney-proxy 实现 + */ +package cn.iocoder.yudao.framework.ai.core.model; \ No newline at end of file diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/YiYanChatClient.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/YiYanChatClient.java new file mode 100644 index 000000000..a613af6db --- /dev/null +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/YiYanChatClient.java @@ -0,0 +1,154 @@ +package cn.iocoder.yudao.framework.ai.core.model.yiyan; + +import cn.hutool.core.bean.BeanUtil; +import cn.iocoder.yudao.framework.ai.core.exception.ChatException; +import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanApi; +import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanChatCompletionResponse; +import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanChatCompletionRequest; +import cn.iocoder.yudao.framework.ai.core.model.yiyan.exception.YiYanApiException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.ai.chat.ChatClient; +import org.springframework.ai.chat.ChatResponse; +import org.springframework.ai.chat.Generation; +import org.springframework.ai.chat.StreamingChatClient; +import org.springframework.ai.chat.messages.Message; +import org.springframework.ai.chat.messages.MessageType; +import org.springframework.ai.chat.prompt.ChatOptions; +import org.springframework.ai.chat.prompt.Prompt; +import org.springframework.http.ResponseEntity; +import org.springframework.retry.RetryCallback; +import org.springframework.retry.RetryContext; +import org.springframework.retry.RetryListener; +import org.springframework.retry.support.RetryTemplate; +import org.springframework.util.Assert; +import reactor.core.publisher.Flux; + +import java.time.Duration; +import java.util.List; +import java.util.stream.Collectors; + +/** + * 文心一言的 {@link ChatClient} 实现类 + * + * @author fansili + */ +@Slf4j +public class YiYanChatClient implements ChatClient, StreamingChatClient { + + private final YiYanApi yiYanApi; + + private YiYanChatOptions defaultOptions; + + // TODO @fan:参考 OpenAiChatClient 调整下 retryTemplate;使用 RetryUtils.DEFAULT_RETRY_TEMPLATE;加允许传入? + + public YiYanChatClient(YiYanApi yiYanApi) { + this.yiYanApi = yiYanApi; + // TODO @fan:这个情况,是不是搞个 defaultOptions;OpenAiChatOptions.builder().withModel(OpenAiApi.DEFAULT_CHAT_MODEL).withTemperature(0.7f).build() + } + + public YiYanChatClient(YiYanApi yiYanApi, YiYanChatOptions defaultOptions) { + Assert.notNull(yiYanApi, "OllamaApi must not be null"); + Assert.notNull(defaultOptions, "DefaultOptions must not be null"); + this.yiYanApi = yiYanApi; + this.defaultOptions = defaultOptions; + } + + public final RetryTemplate retryTemplate = RetryTemplate.builder() + .maxAttempts(10) + .retryOn(YiYanApiException.class) + .exponentialBackoff(Duration.ofMillis(3000), 2, Duration.ofMillis(3 * 60000)) + .withListener(new RetryListener() { + + @Override + public void onError(RetryContext context, + RetryCallback callback, Throwable throwable) { + log.warn("重试异常:" + context.getRetryCount(), throwable); + } + + }) + .build(); + + @Override + public ChatResponse call(Prompt prompt) { + YiYanChatCompletionRequest request = createRequest(prompt, false); + return this.retryTemplate.execute(ctx -> { + // 发送请求 + ResponseEntity response = yiYanApi.chatCompletionEntity(request); + // 获取结果封装 ChatResponse + YiYanChatCompletionResponse chatCompletion = response.getBody(); + // TODO @fan:为空时,参考 OpenAiChatClient 的封装; + // TODO @fan:chatResponseMetadata,参考 OpenAiChatResponseMetadata.from(completionEntity.getBody()) + return new ChatResponse(List.of(new Generation(chatCompletion.getResult()))); + }); + } + + @Override + public Flux stream(Prompt prompt) { + YiYanChatCompletionRequest request = this.createRequest(prompt, true); + // TODO @fan:return this.retryTemplate.execute(ctx -> { + // 调用 callWithFunctionSupport 发送请求 + Flux response = this.yiYanApi.chatCompletionStream(request); + // TODO @fan:下面的 doOnComplete 是不是可以删除哈? + response.doOnComplete(new Runnable() { + @Override + public void run() { + String a = ";"; + } + }); + return response.map(chunk -> { + // TODO @fan:ChatResponseMetadata chatResponseMetadata + return new ChatResponse(List.of(new Generation(chunk.getResult()))); + }); + } + + private YiYanChatCompletionRequest createRequest(Prompt prompt, boolean stream) { + // 参考 https://cloud.baidu.com/doc/WENXINWORKSHOP/s/clntwmv7t 文档,system 是独立字段 + // 1.1 获取 user 和 assistant + List messageList = prompt.getInstructions().stream() + // 过滤 system + .filter(msg -> MessageType.SYSTEM != msg.getMessageType()) + .map(message -> new YiYanChatCompletionRequest.Message() + .setRole(message.getMessageType().getValue()).setContent(message.getContent()) + ).toList(); + // 1.2 获取 system + String systemPrompt = prompt.getInstructions().stream() + .filter(message -> MessageType.SYSTEM == message.getMessageType()) + .map(Message::getContent) + .collect(Collectors.joining()); + + // 3. 创建 request + YiYanChatCompletionRequest request = new YiYanChatCompletionRequest(messageList); + // 复制 YiYanOptions 属性,到 request 中(这里 options 属性和 request 基本保持一致) + YiYanChatOptions useOptions = getYiYanOptions(prompt); + BeanUtil.copyProperties(useOptions, request); + request.setTop_p(useOptions.getTopP()) + .setMax_output_tokens(useOptions.getMaxOutputTokens()) + .setTemperature(useOptions.getTemperature()) + .setSystem(systemPrompt) + .setStream(stream); + return request; + } + + // TODO @fan:Options 的处理,参考下 OpenAiChatClient 的 createRequest + private YiYanChatOptions getYiYanOptions(Prompt prompt) { + // 两个都为null 则没有配置文件 + if (defaultOptions == null && prompt.getOptions() == null) { + // TODO @fan:IllegalArgumentException 参数更好哈 + throw new ChatException("ChatOptions 未配置参数!"); + } + // 优先使用 Prompt 里面的 ChatOptions + ChatOptions options = defaultOptions; + if (prompt.getOptions() != null) { + options = (ChatOptions) prompt.getOptions(); + } + // Prompt 里面是一个 ChatOptions,用户可以随意传入,这里做一下判断 + if (!(options instanceof YiYanChatOptions)) { + // TODO @fan:IllegalArgumentException 参数更好哈 + // TODO @fan:需要兼容 ChatOptionsBuilder 创建出来的 + throw new ChatException("Prompt 传入的不是 YiYanOptions!"); + } + // 转换 YiYanOptions + return (YiYanChatOptions) options; + } + +} diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/YiYanOptions.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/YiYanChatOptions.java similarity index 82% rename from yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/YiYanOptions.java rename to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/YiYanChatOptions.java index 0cb1481b3..14146a322 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/YiYanOptions.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/YiYanChatOptions.java @@ -1,23 +1,22 @@ -package org.springframework.ai.models.yiyan; +package cn.iocoder.yudao.framework.ai.core.model.yiyan; -import org.springframework.ai.chat.prompt.ChatOptions; -import org.springframework.ai.models.yiyan.api.YiYanChatCompletionRequest; +import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanChatCompletionRequest; import lombok.Data; -import lombok.experimental.Accessors; +import org.springframework.ai.chat.prompt.ChatOptions; import java.util.List; +// TODO @fan:字段命名,penalty_score 类似的,建议改成驼峰原则 +// TODO @fan:字段的注释,可以都删除掉,让用户 https://cloud.baidu.com/doc/WENXINWORKSHOP/s/clntwmv7t 即可 /** - * 百度 问心一言 + * 文心一言的 {@link ChatOptions} 实现类 * - * 文档地址:https://cloud.baidu.com/doc/WENXINWORKSHOP/s/clntwmv7t + * 字段说明:参考 ERNIE-4.0-8K * - * author: fansili - * time: 2024/3/16 19:33 + * @author fansili */ @Data -@Accessors(chain = true) -public class YiYanOptions implements ChatOptions { +public class YiYanChatOptions implements ChatOptions { /** * 一个可触发函数的描述列表,说明: @@ -106,37 +105,24 @@ public class YiYanOptions implements ChatOptions { */ private String tool_choice; - // - // 以下兼容 spring-ai ChatOptions 暂时没有其他地方用到 - @Override public Float getTemperature() { return this.temperature; } -// @Override -// public void setTemperature(Float temperature) { -// this.temperature = temperature; -// } - @Override public Float getTopP() { return topP; } -// @Override -// public void setTopP(Float topP) { -// this.topP = topP; -// } - - // 百度么有 topK - + /** + * 百度么有 topK + * + * @return null + */ @Override public Integer getTopK() { return null; } -// @Override -// public void setTopK(Integer topK) { -// } } diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/api/YiYanApi.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanApi.java similarity index 57% rename from yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/api/YiYanApi.java rename to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanApi.java index a8f4aa322..535901f25 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/api/YiYanApi.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanApi.java @@ -1,8 +1,6 @@ -package org.springframework.ai.models.yiyan.api; +package cn.iocoder.yudao.framework.ai.core.model.yiyan.api; -import org.springframework.ai.models.yiyan.YiYanChatModel; -import org.springframework.ai.models.yiyan.exception.YiYanApiException; -import lombok.Data; +import cn.iocoder.yudao.framework.ai.core.model.yiyan.exception.YiYanApiException; import org.springframework.http.HttpStatusCode; import org.springframework.http.ResponseEntity; import org.springframework.web.reactive.function.client.WebClient; @@ -10,47 +8,55 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; /** - * 文心一言 - *

- * author: fansili - * time: 2024/3/8 21:47 + * 文心一言 API + * + * @author fansili */ -@Data public class YiYanApi { private static final String DEFAULT_BASE_URL = "https://aip.baidubce.com"; private static final String AUTH_2_TOKEN_URI = "/oauth/2.0/token"; - public static final String DEFAULT_CHAT_MODEL = "ERNIE 4.0"; + public static final String DEFAULT_CHAT_MODEL = YiYanChatModel.ERNIE4_0.getModel(); - // 获取access_token流程 https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Ilkkrb0i5 - private String appKey; - private String secretKey; - private String token; - // token刷新时间(秒) + private final String appKey; + private final String secretKey; + /** + * TODO fan:这个是不是要有个刷新机制哈;如果目前不需要,可以删除掉 refreshTokenSecondTime;整体更简洁; + */ + private final String token; + /** + * token 刷新时间(秒) + */ private int refreshTokenSecondTime; - // 发送请求 webClient + /** + * 发送请求 webClient + */ private final WebClient webClient; - // 使用的模型 - private YiYanChatModel useChatModel; + /** + * 使用的模型 + */ + private final YiYanChatModel useChatModel; public YiYanApi(String appKey, String secretKey, YiYanChatModel useChatModel, int refreshTokenSecondTime) { this.appKey = appKey; this.secretKey = secretKey; this.useChatModel = useChatModel; this.refreshTokenSecondTime = refreshTokenSecondTime; - - this.webClient = WebClient.builder() - .baseUrl(DEFAULT_BASE_URL) - .build(); - + this.webClient = WebClient.builder().baseUrl(DEFAULT_BASE_URL).build(); + // 获取访问令牌 token = getToken(); } + /** + * 获得访问令牌 + * + * @see 文档地址 + * @return 访问令牌 + */ private String getToken() { - // 文档地址: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Ilkkrb0i5 - ResponseEntity response = this.webClient.post() + ResponseEntity response = this.webClient.post() .uri(uriBuilder -> uriBuilder.path(AUTH_2_TOKEN_URI) .queryParam("grant_type", "client_credentials") .queryParam("client_id", appKey) @@ -58,17 +64,19 @@ public class YiYanApi { .build() ) .retrieve() - .toEntity(YiYanAuthRes.class) + .toEntity(YiYanAuthResponse.class) .block(); // 检查请求状态 - if (HttpStatusCode.valueOf(200) != response.getStatusCode()) { + // TODO @fan:可以使用 response.getStatusCode().is2xxSuccessful() + if (HttpStatusCode.valueOf(200) != response.getStatusCode() + || response.getBody() == null) { + // TODO @fan:可以使用 IllegalStateException 替代;另外,最好打印下返回;方便排错; throw new YiYanApiException("一言认证失败! api:https://aip.baidubce.com/oauth/2.0/token 请检查 client_id、client_secret 是否正确!"); } - YiYanAuthRes body = response.getBody(); - return body.getAccess_token(); + return response.getBody().getAccess_token(); } - public ResponseEntity chatCompletionEntity(YiYanChatCompletionRequest request) { + public ResponseEntity chatCompletionEntity(YiYanChatCompletionRequest request) { // TODO: 2024/3/10 小范 这里错误信息返回的结构不一样 // {"error_code":17,"error_msg":"Open api daily request limit reached"} return this.webClient.post() @@ -78,11 +86,11 @@ public class YiYanApi { .build()) .body(Mono.just(request), YiYanChatCompletionRequest.class) .retrieve() - .toEntity(YiYanChatCompletion.class) + .toEntity(YiYanChatCompletionResponse.class) .block(); } - public Flux chatCompletionStream(YiYanChatCompletionRequest request) { + public Flux chatCompletionStream(YiYanChatCompletionRequest request) { return this.webClient.post() .uri(uriBuilder -> uriBuilder.path(useChatModel.getUri()) @@ -90,6 +98,7 @@ public class YiYanApi { .build()) .body(Mono.just(request), YiYanChatCompletionRequest.class) .retrieve() - .bodyToFlux(YiYanChatCompletion.class); + .bodyToFlux(YiYanChatCompletionResponse.class); } + } diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/api/YiYanAuthRes.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanAuthResponse.java similarity index 78% rename from yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/api/YiYanAuthRes.java rename to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanAuthResponse.java index f9f1f27db..134b33994 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/api/YiYanAuthRes.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanAuthResponse.java @@ -1,15 +1,15 @@ -package org.springframework.ai.models.yiyan.api; +package cn.iocoder.yudao.framework.ai.core.model.yiyan.api; import lombok.Data; +// TODO @fan:字段驼峰;字段注释都可以删除,贴个链接就好; /** - * 一言 获取access_token + * 获取文心一言的 access_token 的 Response * - * author: fansili - * time: 2024/3/10 08:51 + * @author fansili */ @Data -public class YiYanAuthRes { +public class YiYanAuthResponse { /** * 访问凭证 diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/api/YiYanChatCompletionRequest.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanChatCompletionRequest.java similarity index 95% rename from yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/api/YiYanChatCompletionRequest.java rename to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanChatCompletionRequest.java index 22e918835..250131854 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/api/YiYanChatCompletionRequest.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanChatCompletionRequest.java @@ -1,16 +1,16 @@ -package org.springframework.ai.models.yiyan.api; +package cn.iocoder.yudao.framework.ai.core.model.yiyan.api; import lombok.Data; import java.util.List; +// TODO @fan:字段驼峰;字段注释都可以删除,贴个链接就好; /** - * 一言 Completion req + * 文心一言 Completion Request * - * 百度千帆文档:https://cloud.baidu.com/doc/WENXINWORKSHOP/s/jlil56u11 + * 百度千帆文档:https://cloud.baidu.com/doc/WENXINWORKSHOP/s/jlil56u11 * - * author: fansili - * time: 2024/3/9 10:34 + * @author fansili */ @Data public class YiYanChatCompletionRequest { @@ -114,9 +114,11 @@ public class YiYanChatCompletionRequest { @Data public static class Message { + private String role; private String content; + } @Data diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/api/YiYanChatCompletion.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanChatCompletionResponse.java similarity index 93% rename from yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/api/YiYanChatCompletion.java rename to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanChatCompletionResponse.java index 8e02db659..5082302f8 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/api/YiYanChatCompletion.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanChatCompletionResponse.java @@ -1,16 +1,16 @@ -package org.springframework.ai.models.yiyan.api; +package cn.iocoder.yudao.framework.ai.core.model.yiyan.api; import lombok.Data; /** - * 聊天返回 + * 文心一言 Completion Response + * * 百度链接: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/clntwmv7t * - * author: fansili - * time: 2024/3/9 10:34 + * @author fansili */ @Data -public class YiYanChatCompletion { +public class YiYanChatCompletionResponse { /** * 本轮对话的id @@ -88,4 +88,5 @@ public class YiYanChatCompletion { */ private int total_tokens; } + } diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/YiYanChatModel.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanChatModel.java similarity index 65% rename from yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/YiYanChatModel.java rename to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanChatModel.java index abed185e6..9b400cdad 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/YiYanChatModel.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanChatModel.java @@ -1,15 +1,14 @@ -package org.springframework.ai.models.yiyan; +package cn.iocoder.yudao.framework.ai.core.model.yiyan.api; import lombok.AllArgsConstructor; import lombok.Getter; /** - * 一言模型 + * 文心一言的模型枚举 * - * 可参考百度文档:https://cloud.baidu.com/doc/WENXINWORKSHOP/s/clntwmv7t + * 可参考 百度文档 * - * author: fansili - * time: 2024/3/9 12:01 + * @author fansili */ @Getter @AllArgsConstructor @@ -18,21 +17,24 @@ public enum YiYanChatModel { ERNIE4_0("ERNIE 4.0", "/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro"), ERNIE4_3_5_8K("ERNIE-3.5-8K", "/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions"), ERNIE4_3_5_8K_0205("ERNIE-3.5-8K-0205", "/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-3.5-8k-0205"), - ERNIE4_3_5_8K_1222("ERNIE-3.5-8K-1222", "/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-3.5-8k-1222"), ERNIE4_BOT_8K("ERNIE-Bot-8K", "/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie_bot_8k"), ERNIE4_3_5_4K_0205("ERNIE-3.5-4K-0205", "/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-3.5-4k-0205"), - ; - private String model; - - private String uri; + /** + * 模型名 + */ + private final String model; + /** + * API URL + */ + private final String uri; public static YiYanChatModel valueOfModel(String model) { - for (YiYanChatModel itemEnum : YiYanChatModel.values()) { - if (itemEnum.getModel().equals(model)) { - return itemEnum; + for (YiYanChatModel modelEnum : YiYanChatModel.values()) { + if (modelEnum.getModel().equals(model)) { + return modelEnum; } } throw new IllegalArgumentException("Invalid MessageType value: " + model); diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/exception/YiYanApiException.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/exception/YiYanApiException.java similarity index 79% rename from yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/exception/YiYanApiException.java rename to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/exception/YiYanApiException.java index 94850f56a..f61badfcb 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/exception/YiYanApiException.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/exception/YiYanApiException.java @@ -1,4 +1,4 @@ -package org.springframework.ai.models.yiyan.exception; +package cn.iocoder.yudao.framework.ai.core.model.yiyan.exception; /** * 一言 api 调用异常 diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/tongyi/QianWenChatClient.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/tongyi/QianWenChatClient.java index f038bc83e..31e60d119 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/tongyi/QianWenChatClient.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/tongyi/QianWenChatClient.java @@ -5,7 +5,7 @@ import org.springframework.ai.chat.*; import org.springframework.ai.chat.prompt.ChatOptions; import org.springframework.ai.chat.prompt.Prompt; import org.springframework.ai.models.tongyi.api.QianWenApi; -import org.springframework.ai.models.yiyan.exception.YiYanApiException; +import cn.iocoder.yudao.framework.ai.core.model.yiyan.exception.YiYanApiException; import com.alibaba.dashscope.aigc.generation.GenerationResult; import com.alibaba.dashscope.aigc.generation.models.QwenParam; import com.alibaba.dashscope.common.Message; diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/YiYanChatClient.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/YiYanChatClient.java deleted file mode 100644 index e0da409ec..000000000 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/YiYanChatClient.java +++ /dev/null @@ -1,158 +0,0 @@ -package org.springframework.ai.models.yiyan; - -import cn.hutool.core.bean.BeanUtil; -import cn.iocoder.yudao.framework.ai.core.exception.ChatException; -import org.springframework.ai.chat.*; -import org.springframework.ai.chat.messages.Message; -import org.springframework.ai.chat.messages.MessageType; -import org.springframework.ai.chat.prompt.ChatOptions; -import org.springframework.ai.chat.prompt.Prompt; -import org.springframework.ai.models.yiyan.api.YiYanApi; -import org.springframework.ai.models.yiyan.api.YiYanChatCompletion; -import org.springframework.ai.models.yiyan.api.YiYanChatCompletionRequest; -import org.springframework.ai.models.yiyan.exception.YiYanApiException; -import lombok.extern.slf4j.Slf4j; -import org.jetbrains.annotations.NotNull; -import org.springframework.http.ResponseEntity; -import org.springframework.retry.RetryCallback; -import org.springframework.retry.RetryContext; -import org.springframework.retry.RetryListener; -import org.springframework.retry.support.RetryTemplate; -import reactor.core.publisher.Flux; - -import java.time.Duration; -import java.util.List; -import java.util.stream.Collectors; - -/** - * 文心一言 - *

- * author: fansili - * time: 2024/3/8 19:11 - */ -@Slf4j -public class YiYanChatClient implements ChatClient, StreamingChatClient { - - private YiYanApi yiYanApi; - - private YiYanOptions yiYanOptions; - - public YiYanChatClient(YiYanApi yiYanApi) { - this.yiYanApi = yiYanApi; - } - - public YiYanChatClient(YiYanApi yiYanApi, YiYanOptions yiYanOptions) { - this.yiYanApi = yiYanApi; - this.yiYanOptions = yiYanOptions; - } - - public final RetryTemplate retryTemplate = RetryTemplate.builder() - // 最大重试次数 10 - .maxAttempts(10) - .retryOn(YiYanApiException.class) - // 最大重试5次,第一次间隔3000ms,第二次3000ms * 2,第三次3000ms * 3,以此类推,最大间隔3 * 60000ms - .exponentialBackoff(Duration.ofMillis(3000), 2, Duration.ofMillis(3 * 60000)) - .withListener(new RetryListener() { - @Override - public void onError(RetryContext context, - RetryCallback callback, Throwable throwable) { - log.warn("重试异常:" + context.getRetryCount(), throwable); - } - - ; - }) - .build(); - - @Override - public String call(String message) { - return ChatClient.super.call(message); - } - - @Override - public ChatResponse call(Prompt prompt) { - return this.retryTemplate.execute(ctx -> { - // ctx 会有重试的信息 - // 创建 request 请求,stream模式需要供应商支持 - YiYanChatCompletionRequest request = this.createRequest(prompt, false); - // 调用 callWithFunctionSupport 发送请求 - ResponseEntity response = yiYanApi.chatCompletionEntity(request); - // 获取结果封装 ChatResponse - YiYanChatCompletion chatCompletion = response.getBody(); - return new ChatResponse(List.of(new Generation(chatCompletion.getResult()))); - }); - } - - @Override - public Flux stream(Prompt prompt) { - // ctx 会有重试的信息 - // 创建 request 请求,stream模式需要供应商支持 - YiYanChatCompletionRequest request = this.createRequest(prompt, true); - // 调用 callWithFunctionSupport 发送请求 - Flux response = this.yiYanApi.chatCompletionStream(request); - response.doOnComplete(new Runnable() { - @Override - public void run() { - String a = ";"; - } - }); - return response.map(res -> { - // TODO @fan:这里缺少了 usage 的封装 - return new ChatResponse(List.of(new Generation(res.getResult()))); - }); - } - - private YiYanChatCompletionRequest createRequest(Prompt prompt, boolean stream) { - // 获取配置 - YiYanOptions useOptions = getYiYanOptions(prompt); - // 创建 request - - // tip: 百度的 system 不在 message 里面 - // tip:百度的 message 只有 user 和 assistant - // https://cloud.baidu.com/doc/WENXINWORKSHOP/s/clntwmv7t - - // 获取 user 和 assistant - List messageList = prompt.getInstructions().stream() - // 过滤 system - .filter(msg -> MessageType.SYSTEM != msg.getMessageType()) - .map(msg -> new YiYanChatCompletionRequest.Message() - .setRole(msg.getMessageType().getValue()) - .setContent(msg.getContent()) - ).toList(); - // 获取 system - String systemPrompt = prompt.getInstructions().stream() - .filter(msg -> MessageType.SYSTEM == msg.getMessageType()) - .map(Message::getContent) - .collect(Collectors.joining()); - - YiYanChatCompletionRequest request = new YiYanChatCompletionRequest(messageList); - // 复制 qianWenOptions 属性取 request(这里 options 属性和 request 基本保持一致) - // top: 由于遵循 spring-ai规范,支持在构建client的时候传入默认的 chatOptions - BeanUtil.copyProperties(useOptions, request); - request.setTop_p(useOptions.getTopP()); - request.setMax_output_tokens(useOptions.getMaxOutputTokens()); - request.setTemperature(useOptions.getTemperature()); - request.setSystem(systemPrompt); - // 设置 stream - request.setStream(stream); - return request; - } - - private @NotNull YiYanOptions getYiYanOptions(Prompt prompt) { - // 两个都为null 则没有配置文件 - if (yiYanOptions == null && prompt.getOptions() == null) { - throw new ChatException("ChatOptions 未配置参数!"); - } - // 优先使用 Prompt 里面的 ChatOptions - ChatOptions options = yiYanOptions; - if (prompt.getOptions() != null) { - options = (ChatOptions) prompt.getOptions(); - } - // Prompt 里面是一个 ChatOptions,用户可以随意传入,这里做一下判断 - if (!(options instanceof YiYanOptions)) { - throw new ChatException("Prompt 传入的不是 YiYanOptions!"); - } - // 转换 YiYanOptions - YiYanOptions useOptions = (YiYanOptions) options; - return useOptions; - } -} diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/api/YiYanChatCompletionMessage.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/api/YiYanChatCompletionMessage.java deleted file mode 100644 index 278b1a7d6..000000000 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/api/YiYanChatCompletionMessage.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.springframework.ai.models.yiyan.api; - -/** - * author: fansili - * time: 2024/3/9 10:37 - */ -public class YiYanChatCompletionMessage { -} diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/chat/YiYanChatTests.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/chat/YiYanChatTests.java index f54ae39c8..55d16eab9 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/chat/YiYanChatTests.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/chat/YiYanChatTests.java @@ -5,10 +5,10 @@ import org.springframework.ai.chat.messages.Message; import org.springframework.ai.chat.messages.SystemMessage; import org.springframework.ai.chat.messages.UserMessage; import org.springframework.ai.chat.prompt.Prompt; -import org.springframework.ai.models.yiyan.YiYanChatClient; -import org.springframework.ai.models.yiyan.YiYanChatModel; -import org.springframework.ai.models.yiyan.YiYanOptions; -import org.springframework.ai.models.yiyan.api.YiYanApi; +import cn.iocoder.yudao.framework.ai.core.model.yiyan.YiYanChatClient; +import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanChatModel; +import cn.iocoder.yudao.framework.ai.core.model.yiyan.YiYanChatOptions; +import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanApi; import org.junit.Before; import org.junit.Test; import reactor.core.publisher.Flux; @@ -35,7 +35,7 @@ public class YiYanChatTests { YiYanChatModel.ERNIE4_3_5_8K, 86400 ); - YiYanOptions yiYanOptions = new YiYanOptions(); + YiYanChatOptions yiYanOptions = new YiYanChatOptions(); yiYanOptions.setMaxOutputTokens(2048); yiYanOptions.setTopP(0.6f); yiYanOptions.setTemperature(0.85f); diff --git a/yudao-server/src/main/resources/application-local.yaml b/yudao-server/src/main/resources/application-local.yaml index 2f5e70b27..93302bddf 100644 --- a/yudao-server/src/main/resources/application-local.yaml +++ b/yudao-server/src/main/resources/application-local.yaml @@ -230,17 +230,6 @@ yudao: appKey: cb6415c19d6162cda07b47316fcb0416 secretKey: Y2JiYTIxZjA3MDMxMjNjZjQzYzVmNzdh model: XING_HUO_3_5 - yiyan: - enable: true - aiPlatform: YI_YAN - max-tokens: 1500 - temperature: 0.85 - topP: 0.8 - topK: 0 - appKey: x0cuLZ7XsaTCU08vuJWO87Lg - secretKey: R9mYF9dl9KASgi5RUq0FQt3wRisSnOcK - refreshTokenSecondTime: 86400 - model: ERNIE4_3_5_8K openAiImage: enable: true api-key: ${OPEN_AI_KEY} diff --git a/yudao-server/src/main/resources/application.yaml b/yudao-server/src/main/resources/application.yaml index 886644e74..52dea3fda 100644 --- a/yudao-server/src/main/resources/application.yaml +++ b/yudao-server/src/main/resources/application.yaml @@ -150,6 +150,19 @@ spring.ai: chat: model: llama3 +yudao.ai: + yiyan: + enable: true + aiPlatform: YIYAN # TODO @fan:建议每个都独立配置属性类 + max-tokens: 1500 + temperature: 0.85 + topP: 0.8 + topK: 0 + appKey: x0cuLZ7XsaTCU08vuJWO87Lg + secretKey: R9mYF9dl9KASgi5RUq0FQt3wRisSnOcK + refreshTokenSecondTime: 86400 + model: ERNIE4_3_5_8K + --- #################### 芋道相关配置 #################### yudao: