| | |
| | | package cn.iocoder.yudao.module.ai.framework.ai.core.model.chat; |
| | | |
| | | import cn.hutool.system.SystemUtil; |
| | | import cn.iocoder.yudao.module.ai.framework.ai.core.model.zhipu.ZhiPuChatModel; |
| | | import org.junit.jupiter.api.Disabled; |
| | | import org.junit.jupiter.api.Test; |
| | | import org.springframework.ai.chat.messages.Message; |
| | |
| | | import org.springframework.ai.chat.messages.UserMessage; |
| | | import org.springframework.ai.chat.model.ChatResponse; |
| | | import org.springframework.ai.chat.prompt.Prompt; |
| | | import org.springframework.ai.zhipuai.ZhiPuAiChatModel; |
| | | import org.springframework.ai.zhipuai.ZhiPuAiChatOptions; |
| | | import org.springframework.ai.zhipuai.api.ZhiPuAiApi; |
| | | import org.springframework.ai.deepseek.DeepSeekChatModel; |
| | | import org.springframework.ai.deepseek.DeepSeekChatOptions; |
| | | import org.springframework.ai.deepseek.api.DeepSeekApi; |
| | | import reactor.core.publisher.Flux; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | |
| | | import static cn.iocoder.yudao.module.ai.util.AiUtils.validateApiKey; |
| | | |
| | | /** |
| | | * {@link ZhiPuAiChatModel} 的集成测试 |
| | | * {@link ZhiPuChatModel} 的集成测试 |
| | | * |
| | | * @author 芋道源码 |
| | | */ |
| | | public class ZhiPuAiChatModelTests { |
| | | |
| | | private final ZhiPuAiChatModel chatModel = new ZhiPuAiChatModel( |
| | | ZhiPuAiApi.builder().apiKey("2f35fb6ca4ea41fab898729b7fac086c.6ESSfPcCkxaKEUlR").build(), // 密钥 |
| | | ZhiPuAiChatOptions.builder() |
| | | .model(ZhiPuAiApi.ChatModel.GLM_4.getName()) // 模型 |
| | | private static final String API_KEY = SystemUtil.get("ZHIPU_API_KEY", |
| | | "sk-xxxx"); // 按需改成你的智谱 API Key |
| | | private static final String MODEL = SystemUtil.get("ZHIPU_MODEL", |
| | | ZhiPuChatModel.MODEL_DEFAULT); |
| | | |
| | | private final ZhiPuChatModel chatModel = new ZhiPuChatModel(DeepSeekChatModel.builder() |
| | | .deepSeekApi(DeepSeekApi.builder() |
| | | .baseUrl(ZhiPuChatModel.BASE_URL) |
| | | .apiKey(API_KEY) |
| | | .build()) |
| | | .options(DeepSeekChatOptions.builder() |
| | | .model(MODEL) |
| | | .build() |
| | | ); |
| | | ).build()); |
| | | |
| | | @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(); |
| | | } |
| | | |
| | | // TODO @芋艿:暂时没解析 reasoning_content 结果,需要等官方修复 |
| | | @Test |
| | | @Disabled |
| | | public void testStream_thinking() { |
| | | validateApiKey(API_KEY); |
| | | // 准备参数 |
| | | List<Message> messages = new ArrayList<>(); |
| | | messages.add(new UserMessage("详细分析下,如何设计一个电商系统?")); |
| | | ZhiPuAiChatOptions options = ZhiPuAiChatOptions.builder() |
| | | .model("GLM-4.5") |
| | | DeepSeekChatOptions options = DeepSeekChatOptions.builder() |
| | | .model(MODEL) |
| | | .build(); |
| | | |
| | | // 调用 |
| | |
| | | // 打印结果 |
| | | flux.doOnNext(response -> { |
| | | // System.out.println(response); |
| | | System.out.println(response.getResult().getOutput()); |
| | | System.out.println(Objects.requireNonNull(response.getResult()).getOutput()); |
| | | }).then().block(); |
| | | } |
| | | |