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

Variables


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

Returns

Raises

Usage Example

try:
    prompt_text = load_prompt("welcome_message")
    print(prompt_text)
except FileNotFoundError as e:
    print(e)

Implementation Details and Algorithms


Interaction with Other System Components


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.