Centralized prompt management for LLM applications
Project description
Python Prompt Manager
A lightweight, extensible prompt management system for LLM applications. Centralize and version your prompts while keeping your codebase clean.
Why Use This?
Managing prompts for LLM applications can quickly become messy. Hardcoded strings, scattered prompt files / urls, and unclear versioning make maintenance difficult. This package solves these problems by providing a clean, centralized way to manage your prompts.
Features
- Multiple Storage Backends: Store prompts in OpenAI's system, local files, or create your own storage extension
- Environment-Based Configuration: No hardcoded secrets or paths in your code
- Flexible Caching: Reduce API calls with configurable caching
- Framework Agnostic: Use with any Python framework or standalone scripts
- Type Safe: Full type hints for better development experience
- Extensible: Easy to add new storage backends
Installation
# Basic installation
pip install python-prompt-manager
# With OpenAI support
pip install python-prompt-manager[openai]
# With all optional dependencies
pip install python-prompt-manager[all]
Quick Start
Basic Usage
from prompt_manager import get_prompt
# Get a prompt (reads from configured source)
prompt = get_prompt("welcome_message")
print(prompt)
With Variables
# Use template variables in your prompts
prompt = get_prompt(
"greeting",
variables={"name": "Alice", "day": "Monday"}
)
# "Hello Alice! Happy Monday!"
Configuration
Configure your prompts using a simple Python dictionary:
PROMPT_CONFIG = {
"prompts": {
"welcome": {
"source": "openai",
"id": "pmpt_1234567890",
"version": "1.0"
},
"greeting": {
"source": "local",
"path": "greeting.txt"
},
"analysis": {
"source": "openai",
"id": "pmpt_0987654321",
"cache_ttl": 300 # 5 minutes
}
},
"sources": {
"openai": {
"api_key": os.getenv("OPENAI_API_KEY"), # Keep secrets in env vars
"timeout": 30,
"max_retries": 3
},
"local": {
"base_dir": "./prompts"
}
}
}
# Initialize with your config
from prompt_manager import PromptManager
pm = PromptManager(PROMPT_CONFIG)
Django Configuration
In your Django settings:
# settings.py
PROMPT_MANAGER = {
"prompts": {
"welcome": {"source": "openai", "id": "pmpt_123"},
"email_template": {"source": "local", "path": "emails/welcome.txt"}
},
"sources": {
"openai": {
"api_key": env("OPENAI_API_KEY")
}
}
}
Usage Examples
Simple Example
from prompt_manager import PromptManager
# Initialize with config
pm = PromptManager({
"prompts": {
"welcome": {"source": "openai", "id": "pmpt_123"},
"goodbye": {"source": "local", "path": "goodbye.txt"}
}
})
# Get prompts
welcome = pm.get("welcome")
goodbye = pm.get("goodbye")
With Default Fallback
# Provide a default if prompt is not found
prompt = pm.get("optional_prompt", default="This is a fallback prompt")
Dynamic Configuration
# Load config from a file
import json
with open("prompts.json") as f:
config = json.load(f)
pm = PromptManager(config)
Template Variables
# Configure a prompt with variables
config = {
"prompts": {
"greeting": {"source": "local", "path": "greeting.txt"}
}
}
pm = PromptManager(config)
# Apply variables when retrieving
prompt = pm.get(
"greeting",
variables={"name": "Alice", "app_name": "AwesomeApp"}
)
# "Hello Alice! Welcome to AwesomeApp."
Django Integration
Add your prompt configuration to settings:
# settings.py
PROMPT_MANAGER = {
"prompts": {
"welcome_email": {"source": "openai", "id": "pmpt_email_123"},
"user_greeting": {"source": "local", "path": "templates/greeting.txt"},
"error_message": {"source": "openai", "id": "pmpt_error_456"}
},
"sources": {
"openai": {"api_key": env("OPENAI_API_KEY")}
}
}
# Optional: Add the Django app for additional features
INSTALLED_APPS = [
...
'prompt_manager.integrations.django', # Optional
]
Use in your views:
# views.py
from django.conf import settings
from prompt_manager import PromptManager
# Initialize once
pm = PromptManager(settings.PROMPT_MANAGER)
def my_view(request):
prompt = pm.get("welcome_email", variables={"user": request.user.name})
# Use prompt with your LLM
...
Validation
By default, prompts are validated when first accessed. To validate all prompts on startup:
config = {
"prompts": {...},
"validate_on_startup": True # Validate all prompts exist
}
pm = PromptManager(config)
Advanced Usage
Error Handling
from prompt_manager import PromptManager, PromptNotFoundError
pm = PromptManager(config)
try:
prompt = pm.get("my_prompt")
except PromptNotFoundError:
# Handle missing prompt
logger.error("Prompt not found")
except Exception as e:
# Handle other errors
logger.error(f"Error loading prompt: {e}")
Caching
# Configure cache TTL per prompt
config = {
"prompts": {
"static_prompt": {"source": "local", "path": "static.txt"}, # Uses default cache
"dynamic_prompt": {"source": "openai", "id": "pmpt_123", "cache_ttl": 0} # No cache
},
"cache_ttl": 3600 # Default 1 hour
}
# Clear cache manually
pm.clear_cache()
Custom Sources
Extend the base class to add new sources:
from prompt_manager.sources.base import BasePromptSource
class DatabaseSource(BasePromptSource):
def fetch(self, config):
# Your implementation
return prompt_content
Best Practices
- Keep Secrets in Environment Variables: API keys should never be in code
- Use Clear Naming: Choose descriptive names for your prompts
- Set Appropriate Cache TTLs: Static prompts can cache longer than dynamic ones
- Handle Errors Gracefully: Always provide fallbacks for critical prompts
- Version Your Prompts: Use the version field to track prompt iterations
Development
# Clone the repository
git clone https://github.com/yourusername/python-prompt-manager.git
cd python-prompt-manager
# Install in development mode
pip install -e ".[dev]"
# Run tests
pytest
# Run linting
black src tests
flake8 src tests
mypy src
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Built for the modern LLM application stack
- Designed with production use in mind
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_prompt_manager-0.1.0.tar.gz.
File metadata
- Download URL: python_prompt_manager-0.1.0.tar.gz
- Upload date:
- Size: 28.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18a3cc6785425766a72550d3af2047b5e663f21ebfbddcb787e3f153511c3595
|
|
| MD5 |
a15f898e52598d29aacd3fd67b9da90c
|
|
| BLAKE2b-256 |
513df22ab30af73134ce86002622b7ace4434e3361f0038ecb6970ac4661a29b
|
File details
Details for the file python_prompt_manager-0.1.0-py3-none-any.whl.
File metadata
- Download URL: python_prompt_manager-0.1.0-py3-none-any.whl
- Upload date:
- Size: 25.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11d31e98a50749456d8004f952c6294c292c11be31d666bbbad1465f891a786e
|
|
| MD5 |
60e96630ad09671d25b04326b2c481e9
|
|
| BLAKE2b-256 |
722198c4c93ef55a965717979270d260af4dc7ca33788328e873bb81b54171a8
|