Introduction and Overview

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.

Recall: Basics of File Handling

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 content variable.
  • The file is automatically closed after the block is executed.
Understanding the Story Prompt Template

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.

Implementing the PromptManager Class

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_prompt method attempts to open and read the file specified by file_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_prompt method calls load_base_prompt to get the template, then uses the format method to replace {user_input} with the actual user input.
Integrating the PromptManager in the Main Application

Finally, let's see how to use the PromptManager class in the main application to generate a formatted prompt.

  • Import the PromptManager class.
  • Define a user_input string with the desired theme.
  • Call format_prompt to generate the formatted prompt.
  • Print the generated prompt to see the result.

Output:

Note:
The example does not sanitize or validate the user_input and tone strings. 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.

Summary and Preparation for Practice

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.

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