Prompt management is part of 14 ML/AI infrastructure modules including LLM gateway, model versioning, and A/B testing.
What is the Prompts Module?
The Prompts module provides comprehensive prompt management with security features, version control, and safe rendering for Large Language Models (LLMs).
importlogginglogger=logging.getLogger(__name__)fromagenticaiframework.promptsimportPrompt# Create a prompt with security enabledprompt=Prompt(template="Hello {name}, your task is: {task}",metadata={"category":"greeting","version":"1.0"},enable_security=True)# Render with variablesresult=prompt.render(name="Alice",task="analyze data")logger.info(result)# Safe rendering with automatic sanitizationsafe_result=prompt.render_safe(name="Bob",task="<script>alert('xss')</script>")logger.info(safe_result)# Script tags removed
importlogginglogger=logging.getLogger(__name__)fromagenticaiframework.promptsimportPrompt,PromptManager# Create manager with security enabledmanager=PromptManager(enable_security=True)# Register multiple promptsgreeting_prompt=Prompt(template="Welcome {user}! How can I help you today?",metadata={"type":"greeting"})manager.register_prompt(greeting_prompt)task_prompt=Prompt(template="Task: {task}\nContext: {context}\nOutput format: {format}",metadata={"type":"task"})manager.register_prompt(task_prompt)# Render by IDresult=manager.render_prompt(greeting_prompt.id,user="Alice")logger.info(result)# List all promptsprompts=manager.list_prompts()logger.info(f"Registered prompts: {len(prompts)}")
The Prompt Versioning module provides enterprise-grade version control for prompts with semantic versioning, lifecycle management, and full audit trails.
importlogginglogger=logging.getLogger(__name__)fromagenticaiframework.prompt_versioningimport(PromptVersionManager,PromptVersion,PromptStatus,prompt_version_manager)# Create manager with persistent storagemanager=PromptVersionManager(storage_path="/path/to/prompts")# Create a new prompt (starts at v1.0.0)prompt=manager.create_prompt(name="customer_support",template="Hello {customer_name}, I'm here to help with {issue}. {instructions}",created_by="admin",tags=["support","customer-facing"])logger.info(f"Created: {prompt.name} v{prompt.version}")# v1.0.0
importlogginglogger=logging.getLogger(__name__)# Create a new version with semantic versioningnew_version=manager.create_version(prompt_id=prompt.prompt_id,template="Hi {customer_name}! I'll assist you with {issue}. {instructions}",version_bump="minor",# major, minor, or patchcreated_by="developer",changelog="Improved greeting tone")logger.info(f"New version: v{new_version.version}")# v1.1.0
importlogginglogger=logging.getLogger(__name__)# Activate a version (makes it the default)manager.activate(prompt_id=prompt.prompt_id,version="1.1.0",activated_by="admin")# Rollback to a previous versionrolled_back=manager.rollback(prompt_id=prompt.prompt_id,target_version="1.0.0",rolled_back_by="admin")logger.info(f"Rolled back, new version: v{rolled_back.version}")# v1.1.1
# Render the active versionresult=manager.render(prompt_id=prompt.prompt_id,variables={"customer_name":"Alice","issue":"billing","instructions":"I'll look into this right away."})# Render a specific versionresult_v1=manager.render(prompt_id=prompt.prompt_id,variables={"customer_name":"Bob","issue":"shipping","instructions":""},version="1.0.0")
importlogginglogger=logging.getLogger(__name__)# Get audit log for a promptaudit_log=manager.get_audit_log(prompt_id=prompt.prompt_id,limit=50)forentryinaudit_log:logger.info(f"{entry['action']} by {entry['actor']} at {entry['timestamp']}")
fromagenticaiframework.prompt_versioningimportPromptLibrary,prompt_library# Register reusable componentsprompt_library.register_component(name="system_header",content="You are a helpful AI assistant. Be concise and accurate.",category="system",description="Standard system message header")prompt_library.register_component(name="safety_footer",content="Remember: Never share sensitive information. If unsure, ask for clarification.",category="safety",description="Safety guidelines footer")prompt_library.register_component(name="code_assistant_base",content="You are a coding assistant specializing in {language}. {context}",category="code",description="Base template for code assistants")
# Extend a base componentpython_prompt=prompt_library.extend(base_component="code_assistant_base",extensions={"replace_language":"Python","replace_context":"Focus on clean, PEP8-compliant code.","suffix":"Include type hints where appropriate."})
# Search componentsresults=prompt_library.search("code")# List by categorycode_components=prompt_library.list_by_category("code")# Get all categoriescategories=prompt_library.get_categories()