title

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

1

Dynamic Content:

Use variables to personalize prompts based on user input or context.

2

Conditional Content:

Include or exclude parts of your prompts depending on the situation.

3

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

1

Log into Langtail and select your project.

2

Navigate to 'Prompts' to either open an existing prompt or create a new one.

3

In the 'Template' section, craft your prompt.

Utilize Handlebars syntax and variables for dynamic content creation.

Example
Hello, {{name}}!

That’s it. You’re ready to use templating.

title

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:
Example
Hello, {{userName}}! How can I assist you today?

Date helper

You can use the $date helper to print the current date in the format MMMM dd, yyyy:

Example
{{$date}}

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:

Example
{{$date "yyyy-MM-dd'T'HH:mm:ss.SSSxxx"}}

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”:

Example
{{$date dateVar "MM"}}

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:
Example
{{#if (lt hour 12)}}
Good morning! How can I assist you today?
{{else}}
Good afternoon! How can I assist you today?
{{/if}}

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.
Example
{{#if (eq subscriptionStatus 'active')}}
Thank you for being a valued subscriber! Here's what's new...
{{else}}
Consider subscribing to get full access.
{{/if}}

Greater Than (gt)

  • Purpose: Check if one value is greater than another.
  • Example: Offer content based on user’s score.
Example
{{#if (gt userScore 50)}}
Great job! You scored above 50!
{{else}}
Try again to score more than 50.
{{/if}}

Logical AND (and)

  • Purpose: Check if multiple conditions are true.
  • Example: Provide options based on user’s age and consent.
Example
{{#if (and (gte userAge 18) (eq consentGiven true))}}
You have access to all features.
{{else}}
Some features may be restricted.
{{/if}}

Logical OR (or)

  • Purpose: Check if at least one of multiple conditions is true.
  • Example: Adjust prompt based on user’s role.
Example
{{#if (or (eq userRole 'admin') (eq userRole 'moderator'))}}
Access to admin controls granted.
{{else}}
Standard user access.
{{/if}}

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:

Example
{{#if (lt hour 12)}}
Good morning! How can I assist you today?
{{else}}
Good afternoon! How can I assist you today?
{{/if}}

Will be sended without the first newline after the {{#if}} tag as follows:

Example
Good morning! How can I assist you today?

This automatic newline management ensures that your templates produce cleaner, more coherent prompts for the LLM.