Anthropic 适配器提供对 Claude 模型的访问,包括 Claude Sonnet 4.5、Claude Opus 4.5 等。
npm install @tanstack/ai-anthropic
import { chat } from "@tanstack/ai";
import { anthropicText } from "@tanstack/ai-anthropic";
const stream = chat({
adapter: anthropicText("claude-sonnet-4-5"),
messages: [{ role: "user", content: "Hello!" }],
});
import { chat } from "@tanstack/ai";
import { createAnthropicChat } from "@tanstack/ai-anthropic";
const adapter = createAnthropicChat(process.env.ANTHROPIC_API_KEY!, {
// ... your config options
});
const stream = chat({
adapter: adapter("claude-sonnet-4-5"),
messages: [{ role: "user", content: "Hello!" }],
});
import { createAnthropicChat, type AnthropicChatConfig } from "@tanstack/ai-anthropic";
const config: Omit<AnthropicChatConfig, 'apiKey'> = {
baseURL: "https://api.anthropic.com", // Optional, for custom endpoints
};
const adapter = createAnthropicChat(process.env.ANTHROPIC_API_KEY!, config);
import { chat, toServerSentEventsResponse } from "@tanstack/ai";
import { anthropicText } from "@tanstack/ai-anthropic";
export async function POST(request: Request) {
const { messages } = await request.json();
const stream = chat({
adapter: anthropicText("claude-sonnet-4-5"),
messages,
});
return toServerSentEventsResponse(stream);
}
import { chat, toolDefinition } from "@tanstack/ai";
import { anthropicText } from "@tanstack/ai-anthropic";
import { z } from "zod";
const searchDatabaseDef = toolDefinition({
name: "search_database",
description: "Search the database",
inputSchema: z.object({
query: z.string(),
}),
});
const searchDatabase = searchDatabaseDef.server(async ({ query }) => {
// Search database
return { results: [] };
});
const stream = chat({
adapter: anthropicText("claude-sonnet-4-5"),
messages,
tools: [searchDatabase],
});
Anthropic 支持各种特定于提供商的选项
const stream = chat({
adapter: anthropicText("claude-sonnet-4-5"),
messages,
modelOptions: {
max_tokens: 4096,
temperature: 0.7,
top_p: 0.9,
top_k: 40,
stop_sequences: ["END"],
},
});
使用 token 预算启用扩展思考。这允许 Claude 展示其推理过程,该过程以 思考 块的形式流式传输
modelOptions: {
thinking: {
type: "enabled",
budget_tokens: 2048, // Maximum tokens for thinking
},
}
注意: max_tokens 必须大于 budget_tokens。适配器会自动调整 max_tokens(如果需要)。
缓存提示以提高性能并降低成本
const stream = chat({
adapter: anthropicText("claude-sonnet-4-5"),
messages: [
{
role: "user",
content: [
{
type: "text",
content: "What is the capital of France?",
metadata: {
cache_control: {
type: "ephemeral",
},
},
},
],
},
],
});
Anthropic 支持文本摘要
import { summarize } from "@tanstack/ai";
import { anthropicSummarize } from "@tanstack/ai-anthropic";
const result = await summarize({
adapter: anthropicSummarize("claude-sonnet-4-5"),
text: "Your long text to summarize...",
maxLength: 100,
style: "concise", // "concise" | "bullet-points" | "paragraph"
});
console.log(result.summary);
将您的 API 密钥设置为环境变量
ANTHROPIC_API_KEY=sk-ant-...
使用环境变量创建一个 Anthropic 聊天适配器。
返回值: 一个 Anthropic 聊天适配器实例。
使用显式 API 密钥创建一个 Anthropic 聊天适配器。
参数
返回值: 一个 Anthropic 聊天适配器实例。
使用环境变量创建一个 Anthropic 摘要适配器。
返回值: 一个 Anthropic 摘要适配器实例。
使用显式 API 密钥创建一个 Anthropic 摘要适配器。
参数
返回值: 一个 Anthropic 摘要适配器实例。