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 interactions. Prompts are essential in guiding behavior, allowing us to tailor responses to specific needs. By the end of this lesson, you will understand how to load and render templates using variables — a crucial skill in building the AI Cooking Helper.

Recall: Basics of File Handling

Before we dive into templates, let's briefly recall how to handle files and navigate directories. This knowledge is essential, as we will be loading template files stored in specific folders. Instead of using hardcoded strings for paths, it is safer to use path manipulation tools to ensure that the application works regardless of where it is run.

import os

# Get the directory of the current script
current_dir = os.path.dirname(__file__)

# Construct a path to a file in the same directory
file_path = os.path.join(current_dir, "example.txt")

# Open and read the file
with open(file_path, "r", encoding="utf-8") as handle:
    content = handle.read()
print(content)

In this snippet, os.path.join creates a valid path, and the with statement ensures that the file is handled properly and closed automatically. We use encoding="utf-8" to ensure that text characters are read correctly.

Understanding Template Structure

Our templates will represent prompts that can be enriched with context. These are stored as text files with placeholders. Placeholders are enclosed in double curly braces, like {{variable_name}}.

For example, consider a template for a recipe generator:

Create a recipe for {{dish_name}} that serves {{servings}} people.
Include the following dietary restrictions: {{restrictions}}.

In this template, {{dish_name}}, {{servings}}, and {{restrictions}} are placeholders. When rendering the template, we provide actual values for these keys, making the prompt adaptable.

Loading Templates with load_template

To use a template, we first need to load it from the project's file structure. In our project, prompt files are stored in a specific directory: static/prompts.

import os

def load_template(template_name: str) -> str:
    """
    Load a template file from the 'static/prompts' directory.
    Automatically appends '.txt' to the filename.
    """
    # Navigate to the parent directory to find the 'static' folder
    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 handle:
        return handle.read()
  • base_dir calculates the absolute path to the project root.
  • prompts_dir points specifically to the static/prompts folder.
  • The function automatically appends .txt to the template_name, so you only need to provide the base name (e.g., recipe_request).
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