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 and render templates using variables, a crucial skill in building the DeepResearcher
tool.
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:
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.
Our templates will represent prompts that can be enriched with variables, in the form of text files with placeholders. These placeholders will be enclosed in curly braces, like {{input}}
. Let's look at an example template:
In this template, {{input}}
is a placeholder that will be replaced with a specific sentence when the template is rendered. This structure allows us to create flexible prompts that can adapt to different inputs.
To use a template, we first need to load it from a file. Let's break down the load_template
function:
We start by defining the load_template
function, which takes a template_name
as an argument.
Here, base_dir
is the directory where the current script is located. We construct the full file path by joining base_dir
, the data
directory, and the template filename.
Next, we open the file and read its contents.
The with
statement ensures the file is closed after reading. The read()
method returns the entire content of the file as a string.
Once we have loaded a template, we can render it by replacing placeholders with actual values. Let's explore the render_template
function:
The render_template
function takes a template_str
and an input_value
.
We use the re
module to compile a pattern that matches {{input}}
. The sub()
method replaces all occurrences of this pattern with the input_value
.
Finally, let's see how loading and rendering are combined in the render_prompt_from_file
function:
This function will take a file_path
and an input_value
.
We first load the template using load_template()
, then render it with render_template()
, replacing placeholders with the provided input_value
.
In this lesson, you learned how to structure prompts using templates and variables. We covered loading templates from files, rendering them with actual values, and combining these steps to create dynamic prompts. As you move on to the practice exercises, you'll apply these skills to create and render your own templates. Experiment with different inputs to see how they affect the output. Keep up the great work, and enjoy the hands-on practice!
