simplified
This commit is contained in:
130
test_simplified.py
Normal file
130
test_simplified.py
Normal file
@@ -0,0 +1,130 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Test the simplified prompt manager that relies on Jinja2's built-in handling"""
|
||||
|
||||
from pathlib import Path
|
||||
from jinja2 import UndefinedError, DebugUndefined
|
||||
from src.llmutils.prompt_manager_simple import PromptManager
|
||||
|
||||
# Create test prompts directory
|
||||
test_dir = Path("test_prompts")
|
||||
test_dir.mkdir(exist_ok=True)
|
||||
|
||||
# Create templates
|
||||
greeting = test_dir / "greeting.md"
|
||||
greeting.write_text("Hello {{ name }}, you are {{ age }} years old!")
|
||||
|
||||
# Template with optional variables using default filter
|
||||
flexible = test_dir / "flexible.md"
|
||||
flexible.write_text("""# Report for {{ title }}
|
||||
|
||||
Status: {{ status | default('Unknown') }}
|
||||
|
||||
{% if error is defined %}
|
||||
Error: {{ error }}
|
||||
{% endif %}
|
||||
|
||||
Author: {{ author | default('System') }}""")
|
||||
|
||||
# Create a schema file
|
||||
schema_file = test_dir / "flexible.json"
|
||||
schema_file.write_text("""{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"title": {"type": "string"},
|
||||
"status": {"type": "string"},
|
||||
"error": {"type": "string"},
|
||||
"author": {"type": "string"}
|
||||
},
|
||||
"required": ["title"]
|
||||
}""")
|
||||
|
||||
# Configure PromptManager
|
||||
PromptManager.configure(path=test_dir)
|
||||
|
||||
print("=" * 70)
|
||||
print("Testing Simplified Prompt Manager")
|
||||
print("=" * 70)
|
||||
|
||||
# Test 1: Normal usage with all variables
|
||||
print("\n1. Normal usage with all variables:")
|
||||
print("-" * 40)
|
||||
prompt = PromptManager.get_prompt("greeting")
|
||||
filled = prompt.fill(name="Alice", age=30)
|
||||
print(filled)
|
||||
|
||||
# Test 2: Missing required variable (will raise error)
|
||||
print("\n2. Missing required variable (should raise error):")
|
||||
print("-" * 40)
|
||||
prompt = PromptManager.get_prompt("greeting")
|
||||
try:
|
||||
filled = prompt.fill(name="Bob") # Missing 'age'
|
||||
print(filled)
|
||||
except UndefinedError as e:
|
||||
print(f"✓ Error raised as expected: {e}")
|
||||
|
||||
# Test 3: Template with defaults and conditionals
|
||||
print("\n3. Template with optional variables (using defaults and conditionals):")
|
||||
print("-" * 40)
|
||||
prompt = PromptManager.get_prompt("flexible")
|
||||
filled = prompt.fill(title="Daily Report") # Only required variable
|
||||
print(filled)
|
||||
|
||||
# Test 4: Same template with all variables
|
||||
print("\n4. Same template with all variables provided:")
|
||||
print("-" * 40)
|
||||
prompt = PromptManager.get_prompt("flexible")
|
||||
filled = prompt.fill(
|
||||
title="Error Report",
|
||||
status="Failed",
|
||||
error="Database connection timeout",
|
||||
author="Admin"
|
||||
)
|
||||
print(filled)
|
||||
|
||||
# Test 5: Pre-fill on retrieval
|
||||
print("\n5. Pre-fill on retrieval:")
|
||||
print("-" * 40)
|
||||
prompt = PromptManager.get_prompt("greeting", name="Charlie", age=25)
|
||||
print(prompt.prompt) # Already filled
|
||||
|
||||
# Test 6: Schema is automatically loaded
|
||||
print("\n6. Schema automatically loaded:")
|
||||
print("-" * 40)
|
||||
prompt = PromptManager.get_prompt("flexible")
|
||||
if prompt.schema:
|
||||
print(f"✓ Schema found: {prompt.schema}")
|
||||
else:
|
||||
print("✗ No schema")
|
||||
|
||||
# Test 7: Configure with DebugUndefined
|
||||
print("\n7. Using DebugUndefined mode:")
|
||||
print("-" * 40)
|
||||
PromptManager.configure(undefined=DebugUndefined)
|
||||
PromptManager.reload_prompts() # Clear cache
|
||||
prompt = PromptManager.get_prompt("greeting")
|
||||
filled = prompt.fill(name="Debug") # Missing 'age' - will show as undefined
|
||||
print(filled)
|
||||
|
||||
# Test 8: List available prompts
|
||||
print("\n8. List available prompts:")
|
||||
print("-" * 40)
|
||||
prompts = PromptManager.list_prompts()
|
||||
for name, info in prompts.items():
|
||||
print(f"- {name}: {'has schema' if info['has_schema'] else 'no schema'}")
|
||||
|
||||
print("\n" + "=" * 70)
|
||||
print("Benefits of this simplified approach:")
|
||||
print("-" * 70)
|
||||
print("✓ Jinja2 handles undefined variables for us")
|
||||
print("✓ No need for complex variable extraction and validation")
|
||||
print("✓ Users can choose undefined behavior (strict, debug, silent)")
|
||||
print("✓ Templates can use Jinja2's built-in features for optional vars:")
|
||||
print(" - {{ var | default('value') }}")
|
||||
print(" - {% if var is defined %}")
|
||||
print("✓ Schemas are automatically loaded if present")
|
||||
print("✓ Much simpler and cleaner codebase")
|
||||
print("=" * 70)
|
||||
|
||||
# Cleanup
|
||||
import shutil
|
||||
shutil.rmtree(test_dir)
|
||||
Reference in New Issue
Block a user