Prompt Templates
Managing and rendering prompts in Instructor
Overview
As your applications grow in complexity, your prompts may become large and complex, with multiple variables and metadata. Managing these prompts can become a challenge, especially when you need to reuse them across different parts of your application. Large prompts are also hard to maintain if they are part of your codebase.
Prompt
addon provides a powerful and flexible way to manage your prompts. It supports
multiple template engines (Twig, Blade), prompt metadata, variable injection, and validation.
What are Prompts
Prompts in Instructor are based on structured text templates that can be rendered to text or series of chat messages. As many of your prompts will be dynamically generated based on input data, you can use syntax of one of the supported template engines (Twig, Blade) to define your prompts.
This document will be using Twig syntax for prompt templates for simplicity and consistency, but you can use Blade syntax in your prompts if you prefer it.
- For more information on Twig syntax see Twig documentation.
- For more information on Blade syntax see Blade documentation.
Basic Prompt Template
Example prompt template in Twig:
Prompt Template with Variables
You can define variables in your prompt templates and inject values when rendering the prompt.
Chat Messages
You can define chat messages in your prompts, which can be used to generate a sequence of messages for LLM chat APIs.
Prompt Template Metadata
We recommend to preface each prompt with front matter - a block of metadata that describes the prompt and its variables. This metadata can be used for validation, documentation, and schema generation.
Template Libraries
Instructor allows you to define multiple template libraries
in your app. Library is just a collection of
prompt templates which is stored under a specific directory. Library can have a nested structure, which allows
you to organize your prompts in a way that makes sense for your application.
Library properties are specified in config/prompt.php
configuration file.
where you can define:
templateEngine
- template engine used for prompts in this library,resourcePath
- path to prompt templates,cachePath
- path to compiled templates,extension
- file extension for prompt templates,frontMatterTags
- start and end tags for front matter,frontMatterFormat
- format of front matter (yaml, json, toml),metadata
- engine-specific configuration.
Instructor comes with 3 default prompt libraries:
system
- prompt templates used by Instructor itself,demo-twig
- demo prompt templates using Twig template engine,demo-blade
- demo prompt templates using Blade template engine.
Instructor’s does not specify how you should organize or manage your prompt templates, but it provides a flexible way to do it in a way that suits your application.
Using Prompt Templates
Rendering a Simple Prompt
To get started, you can create and render a simple prompt defined in the bundled library using the Prompt::using
or Prompt::make
methods. Here’s how you can use them:
Or, using the shorthand make()
syntax:
Rendering a Chat Template
The Prompt class can also render prompts directly as chat-style messages:
Custom Configuration and Template Content
If you need to customize the configuration or set the template content directly, you can do so with additional methods:
In Memory Templates
If you need to create an inline prompt (without saving it to a file), you can use following syntax:
There’s shorter syntax for creating in-memory prompts:
Handling Template Variables
To check which variables are available in a prompt template:
Loading Templates by Name and Using DSNs
For more flexible template loading, you can load templates by name or use a ‘DSN-like’ (Data Source Name) syntax:
Converting to Messages with Markup
The Prompt class also supports converting templates containing chat-specific markup into structured messages:
Here is an example XML that can be used to generate a sequence of chat messages:
And here is how you use Prompt
class to convert XML template into a sequence of messages: