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.
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.
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.
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:
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.
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.
base_dircalculates the absolute path to the project root.prompts_dirpoints specifically to thestatic/promptsfolder.- The function automatically appends
.txtto thetemplate_name, so you only need to provide the base name (e.g.,recipe_request).
Once a template is loaded as a string, we need to replace the {{variable}} placeholders with real data. We use regular expressions (the re module) to find these patterns and swap them out.
- The
replacerfunction is a helper that determines what to put in place of a match. - If the variable exists in our
variablesdictionary, it returns the value as a string. - If the variable is missing, it prints a warning to the console and returns
match.group(0), which keeps the original{{variable_name}}text in the prompt so we can identify what went wrong.
To make the process seamless, we combine these two steps into a single function. This allows us to get a final, usable prompt string by simply providing a file name and the data.
This approach keeps our prompt logic clean: the text is managed in external files, and the code handles the injection of dynamic content.
In this lesson, you learned how to structure prompts using templates and variables. We covered:
- Navigating directories to locate template files.
- Loading text files using the correct
encoding. - Using
regular expressionsto find and replace placeholders. - Handling missing data by providing warnings while keeping the prompt structure intact.
As you move on to the practice exercises, you will implement these functions to build the foundation for the AI Cooking Helper. Experiment with different variable names and see how the template engine handles them!
