2026-06-30 24681c81c09022f584a57006f2534b5f74723414
yudao-module-ai/src/test/java/cn/iocoder/yudao/module/ai/framework/ai/core/model/chat/AnthropicChatModelTest.java
@@ -1,6 +1,6 @@
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;
@@ -14,6 +14,9 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import static cn.iocoder.yudao.module.ai.util.AiUtils.validateApiKey;
/**
 * {@link AnthropicChatModel} 集成测试类
@@ -22,13 +25,18 @@
 */
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())
@@ -37,6 +45,7 @@
    @Test
    @Disabled
    public void testCall() {
        validateApiKey(API_KEY);
        // 准备参数
        List<Message> messages = new ArrayList<>();
        messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。"));
@@ -46,11 +55,13 @@
        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("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。"));
@@ -59,19 +70,25 @@
        // 调用
        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();
        // 调用
@@ -79,7 +96,7 @@
        // 打印结果
        flux.doOnNext(response -> {
//            System.out.println(response);
            System.out.println(response.getResult());
            System.out.println(Objects.requireNonNull(response.getResult()).getOutput());
        }).then().block();
    }