This class is an extension for the Nunjucks template engine that allows for the use of an AI prompt in a template.

Hierarchy (view full)

Constructors

Properties

AllowFormattingText: string = "Do not use markdown, HTML, or any other special formatting."
tags: string[] = []

One or more tags that represent case-sensitive tag names that will invoke this extension.

Accessors

Methods

  • Executes AI prompt processing using the configured language model.

    Parameter Mapping from CallExtensionAsync:

    • In parse(): new nodes.CallExtensionAsync(this, 'run', params, [body])
    • Results in: run(context, params, body, callBack)

    The key difference from TemplateEmbed is that we pass [body] as the 4th parameter to CallExtensionAsync, which shifts the parameter order to include both params and body as separate arguments. The body contains the prompt text between the opening and closing tags.

    Parameters

    • context: any

      Nunjucks template rendering context containing variables and data

    • params: any

      Parsed parameters from the opening tag (AIModel, AllowFormatting, etc.)

    • body: any

      Function that returns the rendered prompt content between {% AIPrompt %} and {% endAIPrompt %}

    • callBack: NunjucksCallback

      Async callback function to return the AI-generated response or errors

    Returns void

    Example

    {% AIPrompt AIModel="gpt-4", AllowFormatting=true %}
    Generate a personalized greeting for {{ user.name }} who works as {{ user.jobTitle }}.
    Make it professional but warm.
    {% endAIPrompt %}