---
title: "注册自定义命令和提示"
description: "通过扩展 AI 生成扩展，为定制内容转换工作流程创建带有定制提示的自定义 AI 命令。"
canonical_url: "https://tiptap.zhcndoc.com/content-ai/capabilities/generation/text-generation/custom-commands"
---

# 注册自定义命令和提示

通过扩展 AI 生成扩展，为定制内容转换工作流程创建带有定制提示的自定义 AI 命令。

- **1. 激活试用或订阅**

  在你的账户中开始一个免费试用或订阅入门计划。
- **2. 集成 AI 提供商**

  在你的AI 设置中配置 OpenAI，或查看自定义大语言模型指南。
- **3. 从私有仓库安装**

  若要安装前端扩展，请按照设置指南验证身份并登录 Tiptap 私有 npm 仓库。

AI 生成扩展提供了一个[内置命令列表](https://tiptap.zhcndoc.com/content-ai/capabilities/generation/text-generation/built-in-commands.md)，但你可以扩展它来定义你自己的命令。这些自定义命令可以用来用你自己的提示调用大语言模型。

> **Interactive demo:** [AiCustomCommandUsingAiTextPrompt](https://embed-pro.tiptap.dev/preview/Extensions/AiCustomCommandUsingAiTextPrompt)

## 注册自定义命令

要注册你自己的 AI 命令，只需扩展 AI 生成扩展，在 `addCommands()` 中添加你的命令（别忘了继承预定义命令，调用 `this.parent?.()`），然后执行 `aiTextPrompt()` 来运行你的自定义提示。

请注意，此示例在客户端使用了您的提示，这意味着用户可以看到它。如果您希望在后端使用自定义语言模型（LLM）或提示，请参阅[自定义 LLM 指南](https://tiptap.zhcndoc.com/content-ai/capabilities/generation/custom-llms.md)。

```js
import { Ai, getHTMLContentBetween } from '@tiptap-pro/extension-ai'

// … 其他导入

// 如果使用 TypeScript，声明扩展类型。
// 更多信息：https://tiptap.dev/docs/guides/typescript
//
// declare module '@tiptap/core' {
//   interface Commands<ReturnType> {
//     ai: {
//       aiCustomTextCommand: (options?: TextOptions) => ReturnType,
//     }
//   }
// }

const AiExtended = Ai.extend({
  addCommands() {
    return {
      ...this.parent?.(),

      aiCustomTextCommand:
        (options = {}) =>
        ({ editor, state }) => {
          const { from, to } = state.selection
          const selectedText = getHTMLContentBetween(editor, from, to)

          return editor.commands.aiTextPrompt({
            text: `将以下文本翻译成法语并添加一些表情符号: ${selectedText}`,
            ...options,
          })
        },
    }
  },
})

// 初始化你的 Tiptap 编辑器实例并注册扩展后的扩展

const editor = new Editor({
  extensions: [
    StarterKit,
    AiExtended.configure({
      /* … */
    }),
  ],
  content: '',
})

// 运行你的自定义命令
// editor.chain().focus().aiCustomTextCommand().run()
```
