From 24681c81c09022f584a57006f2534b5f74723414 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期二, 30 六月 2026 09:27:31 +0800
Subject: [PATCH] 初始化项目
---
yudao-module-ai/src/test/java/cn/iocoder/yudao/module/ai/framework/ai/core/model/chat/AnthropicChatModelTest.java | 39 ++++++++++++++++++++++++++++-----------
1 files changed, 28 insertions(+), 11 deletions(-)
diff --git a/yudao-module-ai/src/test/java/cn/iocoder/yudao/module/ai/framework/ai/core/model/chat/AnthropicChatModelTest.java b/yudao-module-ai/src/test/java/cn/iocoder/yudao/module/ai/framework/ai/core/model/chat/AnthropicChatModelTest.java
index c89a9cc..0bf3a9a 100644
--- a/yudao-module-ai/src/test/java/cn/iocoder/yudao/module/ai/framework/ai/core/model/chat/AnthropicChatModelTest.java
+++ b/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 鍗囩骇锛歨ttps://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();
}
--
Gitblit v1.9.3