AI prompt schema for PhoenixKit AI system.
A prompt is a reusable text template with variable substitution support.
Variables use the {{VariableName}} syntax and are automatically extracted
from the content when saved.
Schema Fields
Identity
name: Display name for the prompt (unique)slug: URL-friendly identifier (auto-generated from name, unique)description: Optional description of the prompt's purpose
Content
content: The prompt template text with optional{{variables}}variables: Auto-extracted variable names from content
Status
enabled: Whether the prompt is activesort_order: Display order for listing
Usage Tracking
usage_count: Number of times the prompt has been usedlast_used_at: Timestamp of the last usage
Metadata
metadata: Flexible JSON storage for additional data
Variable Syntax
Variables use double curly braces: {{VariableName}}
- Variable names must be alphanumeric with underscores
- Variables are case-sensitive
- Unmatched variables remain in the output as-is
Usage Examples
# Create a prompt
{:ok, prompt} = PhoenixKit.Modules.AI.create_prompt(%{
name: "Translator",
content: "Translate the following text to {{Language}}:\n\n{{Text}}"
})
# Variables auto-extracted: ["Language", "Text"]
# Render with variables
{:ok, text} = PhoenixKit.Modules.AI.Prompt.render(prompt, %{
"Language" => "French",
"Text" => "Hello, world!"
})
# => "Translate the following text to French:\n\nHello, world!"
# Use with AI completion
{:ok, response} = PhoenixKit.Modules.AI.ask_with_prompt(
endpoint_id,
prompt.id,
%{"Language" => "Spanish", "Text" => "Good morning"}
)
Summary
Functions
Creates a changeset for prompt creation and updates.
Returns a truncated preview of the content for display.
Extracts variable names from content.
Formats variables for display in the UI.
Generates a URL-friendly slug from the name.
Checks if a prompt has any variables defined.
Returns a list of invalid variable patterns in the content.
Merges provided variables with defaults for missing ones.
Renders a prompt by replacing variables with provided values.
Renders content string directly (without a Prompt struct).
Creates a changeset for incrementing usage.
Checks if content has valid variable syntax.
Validates that all required variables are provided.
Returns the number of variables in a prompt.
Returns the variable regex pattern.
Functions
Creates a changeset for prompt creation and updates.
Returns a truncated preview of the content for display.
Extracts variable names from content.
Variables are matched using the {{VariableName}} syntax.
Returns a list of unique variable names in order of appearance.
Examples
iex> PhoenixKit.Modules.AI.Prompt.extract_variables("Hello {{Name}}, welcome to {{Place}}!")
["Name", "Place"]
iex> PhoenixKit.Modules.AI.Prompt.extract_variables("No variables here")
[]
iex> PhoenixKit.Modules.AI.Prompt.extract_variables("{{A}} and {{B}} and {{A}} again")
["A", "B"]
Formats variables for display in the UI.
Returns a string like "{{Name}}, {{Age}}" for easy display.
Examples
iex> prompt = %PhoenixKit.Modules.AI.Prompt{variables: ["Name", "Age"]}
iex> PhoenixKit.Modules.AI.Prompt.format_variables_for_display(prompt)
"{{Name}}, {{Age}}"
iex> prompt = %PhoenixKit.Modules.AI.Prompt{variables: []}
iex> PhoenixKit.Modules.AI.Prompt.format_variables_for_display(prompt)
""
Generates a URL-friendly slug from the name.
Uses PhoenixKit.Utils.Slug.slugify/1 for consistent slug generation.
Examples
iex> PhoenixKit.Modules.AI.Prompt.generate_slug("My Cool Prompt!")
"my-cool-prompt"
iex> PhoenixKit.Modules.AI.Prompt.generate_slug("Translate to French")
"translate-to-french"
Checks if a prompt has any variables defined.
Examples
iex> prompt = %PhoenixKit.Modules.AI.Prompt{variables: ["Name", "Age"]}
iex> PhoenixKit.Modules.AI.Prompt.has_variables?(prompt)
true
iex> prompt = %PhoenixKit.Modules.AI.Prompt{variables: []}
iex> PhoenixKit.Modules.AI.Prompt.has_variables?(prompt)
false
Returns a list of invalid variable patterns in the content.
Useful for showing validation errors in the UI.
Examples
iex> PhoenixKit.Modules.AI.Prompt.invalid_variables("Hello {{Name}}!")
[]
iex> PhoenixKit.Modules.AI.Prompt.invalid_variables("{{User Name}} and {{ok}}")
["User Name"]
Merges provided variables with defaults for missing ones.
Returns a map with all required variables, using defaults for any not provided.
Examples
iex> prompt = %PhoenixKit.Modules.AI.Prompt{variables: ["Name", "Age"]}
iex> PhoenixKit.Modules.AI.Prompt.merge_with_defaults(prompt, %{"Name" => "John"}, %{"Age" => "Unknown"})
%{"Name" => "John", "Age" => "Unknown"}
Renders a prompt by replacing variables with provided values.
Variables not found in the values map remain as-is in the output. Supports both string and atom keys in the values map.
Examples
iex> prompt = %PhoenixKit.Modules.AI.Prompt{content: "Hello {{Name}}!"}
iex> PhoenixKit.Modules.AI.Prompt.render(prompt, %{"Name" => "World"})
{:ok, "Hello World!"}
iex> prompt = %PhoenixKit.Modules.AI.Prompt{content: "Translate to {{Lang}}: {{Text}}"}
iex> PhoenixKit.Modules.AI.Prompt.render(prompt, %{Lang: "French", Text: "Hello"})
{:ok, "Translate to French: Hello"}
iex> prompt = %PhoenixKit.Modules.AI.Prompt{content: "Missing {{Var}}"}
iex> PhoenixKit.Modules.AI.Prompt.render(prompt, %{})
{:ok, "Missing {{Var}}"}
Renders content string directly (without a Prompt struct).
Useful for previewing variable substitution.
Examples
iex> PhoenixKit.Modules.AI.Prompt.render_content("Hello {{Name}}!", %{"Name" => "World"})
{:ok, "Hello World!"}
Creates a changeset for incrementing usage.
Checks if content has valid variable syntax.
Returns true if all {{...}} patterns contain valid variable names
(alphanumeric and underscores only), or if there are no variables.
Examples
iex> PhoenixKit.Modules.AI.Prompt.valid_content?("Hello {{Name}}!")
true
iex> PhoenixKit.Modules.AI.Prompt.valid_content?("No variables here")
true
iex> PhoenixKit.Modules.AI.Prompt.valid_content?("Hello {{User Name}}!")
false
Validates that all required variables are provided.
Returns :ok if all variables are present, or {:error, missing} with
a list of missing variable names.
Examples
iex> prompt = %PhoenixKit.Modules.AI.Prompt{variables: ["Name", "Age"]}
iex> PhoenixKit.Modules.AI.Prompt.validate_variables(prompt, %{"Name" => "John", "Age" => "30"})
:ok
iex> prompt = %PhoenixKit.Modules.AI.Prompt{variables: ["Name", "Age"]}
iex> PhoenixKit.Modules.AI.Prompt.validate_variables(prompt, %{"Name" => "John"})
{:error, ["Age"]}
Returns the number of variables in a prompt.
Examples
iex> prompt = %PhoenixKit.Modules.AI.Prompt{variables: ["Name", "Age"]}
iex> PhoenixKit.Modules.AI.Prompt.variable_count(prompt)
2
Returns the variable regex pattern.