| | |
| | | package cn.iocoder.yudao.module.ai.framework.ai.core.model.chat; |
| | | |
| | | import com.anthropic.client.okhttp.AnthropicOkHttpClient; |
| | | import cn.hutool.system.SystemUtil; |
| | | import org.junit.jupiter.api.Disabled; |
| | | import org.junit.jupiter.api.Test; |
| | | import org.springframework.ai.anthropic.AnthropicChatModel; |
| | |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | |
| | | import static cn.iocoder.yudao.module.ai.util.AiUtils.validateApiKey; |
| | | |
| | | /** |
| | | * {@link AnthropicChatModel} 集成测试类 |
| | |
| | | */ |
| | | public class AnthropicChatModelTest { |
| | | |
| | | private static final String BASE_URL = SystemUtil.get("ANTHROPIC_BASE_URL", |
| | | "https://api.teamorouter.com"); |
| | | private static final String API_KEY = SystemUtil.get("ANTHROPIC_API_KEY", |
| | | "sk-xxxx"); // 按需改成你的 Anthropic API Key |
| | | private static final String MODEL = SystemUtil.get("ANTHROPIC_MODEL", |
| | | "claude-sonnet-4-6"); |
| | | |
| | | private final AnthropicChatModel chatModel = AnthropicChatModel.builder() |
| | | .anthropicClient(AnthropicOkHttpClient.builder() |
| | | .apiKey("sk-muubv7cXeLw0Etgs743f365cD5Ea44429946Fa7e672d8942") |
| | | .baseUrl("https://aihubmix.com") |
| | | .build()) |
| | | .options(AnthropicChatOptions.builder() |
| | | .model("claude-sonnet-4-5") |
| | | .baseUrl(BASE_URL) |
| | | .apiKey(API_KEY) |
| | | .model(MODEL) |
| | | .temperature(0.7) |
| | | .maxTokens(4096) |
| | | .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(Objects.requireNonNull(response.getResult()).getOutput()); |
| | | } |
| | | |
| | | @Test |
| | | @Disabled |
| | | public void testStream() { |
| | | validateApiKey(API_KEY); |
| | | // 准备参数 |
| | | List<Message> messages = new ArrayList<>(); |
| | | messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。")); |
| | |
| | | // 调用 |
| | | 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(); |
| | | } |
| | | |
| | | // TODO @芋艿:需要等 spring ai 升级:https://github.com/spring-projects/spring-ai/pull/2800 |
| | | @Test |
| | | @Disabled |
| | | public void testStream_thinking() { |
| | | validateApiKey(API_KEY); |
| | | // 准备参数 |
| | | List<Message> messages = new ArrayList<>(); |
| | | messages.add(new UserMessage("thkinking 下,1+1 为什么等于 2 ")); |
| | | AnthropicChatOptions options = AnthropicChatOptions.builder() |
| | | .model("claude-sonnet-4-5") |
| | | .temperature(1D) |
| | | .baseUrl(BASE_URL) |
| | | .apiKey(API_KEY) |
| | | .model(MODEL) |
| | | .thinkingEnabled(1024) // https://platform.claude.com/docs/en/build-with-claude/extended-thinking |
| | | .maxTokens(4096) |
| | | .build(); |
| | | |
| | | // 调用 |
| | |
| | | // 打印结果 |
| | | flux.doOnNext(response -> { |
| | | // System.out.println(response); |
| | | System.out.println(response.getResult()); |
| | | System.out.println(Objects.requireNonNull(response.getResult()).getOutput()); |
| | | }).then().block(); |
| | | } |
| | | |