注册自定义命令和提示
Available in Start plan
AI 生成扩展提供了一个内置命令列表,但你可以扩展它来定义你自己的命令。这些自定义命令可以用来用你自己的提示调用大语言模型。
注册自定义命令
要注册你自己的 AI 命令,只需扩展 AI 生成扩展,在 addCommands() 中添加你的命令(别忘了继承预定义命令,调用 this.parent?.()),然后执行 aiTextPrompt() 来运行你的自定义提示。
请注意,此示例在客户端使用你的提示,这意味着用户可以查看它。如果你想在后端使用自定义语言模型 (LLM) 或提示,请参阅这里提供的示例。
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()