使用 AI 工具包进行 AI 工程
通过为每种情况选择最佳的 AI 模型和提示语,充分发挥 AI 工具包的优势。
选择 AI 模型
AI 工具包的工具设计为可与任何支持函数调用的 AI 模型配合使用。它们已在多种来自不同 AI 提供商的模型上进行过测试。
选择合适 AI 模型的一个关键区别是你是否需要构建一个智能体(agent)或工作流:
- AI 智能体接收一个任务,并独立朝该任务推进,在每一步决定接下来执行什么操作。例如:AI 聊天助手
- 在 AI 工作流中,AI 执行一个或多个预定义的操作,但不会选择下一步操作。这种情况出现在简单的 AI 内容生成或自动补全中。
适合智能体任务的最佳模型
对于复杂的智能体 AI 助手(如AI 智能体聊天机器人指南中的示例),推荐使用具有先进工具调用能力的前沿模型。特别地,以下模型表现优异:
- OpenAI 模型:GPT-5、GPT-5 mini、GPT-5.4、GPT-5.4 mini
- Anthropic 模型:Opus、Sonnet 和 Haiku(4 版及以后)
- Google 模型:Gemini Pro、Gemini Flash(3 版及以后)
- Mistral 模型:Mistral Large、Mistral Medium
- Moonshot 模型:Kimi-K2
- xAI 模型:Grok 4.1 Fast 及以后
- Z.ai 模型:GLM-4.5 及以后
适合智能体任务的经济型模型
如果你有智能体文档编辑的需求,但优先考虑成本控制,可以选择仍支持函数调用的较小模型。特别可以考虑以下模型:
- Claude Haiku 4.5(Anthropic)及以后
- GPT-5 mini(OpenAI)及以后
- Gemini 3 Flash(Google)及以后
- Mistral Medium 3.1(Mistral)及以后
非智能体工作流的模型选择
并非所有文档编辑的 AI 用例都是智能体类型,有时你只是需要 AI 生成内容并插入文档。大量经济型模型适合此用途。比如前文提到的那些。
是否应启用推理能力?
根据我们的内部测试,在文档编辑任务中启用推理后,准确性和性能会略有提升。不过,从低到高提升推理强度似乎并没有明显改善。由于推理会增加 token 消耗和延迟,建议将推理级别设置为较低水平,除非你的 AI 智能体还会将推理用于其他目的(例如规划或解决复杂数学问题)。
例如,对于 AI 工具包推荐的 GPT-5 配置是将 reasoning 参数设置为 'minimal'。
关于 Gemini 3 Pro 和 Gemini 3 Flash,我们发现将推理设置为 minimal 后,响应更快且准确度相当。
设计合适的提示语
你可以通过提供自定义系统提示语来调整 AI 模型的行为和内容生成方式。
// app/api/chat/route.ts
import { openai } from '@ai-sdk/openai'
import { toolDefinitions } from '@tiptap-pro/ai-toolkit-ai-sdk'
import { createAgentUIStreamResponse, ToolLoopAgent, UIMessage } from 'ai'
export async function POST(req: Request) {
const { messages }: { messages: UIMessage[] } = await req.json()
const agent = new ToolLoopAgent({
model: openai('gpt-5.4-mini'),
instructions: `You are an assistant that edits rich text documents in the style of Shakespeare.
You should respond in the style of Shakespeare, and when editing the document,
the content you generate and add to the document should be written in the style
of Shakespeare's plays.`,
tools: toolDefinitions(),
})
return createAgentUIStreamResponse({
agent,
uiMessages: messages,
})
}在系统提示中不需要提及 AI 模型有哪些工具可用。因为它们已经包含在工具定义中,AI 模型会自动识别。
不过,在系统提示里你可以按名称引用可用工具,这样你可以指导 AI 模型如何以及何时使用工具,并引导它变得更健谈、有创造力或更细致。
// app/api/chat/route.ts
import { openai } from '@ai-sdk/openai'
import { toolDefinitions } from '@tiptap-pro/ai-toolkit-ai-sdk'
import { createAgentUIStreamResponse, ToolLoopAgent, UIMessage } from 'ai'
export async function POST(req: Request) {
const { messages }: { messages: UIMessage[] } = await req.json()
const agent = new ToolLoopAgent({
model: openai('gpt-5.4-mini'),
instructions: `You are an assistant that can edit rich text documents.
Before calling the document editing tools like tiptapEdit,
you should first read the document to get a sense of the content and context.
Then, you should inform the user of the plan of action you will take to edit
the document, in a very detailed step-by-step description. Only after planning
in detail, you should call the document editing tools.`,
tools: toolDefinitions(),
})
return createAgentUIStreamResponse({
agent,
uiMessages: messages,
})
}我们推荐以下资源帮助你深入学习提示工程和AI工程:
提升速度和延迟
为了提升 AI 工具包的响应速度,可以采用以下策略:
实现响应流式传输
将内容流式传输到编辑器中,会让用户感觉响应更快。请参阅流式传输指南以实现它。
选择更快的模型或提供商
选择模型时,注意其速度和延迟。可以参考如Artificial Analysis这类排行榜,比较不同模型的多个指标。
速度和延迟不仅取决于模型,还受模型所托管的提供商影响。如果你优先考虑输出速度,可选择专注于快速推理的提供商,如groq、SambaNova或Cerebras。
关闭推理功能
推理会增加 token 消耗和延迟。如果你的具体用例不需要推理,建议关闭或最小化此功能。例如,在 GPT-5 和 GPT-5 mini 中,推荐将 reasoningEffort 提供商选项设置为 'minimal'。
// app/api/chat/route.ts
import { openai } from '@ai-sdk/openai'
import { toolDefinitions } from '@tiptap-pro/ai-toolkit-ai-sdk'
import { createAgentUIStreamResponse, ToolLoopAgent, UIMessage } from 'ai'
export async function POST(req: Request) {
const { messages }: { messages: UIMessage[] } = await req.json()
const agent = new ToolLoopAgent({
model: openai('gpt-5.4-mini'),
instructions: `You are an assistant that can edit rich text documents.`,
tools: toolDefinitions(),
// 将推理努力设置为 'minimal'
providerOptions: {
openai: {
reasoningEffort: 'minimal',
},
},
})
return createAgentUIStreamResponse({
agent,
uiMessages: messages,
})
}为 AI 模型提供足够的上下文
在编辑文档之前,AI 模型需要先读取文档内容。通过将文档内容包含在用户消息或系统提示中,可以加快这一过程。
const toolkit = getAiToolkit(editor)
// 在发送用户消息之前,先读取文档开头内容
const { output } = toolkit.executeTool({
toolName: 'tiptapRead',
input: {
from: 0,
},
})
// 然后,在用户消息中包含文档内容
let userMessage = `将最后一段替换为一个短故事`
userMessage += `
---
用户已调用过 'tiptapRead' 工具,其返回内容如下:
${output}`
// 将用户消息发送给 AI 模型这样,AI 模型无需再次调用 tiptapRead 工具读取文档,而可直接进入编辑阶段。
优先使用工作流而非智能体
工作流比 AI 智能体更易实现。它们通常需要 AI 生成更少的 token,因此执行更快。此外,工作流通常可以配合更快、更小的模型使用。
AI 工具包内置了多种内置工作流,可用于执行简单的文档编辑任务。