Commit ca0024988caefc33b4e353db16227a6ef3b7e471
1 parent
a34d9259
【优化】模型enum统一交 model
Showing
7 changed files
with
68 additions
and
12 deletions
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatqianwen/QianWenChatClient.java
| @@ -100,7 +100,7 @@ public class QianWenChatClient implements ChatClient, StreamingChatClient { | @@ -100,7 +100,7 @@ public class QianWenChatClient implements ChatClient, StreamingChatClient { | ||
| 100 | messageList.add(message); | 100 | messageList.add(message); |
| 101 | }); | 101 | }); |
| 102 | return QwenParam.builder() | 102 | return QwenParam.builder() |
| 103 | - .model(qianWenApi.getQianWenChatModal().getValue()) | 103 | + .model(qianWenApi.getQianWenChatModal().getModel()) |
| 104 | .prompt(prompt.getContents()) | 104 | .prompt(prompt.getContents()) |
| 105 | .messages(messageList) | 105 | .messages(messageList) |
| 106 | .maxTokens(chatOptions.getMaxTokens()) | 106 | .maxTokens(chatOptions.getMaxTokens()) |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatqianwen/QianWenChatModal.java
| @@ -34,5 +34,14 @@ public enum QianWenChatModal { | @@ -34,5 +34,14 @@ public enum QianWenChatModal { | ||
| 34 | 34 | ||
| 35 | private String name; | 35 | private String name; |
| 36 | 36 | ||
| 37 | - private String value; | 37 | + private String model; |
| 38 | + | ||
| 39 | + public static QianWenChatModal valueOfModel(String model) { | ||
| 40 | + for (QianWenChatModal itemEnum : QianWenChatModal.values()) { | ||
| 41 | + if (itemEnum.getModel().equals(model)) { | ||
| 42 | + return itemEnum; | ||
| 43 | + } | ||
| 44 | + } | ||
| 45 | + throw new IllegalArgumentException("Invalid MessageType value: " + model); | ||
| 46 | + } | ||
| 38 | } | 47 | } |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatxinghuo/XingHuoChatClient.java
| @@ -114,7 +114,7 @@ public class XingHuoChatClient implements ChatClient, StreamingChatClient { | @@ -114,7 +114,7 @@ public class XingHuoChatClient implements ChatClient, StreamingChatClient { | ||
| 114 | // 创建 params | 114 | // 创建 params |
| 115 | XingHuoChatCompletionRequest.Parameter.Chat chatParameter = new XingHuoChatCompletionRequest.Parameter.Chat(); | 115 | XingHuoChatCompletionRequest.Parameter.Chat chatParameter = new XingHuoChatCompletionRequest.Parameter.Chat(); |
| 116 | BeanUtil.copyProperties(xingHuoOptions, chatParameter); | 116 | BeanUtil.copyProperties(xingHuoOptions, chatParameter); |
| 117 | - chatParameter.setDomain(xingHuoOptions.getChatModel().getValue()); | 117 | + chatParameter.setDomain(xingHuoOptions.getChatModel().getModel()); |
| 118 | XingHuoChatCompletionRequest.Parameter parameter = new XingHuoChatCompletionRequest.Parameter().setChat(chatParameter); | 118 | XingHuoChatCompletionRequest.Parameter parameter = new XingHuoChatCompletionRequest.Parameter().setChat(chatParameter); |
| 119 | // 创建 payload text 信息 | 119 | // 创建 payload text 信息 |
| 120 | List<XingHuoChatCompletionRequest.Payload.Message.Text> texts = prompt.getInstructions().stream().map(message -> { | 120 | List<XingHuoChatCompletionRequest.Payload.Message.Text> texts = prompt.getInstructions().stream().map(message -> { |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatxinghuo/XingHuoChatModel.java
| @@ -30,15 +30,24 @@ public enum XingHuoChatModel { | @@ -30,15 +30,24 @@ public enum XingHuoChatModel { | ||
| 30 | 30 | ||
| 31 | ; | 31 | ; |
| 32 | 32 | ||
| 33 | - XingHuoChatModel(String name, String value, String uri) { | 33 | + XingHuoChatModel(String name, String model, String uri) { |
| 34 | this.name = name; | 34 | this.name = name; |
| 35 | - this.value = value; | 35 | + this.model = model; |
| 36 | this.uri = uri; | 36 | this.uri = uri; |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | private String name; | 39 | private String name; |
| 40 | 40 | ||
| 41 | - private String value; | 41 | + private String model; |
| 42 | 42 | ||
| 43 | private String uri; | 43 | private String uri; |
| 44 | + | ||
| 45 | + public static XingHuoChatModel valueOfModel(String model) { | ||
| 46 | + for (XingHuoChatModel itemEnum : XingHuoChatModel.values()) { | ||
| 47 | + if (itemEnum.getModel().equals(model)) { | ||
| 48 | + return itemEnum; | ||
| 49 | + } | ||
| 50 | + } | ||
| 51 | + throw new IllegalArgumentException("Invalid MessageType value: " + model); | ||
| 52 | + } | ||
| 44 | } | 53 | } |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatyiyan/YiYanChatModel.java
| @@ -25,8 +25,16 @@ public enum YiYanChatModel { | @@ -25,8 +25,16 @@ public enum YiYanChatModel { | ||
| 25 | 25 | ||
| 26 | ; | 26 | ; |
| 27 | 27 | ||
| 28 | - private String value; | 28 | + private String model; |
| 29 | 29 | ||
| 30 | private String uri; | 30 | private String uri; |
| 31 | 31 | ||
| 32 | + public static YiYanChatModel valueOfModel(String model) { | ||
| 33 | + for (YiYanChatModel itemEnum : YiYanChatModel.values()) { | ||
| 34 | + if (itemEnum.getModel().equals(model)) { | ||
| 35 | + return itemEnum; | ||
| 36 | + } | ||
| 37 | + } | ||
| 38 | + throw new IllegalArgumentException("Invalid MessageType value: " + model); | ||
| 39 | + } | ||
| 32 | } | 40 | } |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/config/YudaoAiAutoConfiguration.java
| @@ -10,6 +10,9 @@ import cn.iocoder.yudao.framework.ai.chatxinghuo.api.XingHuoApi; | @@ -10,6 +10,9 @@ import cn.iocoder.yudao.framework.ai.chatxinghuo.api.XingHuoApi; | ||
| 10 | import cn.iocoder.yudao.framework.ai.chatyiyan.YiYanChatClient; | 10 | import cn.iocoder.yudao.framework.ai.chatyiyan.YiYanChatClient; |
| 11 | import cn.iocoder.yudao.framework.ai.chatyiyan.YiYanOptions; | 11 | import cn.iocoder.yudao.framework.ai.chatyiyan.YiYanOptions; |
| 12 | import cn.iocoder.yudao.framework.ai.chatyiyan.api.YiYanApi; | 12 | import cn.iocoder.yudao.framework.ai.chatyiyan.api.YiYanApi; |
| 13 | +import cn.iocoder.yudao.framework.ai.imageopenai.OpenAiImageApi; | ||
| 14 | +import cn.iocoder.yudao.framework.ai.imageopenai.OpenAiImageClient; | ||
| 15 | +import cn.iocoder.yudao.framework.ai.imageopenai.OpenAiImageOptions; | ||
| 13 | import org.springframework.boot.autoconfigure.AutoConfiguration; | 16 | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 14 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | 17 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 15 | import org.springframework.boot.context.properties.EnableConfigurationProperties; | 18 | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| @@ -32,7 +35,7 @@ public class YudaoAiAutoConfiguration { | @@ -32,7 +35,7 @@ public class YudaoAiAutoConfiguration { | ||
| 32 | YudaoAiProperties.XingHuoProperties xingHuoProperties = yudaoAiProperties.getXinghuo(); | 35 | YudaoAiProperties.XingHuoProperties xingHuoProperties = yudaoAiProperties.getXinghuo(); |
| 33 | // 转换配置 | 36 | // 转换配置 |
| 34 | XingHuoOptions xingHuoOptions = new XingHuoOptions(); | 37 | XingHuoOptions xingHuoOptions = new XingHuoOptions(); |
| 35 | - xingHuoOptions.setChatModel(xingHuoProperties.getChatModel()); | 38 | + xingHuoOptions.setChatModel(xingHuoProperties.getModel()); |
| 36 | xingHuoOptions.setTopK(xingHuoProperties.getTopK()); | 39 | xingHuoOptions.setTopK(xingHuoProperties.getTopK()); |
| 37 | xingHuoOptions.setTemperature(xingHuoProperties.getTemperature()); | 40 | xingHuoOptions.setTemperature(xingHuoProperties.getTemperature()); |
| 38 | xingHuoOptions.setMaxTokens(xingHuoProperties.getMaxTokens()); | 41 | xingHuoOptions.setMaxTokens(xingHuoProperties.getMaxTokens()); |
| @@ -79,10 +82,24 @@ public class YudaoAiAutoConfiguration { | @@ -79,10 +82,24 @@ public class YudaoAiAutoConfiguration { | ||
| 79 | new YiYanApi( | 82 | new YiYanApi( |
| 80 | yiYanProperties.getAppKey(), | 83 | yiYanProperties.getAppKey(), |
| 81 | yiYanProperties.getSecretKey(), | 84 | yiYanProperties.getSecretKey(), |
| 82 | - yiYanProperties.getChatModel(), | 85 | + yiYanProperties.getModel(), |
| 83 | yiYanProperties.getRefreshTokenSecondTime() | 86 | yiYanProperties.getRefreshTokenSecondTime() |
| 84 | ), | 87 | ), |
| 85 | yiYanOptions | 88 | yiYanOptions |
| 86 | ); | 89 | ); |
| 87 | } | 90 | } |
| 91 | + | ||
| 92 | + | ||
| 93 | + @Bean | ||
| 94 | + @ConditionalOnProperty(value = "yudao.ai.openAiImage.enable", havingValue = "true") | ||
| 95 | + public OpenAiImageClient openAiImageClient(YudaoAiProperties yudaoAiProperties) { | ||
| 96 | + YudaoAiProperties.OpenAiImageProperties openAiImageProperties = yudaoAiProperties.getOpenAiImage(); | ||
| 97 | + // 创建 client | ||
| 98 | + return new OpenAiImageClient( | ||
| 99 | + new OpenAiImageApi(openAiImageProperties.getApiKey()), | ||
| 100 | + new OpenAiImageOptions() | ||
| 101 | + .setModel(openAiImageProperties.getModel()) | ||
| 102 | + .setResponseFormat(OpenAiImageOptions.ResponseFormatEnum.URL.getValue()) | ||
| 103 | + ); | ||
| 104 | + } | ||
| 88 | } | 105 | } |
| 89 | \ No newline at end of file | 106 | \ No newline at end of file |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/config/YudaoAiProperties.java
| @@ -3,6 +3,7 @@ package cn.iocoder.yudao.framework.ai.config; | @@ -3,6 +3,7 @@ package cn.iocoder.yudao.framework.ai.config; | ||
| 3 | import cn.iocoder.yudao.framework.ai.AiPlatformEnum; | 3 | import cn.iocoder.yudao.framework.ai.AiPlatformEnum; |
| 4 | import cn.iocoder.yudao.framework.ai.chatxinghuo.XingHuoChatModel; | 4 | import cn.iocoder.yudao.framework.ai.chatxinghuo.XingHuoChatModel; |
| 5 | import cn.iocoder.yudao.framework.ai.chatyiyan.YiYanChatModel; | 5 | import cn.iocoder.yudao.framework.ai.chatyiyan.YiYanChatModel; |
| 6 | +import cn.iocoder.yudao.framework.ai.imageopenai.OpenAiImageModelEnum; | ||
| 6 | import lombok.Data; | 7 | import lombok.Data; |
| 7 | import lombok.experimental.Accessors; | 8 | import lombok.experimental.Accessors; |
| 8 | import org.springframework.boot.context.properties.ConfigurationProperties; | 9 | import org.springframework.boot.context.properties.ConfigurationProperties; |
| @@ -23,6 +24,7 @@ public class YudaoAiProperties { | @@ -23,6 +24,7 @@ public class YudaoAiProperties { | ||
| 23 | private QianWenProperties qianwen; | 24 | private QianWenProperties qianwen; |
| 24 | private XingHuoProperties xinghuo; | 25 | private XingHuoProperties xinghuo; |
| 25 | private YiYanProperties yiyan; | 26 | private YiYanProperties yiyan; |
| 27 | + private OpenAiImageProperties openAiImage; | ||
| 26 | 28 | ||
| 27 | @Data | 29 | @Data |
| 28 | @Accessors(chain = true) | 30 | @Accessors(chain = true) |
| @@ -64,7 +66,7 @@ public class YudaoAiProperties { | @@ -64,7 +66,7 @@ public class YudaoAiProperties { | ||
| 64 | private String appId; | 66 | private String appId; |
| 65 | private String appKey; | 67 | private String appKey; |
| 66 | private String secretKey; | 68 | private String secretKey; |
| 67 | - private XingHuoChatModel chatModel; | 69 | + private XingHuoChatModel model; |
| 68 | } | 70 | } |
| 69 | 71 | ||
| 70 | @Data | 72 | @Data |
| @@ -81,12 +83,23 @@ public class YudaoAiProperties { | @@ -81,12 +83,23 @@ public class YudaoAiProperties { | ||
| 81 | /** | 83 | /** |
| 82 | * 模型 | 84 | * 模型 |
| 83 | */ | 85 | */ |
| 84 | - private YiYanChatModel chatModel = YiYanChatModel.ERNIE4_3_5_8K; | 86 | + private YiYanChatModel model = YiYanChatModel.ERNIE4_3_5_8K; |
| 85 | /** | 87 | /** |
| 86 | * token 刷新时间(默认 86400 = 24小时) | 88 | * token 刷新时间(默认 86400 = 24小时) |
| 87 | */ | 89 | */ |
| 88 | private int refreshTokenSecondTime = 86400; | 90 | private int refreshTokenSecondTime = 86400; |
| 91 | + } | ||
| 89 | 92 | ||
| 90 | - private YiYanChatModel model; | 93 | + @Data |
| 94 | + @Accessors(chain = true) | ||
| 95 | + public static class OpenAiImageProperties { | ||
| 96 | + /** | ||
| 97 | + * api key | ||
| 98 | + */ | ||
| 99 | + private String apiKey; | ||
| 100 | + /** | ||
| 101 | + * 模型 | ||
| 102 | + */ | ||
| 103 | + private OpenAiImageModelEnum model = OpenAiImageModelEnum.DALL_E_2; | ||
| 91 | } | 104 | } |
| 92 | } | 105 | } |