prompt_template.py
Overview
The prompt_template.py file provides a simple utility for loading text prompt templates stored as Markdown (.md) files from the same directory as the script. It manages prompt caching to avoid redundant file reads, improving efficiency when prompts are reused multiple times within an application. This functionality is commonly used in systems that dynamically retrieve and use textual templates, such as AI prompt engineering, chatbot responses, or configuration-driven text generation.
Detailed Explanation
Constants
PROMPT_DIR(str):
This constant holds the absolute directory path where theprompt_template.pyfile is located. It is used as the base directory to locate prompt Markdown files.
Variables
_loaded_prompts(dict[str, str]):
A private dictionary used as an in-memory cache to store the contents of loaded prompts. The keys are prompt names (without extension), and the values are the string contents of the corresponding prompt files.
Function: load_prompt
def load_prompt(name: str) -> str:
Description
Loads the content of a prompt template from a Markdown file named <name>.md located in the same directory as prompt_template.py. If the prompt content was previously loaded, it returns the cached version instead of reading from disk again.
Parameters
name(str):
The base name of the prompt file (without the.mdextension) to be loaded. For example, ifnameis"welcome", the function attempts to loadwelcome.md.
Returns
str:
The content of the prompt file as a stripped Unicode string.
Raises
FileNotFoundError:
Raised if the file<name>.mddoes not exist in the prompt directory.
Usage Example
try:
prompt_text = load_prompt("welcome_message")
print(prompt_text)
except FileNotFoundError as e:
print(e)
Implementation Details and Algorithms
Caching Mechanism:
The function implements a simple caching mechanism using the_loaded_promptsdictionary. When a prompt is loaded for the first time, its content is read from disk and stored in_loaded_prompts. Subsequent calls to load the same prompt return the cached content, reducing file I/O overhead.File Path Resolution:
The prompt files are assumed to be stored in the same directory as the script (prompt_template.py). The path to the prompt file is constructed by joiningPROMPT_DIRwith the prompt's filename ({name}.md). This approach makes the system's prompt storage location predictable and consistent.File Content Handling:
The function reads the entire content of the Markdown file at once using UTF-8 encoding and strips whitespace from the start and end of the content before caching and returning it.
Interaction with Other System Components
Prompt Management:
This file functions as a utility module, likely imported by other components that require prompt templates for processing user input, generating responses, or configuring AI behavior.File Organization:
The file expects prompt templates to be maintained as Markdown files alongside this script or in the same directory, encouraging modular prompt management.Error Propagation:
By raisingFileNotFoundErrorwhen a prompt file is missing, it allows calling code to handle missing prompts explicitly, enhancing robustness.
Visual Diagram
flowchart TD
A[load_prompt(name: str)] --> B{Is prompt in _loaded_prompts?}
B -- Yes --> C[Return cached prompt content]
B -- No --> D[Construct path: PROMPT_DIR + name.md]
D --> E{Does file exist?}
E -- No --> F[Raise FileNotFoundError]
E -- Yes --> G[Open and read file content]
G --> H[Strip content whitespace]
H --> I[Cache content in _loaded_prompts]
I --> C
Summary
The prompt_template.py file is a focused utility for loading and caching Markdown-based prompt templates from the local directory. It provides a simple, efficient way to manage prompt content for applications that rely on text templates, facilitating reusable, modular prompt management with minimal overhead.