Advanced Prompt Templating Using Mustache

Enable logic flow in your prompt templates

In addition to the basic prompt template features in Freeplay, we also support advanced prompt templating based on Mustache (full docs here). Mustache is a logic-less template syntax used to dynamically render new data within your prompts. It can be used for more advanced prompt templating use cases, like using conditional statements, if/else logic, and passing lists of variables into a prompt.

Some benefits include:

  • Dynamic Content Generation: Create more flexible and context-aware prompts.
  • Simplified Template Management: Less need for multiple similar templates, as one template can handle various data scenarios.

In practice, Mustache gives advanced controls to create more complex prompt templates, and is still user-friendly enough for non-developers to interpret and edit in Freeplay. However, just like the variable names described above, using advanced Mustache features depends on a strong contract with the code that will be used by your system to interact with the prompt template. It's important to align on expectations about what's in the code for anyone editing prompts in Freeplay.

Basic Mustache Syntax
Mustache templates are simple yet powerful. They work by expanding tags in a template using values provided in a hash or object. A Mustache template might look like this:

{{#repo}} // IF
  {{name}}
{{/repo}}
{{^repo}} // ELSE
  No repos :(
{{/repo}}

Given a hash with data for repo and name Mustache will render this template with appropriate substitutions.

Supported Tag Types

  • Variables: Basic tags like {{name}} are replaced with the value associated with that key.
  • Dotted Names: For nested objects, use dotted names like {{client.name}}.
  • Sections: For conditional rendering, use sections like {{#logged_in}}...{{/logged_in}}. This will render the block only if logged_in is true or non-empty.
  • Inverted Sections: Use {{^logged_in}}...{{/logged_in}} for the opposite: rendering when logged_in is false or empty.

Note that we do not follow the Mustache spec exactly. We don't currently support all tag types (see limitations below). Also, our Mustache implementation does /not/ escape special characters (quotes, braces, etc.).
We think this is more intuitive, but is not technically aligned to the Mustache spec.

Limitations

We currently disallow Mustache Partials (syntax like {{> prompt_rules}}) that would recursively include other prompts or code.


What’s Next

Dig into deeper with Prompt Management and learn about Prompt Bundling, or jump into Evaluations.

Did this page help you?