Welcome to the first lesson of our course on building a short story generation service. In this lesson, we will focus on creating the base prompt, which is a crucial component for generating high-quality stories with large language models (LLMs).
A well-structured prompt does more than just guide the story generation process, it gives you better control over the output, ensures consistency, and reduces ambiguity in the instructions provided to the LLM. By clearly specifying the role, theme, style, and requirements, you help the model understand exactly what is expected, resulting in more relevant, coherent, and creative stories.
By the end of this lesson, you will understand how to create and manage a story prompt template, which will be used throughout the course to generate engaging short stories.
Before we dive into creating the base prompt, let's quickly recall some basics of file handling. This will be essential, as we will be working with a text file to store our story prompt template.
To work with files, you typically need to:
- Specify the file path and the mode in which you want to open the file (for example, reading or writing).
- Open the file, which gives you access to its contents.
- Read the contents of the file.
- Close the file when you are done to free up system resources.
A common and safe way to handle files is to use a context management approach, which ensures that the file is automatically closed after you are done working with it. This helps prevent issues such as forgetting to close the file or running into resource leaks if an error occurs.
Here is an example of reading a file using this approach:
In this example:
- The file is opened in read mode.
- The contents are read and stored in the
contentvariable. - The file is automatically closed after the block is executed.
The story prompt template is a text file that outlines the structure and requirements for generating a short story. Let's examine the components of the story_prompt_template.txt file:
- Role: Defines the perspective of the writer.
- Theme: A placeholder
{user_input}that will be replaced with the user's input. - Task: Describes the main objective of the story.
- Style Requirements: Specify the language, structure, and length of the story. A placeholder
{tone}that will be replaced with the user's input. - Output Requirements: List the intended uses for the story.
These components guide the story generation process, ensuring consistency and alignment with the desired output.
Now, let's implement the PromptManager class, which will manage the loading and formatting of the story prompt template.
Start by defining the PromptManager class with a docstring to describe its purpose.
- The
load_base_promptmethod attempts to open and read the file specified byfile_path. If successful, it returns the file's contents. If an error occurs, it prints an error message and returns a default prompt. - The
format_promptmethod callsload_base_promptto get the template, then uses theformatmethod to replace{user_input}with the actual user input.
Finally, let's see how to use the PromptManager class in the main application to generate a formatted prompt.
- Import the
PromptManagerclass. - Define a
user_inputstring with the desired theme. - Call
format_promptto generate the formatted prompt. - Print the generated prompt to see the result.
Output:
Note:
The example does not sanitize or validate theuser_inputandtonestrings. If these values come directly from user input, this could lead to formatting issues or even prompt injection vulnerabilities. In a production environment, always validate and sanitize user-provided data before inserting it into templates.
In this lesson, we explored the importance of a structured prompt in guiding the story generation process. We examined the components of the story_prompt_template.txt file and implemented the PromptManager class to manage and format the prompt. By integrating the PromptManager in the main application, we demonstrated how to generate a formatted prompt using user input.
As you move on to the practice exercises, focus on experimenting with different themes and observe how the prompt adapts. This hands-on practice will reinforce the concepts learned and prepare you for more advanced topics in subsequent lessons.
