Prompt Writing

Prompt Wrangler uses Handlebars, a popular templating language, to construct prompts dynamically. This guide will walk you through how to construct and manipulate prompts using Handlebars and the special features provided by Prompt Wrangler.

Accessing Arguments

You can access arguments within your prompt using the following syntax:

{{{ args.ARG_NAME }}}

Replace ARG_NAME with the name of the argument you wish to access.

We recommend using {{{ }}} instead of {{ }}, as the former does not escape output, which is useful since we’re not dealing with HTML here.

Special Variables

There are a couple of special system variables you can use:

  • {{{ system.today }}}: Inserts the current date
  • {{{ system.now }}}: Inserts the current date and time

Helpers

Prompt Wrangler comes with some built in helpers to deal with common scenarios.

Limit Strings

Prompt Wrangler also includes a limit helper that allows you to limit the number of tokens in a text string. This can be useful for constraining the length of your output text.

The limit helper takes two parameters: the text to limit, and the maximum number of tokens. Please note that the number of tokens is an approximation.

Here’s how to use the limit helper:

{{{limit args.test 100}}}

In this example, args.test is the text string you want to limit, and 100 is the maximum number of tokens. If args.test contains more than 100 tokens, only the first 100 will be displayed.

Note: Tokens in this context are not necessarily single words; they can also be punctuation, special characters, or other meaningful units of text as determined by the underlying system.

JSON Formatting

Prompt Wrangler provides several built-in helpers to format JSON arguments:

  • table: formats the JSON as a markdown table
  • json: formats the JSON as a JSON string
  • yaml: formats the JSON as a YAML string

For the table helper, you can use the c option to order and filter the columns. The c option takes a comma-separated list of column names.

Examples:

Here’s an example that uses the table helper:

{{{table args.json c="id,animal"}}}

Here’s an example that uses the json helper:

{{{json args.json}}}

Here’s an example that uses the yaml helper:

{{{yaml args.json}}}

Randomization

In certain situations you want to force random results and using random prompt injection is a great way to do that. There are two helpers that can be used for this.

Random

The random helper picks a random integer in a given range. For example if you wanted to pick a random number between 1 and 10 you could do the following:

{{{random 1 10}}}

Pick Random

The pickRandom helper picks a random item from a list of items. For example if you wanted to pick a random item from a list of strings you could do the following:

{{{pickRandom "short" "medium" "long" "very long"}}}

You can also use this helper with an array passed in as an argument. If you passed in an argument named string_array you could do the following:

{{{pickRandom args.string_array}}}

Combining Helpers

Helpers can be combined together uses parentheses and a nested syntax. For example if you want to convert to yaml but only include the first 20 tokens you could do the following:

{{{limit (yaml args.json) 20}}}

Message Seperators (System Messages)

If you are using a chat style model gpt-4, gpt-3.5-turbo you can improve prompt performance by splitting up assistant, user and system messages. To do this in prompt wrangler simply insert the special message break syntax: >>> ROLE

Here is a quick example:

>>> SYSTEM
You are a chatbot who can only respond in emojis

>>> ASSISTANT
👋

>>> USER
Doing well. What's your favorite color

This is equivalent to the following JSON structure in the API:

[
  {
    "role": "SYSTEM",
    "text": "You are a chatbot who can only respond in emojis"
  },
  {
    "role": "ASSISTANT",
    "text": "👋"
  },
  {
    "role": "USER",
    "text": "Doing well. What's your favorite color"
  }
]

Here are some additional important notes:

  • You can use variables in any of the message roles.
  • If you don’t specify a role it will default to USER.
  • If you don’t specify a system message and use a chat model a default system message will be added
  • Any message splits will automatically be removed if you are using a non-chat model
  • There are only three valid roles: SYSTEM, ASSISTANT, and USER. Any other role will be ignored.

Advanced Handlebars Features

You can utilize all the features of Handlebars. Here are some common examples:

Conditional Logic

{{#if args.condition}}
  This will display if args.condition is truthy.
{{else}}
  This will display if args.condition is falsy.
{{/if}}

Loops

If you pass in JSON that contain and array you can use the #each helper to loop over the array. For example.

{{#each args.tools}}
  {{{this.name}}}:
  {{{this.description}}}
{{/each}}

Frequently Asked Questions

1. What is the Handlebars syntax used for in Prompt Wrangler?

Handlebars is a popular templating language that Prompt Wrangler uses for constructing dynamic prompts. It provides various features like accessing arguments, conditional logic, and using built-in helpers for formatting.

2. How do I access arguments in my prompt?

You can access arguments in your prompt using the {{{ args.ARG_NAME }}} syntax. Replace ARG_NAME with the actual name of the argument you want to access.

3. Why should I use {{{ }}} instead of {{ }} in Prompt Wrangler?

We recommend using {{{ }}} because it does not escape output. This is useful since we’re not dealing with HTML in Prompt Wrangler.

4. How do I use the special system variables?

There are two special system variables that you can use in Prompt Wrangler. {{{ system.today }}} inserts the current date, and {{{ system.now }}} inserts the current date and time.

5. Can I use conditional logic in Handlebars?

Yes, Handlebars supports conditional logic. For example:

{{#if args.condition}}
  This will display if args.condition is truthy.
{{else}}
  This will display if args.condition is falsy.
{{/if}}

6. How do I format JSON arguments in Prompt Wrangler?

You can use the built-in helpers table, json, and yaml to format JSON arguments. You can also order and filter columns in the table helper using the c option.

7. How do I limit the number of tokens in a text string?

You can use the limit helper to restrict the number of tokens in a text string. For example, {{{ limit args.test 100 }}} will limit the text string args.test to approximately 100 tokens.

8. What is a token in the context of the limit helper?

The limit helper uses an approximation of 2.5 characters per token. This is a conservative estimate that should work for most use cases.

9. Why do I see [object Object] in my prompt?

You will see [object object] if you try to pass in a JSON argument without formatting it. For example if the argument is tools and you pass in {{{ tools }}} you will see [object object]. Instead you should use {{{ json tools }}} or another one of the formatters to format the JSON argument.