Skip to content

Prompt System

When to Use

Use the prompt system when you need reusable, deployable prompts with variable substitution. For one-off prompts in code, string interpolation is simpler.

Decision

Situation Choose Why
Reusable, site-configurable prompts ai.ai_prompt.* config entities Deployable via config sync; overridable per environment
Define prompt variables/schema ai.ai_prompt_type.* config entities Named variables for substitution
Modify prompts without code ai.prompt_manager service Loads and renders prompts with variable values

Config Entities

  • ai.ai_prompt_type.* — defines prompt types with named variables
  • ai.ai_prompt.* — individual prompt instances with text content

Pattern

$promptManager = \Drupal::service('ai.prompt_manager');
// Load and render a prompt with variables.

Variables

Prompts support Twig-style syntax: - {variableName} — simple substitution - {% if variableName %}...{% endif %} — conditional blocks

Example (AI Translate default prompt):

You are a helpful translator.
{% if sourceLangName %}Translate from {sourceLangName} {% endif %}to {destLangName}.
Preserve all HTML tags. Translate alt and title attributes.
{inputText}

Common Mistakes

  • Wrong: Hardcoding prompts in PHP strings → Right: Use prompt config entities so prompts are deployable and configurable without code changes

See Also