Anthropic - AI 代理工具
@tiptap-pro/ai-toolkit-anthropic 包提供了工具定义,您可以将它们添加到使用 Anthropic 的 JavaScript SDK 构建的 AI 代理中。
随后,模型生成的工具调用可以通过 executeTool 方法 在客户端执行。
示例用法
安装该包。
npm install @tiptap-pro/ai-toolkit-anthropic将工具定义提供给您的 Anthropic 模型。
import Anthropic from '@anthropic-ai/sdk'
import { toolDefinitions } from '@tiptap-pro/ai-toolkit-anthropic'
const anthropic = new Anthropic()
const response = await anthropic.messages.create({
model: 'claude-sonnet-4-5',
max_tokens: 2048,
system: '你是一个可以编辑富文本文档的有用助手。',
messages: [{ role: 'user', content: '帮我编辑这份文档' }],
tools: toolDefinitions(),
})将工具定义与您的自定义工具结合使用。
import Anthropic from '@anthropic-ai/sdk'
import { toolDefinitions } from '@tiptap-pro/ai-toolkit-anthropic'
const anthropic = new Anthropic()
const customTools = [
{
name: 'get_weather',
description: '获取某地的天气',
input_schema: {
type: 'object',
properties: {
location: {
type: 'string',
description: '城市和州,例如 San Francisco, CA',
},
},
required: ['location'],
},
},
]
const response = await anthropic.messages.create({
model: 'claude-sonnet-4-5',
max_tokens: 1024,
messages: [{ role: 'user', content: '天气怎么样,帮我编辑这份文档' }],
tools: [...toolDefinitions(), ...customTools],
})API 参考
toolDefinitions
创建适配于 Anthropic 的 Messages API 的 Tiptap AI 工具包工具定义。
参数 (ToolDefinitionsOptions)
tools?:EnabledTools- 通过将某值设置为true(启用)或false(禁用)来启用/禁用特定工具。tiptapRead?:boolean- 启用/禁用tiptapRead工具(默认:true)tiptapEdit?:boolean- 启用/禁用tiptapEdit工具(默认:true)tiptapReadSelection?:boolean- 启用/禁用tiptapReadSelection工具(默认:true)getThreads?:boolean- 启用/禁用getThreads工具(默认:false)editThreads?:boolean- 启用/禁用editThreads工具(默认:false)
返回值
返回一个数组,包含可与 Anthropic 的 Messages API 一起使用的已启用工具定义。完整工具列表请参见 可用工具 页面。