| | |
| | | package cn.iocoder.yudao.module.ai.framework.ai.core.model.chat; |
| | | |
| | | import cn.hutool.system.SystemUtil; |
| | | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| | | import cn.iocoder.yudao.module.ai.util.AiUtils; |
| | | import com.alibaba.cloud.ai.dashscope.api.DashScopeApi; |
| | | import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel; |
| | | import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions; |
| | |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | |
| | | import static cn.iocoder.yudao.module.ai.util.AiUtils.validateApiKey; |
| | | import static java.util.Arrays.asList; |
| | | |
| | | /** |
| | |
| | | */ |
| | | public class TongYiChatModelTests { |
| | | |
| | | private static final String API_KEY = SystemUtil.get("DASHSCOPE_API_KEY", |
| | | "sk-xxxx"); // 按需改成你的 DashScope API Key |
| | | private static final String MODEL = SystemUtil.get("DASHSCOPE_MODEL", |
| | | "qwen3.7-plus"); |
| | | |
| | | private final DashScopeChatModel chatModel = DashScopeChatModel.builder() |
| | | .dashScopeApi(DashScopeApi.builder() |
| | | .apiKey("sk-cd9f39e99ea54840bd1888282325f55a") // https://bailian.console.aliyun.com/cn-beijing/?tab=model#/api-key 获取密钥 |
| | | .apiKey(API_KEY) |
| | | .build()) |
| | | .defaultOptions(DashScopeChatOptions.builder() |
| | | .multiModel(true) // 注意:当使用 qwen3.6-plus 等多模态模型,需要设置为 true,可见 https://help.aliyun.com/zh/model-studio/error-code#error-url 链接 |
| | | .model("qwen3.6-plus") // 模型 |
| | | .multiModel(AiUtils.TONG_YI_MULTI_MODELS.contains(MODEL)) // 多模态模型需要设置为 true,可见 https://help.aliyun.com/zh/model-studio/error-code#error-url |
| | | .model(MODEL) // 模型 |
| | | // .model("deepseek-r1") // 模型(deepseek-r1) |
| | | // .model("deepseek-v3") // 模型(deepseek-v3) |
| | | // .model("deepseek-r1-distill-qwen-1.5b") // 模型(deepseek-r1-distill-qwen-1.5b) |
| | |
| | | @Test |
| | | @Disabled |
| | | public void testCall() { |
| | | validateApiKey(API_KEY); |
| | | // 准备参数 |
| | | List<Message> messages = new ArrayList<>(); |
| | | messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。")); |
| | |
| | | ChatResponse response = chatModel.call(new Prompt(messages)); |
| | | // 打印结果 |
| | | System.out.println(response); |
| | | System.out.println(response.getResult().getOutput()); |
| | | System.out.println(Objects.requireNonNull(response.getResult()).getOutput()); |
| | | } |
| | | |
| | | @Test |
| | | @Disabled |
| | | public void testStream() { |
| | | validateApiKey(API_KEY); |
| | | // 准备参数 |
| | | List<Message> messages = new ArrayList<>(); |
| | | // messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。")); |
| | |
| | | // 打印结果 |
| | | flux.doOnNext(response -> { |
| | | // System.out.println(response); |
| | | System.out.println(response.getResult().getOutput()); |
| | | System.out.println(Objects.requireNonNull(response.getResult()).getOutput()); |
| | | }).then().block(); |
| | | } |
| | | |
| | | @Test |
| | | @Disabled |
| | | public void testStream_thinking() { |
| | | validateApiKey(API_KEY); |
| | | // 准备参数 |
| | | List<Message> messages = new ArrayList<>(); |
| | | messages.add(new UserMessage("详细分析下,如何设计一个电商系统?")); |
| | | DashScopeChatOptions options = DashScopeChatOptions.builder() |
| | | .model("qwen3.6-plus").multiModel(true) |
| | | .model(MODEL).multiModel(AiUtils.TONG_YI_MULTI_MODELS.contains(MODEL)) |
| | | // .withModel("qwen-max-2025-01-25") |
| | | .enableThinking(true) // 必须设置,否则会报错 |
| | | .build(); |
| | |
| | | // 打印结果 |
| | | flux.doOnNext(response -> { |
| | | // System.out.println(response); |
| | | System.out.println(response.getResult().getOutput()); |
| | | System.out.println(Objects.requireNonNull(response.getResult()).getOutput()); |
| | | }).then().block(); |
| | | } |
| | | |
| | | @Test |
| | | @Disabled |
| | | public void testRerank() { |
| | | validateApiKey(API_KEY); |
| | | // 准备环境 |
| | | RerankModel rerankModel = new DashScopeRerankModel( |
| | | DashScopeApi.builder() |
| | | .apiKey("sk-47aa124781be4bfb95244cc62f63f7d0") |
| | | .apiKey(API_KEY) |
| | | .build()); |
| | | // 准备参数 |
| | | String query = "spring"; |