Define rules for the AI Suggestion extension

The AI Suggestion extension must be configured with a list of rules to help the LLM analyze the editor's content and generate suggestions.

AiSuggestion.configure({
  rules: [
    {
      id: '1',
      title: 'Spell Check',
      prompt: 'Identify and correct any spelling mistakes',
      color: '#DC143C',
      backgroundColor: 'FFE6E6',
    },
  ],
})

Each suggestion is associated with a rule. If no rules are provided, the AI Suggestion extension will not generate any suggestions.

Each rule must have a unique string id, so that each suggestion can be matched with its corresponding rule.

The prompt property is a string that describes the rule. It is provided to the AI model to help it generate suggestions.

Only the id and the prompt are sent to the AI model. The title, color and backgroundColor properties are used to style and display the suggestions in the editor.

Are Rules Stored in Tiptap Cloud?

If you use Tiptap Content AI Cloud to generate suggestions for your content, rules are not stored in the cloud. They are sent on each request.

Because we do not store your rules, you can change them at any time and enable them based on your own application-specific logic. For example, you can have rules that apply to all documents, document-specific rules, rules that apply to certain users, or even have the user define their own rules.

If you want rules to persist across sessions, you should store them in your own database.

How to Change Rules After the Editor Is Loaded

You can change the rules at any time without having to reload the editor. Use the setAiSuggestionRules command to update the rules.

const newRules = [
  {
    id: '2',
    title: 'Grammar Check',
    prompt: 'Identify and correct any grammar mistakes',
    color: '#FFA500',
    backgroundColor: 'FFF5E6',
  },
]

editor.commmands.setAiSuggestionRules(newRules)

Warning: the setAiSuggestionRules command will not automatically reload the suggestions. You need to call the loadAiSuggestions command to update the suggestions based on the new rules. A common pattern is to chain the two commands together.

editor.chain().setAiSuggestionRules(newRules).loadAiSuggestions().run()