Prompt Templating
Use Handlebars to add conditions to your templates
Introduction
In Langtail, templating with Handlebars helps you build smarter, dynamic prompts for LLM apps. It’s straightforward: templates let your prompts change based on the data they get. This means you can handle complex cases without writing tons of code.
Why it’s useful
Dynamic Content:
Use variables to personalize prompts based on user input or context.
Conditional Content:
Include or exclude parts of your prompts depending on the situation.
Prompt Variations:
Quickly test different ways of asking or responding without redoing your work.
Templating makes your LLM apps more adaptable. Let’s get into how you can use Handlebars in Langtail to make this happen.
Setting Up Your Environment
Log into Langtail and select your project.
Navigate to 'Prompts' to either open an existing prompt or create a new one.
In the 'Template' section, craft your prompt.
Utilize Handlebars syntax and variables for dynamic content creation.
That’s it. You’re ready to use templating.
Simple Templating with Variables
Use variables to customize prompts based on user input or context, making your interactions with GPT models more dynamic.
- Inserting Variables:
- Syntax:
{{variableName}}
- Example: To customize a chatbot response, you might use:
- Syntax:
Date helper
You can use the $date
helper to print the current date in the format MMMM dd, yyyy
:
You can use one argument to specify the date and time format.
If you need time as well, LLM can work with a format that includes timezone like ISO 8601:
You can also pass your own date as a first argument and format as a second argument. Even variables are supported as $date input.
For example, if dateVar
variable is set to 2024-09-01
, this example will print just “09”:
Using Conditionals in Templates
Conditionals in your templates allow for dynamic changes in the prompts based on specific input data you send to the LLM. This enables you to tailor the interaction based on predefined conditions.
- Basic Conditionals:
- Syntax:
{{#if condition}}...{{/if}}
- Example: If you’re creating a prompt where the response varies based on the time of day provided as input, you might use:
- Syntax:
In this example, hour
is a variable that you would set based on the user’s
local time before sending the prompt to the LLM.
Section 4: Advanced Conditionals with Helpers
For more complex logic within your Langtail prompts, you can use Handlebars helpers. These helpers allow you to perform operations like comparison, logical AND/OR, and more, enabling sophisticated control over the content of your prompts based on the input data.
Using Helpers
Helpers like eq
, ne
, lt
, gt
, lte
, gte
, and
, or
can be used to evaluate conditions. This enables you to create dynamic prompts that respond to specific input data.
- Syntax for Helpers:
{{#if (helperName arg1 arg2)}}...{{/if}}
- Helpers are used within the
{{#if ...}}
block to evaluate conditions.
Examples
Equality Check (eq)
- Purpose: Check if two values are equal.
- Example: Customize prompt based on user subscription status.
Greater Than (gt)
- Purpose: Check if one value is greater than another.
- Example: Offer content based on user’s score.
Logical AND (and)
- Purpose: Check if multiple conditions are true.
- Example: Provide options based on user’s age and consent.
Logical OR (or)
- Purpose: Check if at least one of multiple conditions is true.
- Example: Adjust prompt based on user’s role.
By using these helpers, you can create prompts that directly respond to the input data. This approach allows for more precise and relevant LLM interactions.
Handling Newlines in Templates
When working with templates in Langtail, it’s important to manage newlines carefully to ensure they don’t negatively impact the LLM’s response quality. To address this, Langtail automatically adjusts newlines in specific cases within your templates. This adjustment applies to structures like if
, else
, each
, unless
, and with
. Here’s how it works:
- First Newline After Opening Tags: The first newline immediately following an opening tag (
{{#if}}
,{{#each}}
,{{#unless}}
,{{#with}}
) is automatically removed. This prevents unintended breaks that could confuse the LLM. - Newlines Around
{{else}}
: Newlines immediately before and after{{else}}
are removed. This ensures that the content before and after the{{else}}
is treated as a continuous block without unnecessary breaks. - Newlines Before and After Closing Tags: Newlines immediately before and after closing tags (
{{/if}}
,{{/each}}
,{{/unless}}
,{{/with}}
) are also removed. This action keeps the template compact and avoids introducing breaks that the LLM might misinterpret.
Example
For example this template when hour is less than 12:
Will be sended without the first newline after the {{#if}}
tag as follows:
This automatic newline management ensures that your templates produce cleaner, more coherent prompts for the LLM.
Was this page helpful?