Commit 04021ce0682c9eea39cb2c06ac081d3c5c615db6
1 parent
276ef98f
【代码评审】AI:文心一言的接入调整
Showing
24 changed files
with
205 additions
and
213 deletions
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; | @@ -5,7 +5,7 @@ import org.springframework.ai.chat.ChatClient; | ||
| 5 | import org.springframework.ai.chat.StreamingChatClient; | 5 | import org.springframework.ai.chat.StreamingChatClient; |
| 6 | import org.springframework.ai.models.tongyi.QianWenChatClient; | 6 | import org.springframework.ai.models.tongyi.QianWenChatClient; |
| 7 | import org.springframework.ai.models.xinghuo.XingHuoChatClient; | 7 | import org.springframework.ai.models.xinghuo.XingHuoChatClient; |
| 8 | -import org.springframework.ai.models.yiyan.YiYanChatClient; | 8 | +import cn.iocoder.yudao.framework.ai.core.model.yiyan.YiYanChatClient; |
| 9 | import org.springframework.ai.ollama.OllamaChatClient; | 9 | import org.springframework.ai.ollama.OllamaChatClient; |
| 10 | import org.springframework.beans.factory.annotation.Autowired; | 10 | import org.springframework.beans.factory.annotation.Autowired; |
| 11 | import org.springframework.context.ApplicationContext; | 11 | import org.springframework.context.ApplicationContext; |
| @@ -27,7 +27,7 @@ public class AiChatClientFactory { | @@ -27,7 +27,7 @@ public class AiChatClientFactory { | ||
| 27 | public ChatClient getChatClient(AiPlatformEnum platformEnum) { | 27 | public ChatClient getChatClient(AiPlatformEnum platformEnum) { |
| 28 | if (AiPlatformEnum.QIAN_WEN == platformEnum) { | 28 | if (AiPlatformEnum.QIAN_WEN == platformEnum) { |
| 29 | return applicationContext.getBean(QianWenChatClient.class); | 29 | return applicationContext.getBean(QianWenChatClient.class); |
| 30 | - } else if (AiPlatformEnum.YI_YAN == platformEnum) { | 30 | + } else if (AiPlatformEnum.YIYAN == platformEnum) { |
| 31 | return applicationContext.getBean(YiYanChatClient.class); | 31 | return applicationContext.getBean(YiYanChatClient.class); |
| 32 | } else if (AiPlatformEnum.XING_HUO == platformEnum) { | 32 | } else if (AiPlatformEnum.XING_HUO == platformEnum) { |
| 33 | return applicationContext.getBean(XingHuoChatClient.class); | 33 | return applicationContext.getBean(XingHuoChatClient.class); |
| @@ -42,7 +42,7 @@ public class AiChatClientFactory { | @@ -42,7 +42,7 @@ public class AiChatClientFactory { | ||
| 42 | // } | 42 | // } |
| 43 | if (AiPlatformEnum.QIAN_WEN == platformEnum) { | 43 | if (AiPlatformEnum.QIAN_WEN == platformEnum) { |
| 44 | return applicationContext.getBean(QianWenChatClient.class); | 44 | return applicationContext.getBean(QianWenChatClient.class); |
| 45 | - } else if (AiPlatformEnum.YI_YAN == platformEnum) { | 45 | + } else if (AiPlatformEnum.YIYAN == platformEnum) { |
| 46 | return applicationContext.getBean(YiYanChatClient.class); | 46 | return applicationContext.getBean(YiYanChatClient.class); |
| 47 | } else if (AiPlatformEnum.XING_HUO == platformEnum) { | 47 | } else if (AiPlatformEnum.XING_HUO == platformEnum) { |
| 48 | return applicationContext.getBean(XingHuoChatClient.class); | 48 | return applicationContext.getBean(XingHuoChatClient.class); |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/chat/vo/conversation/AiChatConversationRespVO.java
| 1 | package cn.iocoder.yudao.module.ai.controller.admin.chat.vo.conversation; | 1 | package cn.iocoder.yudao.module.ai.controller.admin.chat.vo.conversation; |
| 2 | 2 | ||
| 3 | -import cn.iocoder.yudao.module.ai.controller.admin.model.vo.chatModel.AiChatModelRespVO; | ||
| 4 | import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatModelDO; | 3 | import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatModelDO; |
| 5 | import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatRoleDO; | 4 | import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatRoleDO; |
| 6 | -import com.baomidou.mybatisplus.annotation.FieldFill; | ||
| 7 | -import com.baomidou.mybatisplus.annotation.TableField; | ||
| 8 | import com.fhs.core.trans.anno.Trans; | 5 | import com.fhs.core.trans.anno.Trans; |
| 9 | import com.fhs.core.trans.constant.TransType; | 6 | import com.fhs.core.trans.constant.TransType; |
| 10 | import com.fhs.core.trans.vo.VO; | 7 | import com.fhs.core.trans.vo.VO; |
| 11 | import io.swagger.v3.oas.annotations.media.Schema; | 8 | import io.swagger.v3.oas.annotations.media.Schema; |
| 12 | -import jakarta.validation.constraints.NotNull; | ||
| 13 | import lombok.Data; | 9 | import lombok.Data; |
| 14 | -import lombok.experimental.Accessors; | ||
| 15 | 10 | ||
| 16 | import java.time.LocalDateTime; | 11 | import java.time.LocalDateTime; |
| 17 | -import java.time.LocalTime; | ||
| 18 | 12 | ||
| 19 | @Schema(description = "管理后台 - AI 聊天会话 Response VO") | 13 | @Schema(description = "管理后台 - AI 聊天会话 Response VO") |
| 20 | @Data | 14 | @Data |
| @@ -58,7 +52,7 @@ public class AiChatConversationRespVO implements VO { | @@ -58,7 +52,7 @@ public class AiChatConversationRespVO implements VO { | ||
| 58 | @Schema(description = "上下文的最大 Message 数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "10") | 52 | @Schema(description = "上下文的最大 Message 数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "10") |
| 59 | private Integer maxContexts; | 53 | private Integer maxContexts; |
| 60 | 54 | ||
| 61 | - @Schema(description = "最后更新时间", requiredMode = Schema.RequiredMode.REQUIRED, example = "2024-05-16") | 55 | + @Schema(description = "最后更新时间", requiredMode = Schema.RequiredMode.REQUIRED) |
| 62 | private LocalDateTime updateTime; | 56 | private LocalDateTime updateTime; |
| 63 | 57 | ||
| 64 | // ========== 关联 role 信息 ========== | 58 | // ========== 关联 role 信息 ========== |
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; | @@ -4,7 +4,7 @@ import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum; | ||
| 4 | import lombok.Data; | 4 | import lombok.Data; |
| 5 | import lombok.experimental.Accessors; | 5 | import lombok.experimental.Accessors; |
| 6 | import org.springframework.ai.models.xinghuo.XingHuoChatModel; | 6 | import org.springframework.ai.models.xinghuo.XingHuoChatModel; |
| 7 | -import org.springframework.ai.models.yiyan.YiYanChatModel; | 7 | +import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanChatModel; |
| 8 | 8 | ||
| 9 | /** | 9 | /** |
| 10 | * modal config | 10 | * modal config |
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 { | @@ -150,9 +150,9 @@ public class AiChatServiceImpl implements AiChatService { | ||
| 150 | // 1.3 user message 新发送消息 | 150 | // 1.3 user message 新发送消息 |
| 151 | chatMessages.add(new UserMessage(sendReqVO.getContent())); | 151 | chatMessages.add(new UserMessage(sendReqVO.getContent())); |
| 152 | 152 | ||
| 153 | - // 2. 构建 ChatOptions 对象 | ||
| 154 | - ChatOptions chatOptions = ChatOptionsBuilder.builder().withTemperature(conversation.getTemperature().floatValue()).build(); | ||
| 155 | - return new Prompt(chatMessages, chatOptions); | 153 | + // 2. 构建 ChatOptions 对象 TODO 芋艿:临时注释掉;等文心一言兼容了; |
| 154 | +// ChatOptions chatOptions = ChatOptionsBuilder.builder().withTemperature(conversation.getTemperature().floatValue()).build(); | ||
| 155 | + return new Prompt(chatMessages, null); | ||
| 156 | } | 156 | } |
| 157 | 157 | ||
| 158 | private AiChatMessageDO createChatMessage(Long conversationId, AiChatModelDO model, | 158 | private AiChatMessageDO createChatMessage(Long conversationId, AiChatModelDO model, |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/model/AiChatRoleServiceImpl.java
| 1 | package cn.iocoder.yudao.module.ai.service.model; | 1 | package cn.iocoder.yudao.module.ai.service.model; |
| 2 | 2 | ||
| 3 | import cn.hutool.core.util.ObjectUtil; | 3 | import cn.hutool.core.util.ObjectUtil; |
| 4 | +import cn.hutool.core.util.StrUtil; | ||
| 4 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | 5 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 5 | import cn.iocoder.yudao.framework.common.pojo.PageResult; | 6 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 6 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | 7 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| @@ -15,6 +16,8 @@ import org.springframework.stereotype.Service; | @@ -15,6 +16,8 @@ import org.springframework.stereotype.Service; | ||
| 15 | 16 | ||
| 16 | import java.util.List; | 17 | import java.util.List; |
| 17 | import java.util.Objects; | 18 | import java.util.Objects; |
| 19 | +import java.util.function.Function; | ||
| 20 | +import java.util.function.Predicate; | ||
| 18 | import java.util.stream.Collectors; | 21 | import java.util.stream.Collectors; |
| 19 | 22 | ||
| 20 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | 23 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| @@ -134,7 +137,7 @@ public class AiChatRoleServiceImpl implements AiChatRoleService { | @@ -134,7 +137,7 @@ public class AiChatRoleServiceImpl implements AiChatRoleService { | ||
| 134 | @Override | 137 | @Override |
| 135 | public List<String> getChatRoleCategoryList() { | 138 | public List<String> getChatRoleCategoryList() { |
| 136 | List<AiChatRoleDO> list = chatRoleMapper.selectListGroupByCategory(CommonStatusEnum.ENABLE.getStatus()); | 139 | List<AiChatRoleDO> list = chatRoleMapper.selectListGroupByCategory(CommonStatusEnum.ENABLE.getStatus()); |
| 137 | - return convertList(list.stream().filter(Objects::nonNull).collect(Collectors.toList()), AiChatRoleDO::getCategory); | 140 | + return convertList(list, AiChatRoleDO::getCategory, role -> StrUtil.isNotBlank(role.getCategory())); |
| 138 | } | 141 | } |
| 139 | 142 | ||
| 140 | } | 143 | } |
yudao-module-ai/yudao-spring-boot-starter-ai/pom.xml
| @@ -10,23 +10,16 @@ | @@ -10,23 +10,16 @@ | ||
| 10 | </parent> | 10 | </parent> |
| 11 | 11 | ||
| 12 | <artifactId>yudao-spring-boot-starter-ai</artifactId> | 12 | <artifactId>yudao-spring-boot-starter-ai</artifactId> |
| 13 | - <!-- TODO 芋艿:这里需要进一步减少 --> | ||
| 14 | 13 | ||
| 15 | <dependencies> | 14 | <dependencies> |
| 16 | <dependency> | 15 | <dependency> |
| 17 | <groupId>io.springboot.ai</groupId> | 16 | <groupId>io.springboot.ai</groupId> |
| 18 | - <artifactId>spring-ai-core</artifactId> | ||
| 19 | - <version>1.0.3</version> | ||
| 20 | - </dependency> | ||
| 21 | - <dependency> | ||
| 22 | - <groupId>io.springboot.ai</groupId> | ||
| 23 | - <artifactId>spring-ai-openai</artifactId> | 17 | + <artifactId>spring-ai-ollama-spring-boot-starter</artifactId> |
| 24 | <version>1.0.3</version> | 18 | <version>1.0.3</version> |
| 25 | </dependency> | 19 | </dependency> |
| 26 | - | ||
| 27 | <dependency> | 20 | <dependency> |
| 28 | <groupId>io.springboot.ai</groupId> | 21 | <groupId>io.springboot.ai</groupId> |
| 29 | - <artifactId>spring-ai-ollama-spring-boot-starter</artifactId> | 22 | + <artifactId>spring-ai-openai-spring-boot-starter</artifactId> |
| 30 | <version>1.0.3</version> | 23 | <version>1.0.3</version> |
| 31 | </dependency> | 24 | </dependency> |
| 32 | 25 | ||
| @@ -35,7 +28,7 @@ | @@ -35,7 +28,7 @@ | ||
| 35 | <artifactId>yudao-common</artifactId> | 28 | <artifactId>yudao-common</artifactId> |
| 36 | </dependency> | 29 | </dependency> |
| 37 | 30 | ||
| 38 | - <!-- 阿里云 通义千问 --> | 31 | + <!-- 阿里云 通义千问 --> |
| 39 | <dependency> | 32 | <dependency> |
| 40 | <groupId>com.alibaba</groupId> | 33 | <groupId>com.alibaba</groupId> |
| 41 | <artifactId>dashscope-sdk-java</artifactId> | 34 | <artifactId>dashscope-sdk-java</artifactId> |
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; | @@ -8,9 +8,9 @@ import org.springframework.ai.models.tongyi.api.QianWenApi; | ||
| 8 | import org.springframework.ai.models.xinghuo.XingHuoChatClient; | 8 | import org.springframework.ai.models.xinghuo.XingHuoChatClient; |
| 9 | import org.springframework.ai.models.xinghuo.XingHuoOptions; | 9 | import org.springframework.ai.models.xinghuo.XingHuoOptions; |
| 10 | import org.springframework.ai.models.xinghuo.api.XingHuoApi; | 10 | import org.springframework.ai.models.xinghuo.api.XingHuoApi; |
| 11 | -import org.springframework.ai.models.yiyan.YiYanChatClient; | ||
| 12 | -import org.springframework.ai.models.yiyan.YiYanOptions; | ||
| 13 | -import org.springframework.ai.models.yiyan.api.YiYanApi; | 11 | +import cn.iocoder.yudao.framework.ai.core.model.yiyan.YiYanChatClient; |
| 12 | +import cn.iocoder.yudao.framework.ai.core.model.yiyan.YiYanChatOptions; | ||
| 13 | +import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanApi; | ||
| 14 | import org.springframework.ai.models.midjourney.MidjourneyConfig; | 14 | import org.springframework.ai.models.midjourney.MidjourneyConfig; |
| 15 | import org.springframework.ai.models.midjourney.MidjourneyMessage; | 15 | import org.springframework.ai.models.midjourney.MidjourneyMessage; |
| 16 | import org.springframework.ai.models.midjourney.api.MidjourneyInteractionsApi; | 16 | import org.springframework.ai.models.midjourney.api.MidjourneyInteractionsApi; |
| @@ -91,7 +91,7 @@ public class YudaoAiAutoConfiguration { | @@ -91,7 +91,7 @@ public class YudaoAiAutoConfiguration { | ||
| 91 | public YiYanChatClient yiYanChatClient(YudaoAiProperties yudaoAiProperties) { | 91 | public YiYanChatClient yiYanChatClient(YudaoAiProperties yudaoAiProperties) { |
| 92 | YudaoAiProperties.YiYanProperties yiYanProperties = yudaoAiProperties.getYiyan(); | 92 | YudaoAiProperties.YiYanProperties yiYanProperties = yudaoAiProperties.getYiyan(); |
| 93 | // 转换配置 | 93 | // 转换配置 |
| 94 | - YiYanOptions yiYanOptions = new YiYanOptions(); | 94 | + YiYanChatOptions yiYanOptions = new YiYanChatOptions(); |
| 95 | // yiYanOptions.setTopK(yiYanProperties.getTopK()); TODO 芋艿:后续弄 | 95 | // yiYanOptions.setTopK(yiYanProperties.getTopK()); TODO 芋艿:后续弄 |
| 96 | yiYanOptions.setTopP(yiYanProperties.getTopP()); | 96 | yiYanOptions.setTopP(yiYanProperties.getTopP()); |
| 97 | yiYanOptions.setTemperature(yiYanProperties.getTemperature()); | 97 | yiYanOptions.setTemperature(yiYanProperties.getTemperature()); |
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; | @@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.ai.config; | ||
| 3 | import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum; | 3 | import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum; |
| 4 | import org.springframework.ai.models.xinghuo.XingHuoChatModel; | 4 | import org.springframework.ai.models.xinghuo.XingHuoChatModel; |
| 5 | import org.springframework.ai.models.xinghuo.XingHuoOptions; | 5 | import org.springframework.ai.models.xinghuo.XingHuoOptions; |
| 6 | -import org.springframework.ai.models.yiyan.YiYanChatModel; | 6 | +import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanChatModel; |
| 7 | import lombok.Data; | 7 | import lombok.Data; |
| 8 | import lombok.experimental.Accessors; | 8 | import lombok.experimental.Accessors; |
| 9 | 9 |
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; | @@ -2,7 +2,7 @@ package cn.iocoder.yudao.framework.ai.config; | ||
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum; | 3 | import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum; |
| 4 | import org.springframework.ai.models.xinghuo.XingHuoChatModel; | 4 | import org.springframework.ai.models.xinghuo.XingHuoChatModel; |
| 5 | -import org.springframework.ai.models.yiyan.YiYanChatModel; | 5 | +import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanChatModel; |
| 6 | import cn.iocoder.yudao.framework.ai.core.enums.OpenAiImageModelEnum; | 6 | import cn.iocoder.yudao.framework.ai.core.enums.OpenAiImageModelEnum; |
| 7 | import cn.iocoder.yudao.framework.ai.core.enums.OpenAiImageStyleEnum; | 7 | import cn.iocoder.yudao.framework.ai.core.enums.OpenAiImageStyleEnum; |
| 8 | import lombok.Data; | 8 | import lombok.Data; |
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 { | @@ -16,8 +16,8 @@ public enum AiPlatformEnum { | ||
| 16 | 16 | ||
| 17 | OPENAI("OpenAI", "OpenAI"), | 17 | OPENAI("OpenAI", "OpenAI"), |
| 18 | OLLAMA("Ollama", "Ollama"), | 18 | OLLAMA("Ollama", "Ollama"), |
| 19 | + YIYAN("YiYan", "文心一言"), | ||
| 19 | 20 | ||
| 20 | - YI_YAN("yiyan", "一言"), | ||
| 21 | QIAN_WEN("qianwen", "千问"), | 21 | QIAN_WEN("qianwen", "千问"), |
| 22 | XING_HUO("xinghuo", "星火"), | 22 | XING_HUO("xinghuo", "星火"), |
| 23 | OPEN_AI_DALL("dall", "dall"), | 23 | OPEN_AI_DALL("dall", "dall"), |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/package-info.java
0 → 100644
| 1 | +/** | ||
| 2 | + * model 包,接入各种大模型,对标 https://github.com/spring-projects/spring-ai/tree/main/models | ||
| 3 | + * | ||
| 4 | + * 1. yiyan 包:【百度】文心一言 | ||
| 5 | + * 2. TODO 芋艿: | ||
| 6 | + * tongyi 包:【阿里】通义千问,对标 spring-cloud-alibaba 提供的 ai 包 | ||
| 7 | + * 2.2 | ||
| 8 | + * 2.3 xinghuo 包:【讯飞】星火,自己实现 | ||
| 9 | + * 2.4 openai 包:【OpenAI】ChatGPT,拷贝 spring-ai 提供的 models/openai 包 | ||
| 10 | + * 2.5 midjourney 包:Midjourney,参考 https://github.com/novicezk/midjourney-proxy 实现 | ||
| 11 | + */ | ||
| 12 | +package cn.iocoder.yudao.framework.ai.core.model; | ||
| 0 | \ No newline at end of file | 13 | \ No newline at end of file |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/YiYanChatClient.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/YiYanChatClient.java
| 1 | -package org.springframework.ai.models.yiyan; | 1 | +package cn.iocoder.yudao.framework.ai.core.model.yiyan; |
| 2 | 2 | ||
| 3 | import cn.hutool.core.bean.BeanUtil; | 3 | import cn.hutool.core.bean.BeanUtil; |
| 4 | import cn.iocoder.yudao.framework.ai.core.exception.ChatException; | 4 | import cn.iocoder.yudao.framework.ai.core.exception.ChatException; |
| 5 | -import org.springframework.ai.chat.*; | 5 | +import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanApi; |
| 6 | +import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanChatCompletionResponse; | ||
| 7 | +import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanChatCompletionRequest; | ||
| 8 | +import cn.iocoder.yudao.framework.ai.core.model.yiyan.exception.YiYanApiException; | ||
| 9 | +import lombok.extern.slf4j.Slf4j; | ||
| 10 | +import org.springframework.ai.chat.ChatClient; | ||
| 11 | +import org.springframework.ai.chat.ChatResponse; | ||
| 12 | +import org.springframework.ai.chat.Generation; | ||
| 13 | +import org.springframework.ai.chat.StreamingChatClient; | ||
| 6 | import org.springframework.ai.chat.messages.Message; | 14 | import org.springframework.ai.chat.messages.Message; |
| 7 | import org.springframework.ai.chat.messages.MessageType; | 15 | import org.springframework.ai.chat.messages.MessageType; |
| 8 | import org.springframework.ai.chat.prompt.ChatOptions; | 16 | import org.springframework.ai.chat.prompt.ChatOptions; |
| 9 | import org.springframework.ai.chat.prompt.Prompt; | 17 | import org.springframework.ai.chat.prompt.Prompt; |
| 10 | -import org.springframework.ai.models.yiyan.api.YiYanApi; | ||
| 11 | -import org.springframework.ai.models.yiyan.api.YiYanChatCompletion; | ||
| 12 | -import org.springframework.ai.models.yiyan.api.YiYanChatCompletionRequest; | ||
| 13 | -import org.springframework.ai.models.yiyan.exception.YiYanApiException; | ||
| 14 | -import lombok.extern.slf4j.Slf4j; | ||
| 15 | -import org.jetbrains.annotations.NotNull; | ||
| 16 | import org.springframework.http.ResponseEntity; | 18 | import org.springframework.http.ResponseEntity; |
| 17 | import org.springframework.retry.RetryCallback; | 19 | import org.springframework.retry.RetryCallback; |
| 18 | import org.springframework.retry.RetryContext; | 20 | import org.springframework.retry.RetryContext; |
| 19 | import org.springframework.retry.RetryListener; | 21 | import org.springframework.retry.RetryListener; |
| 20 | import org.springframework.retry.support.RetryTemplate; | 22 | import org.springframework.retry.support.RetryTemplate; |
| 23 | +import org.springframework.util.Assert; | ||
| 21 | import reactor.core.publisher.Flux; | 24 | import reactor.core.publisher.Flux; |
| 22 | 25 | ||
| 23 | import java.time.Duration; | 26 | import java.time.Duration; |
| @@ -25,134 +28,127 @@ import java.util.List; | @@ -25,134 +28,127 @@ import java.util.List; | ||
| 25 | import java.util.stream.Collectors; | 28 | import java.util.stream.Collectors; |
| 26 | 29 | ||
| 27 | /** | 30 | /** |
| 28 | - * 文心一言 | ||
| 29 | - * <p> | ||
| 30 | - * author: fansili | ||
| 31 | - * time: 2024/3/8 19:11 | 31 | + * 文心一言的 {@link ChatClient} 实现类 |
| 32 | + * | ||
| 33 | + * @author fansili | ||
| 32 | */ | 34 | */ |
| 33 | @Slf4j | 35 | @Slf4j |
| 34 | public class YiYanChatClient implements ChatClient, StreamingChatClient { | 36 | public class YiYanChatClient implements ChatClient, StreamingChatClient { |
| 35 | 37 | ||
| 36 | - private YiYanApi yiYanApi; | 38 | + private final YiYanApi yiYanApi; |
| 39 | + | ||
| 40 | + private YiYanChatOptions defaultOptions; | ||
| 37 | 41 | ||
| 38 | - private YiYanOptions yiYanOptions; | 42 | + // TODO @fan:参考 OpenAiChatClient 调整下 retryTemplate;使用 RetryUtils.DEFAULT_RETRY_TEMPLATE;加允许传入? |
| 39 | 43 | ||
| 40 | public YiYanChatClient(YiYanApi yiYanApi) { | 44 | public YiYanChatClient(YiYanApi yiYanApi) { |
| 41 | this.yiYanApi = yiYanApi; | 45 | this.yiYanApi = yiYanApi; |
| 46 | + // TODO @fan:这个情况,是不是搞个 defaultOptions;OpenAiChatOptions.builder().withModel(OpenAiApi.DEFAULT_CHAT_MODEL).withTemperature(0.7f).build() | ||
| 42 | } | 47 | } |
| 43 | 48 | ||
| 44 | - public YiYanChatClient(YiYanApi yiYanApi, YiYanOptions yiYanOptions) { | 49 | + public YiYanChatClient(YiYanApi yiYanApi, YiYanChatOptions defaultOptions) { |
| 50 | + Assert.notNull(yiYanApi, "OllamaApi must not be null"); | ||
| 51 | + Assert.notNull(defaultOptions, "DefaultOptions must not be null"); | ||
| 45 | this.yiYanApi = yiYanApi; | 52 | this.yiYanApi = yiYanApi; |
| 46 | - this.yiYanOptions = yiYanOptions; | 53 | + this.defaultOptions = defaultOptions; |
| 47 | } | 54 | } |
| 48 | 55 | ||
| 49 | public final RetryTemplate retryTemplate = RetryTemplate.builder() | 56 | public final RetryTemplate retryTemplate = RetryTemplate.builder() |
| 50 | - // 最大重试次数 10 | ||
| 51 | .maxAttempts(10) | 57 | .maxAttempts(10) |
| 52 | .retryOn(YiYanApiException.class) | 58 | .retryOn(YiYanApiException.class) |
| 53 | - // 最大重试5次,第一次间隔3000ms,第二次3000ms * 2,第三次3000ms * 3,以此类推,最大间隔3 * 60000ms | ||
| 54 | .exponentialBackoff(Duration.ofMillis(3000), 2, Duration.ofMillis(3 * 60000)) | 59 | .exponentialBackoff(Duration.ofMillis(3000), 2, Duration.ofMillis(3 * 60000)) |
| 55 | .withListener(new RetryListener() { | 60 | .withListener(new RetryListener() { |
| 61 | + | ||
| 56 | @Override | 62 | @Override |
| 57 | - public <T extends Object, E extends Throwable> void onError(RetryContext context, | ||
| 58 | - RetryCallback<T, E> callback, Throwable throwable) { | 63 | + public <T, E extends Throwable> void onError(RetryContext context, |
| 64 | + RetryCallback<T, E> callback, Throwable throwable) { | ||
| 59 | log.warn("重试异常:" + context.getRetryCount(), throwable); | 65 | log.warn("重试异常:" + context.getRetryCount(), throwable); |
| 60 | } | 66 | } |
| 61 | 67 | ||
| 62 | - ; | ||
| 63 | }) | 68 | }) |
| 64 | .build(); | 69 | .build(); |
| 65 | 70 | ||
| 66 | @Override | 71 | @Override |
| 67 | - public String call(String message) { | ||
| 68 | - return ChatClient.super.call(message); | ||
| 69 | - } | ||
| 70 | - | ||
| 71 | - @Override | ||
| 72 | public ChatResponse call(Prompt prompt) { | 72 | public ChatResponse call(Prompt prompt) { |
| 73 | + YiYanChatCompletionRequest request = createRequest(prompt, false); | ||
| 73 | return this.retryTemplate.execute(ctx -> { | 74 | return this.retryTemplate.execute(ctx -> { |
| 74 | - // ctx 会有重试的信息 | ||
| 75 | - // 创建 request 请求,stream模式需要供应商支持 | ||
| 76 | - YiYanChatCompletionRequest request = this.createRequest(prompt, false); | ||
| 77 | - // 调用 callWithFunctionSupport 发送请求 | ||
| 78 | - ResponseEntity<YiYanChatCompletion> response = yiYanApi.chatCompletionEntity(request); | 75 | + // 发送请求 |
| 76 | + ResponseEntity<YiYanChatCompletionResponse> response = yiYanApi.chatCompletionEntity(request); | ||
| 79 | // 获取结果封装 ChatResponse | 77 | // 获取结果封装 ChatResponse |
| 80 | - YiYanChatCompletion chatCompletion = response.getBody(); | 78 | + YiYanChatCompletionResponse chatCompletion = response.getBody(); |
| 79 | + // TODO @fan:为空时,参考 OpenAiChatClient 的封装; | ||
| 80 | + // TODO @fan:chatResponseMetadata,参考 OpenAiChatResponseMetadata.from(completionEntity.getBody()) | ||
| 81 | return new ChatResponse(List.of(new Generation(chatCompletion.getResult()))); | 81 | return new ChatResponse(List.of(new Generation(chatCompletion.getResult()))); |
| 82 | }); | 82 | }); |
| 83 | } | 83 | } |
| 84 | 84 | ||
| 85 | @Override | 85 | @Override |
| 86 | public Flux<ChatResponse> stream(Prompt prompt) { | 86 | public Flux<ChatResponse> stream(Prompt prompt) { |
| 87 | - // ctx 会有重试的信息 | ||
| 88 | - // 创建 request 请求,stream模式需要供应商支持 | ||
| 89 | YiYanChatCompletionRequest request = this.createRequest(prompt, true); | 87 | YiYanChatCompletionRequest request = this.createRequest(prompt, true); |
| 88 | + // TODO @fan:return this.retryTemplate.execute(ctx -> { | ||
| 90 | // 调用 callWithFunctionSupport 发送请求 | 89 | // 调用 callWithFunctionSupport 发送请求 |
| 91 | - Flux<YiYanChatCompletion> response = this.yiYanApi.chatCompletionStream(request); | 90 | + Flux<YiYanChatCompletionResponse> response = this.yiYanApi.chatCompletionStream(request); |
| 91 | + // TODO @fan:下面的 doOnComplete 是不是可以删除哈? | ||
| 92 | response.doOnComplete(new Runnable() { | 92 | response.doOnComplete(new Runnable() { |
| 93 | @Override | 93 | @Override |
| 94 | public void run() { | 94 | public void run() { |
| 95 | String a = ";"; | 95 | String a = ";"; |
| 96 | } | 96 | } |
| 97 | }); | 97 | }); |
| 98 | - return response.map(res -> { | ||
| 99 | - // TODO @fan:这里缺少了 usage 的封装 | ||
| 100 | - return new ChatResponse(List.of(new Generation(res.getResult()))); | 98 | + return response.map(chunk -> { |
| 99 | + // TODO @fan:ChatResponseMetadata chatResponseMetadata | ||
| 100 | + return new ChatResponse(List.of(new Generation(chunk.getResult()))); | ||
| 101 | }); | 101 | }); |
| 102 | } | 102 | } |
| 103 | 103 | ||
| 104 | private YiYanChatCompletionRequest createRequest(Prompt prompt, boolean stream) { | 104 | private YiYanChatCompletionRequest createRequest(Prompt prompt, boolean stream) { |
| 105 | - // 获取配置 | ||
| 106 | - YiYanOptions useOptions = getYiYanOptions(prompt); | ||
| 107 | - // 创建 request | ||
| 108 | - | ||
| 109 | - // tip: 百度的 system 不在 message 里面 | ||
| 110 | - // tip:百度的 message 只有 user 和 assistant | ||
| 111 | - // https://cloud.baidu.com/doc/WENXINWORKSHOP/s/clntwmv7t | ||
| 112 | - | ||
| 113 | - // 获取 user 和 assistant | 105 | + // 参考 https://cloud.baidu.com/doc/WENXINWORKSHOP/s/clntwmv7t 文档,system 是独立字段 |
| 106 | + // 1.1 获取 user 和 assistant | ||
| 114 | List<YiYanChatCompletionRequest.Message> messageList = prompt.getInstructions().stream() | 107 | List<YiYanChatCompletionRequest.Message> messageList = prompt.getInstructions().stream() |
| 115 | // 过滤 system | 108 | // 过滤 system |
| 116 | .filter(msg -> MessageType.SYSTEM != msg.getMessageType()) | 109 | .filter(msg -> MessageType.SYSTEM != msg.getMessageType()) |
| 117 | - .map(msg -> new YiYanChatCompletionRequest.Message() | ||
| 118 | - .setRole(msg.getMessageType().getValue()) | ||
| 119 | - .setContent(msg.getContent()) | 110 | + .map(message -> new YiYanChatCompletionRequest.Message() |
| 111 | + .setRole(message.getMessageType().getValue()).setContent(message.getContent()) | ||
| 120 | ).toList(); | 112 | ).toList(); |
| 121 | - // 获取 system | 113 | + // 1.2 获取 system |
| 122 | String systemPrompt = prompt.getInstructions().stream() | 114 | String systemPrompt = prompt.getInstructions().stream() |
| 123 | - .filter(msg -> MessageType.SYSTEM == msg.getMessageType()) | 115 | + .filter(message -> MessageType.SYSTEM == message.getMessageType()) |
| 124 | .map(Message::getContent) | 116 | .map(Message::getContent) |
| 125 | .collect(Collectors.joining()); | 117 | .collect(Collectors.joining()); |
| 126 | 118 | ||
| 119 | + // 3. 创建 request | ||
| 127 | YiYanChatCompletionRequest request = new YiYanChatCompletionRequest(messageList); | 120 | YiYanChatCompletionRequest request = new YiYanChatCompletionRequest(messageList); |
| 128 | - // 复制 qianWenOptions 属性取 request(这里 options 属性和 request 基本保持一致) | ||
| 129 | - // top: 由于遵循 spring-ai规范,支持在构建client的时候传入默认的 chatOptions | 121 | + // 复制 YiYanOptions 属性,到 request 中(这里 options 属性和 request 基本保持一致) |
| 122 | + YiYanChatOptions useOptions = getYiYanOptions(prompt); | ||
| 130 | BeanUtil.copyProperties(useOptions, request); | 123 | BeanUtil.copyProperties(useOptions, request); |
| 131 | - request.setTop_p(useOptions.getTopP()); | ||
| 132 | - request.setMax_output_tokens(useOptions.getMaxOutputTokens()); | ||
| 133 | - request.setTemperature(useOptions.getTemperature()); | ||
| 134 | - request.setSystem(systemPrompt); | ||
| 135 | - // 设置 stream | ||
| 136 | - request.setStream(stream); | 124 | + request.setTop_p(useOptions.getTopP()) |
| 125 | + .setMax_output_tokens(useOptions.getMaxOutputTokens()) | ||
| 126 | + .setTemperature(useOptions.getTemperature()) | ||
| 127 | + .setSystem(systemPrompt) | ||
| 128 | + .setStream(stream); | ||
| 137 | return request; | 129 | return request; |
| 138 | } | 130 | } |
| 139 | 131 | ||
| 140 | - private @NotNull YiYanOptions getYiYanOptions(Prompt prompt) { | 132 | + // TODO @fan:Options 的处理,参考下 OpenAiChatClient 的 createRequest |
| 133 | + private YiYanChatOptions getYiYanOptions(Prompt prompt) { | ||
| 141 | // 两个都为null 则没有配置文件 | 134 | // 两个都为null 则没有配置文件 |
| 142 | - if (yiYanOptions == null && prompt.getOptions() == null) { | 135 | + if (defaultOptions == null && prompt.getOptions() == null) { |
| 136 | + // TODO @fan:IllegalArgumentException 参数更好哈 | ||
| 143 | throw new ChatException("ChatOptions 未配置参数!"); | 137 | throw new ChatException("ChatOptions 未配置参数!"); |
| 144 | } | 138 | } |
| 145 | // 优先使用 Prompt 里面的 ChatOptions | 139 | // 优先使用 Prompt 里面的 ChatOptions |
| 146 | - ChatOptions options = yiYanOptions; | 140 | + ChatOptions options = defaultOptions; |
| 147 | if (prompt.getOptions() != null) { | 141 | if (prompt.getOptions() != null) { |
| 148 | options = (ChatOptions) prompt.getOptions(); | 142 | options = (ChatOptions) prompt.getOptions(); |
| 149 | } | 143 | } |
| 150 | // Prompt 里面是一个 ChatOptions,用户可以随意传入,这里做一下判断 | 144 | // Prompt 里面是一个 ChatOptions,用户可以随意传入,这里做一下判断 |
| 151 | - if (!(options instanceof YiYanOptions)) { | 145 | + if (!(options instanceof YiYanChatOptions)) { |
| 146 | + // TODO @fan:IllegalArgumentException 参数更好哈 | ||
| 147 | + // TODO @fan:需要兼容 ChatOptionsBuilder 创建出来的 | ||
| 152 | throw new ChatException("Prompt 传入的不是 YiYanOptions!"); | 148 | throw new ChatException("Prompt 传入的不是 YiYanOptions!"); |
| 153 | } | 149 | } |
| 154 | // 转换 YiYanOptions | 150 | // 转换 YiYanOptions |
| 155 | - YiYanOptions useOptions = (YiYanOptions) options; | ||
| 156 | - return useOptions; | 151 | + return (YiYanChatOptions) options; |
| 157 | } | 152 | } |
| 153 | + | ||
| 158 | } | 154 | } |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/YiYanOptions.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/YiYanChatOptions.java
| 1 | -package org.springframework.ai.models.yiyan; | 1 | +package cn.iocoder.yudao.framework.ai.core.model.yiyan; |
| 2 | 2 | ||
| 3 | -import org.springframework.ai.chat.prompt.ChatOptions; | ||
| 4 | -import org.springframework.ai.models.yiyan.api.YiYanChatCompletionRequest; | 3 | +import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanChatCompletionRequest; |
| 5 | import lombok.Data; | 4 | import lombok.Data; |
| 6 | -import lombok.experimental.Accessors; | 5 | +import org.springframework.ai.chat.prompt.ChatOptions; |
| 7 | 6 | ||
| 8 | import java.util.List; | 7 | import java.util.List; |
| 9 | 8 | ||
| 9 | +// TODO @fan:字段命名,penalty_score 类似的,建议改成驼峰原则 | ||
| 10 | +// TODO @fan:字段的注释,可以都删除掉,让用户 https://cloud.baidu.com/doc/WENXINWORKSHOP/s/clntwmv7t 即可 | ||
| 10 | /** | 11 | /** |
| 11 | - * 百度 问心一言 | 12 | + * 文心一言的 {@link ChatOptions} 实现类 |
| 12 | * | 13 | * |
| 13 | - * 文档地址:https://cloud.baidu.com/doc/WENXINWORKSHOP/s/clntwmv7t | 14 | + * 字段说明:参考 <a href="https://cloud.baidu.com/doc/WENXINWORKSHOP/s/clntwmv7t">ERNIE-4.0-8K</a> |
| 14 | * | 15 | * |
| 15 | - * author: fansili | ||
| 16 | - * time: 2024/3/16 19:33 | 16 | + * @author fansili |
| 17 | */ | 17 | */ |
| 18 | @Data | 18 | @Data |
| 19 | -@Accessors(chain = true) | ||
| 20 | -public class YiYanOptions implements ChatOptions { | 19 | +public class YiYanChatOptions implements ChatOptions { |
| 21 | 20 | ||
| 22 | /** | 21 | /** |
| 23 | * 一个可触发函数的描述列表,说明: | 22 | * 一个可触发函数的描述列表,说明: |
| @@ -106,37 +105,24 @@ public class YiYanOptions implements ChatOptions { | @@ -106,37 +105,24 @@ public class YiYanOptions implements ChatOptions { | ||
| 106 | */ | 105 | */ |
| 107 | private String tool_choice; | 106 | private String tool_choice; |
| 108 | 107 | ||
| 109 | - // | ||
| 110 | - // 以下兼容 spring-ai ChatOptions 暂时没有其他地方用到 | ||
| 111 | - | ||
| 112 | @Override | 108 | @Override |
| 113 | public Float getTemperature() { | 109 | public Float getTemperature() { |
| 114 | return this.temperature; | 110 | return this.temperature; |
| 115 | } | 111 | } |
| 116 | 112 | ||
| 117 | -// @Override | ||
| 118 | -// public void setTemperature(Float temperature) { | ||
| 119 | -// this.temperature = temperature; | ||
| 120 | -// } | ||
| 121 | - | ||
| 122 | @Override | 113 | @Override |
| 123 | public Float getTopP() { | 114 | public Float getTopP() { |
| 124 | return topP; | 115 | return topP; |
| 125 | } | 116 | } |
| 126 | 117 | ||
| 127 | -// @Override | ||
| 128 | -// public void setTopP(Float topP) { | ||
| 129 | -// this.topP = topP; | ||
| 130 | -// } | ||
| 131 | - | ||
| 132 | - // 百度么有 topK | ||
| 133 | - | 118 | + /** |
| 119 | + * 百度么有 topK | ||
| 120 | + * | ||
| 121 | + * @return null | ||
| 122 | + */ | ||
| 134 | @Override | 123 | @Override |
| 135 | public Integer getTopK() { | 124 | public Integer getTopK() { |
| 136 | return null; | 125 | return null; |
| 137 | } | 126 | } |
| 138 | 127 | ||
| 139 | -// @Override | ||
| 140 | -// public void setTopK(Integer topK) { | ||
| 141 | -// } | ||
| 142 | } | 128 | } |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/api/YiYanApi.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanApi.java
| 1 | -package org.springframework.ai.models.yiyan.api; | 1 | +package cn.iocoder.yudao.framework.ai.core.model.yiyan.api; |
| 2 | 2 | ||
| 3 | -import org.springframework.ai.models.yiyan.YiYanChatModel; | ||
| 4 | -import org.springframework.ai.models.yiyan.exception.YiYanApiException; | ||
| 5 | -import lombok.Data; | 3 | +import cn.iocoder.yudao.framework.ai.core.model.yiyan.exception.YiYanApiException; |
| 6 | import org.springframework.http.HttpStatusCode; | 4 | import org.springframework.http.HttpStatusCode; |
| 7 | import org.springframework.http.ResponseEntity; | 5 | import org.springframework.http.ResponseEntity; |
| 8 | import org.springframework.web.reactive.function.client.WebClient; | 6 | import org.springframework.web.reactive.function.client.WebClient; |
| @@ -10,47 +8,55 @@ import reactor.core.publisher.Flux; | @@ -10,47 +8,55 @@ import reactor.core.publisher.Flux; | ||
| 10 | import reactor.core.publisher.Mono; | 8 | import reactor.core.publisher.Mono; |
| 11 | 9 | ||
| 12 | /** | 10 | /** |
| 13 | - * 文心一言 | ||
| 14 | - * <p> | ||
| 15 | - * author: fansili | ||
| 16 | - * time: 2024/3/8 21:47 | 11 | + * 文心一言 API |
| 12 | + * | ||
| 13 | + * @author fansili | ||
| 17 | */ | 14 | */ |
| 18 | -@Data | ||
| 19 | public class YiYanApi { | 15 | public class YiYanApi { |
| 20 | 16 | ||
| 21 | private static final String DEFAULT_BASE_URL = "https://aip.baidubce.com"; | 17 | private static final String DEFAULT_BASE_URL = "https://aip.baidubce.com"; |
| 22 | 18 | ||
| 23 | private static final String AUTH_2_TOKEN_URI = "/oauth/2.0/token"; | 19 | private static final String AUTH_2_TOKEN_URI = "/oauth/2.0/token"; |
| 24 | 20 | ||
| 25 | - public static final String DEFAULT_CHAT_MODEL = "ERNIE 4.0"; | 21 | + public static final String DEFAULT_CHAT_MODEL = YiYanChatModel.ERNIE4_0.getModel(); |
| 26 | 22 | ||
| 27 | - // 获取access_token流程 https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Ilkkrb0i5 | ||
| 28 | - private String appKey; | ||
| 29 | - private String secretKey; | ||
| 30 | - private String token; | ||
| 31 | - // token刷新时间(秒) | 23 | + private final String appKey; |
| 24 | + private final String secretKey; | ||
| 25 | + /** | ||
| 26 | + * TODO fan:这个是不是要有个刷新机制哈;如果目前不需要,可以删除掉 refreshTokenSecondTime;整体更简洁; | ||
| 27 | + */ | ||
| 28 | + private final String token; | ||
| 29 | + /** | ||
| 30 | + * token 刷新时间(秒) | ||
| 31 | + */ | ||
| 32 | private int refreshTokenSecondTime; | 32 | private int refreshTokenSecondTime; |
| 33 | - // 发送请求 webClient | 33 | + /** |
| 34 | + * 发送请求 webClient | ||
| 35 | + */ | ||
| 34 | private final WebClient webClient; | 36 | private final WebClient webClient; |
| 35 | - // 使用的模型 | ||
| 36 | - private YiYanChatModel useChatModel; | 37 | + /** |
| 38 | + * 使用的模型 | ||
| 39 | + */ | ||
| 40 | + private final YiYanChatModel useChatModel; | ||
| 37 | 41 | ||
| 38 | public YiYanApi(String appKey, String secretKey, YiYanChatModel useChatModel, int refreshTokenSecondTime) { | 42 | public YiYanApi(String appKey, String secretKey, YiYanChatModel useChatModel, int refreshTokenSecondTime) { |
| 39 | this.appKey = appKey; | 43 | this.appKey = appKey; |
| 40 | this.secretKey = secretKey; | 44 | this.secretKey = secretKey; |
| 41 | this.useChatModel = useChatModel; | 45 | this.useChatModel = useChatModel; |
| 42 | this.refreshTokenSecondTime = refreshTokenSecondTime; | 46 | this.refreshTokenSecondTime = refreshTokenSecondTime; |
| 43 | - | ||
| 44 | - this.webClient = WebClient.builder() | ||
| 45 | - .baseUrl(DEFAULT_BASE_URL) | ||
| 46 | - .build(); | ||
| 47 | - | 47 | + this.webClient = WebClient.builder().baseUrl(DEFAULT_BASE_URL).build(); |
| 48 | + // 获取访问令牌 | ||
| 48 | token = getToken(); | 49 | token = getToken(); |
| 49 | } | 50 | } |
| 50 | 51 | ||
| 52 | + /** | ||
| 53 | + * 获得访问令牌 | ||
| 54 | + * | ||
| 55 | + * @see <a href="https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Ilkkrb0i5">文档地址</> | ||
| 56 | + * @return 访问令牌 | ||
| 57 | + */ | ||
| 51 | private String getToken() { | 58 | private String getToken() { |
| 52 | - // 文档地址: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Ilkkrb0i5 | ||
| 53 | - ResponseEntity<YiYanAuthRes> response = this.webClient.post() | 59 | + ResponseEntity<YiYanAuthResponse> response = this.webClient.post() |
| 54 | .uri(uriBuilder -> uriBuilder.path(AUTH_2_TOKEN_URI) | 60 | .uri(uriBuilder -> uriBuilder.path(AUTH_2_TOKEN_URI) |
| 55 | .queryParam("grant_type", "client_credentials") | 61 | .queryParam("grant_type", "client_credentials") |
| 56 | .queryParam("client_id", appKey) | 62 | .queryParam("client_id", appKey) |
| @@ -58,17 +64,19 @@ public class YiYanApi { | @@ -58,17 +64,19 @@ public class YiYanApi { | ||
| 58 | .build() | 64 | .build() |
| 59 | ) | 65 | ) |
| 60 | .retrieve() | 66 | .retrieve() |
| 61 | - .toEntity(YiYanAuthRes.class) | 67 | + .toEntity(YiYanAuthResponse.class) |
| 62 | .block(); | 68 | .block(); |
| 63 | // 检查请求状态 | 69 | // 检查请求状态 |
| 64 | - if (HttpStatusCode.valueOf(200) != response.getStatusCode()) { | 70 | + // TODO @fan:可以使用 response.getStatusCode().is2xxSuccessful() |
| 71 | + if (HttpStatusCode.valueOf(200) != response.getStatusCode() | ||
| 72 | + || response.getBody() == null) { | ||
| 73 | + // TODO @fan:可以使用 IllegalStateException 替代;另外,最好打印下返回;方便排错; | ||
| 65 | throw new YiYanApiException("一言认证失败! api:https://aip.baidubce.com/oauth/2.0/token 请检查 client_id、client_secret 是否正确!"); | 74 | throw new YiYanApiException("一言认证失败! api:https://aip.baidubce.com/oauth/2.0/token 请检查 client_id、client_secret 是否正确!"); |
| 66 | } | 75 | } |
| 67 | - YiYanAuthRes body = response.getBody(); | ||
| 68 | - return body.getAccess_token(); | 76 | + return response.getBody().getAccess_token(); |
| 69 | } | 77 | } |
| 70 | 78 | ||
| 71 | - public ResponseEntity<YiYanChatCompletion> chatCompletionEntity(YiYanChatCompletionRequest request) { | 79 | + public ResponseEntity<YiYanChatCompletionResponse> chatCompletionEntity(YiYanChatCompletionRequest request) { |
| 72 | // TODO: 2024/3/10 小范 这里错误信息返回的结构不一样 | 80 | // TODO: 2024/3/10 小范 这里错误信息返回的结构不一样 |
| 73 | // {"error_code":17,"error_msg":"Open api daily request limit reached"} | 81 | // {"error_code":17,"error_msg":"Open api daily request limit reached"} |
| 74 | return this.webClient.post() | 82 | return this.webClient.post() |
| @@ -78,11 +86,11 @@ public class YiYanApi { | @@ -78,11 +86,11 @@ public class YiYanApi { | ||
| 78 | .build()) | 86 | .build()) |
| 79 | .body(Mono.just(request), YiYanChatCompletionRequest.class) | 87 | .body(Mono.just(request), YiYanChatCompletionRequest.class) |
| 80 | .retrieve() | 88 | .retrieve() |
| 81 | - .toEntity(YiYanChatCompletion.class) | 89 | + .toEntity(YiYanChatCompletionResponse.class) |
| 82 | .block(); | 90 | .block(); |
| 83 | } | 91 | } |
| 84 | 92 | ||
| 85 | - public Flux<YiYanChatCompletion> chatCompletionStream(YiYanChatCompletionRequest request) { | 93 | + public Flux<YiYanChatCompletionResponse> chatCompletionStream(YiYanChatCompletionRequest request) { |
| 86 | return this.webClient.post() | 94 | return this.webClient.post() |
| 87 | .uri(uriBuilder | 95 | .uri(uriBuilder |
| 88 | -> uriBuilder.path(useChatModel.getUri()) | 96 | -> uriBuilder.path(useChatModel.getUri()) |
| @@ -90,6 +98,7 @@ public class YiYanApi { | @@ -90,6 +98,7 @@ public class YiYanApi { | ||
| 90 | .build()) | 98 | .build()) |
| 91 | .body(Mono.just(request), YiYanChatCompletionRequest.class) | 99 | .body(Mono.just(request), YiYanChatCompletionRequest.class) |
| 92 | .retrieve() | 100 | .retrieve() |
| 93 | - .bodyToFlux(YiYanChatCompletion.class); | 101 | + .bodyToFlux(YiYanChatCompletionResponse.class); |
| 94 | } | 102 | } |
| 103 | + | ||
| 95 | } | 104 | } |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/api/YiYanAuthRes.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanAuthResponse.java
| 1 | -package org.springframework.ai.models.yiyan.api; | 1 | +package cn.iocoder.yudao.framework.ai.core.model.yiyan.api; |
| 2 | 2 | ||
| 3 | import lombok.Data; | 3 | import lombok.Data; |
| 4 | 4 | ||
| 5 | +// TODO @fan:字段驼峰;字段注释都可以删除,贴个链接就好; | ||
| 5 | /** | 6 | /** |
| 6 | - * 一言 获取access_token | 7 | + * 获取文心一言的 access_token 的 Response |
| 7 | * | 8 | * |
| 8 | - * author: fansili | ||
| 9 | - * time: 2024/3/10 08:51 | 9 | + * @author fansili |
| 10 | */ | 10 | */ |
| 11 | @Data | 11 | @Data |
| 12 | -public class YiYanAuthRes { | 12 | +public class YiYanAuthResponse { |
| 13 | 13 | ||
| 14 | /** | 14 | /** |
| 15 | * 访问凭证 | 15 | * 访问凭证 |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/api/YiYanChatCompletionRequest.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanChatCompletionRequest.java
| 1 | -package org.springframework.ai.models.yiyan.api; | 1 | +package cn.iocoder.yudao.framework.ai.core.model.yiyan.api; |
| 2 | 2 | ||
| 3 | import lombok.Data; | 3 | import lombok.Data; |
| 4 | 4 | ||
| 5 | import java.util.List; | 5 | import java.util.List; |
| 6 | 6 | ||
| 7 | +// TODO @fan:字段驼峰;字段注释都可以删除,贴个链接就好; | ||
| 7 | /** | 8 | /** |
| 8 | - * 一言 Completion req | 9 | + * 文心一言 Completion Request |
| 9 | * | 10 | * |
| 10 | - * 百度千帆文档:https://cloud.baidu.com/doc/WENXINWORKSHOP/s/jlil56u11 | 11 | + * 百度千帆文档:https://cloud.baidu.com/doc/WENXINWORKSHOP/s/jlil56u11 |
| 11 | * | 12 | * |
| 12 | - * author: fansili | ||
| 13 | - * time: 2024/3/9 10:34 | 13 | + * @author fansili |
| 14 | */ | 14 | */ |
| 15 | @Data | 15 | @Data |
| 16 | public class YiYanChatCompletionRequest { | 16 | public class YiYanChatCompletionRequest { |
| @@ -114,9 +114,11 @@ public class YiYanChatCompletionRequest { | @@ -114,9 +114,11 @@ public class YiYanChatCompletionRequest { | ||
| 114 | 114 | ||
| 115 | @Data | 115 | @Data |
| 116 | public static class Message { | 116 | public static class Message { |
| 117 | + | ||
| 117 | private String role; | 118 | private String role; |
| 118 | 119 | ||
| 119 | private String content; | 120 | private String content; |
| 121 | + | ||
| 120 | } | 122 | } |
| 121 | 123 | ||
| 122 | @Data | 124 | @Data |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/api/YiYanChatCompletion.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanChatCompletionResponse.java
| 1 | -package org.springframework.ai.models.yiyan.api; | 1 | +package cn.iocoder.yudao.framework.ai.core.model.yiyan.api; |
| 2 | 2 | ||
| 3 | import lombok.Data; | 3 | import lombok.Data; |
| 4 | 4 | ||
| 5 | /** | 5 | /** |
| 6 | - * 聊天返回 | 6 | + * 文心一言 Completion Response |
| 7 | + * | ||
| 7 | * 百度链接: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/clntwmv7t | 8 | * 百度链接: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/clntwmv7t |
| 8 | * | 9 | * |
| 9 | - * author: fansili | ||
| 10 | - * time: 2024/3/9 10:34 | 10 | + * @author fansili |
| 11 | */ | 11 | */ |
| 12 | @Data | 12 | @Data |
| 13 | -public class YiYanChatCompletion { | 13 | +public class YiYanChatCompletionResponse { |
| 14 | 14 | ||
| 15 | /** | 15 | /** |
| 16 | * 本轮对话的id | 16 | * 本轮对话的id |
| @@ -88,4 +88,5 @@ public class YiYanChatCompletion { | @@ -88,4 +88,5 @@ public class YiYanChatCompletion { | ||
| 88 | */ | 88 | */ |
| 89 | private int total_tokens; | 89 | private int total_tokens; |
| 90 | } | 90 | } |
| 91 | + | ||
| 91 | } | 92 | } |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/YiYanChatModel.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/api/YiYanChatModel.java
| 1 | -package org.springframework.ai.models.yiyan; | 1 | +package cn.iocoder.yudao.framework.ai.core.model.yiyan.api; |
| 2 | 2 | ||
| 3 | import lombok.AllArgsConstructor; | 3 | import lombok.AllArgsConstructor; |
| 4 | import lombok.Getter; | 4 | import lombok.Getter; |
| 5 | 5 | ||
| 6 | /** | 6 | /** |
| 7 | - * 一言模型 | 7 | + * 文心一言的模型枚举 |
| 8 | * | 8 | * |
| 9 | - * 可参考百度文档:https://cloud.baidu.com/doc/WENXINWORKSHOP/s/clntwmv7t | 9 | + * 可参考 <a href="https://cloud.baidu.com/doc/WENXINWORKSHOP/s/clntwmv7t">百度文档</> |
| 10 | * | 10 | * |
| 11 | - * author: fansili | ||
| 12 | - * time: 2024/3/9 12:01 | 11 | + * @author fansili |
| 13 | */ | 12 | */ |
| 14 | @Getter | 13 | @Getter |
| 15 | @AllArgsConstructor | 14 | @AllArgsConstructor |
| @@ -18,21 +17,24 @@ public enum YiYanChatModel { | @@ -18,21 +17,24 @@ public enum YiYanChatModel { | ||
| 18 | ERNIE4_0("ERNIE 4.0", "/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro"), | 17 | ERNIE4_0("ERNIE 4.0", "/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro"), |
| 19 | ERNIE4_3_5_8K("ERNIE-3.5-8K", "/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions"), | 18 | ERNIE4_3_5_8K("ERNIE-3.5-8K", "/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions"), |
| 20 | ERNIE4_3_5_8K_0205("ERNIE-3.5-8K-0205", "/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-3.5-8k-0205"), | 19 | ERNIE4_3_5_8K_0205("ERNIE-3.5-8K-0205", "/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-3.5-8k-0205"), |
| 21 | - | ||
| 22 | ERNIE4_3_5_8K_1222("ERNIE-3.5-8K-1222", "/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-3.5-8k-1222"), | 20 | ERNIE4_3_5_8K_1222("ERNIE-3.5-8K-1222", "/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-3.5-8k-1222"), |
| 23 | ERNIE4_BOT_8K("ERNIE-Bot-8K", "/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie_bot_8k"), | 21 | ERNIE4_BOT_8K("ERNIE-Bot-8K", "/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie_bot_8k"), |
| 24 | ERNIE4_3_5_4K_0205("ERNIE-3.5-4K-0205", "/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-3.5-4k-0205"), | 22 | ERNIE4_3_5_4K_0205("ERNIE-3.5-4K-0205", "/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-3.5-4k-0205"), |
| 25 | - | ||
| 26 | ; | 23 | ; |
| 27 | 24 | ||
| 28 | - private String model; | ||
| 29 | - | ||
| 30 | - private String uri; | 25 | + /** |
| 26 | + * 模型名 | ||
| 27 | + */ | ||
| 28 | + private final String model; | ||
| 29 | + /** | ||
| 30 | + * API URL | ||
| 31 | + */ | ||
| 32 | + private final String uri; | ||
| 31 | 33 | ||
| 32 | public static YiYanChatModel valueOfModel(String model) { | 34 | public static YiYanChatModel valueOfModel(String model) { |
| 33 | - for (YiYanChatModel itemEnum : YiYanChatModel.values()) { | ||
| 34 | - if (itemEnum.getModel().equals(model)) { | ||
| 35 | - return itemEnum; | 35 | + for (YiYanChatModel modelEnum : YiYanChatModel.values()) { |
| 36 | + if (modelEnum.getModel().equals(model)) { | ||
| 37 | + return modelEnum; | ||
| 36 | } | 38 | } |
| 37 | } | 39 | } |
| 38 | throw new IllegalArgumentException("Invalid MessageType value: " + model); | 40 | throw new IllegalArgumentException("Invalid MessageType value: " + model); |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/exception/YiYanApiException.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/yiyan/exception/YiYanApiException.java
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.*; | @@ -5,7 +5,7 @@ import org.springframework.ai.chat.*; | ||
| 5 | import org.springframework.ai.chat.prompt.ChatOptions; | 5 | import org.springframework.ai.chat.prompt.ChatOptions; |
| 6 | import org.springframework.ai.chat.prompt.Prompt; | 6 | import org.springframework.ai.chat.prompt.Prompt; |
| 7 | import org.springframework.ai.models.tongyi.api.QianWenApi; | 7 | import org.springframework.ai.models.tongyi.api.QianWenApi; |
| 8 | -import org.springframework.ai.models.yiyan.exception.YiYanApiException; | 8 | +import cn.iocoder.yudao.framework.ai.core.model.yiyan.exception.YiYanApiException; |
| 9 | import com.alibaba.dashscope.aigc.generation.GenerationResult; | 9 | import com.alibaba.dashscope.aigc.generation.GenerationResult; |
| 10 | import com.alibaba.dashscope.aigc.generation.models.QwenParam; | 10 | import com.alibaba.dashscope.aigc.generation.models.QwenParam; |
| 11 | import com.alibaba.dashscope.common.Message; | 11 | import com.alibaba.dashscope.common.Message; |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/org/springframework/ai/models/yiyan/api/YiYanChatCompletionMessage.java deleted
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; | @@ -5,10 +5,10 @@ import org.springframework.ai.chat.messages.Message; | ||
| 5 | import org.springframework.ai.chat.messages.SystemMessage; | 5 | import org.springframework.ai.chat.messages.SystemMessage; |
| 6 | import org.springframework.ai.chat.messages.UserMessage; | 6 | import org.springframework.ai.chat.messages.UserMessage; |
| 7 | import org.springframework.ai.chat.prompt.Prompt; | 7 | import org.springframework.ai.chat.prompt.Prompt; |
| 8 | -import org.springframework.ai.models.yiyan.YiYanChatClient; | ||
| 9 | -import org.springframework.ai.models.yiyan.YiYanChatModel; | ||
| 10 | -import org.springframework.ai.models.yiyan.YiYanOptions; | ||
| 11 | -import org.springframework.ai.models.yiyan.api.YiYanApi; | 8 | +import cn.iocoder.yudao.framework.ai.core.model.yiyan.YiYanChatClient; |
| 9 | +import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanChatModel; | ||
| 10 | +import cn.iocoder.yudao.framework.ai.core.model.yiyan.YiYanChatOptions; | ||
| 11 | +import cn.iocoder.yudao.framework.ai.core.model.yiyan.api.YiYanApi; | ||
| 12 | import org.junit.Before; | 12 | import org.junit.Before; |
| 13 | import org.junit.Test; | 13 | import org.junit.Test; |
| 14 | import reactor.core.publisher.Flux; | 14 | import reactor.core.publisher.Flux; |
| @@ -35,7 +35,7 @@ public class YiYanChatTests { | @@ -35,7 +35,7 @@ public class YiYanChatTests { | ||
| 35 | YiYanChatModel.ERNIE4_3_5_8K, | 35 | YiYanChatModel.ERNIE4_3_5_8K, |
| 36 | 86400 | 36 | 86400 |
| 37 | ); | 37 | ); |
| 38 | - YiYanOptions yiYanOptions = new YiYanOptions(); | 38 | + YiYanChatOptions yiYanOptions = new YiYanChatOptions(); |
| 39 | yiYanOptions.setMaxOutputTokens(2048); | 39 | yiYanOptions.setMaxOutputTokens(2048); |
| 40 | yiYanOptions.setTopP(0.6f); | 40 | yiYanOptions.setTopP(0.6f); |
| 41 | yiYanOptions.setTemperature(0.85f); | 41 | yiYanOptions.setTemperature(0.85f); |
yudao-server/src/main/resources/application-local.yaml
| @@ -230,17 +230,6 @@ yudao: | @@ -230,17 +230,6 @@ yudao: | ||
| 230 | appKey: cb6415c19d6162cda07b47316fcb0416 | 230 | appKey: cb6415c19d6162cda07b47316fcb0416 |
| 231 | secretKey: Y2JiYTIxZjA3MDMxMjNjZjQzYzVmNzdh | 231 | secretKey: Y2JiYTIxZjA3MDMxMjNjZjQzYzVmNzdh |
| 232 | model: XING_HUO_3_5 | 232 | model: XING_HUO_3_5 |
| 233 | - yiyan: | ||
| 234 | - enable: true | ||
| 235 | - aiPlatform: YI_YAN | ||
| 236 | - max-tokens: 1500 | ||
| 237 | - temperature: 0.85 | ||
| 238 | - topP: 0.8 | ||
| 239 | - topK: 0 | ||
| 240 | - appKey: x0cuLZ7XsaTCU08vuJWO87Lg | ||
| 241 | - secretKey: R9mYF9dl9KASgi5RUq0FQt3wRisSnOcK | ||
| 242 | - refreshTokenSecondTime: 86400 | ||
| 243 | - model: ERNIE4_3_5_8K | ||
| 244 | openAiImage: | 233 | openAiImage: |
| 245 | enable: true | 234 | enable: true |
| 246 | api-key: ${OPEN_AI_KEY} | 235 | api-key: ${OPEN_AI_KEY} |
yudao-server/src/main/resources/application.yaml
| @@ -150,6 +150,19 @@ spring.ai: | @@ -150,6 +150,19 @@ spring.ai: | ||
| 150 | chat: | 150 | chat: |
| 151 | model: llama3 | 151 | model: llama3 |
| 152 | 152 | ||
| 153 | +yudao.ai: | ||
| 154 | + yiyan: | ||
| 155 | + enable: true | ||
| 156 | + aiPlatform: YIYAN # TODO @fan:建议每个都独立配置属性类 | ||
| 157 | + max-tokens: 1500 | ||
| 158 | + temperature: 0.85 | ||
| 159 | + topP: 0.8 | ||
| 160 | + topK: 0 | ||
| 161 | + appKey: x0cuLZ7XsaTCU08vuJWO87Lg | ||
| 162 | + secretKey: R9mYF9dl9KASgi5RUq0FQt3wRisSnOcK | ||
| 163 | + refreshTokenSecondTime: 86400 | ||
| 164 | + model: ERNIE4_3_5_8K | ||
| 165 | + | ||
| 153 | --- #################### 芋道相关配置 #################### | 166 | --- #################### 芋道相关配置 #################### |
| 154 | 167 | ||
| 155 | yudao: | 168 | yudao: |