🎁 100 free AI Toolkit licenses – apply by August 15.Learn more

使用 AI 工具包进行 AI 工程

通过为每种情况选择最佳的 AI 模型和提示语,充分发挥 AI 工具包的优势。

选择 AI 模型

AI 工具包中的工具设计为可与任何支持函数调用的 AI 模型配合使用。它们已在来自不同 AI 提供商的多种模型上进行过测试。

选择合适 AI 模型的一个关键区别在于你是否需要构建一个智能体(agent)或工作流

  • AI 智能体会被赋予一个任务,并独立朝着目标推进,在每一步决定下一步要采取什么行动。例如:AI 智能体聊天机器人
  • 在 AI 工作流中,AI 会执行一个或多个预定义操作,但不会自行选择下一步操作。这种情况适用于简单的 AI 内容生成或自动补全。

适合智能体任务的最佳模型

对于像 AI 智能体聊天机器人 指南中那样的复杂 AI 智能体助手,建议使用具备工具调用能力的前沿模型。尤其是,以下模型表现出了积极效果:

  • OpenAI 模型:GPT-5.6 Sol、Terra 和 Luna
  • Anthropic 模型:Opus 和 Sonnet(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 及更高版本

适合智能体任务的经济型模型

如果你有智能体文档编辑的需求,但优先考虑成本控制,可以选择仍支持函数调用的较小模型。特别可以考虑以下模型:

  • GPT-5.6 Luna(OpenAI)
  • Claude Haiku 4.5(Anthropic)及更高版本
  • 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/client-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.6-luna'),
    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 模型如何以及何时使用这些工具,并引导 AI 模型变得更加详尽、更有创造力或更深思熟虑。

// app/api/chat/route.ts
import { openai } from '@ai-sdk/openai'
import { toolDefinitions } from '@tiptap-pro/client-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.6-luna'),
    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 这类排行榜,比较不同模型的多个指标。

速度和延迟取决于模型以及其托管所在的提供商。如果输出速度是你的首要考虑,可以选择专注于快速推理的提供商,例如 groqSambaNovaCerebras

降低推理工作量

推理会增加 token 消耗和延迟。将 reasoningEffort 提供商选项设置为 'low',可以减少推理 token 的消耗。

// app/api/chat/route.ts
import { openai } from '@ai-sdk/openai'
import { toolDefinitions } from '@tiptap-pro/client-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.6-luna'),
    instructions: `You are an assistant that can edit rich text documents.`,
    tools: toolDefinitions(),
    // 降低推理工作量
    providerOptions: {
      openai: {
        reasoningEffort: 'low',
      },
    },
  })

  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 工具包包含 内置工作流,可用于执行简单的文档编辑任务。