Genkit Anthropic 人工智能插件
@genkit-ai/anthropic 是 Athropic 插件,用于 精灵。 。 。 .
它取代了早期的社区套装 genkitx-anthropic 现由Google维护.
用编码代理? 先安装基因组JS技能.
npx skills add genkit-ai/skills --skill developing-genkit-js它教你的代理人 现任Genkit JS APIs 和普通的gitchas. 其他语言的来源、人工安装和技能: 技能/技能。 。 。 .
支持的模型
该插件支持最近的Anthropic模型, 如 克劳德·海库 4.5, (中文(简体) ), 克劳德·索内 4.5,以及 克劳德·奥普斯 4.5。此外,该插件 支持所有 非退休的旧型号。 。 。 .
安装
使用您最喜欢的软件包管理器在您的工程中安装插件 :
npm install @genkit-ai/anthropicyarn add @genkit-ai/anthropicpnpm add @genkit-ai/anthropic
使用量
初始化
import { genkit } from "genkit";
import { anthropic } from "@genkit-ai/anthropic";
const ai = genkit({
plugins: [anthropic({ apiKey: process.env.ANTHROPIC_API_KEY })],
// specify a default model for generate here if you wish:
model: anthropic.model("claude-sonnet-4-5"),
});
基本实例
生成文本的最简单方法是使用 generate 方法 :
const response = await ai.generate({
model: anthropic.model("claude-haiku-4-5"),
prompt: "Tell me a joke.",
});
console.log(response.text);
多式联运提示
// ...initialize Genkit instance (as shown above)...
const response = await ai.generate({
prompt: [
{ text: "What animal is in the photo?" },
{ media: { url: imageUrl } },
],
});
console.log(response.text);
扩展思维
Claude 4.5模型可以暴露其内部推理. 按请求启用 与Anthropic思想配置并读取反应中的推理:
const response = await ai.generate({
prompt: "Walk me through your reasoning for Fermat’s little theorem.",
config: {
thinking: {
enabled: true,
budgetTokens: 4096, // Must be >= 1024 and less than max_tokens
},
},
});
console.log(response.text); // Final assistant answer
console.log(response.reasoning); // Summarized thinking steps
当思维被启用时,通过插件发送的请求机构包括
thinking 有效载荷({ type: 'enabled', budget_tokens: … }安东尼的
API 期望和流出的回应 reasoning 当它们到达的时候
你可以使思维链 递增.
文件引用
Claude可以引用你提供的文件的特定部分,这样很容易追踪到
答复中的信息来自何处。 使用 anthropicDocument()
帮助创建可读文档。 详情见
Anthropic 引用文件。 。 。 .
import { anthropic, anthropicDocument } from "@genkit-ai/anthropic";
const response = await ai.generate({
model: anthropic.model("claude-sonnet-4-5"),
messages: [
{
role: "user",
content: [
anthropicDocument({
source: {
type: "text",
data: "The grass is green. The sky is blue. Water is wet.",
},
title: "Basic Facts",
citations: { enabled: true },
}),
{ text: "What color is the grass? Cite your source." },
],
},
],
});
// Access citations from response parts
const citations = response.message?.content?.flatMap(
(part) => part.metadata?.citations || [],
) ?? [];
console.log("Citations:", citations);
重要内容: 必须在请求中对所有文件或 没有 无法将文档与启用和禁用的引用相混合 同样的要求.
支持的文档源类型 :
text- 纯文本文档(返回)char_location引用)base64- 基准64编码PDF(返回)page_location引用)url- 可通过URL访问的PDF(返回)page_location引用)content- 自定义包含文字/图像的内容块(返回)content_block_location引用)file- 来自 Anthropic 文件 API 的文件引用,仅β API (返回)page_location引用)
在答复部分的元数据中退回引用,并包括信息 关于文档索引、所引用文本和位置(特征索引、页) 数字,或视来源类型而定的块索引.
快速缓存
您可以通过添加来缓存提示 cache_control 元数据到提示。 使用
cacheControl() 用于类型安全缓存配置的助手 。 您可以缓存系统
信息、用户信息、工具和媒体.
import { anthropic, cacheControl } from "@genkit-ai/anthropic";
const response = await ai.generate({
model: anthropic.model("claude-sonnet-4-5"),
system: {
text: longSystemPrompt,
metadata: { ...cacheControl() }, // default: ephemeral
},
messages: [
{
role: "user",
content: [{ text: "What is the main idea of the text?" }],
},
],
});
// Or with explicit TTL:
metadata: { ...cacheControl({ ttl: '1h' }) }
// Or using the type directly:
import { type AnthropicCacheControl } from "@genkit-ai/anthropic";
metadata: { cache_control: { type: 'ephemeral', ttl: '5m' } as AnthropicCacheControl }
注意: 缓存只有在提示超过一定符号长度时才使用. 这个 符号长度在 Athropic API 文档。 。 。 .
Beta API 限制
Β-API表面提供实验特性,但有些 此插件尚未支持服务器管理的工具块 。 以下内容: 如果遇到β API 特性, 将产生错误 :
web_fetch_tool_resultcode_execution_tool_resultbash_code_execution_tool_resulttext_editor_code_execution_tool_resultmcp_tool_resultmcp_tool_usecontainer_upload
请注意: server_tool_use 财务报告和审定财务报表 web_search_tool_result 获得支助和工作
与稳定与β API同时.
在流动中
import { z } from "genkit";
// ...initialize Genkit instance (as shown above)...
export const jokeFlow = ai.defineFlow(
{
name: "jokeFlow",
inputSchema: z.string(),
outputSchema: z.string(),
},
async (subject) => {
const llmResponse = await ai.generate({
prompt: `tell me a joke about ${subject}`,
});
return llmResponse.text;
},
);
直接使用模型( 不包括 Genkit 实例)
插件支持 Genkit 插件 API v2, 它允许您使用模型 直接初始化整个源基框架:
import { anthropic } from "@genkit-ai/anthropic";
// Create a model reference directly
const claude = anthropic.model("claude-sonnet-4-5");
// Use the model directly
const response = await claude({
messages: [
{
role: "user",
content: [{ text: "Tell me a joke." }],
},
],
});
console.log(response);
您也可以使用插件创建模型引用 model() 方法 :
import { anthropic } from "@genkit-ai/anthropic";
// Create model references
const claudeHaiku45 = anthropic.model("claude-haiku-4-5");
const claudeSonnet45 = anthropic.model("claude-sonnet-4-5");
const claudeOpus45 = anthropic.model("claude-opus-4-5");
// Use the model reference directly
const response = await claudeSonnet45({
messages: [
{
role: "user",
content: [{ text: "Hello!" }],
},
],
});
这种办法有助于:
- 需要原始模型访问的框架开发者
- 单独测试模型
- 在非Genkit 应用中使用 Genkit 模型
鸣谢
此插件以社区工作为基础
genkitx-anthropic
由Bloom Labs Inc. 编著. 它们的Apache 2.0 许可执行提供了
此软件包的基础.
贷项
此插件由 Google 维护, 并向社区确认 来自 布鲁姆实验室股份有限公司。 。 。 .