Prompt Structure and Variables

Introduction and Context Setting

Welcome to the lesson on "Prompt Structure and Variables." In this lesson, we will explore how prompts are structured and how variables are used to create dynamic and flexible AI interactions. Prompts are essential in guiding AI behavior, allowing us to tailor responses to specific needs. By the end of this lesson, you will understand how to load templates and render them using a dictionary of variables, a crucial skill in building the AI Cooking Helper.

Recall: Basics of File Handling in Python

Before we dive into templates, let's briefly recall file handling in Python. This knowledge is essential as we will be loading template files in this lesson. Remember, the open() function is used to open a file, and the read() method is used to read its contents. Here's a quick reminder:

file_path = 'example.txt'
with open(file_path, 'r') as file:
    content = file.read()
print(content)

This code snippet opens a file named example.txt, reads its contents, and prints them. The with statement ensures the file is closed automatically after reading.

Understanding Template Structure

Our templates represent prompts that can be enriched with variables. These templates are stored as text files with placeholders enclosed in double curly braces, like {{input}} or {{style}}. Let's look at a more flexible template example:

Rewrite the following sentence in a {{style}} tone:

Original: {{input}}

Return a list of up to 3 paraphrased versions.

In this template, {{style}} and {{input}} are placeholders. This structure allows us to inject multiple values dynamically into a single prompt.

Loading Templates with load_template

To use a template, we first need to load it from a file. Let's break down the load_template function:

import os

def load_template(template_name):
    base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
    prompts_dir = os.path.join(base_dir, 'static', 'prompts')
    file_path = os.path.join(prompts_dir, f"{template_name}.txt")

    with open(file_path, 'r', encoding='utf-8') as f:
        return f.read()
  1. base_dir: We find the absolute path of the project root.
  2. prompts_dir: We point to the specific folder where our .txt templates live.
  3. file_path: We construct the full path using the template_name.
  4. open: We read the file content and return it as a string.

Rendering Templates with Dictionary Variables

To handle multiple variables efficiently, we use a dictionary and Regular Expressions (regex). Instead of replacing strings one by one, we can use a callback function with re.sub().

The render_template function takes a template_str and a variables dictionary:

import re

def render_template(template_str, variables):
    def replacer(match):
        # match.group(1) captures the text inside {{ }}
        var_name = match.group(1)
        # Return the value from the dict if it exists, otherwise keep the placeholder
        if var_name in variables:
            return str(variables[var_name])
        else:
            print(f"Warning: Missing variable '{var_name}'")
            return match.group(0)

    # regex explanation:
    # \{\{ matches '{{'
    # (\w+) captures one or more alphanumeric characters (the variable name)
    # \}\} matches '}}'
    pattern = re.compile(r"\{\{(\w+)\}\}")
    return pattern.sub(replacer, template_str)

How the Regex and Callback Work:

  • Capture Groups: The (\w+) part of the regex is a "capture group." It identifies the name of the variable inside the braces.
  • The Replacer Function: re.sub calls our replacer function for every match found. We look up the captured name in our variables dictionary and return the replacement text.

Combining Template Loading and Rendering

Finally, we combine these steps into a single utility function:

def render_prompt_from_file(template_name, variables):
    template = load_template(template_name)
    return render_template(template, variables)

You can now call this with a dictionary of data:

data = {
    "style": "professional",
    "input": "The food is good."
}
prompt = render_prompt_from_file("example_template", data)

Summary and Preparation for Practice

In this lesson, you learned how to structure prompts using templates and dynamic variables. We covered loading files, using regex capture groups to identify placeholders, and using a dictionary-based callback to render final strings. As you move on to the practice exercises, you'll implement this logic to build the core prompt engine for your AI Cooking Helper. Experiment with different keys in your dictionaries to see how the regex handles them!

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal