| | |
| | | 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.yiyan.YiYanChatModel; |
| | | import org.junit.jupiter.api.Disabled; |
| | | import org.junit.jupiter.api.Test; |
| | | import org.springaicommunity.qianfan.QianFanChatModel; |
| | | import org.springaicommunity.qianfan.QianFanChatOptions; |
| | | import org.springaicommunity.qianfan.api.QianFanApi; |
| | | 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.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; |
| | | |
| | | // TODO @芋艿:百度千帆 API 提供了 V2 版本,目前 Spring AI 不兼容,可关键 <https://github.com/spring-projects/spring-ai/issues/2179> 进展 |
| | | import static cn.iocoder.yudao.module.ai.util.AiUtils.validateApiKey; |
| | | |
| | | /** |
| | | * {@link QianFanChatModel} 的集成测试 |
| | | * {@link YiYanChatModel} 的集成测试 |
| | | * |
| | | * @author fansili |
| | | */ |
| | | public class YiYanChatModelTests { |
| | | |
| | | private final QianFanChatModel chatModel = new QianFanChatModel( |
| | | new QianFanApi("DGnyzREuaY7av7c38bOM9Ji2", "9aR8myflEOPDrEeLhoXv0FdqANOAyIZW"), // 密钥 |
| | | QianFanChatOptions.builder() |
| | | .model("ERNIE-4.5-8K-Preview") |
| | | .build() |
| | | ); |
| | | private static final String API_KEY = SystemUtil.get("YIYAN_API_KEY", |
| | | "sk-xxxx"); // 按需改成你的文心一言 API Key |
| | | private static final String MODEL = SystemUtil.get("YIYAN_MODEL", |
| | | YiYanChatModel.MODEL_DEFAULT); |
| | | |
| | | private final YiYanChatModel chatModel = new YiYanChatModel(DeepSeekChatModel.builder() |
| | | .deepSeekApi(DeepSeekApi.builder() |
| | | .baseUrl(YiYanChatModel.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<>(); |
| | | // TODO @芋艿:文心一言,只要带上 system message 就报错,已经各种测试,很莫名! |
| | | // messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。")); |
| | | messages.add(new UserMessage("1 + 1 = ?")); |
| | | |
| | | // 调用 |
| | | ChatResponse response = chatModel.call(new Prompt(messages)); |
| | | // 打印结果 |
| | | System.out.println(response); |
| | | System.out.println(Objects.requireNonNull(response.getResult()).getOutput()); |
| | | } |
| | | |
| | | @Test |
| | | @Disabled |
| | | public void testStream() { |
| | | validateApiKey(API_KEY); |
| | | // 准备参数 |
| | | List<Message> messages = new ArrayList<>(); |
| | | // TODO @芋艿:文心一言,只要带上 system message 就报错,已经各种测试,很莫名! |
| | | // messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。")); |
| | | messages.add(new UserMessage("1 + 1 = ?")); |
| | | |
| | | // 调用 |
| | | Flux<ChatResponse> flux = chatModel.stream(new Prompt(messages)); |
| | | // 打印结果 |
| | | flux.doOnNext(System.out::println).then().block(); |
| | | flux.doOnNext(response -> { |
| | | // System.out.println(response); |
| | | System.out.println(Objects.requireNonNull(response.getResult()).getOutput()); |
| | | }).then().block(); |
| | | } |
| | | |
| | | } |