Commit 6c094aaffc6fe25a459be30ef44b75e4b346eba5
1 parent
2d36ec48
【新增】AI:接入 DeepSeek 模型
Showing
11 changed files
with
328 additions
and
53 deletions
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/config/YudaoAiAutoConfiguration.java
| @@ -2,6 +2,8 @@ package cn.iocoder.yudao.framework.ai.config; | @@ -2,6 +2,8 @@ package cn.iocoder.yudao.framework.ai.config; | ||
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.ai.core.factory.AiClientFactory; | 3 | import cn.iocoder.yudao.framework.ai.core.factory.AiClientFactory; |
| 4 | import cn.iocoder.yudao.framework.ai.core.factory.AiClientFactoryImpl; | 4 | import cn.iocoder.yudao.framework.ai.core.factory.AiClientFactoryImpl; |
| 5 | +import cn.iocoder.yudao.framework.ai.core.model.deepseek.DeepSeekChatClient; | ||
| 6 | +import cn.iocoder.yudao.framework.ai.core.model.deepseek.DeepSeekChatOptions; | ||
| 5 | import cn.iocoder.yudao.framework.ai.core.model.midjourney.api.MidjourneyApi; | 7 | import cn.iocoder.yudao.framework.ai.core.model.midjourney.api.MidjourneyApi; |
| 6 | import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi; | 8 | import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi; |
| 7 | import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoChatClient; | 9 | import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoChatClient; |
| @@ -46,6 +48,19 @@ public class YudaoAiAutoConfiguration { | @@ -46,6 +48,19 @@ public class YudaoAiAutoConfiguration { | ||
| 46 | } | 48 | } |
| 47 | 49 | ||
| 48 | @Bean | 50 | @Bean |
| 51 | + @ConditionalOnProperty(value = "yudao.ai.deepseek.enable", havingValue = "true") | ||
| 52 | + public DeepSeekChatClient deepSeekChatClient(YudaoAiProperties yudaoAiProperties) { | ||
| 53 | + YudaoAiProperties.DeepSeekProperties properties = yudaoAiProperties.getDeepSeek(); | ||
| 54 | + DeepSeekChatOptions options = DeepSeekChatOptions.builder() | ||
| 55 | + .model(properties.getModel()) | ||
| 56 | + .temperature(properties.getTemperature()) | ||
| 57 | + .maxTokens(properties.getMaxTokens()) | ||
| 58 | + .topP(properties.getTopP()) | ||
| 59 | + .build(); | ||
| 60 | + return new DeepSeekChatClient(properties.getApiKey(), options); | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + @Bean | ||
| 49 | @ConditionalOnProperty(value = "yudao.ai.midjourney.enable", havingValue = "true") | 64 | @ConditionalOnProperty(value = "yudao.ai.midjourney.enable", havingValue = "true") |
| 50 | public MidjourneyApi midjourneyApi(YudaoAiProperties yudaoAiProperties) { | 65 | public MidjourneyApi midjourneyApi(YudaoAiProperties yudaoAiProperties) { |
| 51 | YudaoAiProperties.MidjourneyProperties config = yudaoAiProperties.getMidjourney(); | 66 | YudaoAiProperties.MidjourneyProperties config = yudaoAiProperties.getMidjourney(); |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/config/YudaoAiProperties.java
| @@ -19,6 +19,11 @@ public class YudaoAiProperties { | @@ -19,6 +19,11 @@ public class YudaoAiProperties { | ||
| 19 | private XingHuoProperties xinghuo; | 19 | private XingHuoProperties xinghuo; |
| 20 | 20 | ||
| 21 | /** | 21 | /** |
| 22 | + * DeepSeek | ||
| 23 | + */ | ||
| 24 | + private DeepSeekProperties deepSeek; | ||
| 25 | + | ||
| 26 | + /** | ||
| 22 | * Midjourney 绘图 | 27 | * Midjourney 绘图 |
| 23 | */ | 28 | */ |
| 24 | private MidjourneyProperties midjourney; | 29 | private MidjourneyProperties midjourney; |
| @@ -44,6 +49,19 @@ public class YudaoAiProperties { | @@ -44,6 +49,19 @@ public class YudaoAiProperties { | ||
| 44 | } | 49 | } |
| 45 | 50 | ||
| 46 | @Data | 51 | @Data |
| 52 | + public static class DeepSeekProperties { | ||
| 53 | + | ||
| 54 | + private String enable; | ||
| 55 | + private String apiKey; | ||
| 56 | + | ||
| 57 | + private String model; | ||
| 58 | + private Float temperature; | ||
| 59 | + private Integer maxTokens; | ||
| 60 | + private Float topP; | ||
| 61 | + | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + @Data | ||
| 47 | public static class MidjourneyProperties { | 65 | public static class MidjourneyProperties { |
| 48 | 66 | ||
| 49 | private String enable; | 67 | private String enable; |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/enums/AiPlatformEnum.java
| @@ -3,7 +3,6 @@ package cn.iocoder.yudao.framework.ai.core.enums; | @@ -3,7 +3,6 @@ package cn.iocoder.yudao.framework.ai.core.enums; | ||
| 3 | import lombok.AllArgsConstructor; | 3 | import lombok.AllArgsConstructor; |
| 4 | import lombok.Getter; | 4 | import lombok.Getter; |
| 5 | 5 | ||
| 6 | -// TODO 芋艿:这块,看看要不要调整下; | ||
| 7 | /** | 6 | /** |
| 8 | * AI 模型平台 | 7 | * AI 模型平台 |
| 9 | * | 8 | * |
| @@ -13,15 +12,20 @@ import lombok.Getter; | @@ -13,15 +12,20 @@ import lombok.Getter; | ||
| 13 | @AllArgsConstructor | 12 | @AllArgsConstructor |
| 14 | public enum AiPlatformEnum { | 13 | public enum AiPlatformEnum { |
| 15 | 14 | ||
| 16 | - OPENAI("OpenAI", "OpenAI"), | ||
| 17 | - OLLAMA("Ollama", "Ollama"), | 15 | + // ========== 国内平台 ========== |
| 16 | + | ||
| 18 | YI_YAN("YiYan", "文心一言"), // 百度 | 17 | YI_YAN("YiYan", "文心一言"), // 百度 |
| 19 | - XING_HUO("XingHuo", "星火"), // 讯飞 | ||
| 20 | QIAN_WEN("QianWen", "千问"), // 阿里 | 18 | QIAN_WEN("QianWen", "千问"), // 阿里 |
| 21 | - GEMIR ("gemir ", "gemir "), // 谷歌 | 19 | + DEEP_SEEK("DeepSeek", "DeepSeek"), // DeepSeek |
| 20 | + XING_HUO("XingHuo", "星火"), // 讯飞 | ||
| 21 | + | ||
| 22 | + // ========== 国外平台 ========== | ||
| 23 | + | ||
| 24 | + OPENAI("OpenAI", "OpenAI"), | ||
| 25 | + OLLAMA("Ollama", "Ollama"), | ||
| 22 | 26 | ||
| 23 | STABLE_DIFFUSION("StableDiffusion", "StableDiffusion"), // Stability AI | 27 | STABLE_DIFFUSION("StableDiffusion", "StableDiffusion"), // Stability AI |
| 24 | - MIDJOURNEY("Midjourney", "Midjourney"), | 28 | + MIDJOURNEY("Midjourney", "Midjourney"), // Midjourney |
| 25 | SUNO("Suno", "Suno"), // Suno AI | 29 | SUNO("Suno", "Suno"), // Suno AI |
| 26 | ; | 30 | ; |
| 27 | 31 |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/exception/AiException.java deleted
| 1 | -package cn.iocoder.yudao.framework.ai.core.exception; | ||
| 2 | - | ||
| 3 | -// TODO @fan:这个有办法干掉么? | ||
| 4 | -/** | ||
| 5 | - * ai 异常 | ||
| 6 | - * | ||
| 7 | - * @author fansili | ||
| 8 | - * @time 2024/4/13 17:05 | ||
| 9 | - * @since 1.0 | ||
| 10 | - */ | ||
| 11 | -public class AiException extends RuntimeException { | ||
| 12 | - | ||
| 13 | - public AiException(String message) { | ||
| 14 | - super(message); | ||
| 15 | - } | ||
| 16 | -} |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/exception/ChatException.java deleted
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/factory/AiClientFactoryImpl.java
| @@ -9,6 +9,7 @@ import cn.hutool.extra.spring.SpringUtil; | @@ -9,6 +9,7 @@ import cn.hutool.extra.spring.SpringUtil; | ||
| 9 | import cn.iocoder.yudao.framework.ai.config.YudaoAiAutoConfiguration; | 9 | import cn.iocoder.yudao.framework.ai.config.YudaoAiAutoConfiguration; |
| 10 | import cn.iocoder.yudao.framework.ai.config.YudaoAiProperties; | 10 | import cn.iocoder.yudao.framework.ai.config.YudaoAiProperties; |
| 11 | import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum; | 11 | import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum; |
| 12 | +import cn.iocoder.yudao.framework.ai.core.model.deepseek.DeepSeekChatClient; | ||
| 12 | import cn.iocoder.yudao.framework.ai.core.model.midjourney.api.MidjourneyApi; | 13 | import cn.iocoder.yudao.framework.ai.core.model.midjourney.api.MidjourneyApi; |
| 13 | import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi; | 14 | import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi; |
| 14 | import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoChatClient; | 15 | import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoChatClient; |
| @@ -64,8 +65,8 @@ public class AiClientFactoryImpl implements AiClientFactory { | @@ -64,8 +65,8 @@ public class AiClientFactoryImpl implements AiClientFactory { | ||
| 64 | return buildXingHuoChatClient(apiKey); | 65 | return buildXingHuoChatClient(apiKey); |
| 65 | case QIAN_WEN: | 66 | case QIAN_WEN: |
| 66 | return buildQianWenChatClient(apiKey); | 67 | return buildQianWenChatClient(apiKey); |
| 67 | -// case GEMIR: | ||
| 68 | -// return buildGoogleGemir(apiKey); | 68 | + case DEEP_SEEK: |
| 69 | + return buildDeepSeekChatClient(apiKey); | ||
| 69 | default: | 70 | default: |
| 70 | throw new IllegalArgumentException(StrUtil.format("未知平台({})", platform)); | 71 | throw new IllegalArgumentException(StrUtil.format("未知平台({})", platform)); |
| 71 | } | 72 | } |
| @@ -182,6 +183,10 @@ public class AiClientFactoryImpl implements AiClientFactory { | @@ -182,6 +183,10 @@ public class AiClientFactoryImpl implements AiClientFactory { | ||
| 182 | return new XingHuoChatClient(appKey, secretKey); | 183 | return new XingHuoChatClient(appKey, secretKey); |
| 183 | } | 184 | } |
| 184 | 185 | ||
| 186 | + private static DeepSeekChatClient buildDeepSeekChatClient(String apiKey) { | ||
| 187 | + return new DeepSeekChatClient(apiKey); | ||
| 188 | + } | ||
| 189 | + | ||
| 185 | /** | 190 | /** |
| 186 | * 可参考 {@link TongYiAutoConfiguration#tongYiChatClient(Generation, TongYiChatProperties, TongYiConnectionProperties)} | 191 | * 可参考 {@link TongYiAutoConfiguration#tongYiChatClient(Generation, TongYiChatProperties, TongYiConnectionProperties)} |
| 187 | */ | 192 | */ |
| @@ -195,13 +200,6 @@ public class AiClientFactoryImpl implements AiClientFactory { | @@ -195,13 +200,6 @@ public class AiClientFactoryImpl implements AiClientFactory { | ||
| 195 | return new TongYiAutoConfiguration().tongYiChatClient(generation, chatOptions, connectionProperties); | 200 | return new TongYiAutoConfiguration().tongYiChatClient(generation, chatOptions, connectionProperties); |
| 196 | } | 201 | } |
| 197 | 202 | ||
| 198 | -// private static VertexAiGeminiChatClient buildGoogleGemir(String key) { | ||
| 199 | -// List<String> keys = StrUtil.split(key, '|'); | ||
| 200 | -// Assert.equals(keys.size(), 2, "VertexAiGeminiChatClient 的密钥需要 (projectId|location) 格式"); | ||
| 201 | -// VertexAI vertexApi = new VertexAI(keys.get(0), keys.get(1)); | ||
| 202 | -// return new VertexAiGeminiChatClient(vertexApi); | ||
| 203 | -// } | ||
| 204 | - | ||
| 205 | private OpenAiImageModel buildOpenAiImageClient(String openAiToken, String url) { | 203 | private OpenAiImageModel buildOpenAiImageClient(String openAiToken, String url) { |
| 206 | url = StrUtil.blankToDefault(url, ApiUtils.DEFAULT_BASE_URL); | 204 | url = StrUtil.blankToDefault(url, ApiUtils.DEFAULT_BASE_URL); |
| 207 | OpenAiImageApi openAiApi = new OpenAiImageApi(url, openAiToken, RestClient.builder()); | 205 | OpenAiImageApi openAiApi = new OpenAiImageApi(url, openAiToken, RestClient.builder()); |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/deepseek/DeepSeekChatClient.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.ai.core.model.deepseek; | ||
| 2 | + | ||
| 3 | +import cn.hutool.core.lang.Assert; | ||
| 4 | +import lombok.extern.slf4j.Slf4j; | ||
| 5 | +import org.springframework.ai.chat.metadata.ChatGenerationMetadata; | ||
| 6 | +import org.springframework.ai.chat.model.ChatModel; | ||
| 7 | +import org.springframework.ai.chat.model.ChatResponse; | ||
| 8 | +import org.springframework.ai.chat.model.Generation; | ||
| 9 | +import org.springframework.ai.chat.prompt.ChatOptions; | ||
| 10 | +import org.springframework.ai.chat.prompt.Prompt; | ||
| 11 | +import org.springframework.ai.model.ModelOptionsUtils; | ||
| 12 | +import org.springframework.ai.openai.OpenAiChatOptions; | ||
| 13 | +import org.springframework.ai.openai.api.OpenAiApi; | ||
| 14 | +import org.springframework.ai.openai.metadata.OpenAiChatResponseMetadata; | ||
| 15 | +import org.springframework.ai.retry.RetryUtils; | ||
| 16 | +import org.springframework.http.ResponseEntity; | ||
| 17 | +import org.springframework.retry.support.RetryTemplate; | ||
| 18 | +import reactor.core.publisher.Flux; | ||
| 19 | + | ||
| 20 | +import java.util.HashMap; | ||
| 21 | +import java.util.List; | ||
| 22 | +import java.util.Map; | ||
| 23 | + | ||
| 24 | +import static cn.iocoder.yudao.framework.ai.core.model.deepseek.DeepSeekChatOptions.MODEL_DEFAULT; | ||
| 25 | + | ||
| 26 | +/** | ||
| 27 | + * DeepSeek {@link ChatModel} 实现类 | ||
| 28 | + * | ||
| 29 | + * @author fansili | ||
| 30 | + */ | ||
| 31 | +@Slf4j | ||
| 32 | +public class DeepSeekChatClient implements ChatModel { | ||
| 33 | + | ||
| 34 | + private static final String BASE_URL = "https://api.deepseek.com"; | ||
| 35 | + | ||
| 36 | + private final DeepSeekChatOptions defaultOptions; | ||
| 37 | + private final RetryTemplate retryTemplate; | ||
| 38 | + | ||
| 39 | + /** | ||
| 40 | + * DeepSeek 兼容 OpenAI 的 HTTP 接口,所以复用它的实现,简化接入成本 | ||
| 41 | + * | ||
| 42 | + * 不过要注意,DeepSeek 没有完全兼容,所以不能使用 {@link org.springframework.ai.openai.OpenAiChatModel} 调用,但是实现会参考它 | ||
| 43 | + */ | ||
| 44 | + private final OpenAiApi openAiApi; | ||
| 45 | + | ||
| 46 | + public DeepSeekChatClient(String apiKey) { | ||
| 47 | + this(apiKey, DeepSeekChatOptions.builder().model(MODEL_DEFAULT).temperature(0.7F).build()); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + public DeepSeekChatClient(String apiKey, DeepSeekChatOptions options) { | ||
| 51 | + this(apiKey, options, RetryUtils.DEFAULT_RETRY_TEMPLATE); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + public DeepSeekChatClient(String apiKey, DeepSeekChatOptions options, RetryTemplate retryTemplate) { | ||
| 55 | + Assert.notEmpty(apiKey, "apiKey 不能为空"); | ||
| 56 | + Assert.notNull(options, "options 不能为空"); | ||
| 57 | + Assert.notNull(retryTemplate, "retryTemplate 不能为空"); | ||
| 58 | + this.openAiApi = new OpenAiApi(BASE_URL, apiKey); | ||
| 59 | + this.defaultOptions = options; | ||
| 60 | + this.retryTemplate = retryTemplate; | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + @Override | ||
| 64 | + public ChatResponse call(Prompt prompt) { | ||
| 65 | + OpenAiApi.ChatCompletionRequest request = createRequest(prompt, false); | ||
| 66 | + return this.retryTemplate.execute(ctx -> { | ||
| 67 | + // 1.1 发起调用 | ||
| 68 | + ResponseEntity<OpenAiApi.ChatCompletion> completionEntity = openAiApi.chatCompletionEntity(request); | ||
| 69 | + // 1.2 校验结果 | ||
| 70 | + OpenAiApi.ChatCompletion chatCompletion = completionEntity.getBody(); | ||
| 71 | + if (chatCompletion == null) { | ||
| 72 | + log.warn("No chat completion returned for prompt: {}", prompt); | ||
| 73 | + return new ChatResponse(List.of()); | ||
| 74 | + } | ||
| 75 | + List<OpenAiApi.ChatCompletion.Choice> choices = chatCompletion.choices(); | ||
| 76 | + if (choices == null) { | ||
| 77 | + log.warn("No choices returned for prompt: {}", prompt); | ||
| 78 | + return new ChatResponse(List.of()); | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + // 2. 转换 ChatResponse 返回 | ||
| 82 | + List<Generation> generations = choices.stream().map(choice -> { | ||
| 83 | + Generation generation = new Generation(choice.message().content(), toMap(chatCompletion.id(), choice)); | ||
| 84 | + if (choice.finishReason() != null) { | ||
| 85 | + generation.withGenerationMetadata(ChatGenerationMetadata.from(choice.finishReason().name(), null)); | ||
| 86 | + } | ||
| 87 | + return generation; | ||
| 88 | + }).toList(); | ||
| 89 | + return new ChatResponse(generations, | ||
| 90 | + OpenAiChatResponseMetadata.from(completionEntity.getBody())); | ||
| 91 | + }); | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + private Map<String, Object> toMap(String id, OpenAiApi.ChatCompletion.Choice choice) { | ||
| 95 | + Map<String, Object> map = new HashMap<>(); | ||
| 96 | + OpenAiApi.ChatCompletionMessage message = choice.message(); | ||
| 97 | + if (message.role() != null) { | ||
| 98 | + map.put("role", message.role().name()); | ||
| 99 | + } | ||
| 100 | + if (choice.finishReason() != null) { | ||
| 101 | + map.put("finishReason", choice.finishReason().name()); | ||
| 102 | + } | ||
| 103 | + map.put("id", id); | ||
| 104 | + return map; | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + @Override | ||
| 108 | + public Flux<ChatResponse> stream(Prompt prompt) { | ||
| 109 | + OpenAiApi.ChatCompletionRequest request = createRequest(prompt, true); | ||
| 110 | + return this.retryTemplate.execute(ctx -> { | ||
| 111 | + // 1. 发起调用 | ||
| 112 | + Flux<OpenAiApi.ChatCompletionChunk> response = this.openAiApi.chatCompletionStream(request); | ||
| 113 | + return response.map(chatCompletion -> { | ||
| 114 | + String id = chatCompletion.id(); | ||
| 115 | + // 2. 转换 ChatResponse 返回 | ||
| 116 | + List<Generation> generations = chatCompletion.choices().stream().map(choice -> { | ||
| 117 | + String finish = (choice.finishReason() != null ? choice.finishReason().name() : ""); | ||
| 118 | + String role = (choice.delta().role() != null ? choice.delta().role().name() : ""); | ||
| 119 | + if (choice.finishReason() == OpenAiApi.ChatCompletionFinishReason.STOP) { | ||
| 120 | + // 兜底处理 DeepSeek 返回 STOP 时,role 为空的情况 | ||
| 121 | + role = OpenAiApi.ChatCompletionMessage.Role.ASSISTANT.name(); | ||
| 122 | + } | ||
| 123 | + Generation generation = new Generation(choice.delta().content(), | ||
| 124 | + Map.of("id", id, "role", role, "finishReason", finish)); | ||
| 125 | + if (choice.finishReason() != null) { | ||
| 126 | + generation = generation.withGenerationMetadata( | ||
| 127 | + ChatGenerationMetadata.from(choice.finishReason().name(), null)); | ||
| 128 | + } | ||
| 129 | + return generation; | ||
| 130 | + }).toList(); | ||
| 131 | + return new ChatResponse(generations); | ||
| 132 | + }); | ||
| 133 | + }); | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + OpenAiApi.ChatCompletionRequest createRequest(Prompt prompt, boolean stream) { | ||
| 137 | + // 1. 构建 ChatCompletionMessage 对象 | ||
| 138 | + List<OpenAiApi.ChatCompletionMessage> chatCompletionMessages = prompt.getInstructions().stream().map(m -> | ||
| 139 | + new OpenAiApi.ChatCompletionMessage(m.getContent(), OpenAiApi.ChatCompletionMessage.Role.valueOf(m.getMessageType().name()))).toList(); | ||
| 140 | + OpenAiApi.ChatCompletionRequest request = new OpenAiApi.ChatCompletionRequest(chatCompletionMessages, stream); | ||
| 141 | + | ||
| 142 | + // 2.1 补充 prompt 内置的 options | ||
| 143 | + if (prompt.getOptions() != null) { | ||
| 144 | + if (prompt.getOptions() instanceof ChatOptions runtimeOptions) { | ||
| 145 | + OpenAiChatOptions updatedRuntimeOptions = ModelOptionsUtils.copyToTarget(runtimeOptions, | ||
| 146 | + ChatOptions.class, OpenAiChatOptions.class); | ||
| 147 | + request = ModelOptionsUtils.merge(updatedRuntimeOptions, request, OpenAiApi.ChatCompletionRequest.class); | ||
| 148 | + } else { | ||
| 149 | + throw new IllegalArgumentException("Prompt options are not of type ChatOptions: " | ||
| 150 | + + prompt.getOptions().getClass().getSimpleName()); | ||
| 151 | + } | ||
| 152 | + } | ||
| 153 | + // 2.2 补充默认 options | ||
| 154 | + if (this.defaultOptions != null) { | ||
| 155 | + request = ModelOptionsUtils.merge(request, this.defaultOptions, OpenAiApi.ChatCompletionRequest.class); | ||
| 156 | + } | ||
| 157 | + return request; | ||
| 158 | + } | ||
| 159 | + | ||
| 160 | + @Override | ||
| 161 | + public ChatOptions getDefaultOptions() { | ||
| 162 | + return DeepSeekChatOptions.fromOptions(defaultOptions); | ||
| 163 | + } | ||
| 164 | + | ||
| 165 | +} |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/deepseek/DeepSeekChatOptions.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.ai.core.model.deepseek; | ||
| 2 | + | ||
| 3 | +import lombok.AllArgsConstructor; | ||
| 4 | +import lombok.Builder; | ||
| 5 | +import lombok.Data; | ||
| 6 | +import lombok.NoArgsConstructor; | ||
| 7 | +import org.springframework.ai.chat.prompt.ChatOptions; | ||
| 8 | + | ||
| 9 | +/** | ||
| 10 | + * DeepSeek {@link ChatOptions} 实现类 | ||
| 11 | + * | ||
| 12 | + * 参考文档:<a href="https://platform.deepseek.com/api-docs/zh-cn/">快速开始</a> | ||
| 13 | + * | ||
| 14 | + * @author fansili | ||
| 15 | + */ | ||
| 16 | +@Data | ||
| 17 | +@NoArgsConstructor | ||
| 18 | +@AllArgsConstructor | ||
| 19 | +@Builder | ||
| 20 | +public class DeepSeekChatOptions implements ChatOptions { | ||
| 21 | + | ||
| 22 | + public static final String MODEL_DEFAULT = "deepseek-chat"; | ||
| 23 | + | ||
| 24 | + /** | ||
| 25 | + * 模型 | ||
| 26 | + */ | ||
| 27 | + private String model; | ||
| 28 | + /** | ||
| 29 | + * 温度 | ||
| 30 | + */ | ||
| 31 | + private Float temperature; | ||
| 32 | + /** | ||
| 33 | + * 最大 Token | ||
| 34 | + */ | ||
| 35 | + private Integer maxTokens; | ||
| 36 | + /** | ||
| 37 | + * topP | ||
| 38 | + */ | ||
| 39 | + private Float topP; | ||
| 40 | + | ||
| 41 | + @Override | ||
| 42 | + public Integer getTopK() { | ||
| 43 | + return null; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + public static DeepSeekChatOptions fromOptions(DeepSeekChatOptions fromOptions) { | ||
| 47 | + return DeepSeekChatOptions.builder() | ||
| 48 | + .model(fromOptions.getModel()) | ||
| 49 | + .temperature(fromOptions.getTemperature()) | ||
| 50 | + .maxTokens(fromOptions.getMaxTokens()) | ||
| 51 | + .topP(fromOptions.getTopP()) | ||
| 52 | + .build(); | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | +} |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/util/AiUtils.java
| @@ -2,6 +2,7 @@ package cn.iocoder.yudao.framework.ai.core.util; | @@ -2,6 +2,7 @@ package cn.iocoder.yudao.framework.ai.core.util; | ||
| 2 | 2 | ||
| 3 | import cn.hutool.core.util.StrUtil; | 3 | import cn.hutool.core.util.StrUtil; |
| 4 | import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum; | 4 | import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum; |
| 5 | +import cn.iocoder.yudao.framework.ai.core.model.deepseek.DeepSeekChatOptions; | ||
| 5 | import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoChatOptions; | 6 | import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoChatOptions; |
| 6 | import com.alibaba.cloud.ai.tongyi.chat.TongYiChatOptions; | 7 | import com.alibaba.cloud.ai.tongyi.chat.TongYiChatOptions; |
| 7 | import org.springframework.ai.chat.messages.*; | 8 | import org.springframework.ai.chat.messages.*; |
| @@ -33,6 +34,8 @@ public class AiUtils { | @@ -33,6 +34,8 @@ public class AiUtils { | ||
| 33 | return XingHuoChatOptions.builder().model(model).temperature(temperatureF).maxTokens(maxTokens).build(); | 34 | return XingHuoChatOptions.builder().model(model).temperature(temperatureF).maxTokens(maxTokens).build(); |
| 34 | case QIAN_WEN: | 35 | case QIAN_WEN: |
| 35 | return TongYiChatOptions.builder().withModel(model).withTemperature(temperature).withMaxTokens(maxTokens).build(); | 36 | return TongYiChatOptions.builder().withModel(model).withTemperature(temperature).withMaxTokens(maxTokens).build(); |
| 37 | + case DEEP_SEEK: | ||
| 38 | + return DeepSeekChatOptions.builder().model(model).temperature(temperatureF).maxTokens(maxTokens).build(); | ||
| 36 | default: | 39 | default: |
| 37 | throw new IllegalArgumentException(StrUtil.format("未知平台({})", platform)); | 40 | throw new IllegalArgumentException(StrUtil.format("未知平台({})", platform)); |
| 38 | } | 41 | } |
yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/chat/DeepSeekChatTests.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.ai.chat; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.ai.core.model.deepseek.DeepSeekChatClient; | ||
| 4 | +import org.junit.jupiter.api.Test; | ||
| 5 | +import org.springframework.ai.chat.messages.Message; | ||
| 6 | +import org.springframework.ai.chat.messages.SystemMessage; | ||
| 7 | +import org.springframework.ai.chat.messages.UserMessage; | ||
| 8 | +import org.springframework.ai.chat.model.ChatResponse; | ||
| 9 | +import org.springframework.ai.chat.prompt.Prompt; | ||
| 10 | +import reactor.core.publisher.Flux; | ||
| 11 | + | ||
| 12 | +import java.util.ArrayList; | ||
| 13 | +import java.util.List; | ||
| 14 | + | ||
| 15 | +/** | ||
| 16 | + * {@link DeepSeekChatClient} 集成测试 | ||
| 17 | + * | ||
| 18 | + * @author 芋道源码 | ||
| 19 | + */ | ||
| 20 | +public class DeepSeekChatTests { | ||
| 21 | + | ||
| 22 | + private final DeepSeekChatClient chatModel = new DeepSeekChatClient("sk-e94db327cc7d457d99a8de8810fc6b12"); | ||
| 23 | + | ||
| 24 | + @Test | ||
| 25 | + public void testCall() { | ||
| 26 | + // 准备参数 | ||
| 27 | + List<Message> messages = new ArrayList<>(); | ||
| 28 | + messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。")); | ||
| 29 | + messages.add(new UserMessage("1 + 1 = ?")); | ||
| 30 | + | ||
| 31 | + // 调用 | ||
| 32 | + ChatResponse response = chatModel.call(new Prompt(messages)); | ||
| 33 | + // 打印结果 | ||
| 34 | + System.out.println(response); | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + @Test | ||
| 38 | + public void testStream() { | ||
| 39 | + // 准备参数 | ||
| 40 | + List<Message> messages = new ArrayList<>(); | ||
| 41 | + messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。")); | ||
| 42 | + messages.add(new UserMessage("1 + 1 = ?")); | ||
| 43 | + | ||
| 44 | + // 调用 | ||
| 45 | + Flux<ChatResponse> flux = chatModel.stream(new Prompt(messages)); | ||
| 46 | + // 打印结果 | ||
| 47 | + flux.doOnNext(System.out::println).then().block(); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | +} |
yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/chat/XingHuoChatClientTests.java
| @@ -19,7 +19,7 @@ import java.util.List; | @@ -19,7 +19,7 @@ import java.util.List; | ||
| 19 | */ | 19 | */ |
| 20 | public class XingHuoChatClientTests { | 20 | public class XingHuoChatClientTests { |
| 21 | 21 | ||
| 22 | - private final XingHuoChatClient client = new XingHuoChatClient( | 22 | + private final XingHuoChatClient chatModel = new XingHuoChatClient( |
| 23 | "cb6415c19d6162cda07b47316fcb0416", | 23 | "cb6415c19d6162cda07b47316fcb0416", |
| 24 | "Y2JiYTIxZjA3MDMxMjNjZjQzYzVmNzdh"); | 24 | "Y2JiYTIxZjA3MDMxMjNjZjQzYzVmNzdh"); |
| 25 | 25 | ||
| @@ -31,9 +31,9 @@ public class XingHuoChatClientTests { | @@ -31,9 +31,9 @@ public class XingHuoChatClientTests { | ||
| 31 | messages.add(new UserMessage("1 + 1 = ?")); | 31 | messages.add(new UserMessage("1 + 1 = ?")); |
| 32 | 32 | ||
| 33 | // 调用 | 33 | // 调用 |
| 34 | - ChatResponse response = client.call(new Prompt(messages)); | 34 | + ChatResponse response = chatModel.call(new Prompt(messages)); |
| 35 | // 打印结果 | 35 | // 打印结果 |
| 36 | - System.err.println(response); | 36 | + System.out.println(response); |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | @Test | 39 | @Test |
| @@ -44,11 +44,9 @@ public class XingHuoChatClientTests { | @@ -44,11 +44,9 @@ public class XingHuoChatClientTests { | ||
| 44 | messages.add(new UserMessage("1 + 1 = ?")); | 44 | messages.add(new UserMessage("1 + 1 = ?")); |
| 45 | 45 | ||
| 46 | // 调用 | 46 | // 调用 |
| 47 | - Flux<ChatResponse> flux = client.stream(new Prompt(messages)); | 47 | + Flux<ChatResponse> flux = chatModel.stream(new Prompt(messages)); |
| 48 | // 打印结果 | 48 | // 打印结果 |
| 49 | - List<ChatResponse> responses = flux.collectList().block(); | ||
| 50 | - assert responses != null; | ||
| 51 | - responses.forEach(System.err::println); | 49 | + flux.doOnNext(System.out::println).then().block(); |
| 52 | } | 50 | } |
| 53 | 51 | ||
| 54 | } | 52 | } |