jinja2 support

This commit is contained in:
2025-09-16 16:05:01 +02:00
parent 2000370544
commit 5ce72ffb25
8 changed files with 647 additions and 36 deletions

156
test_jinja2_prompts.py Normal file
View File

@@ -0,0 +1,156 @@
#!/usr/bin/env python3
"""Test script for Jinja2-enhanced prompt management with list support"""
from pathlib import Path
from src.llmutils.prompt_manager import PromptManager, ManagedPrompt
# Create a test prompts directory
test_prompts_dir = Path("test_prompts")
test_prompts_dir.mkdir(exist_ok=True)
# Create test prompt with simple variable
simple_prompt = test_prompts_dir / "simple.md"
simple_prompt.write_text("""Hello {{name}}!
Your age is {{age}}.""")
# Create test prompt with list iteration
list_prompt = test_prompts_dir / "list_example.md"
list_prompt.write_text("""# Task List
Here are your tasks:
{% for task in tasks %}
- {{ task }}
{% endfor %}
Total tasks: {{ tasks | length }}""")
# Create test prompt with complex data structures
complex_prompt = test_prompts_dir / "complex.md"
complex_prompt.write_text("""# User Report
Name: {{ user.name }}
Email: {{ user.email }}
## Assigned Tasks:
{% for task in user.tasks %}
- [{{ task.status }}] {{ task.title }}
Priority: {{ task.priority }}
{% endfor %}
## Skills:
{{ skills | join(', ') }}
## Recent Activity:
{% for date, activity in activities.items() %}
- {{ date }}: {{ activity }}
{% endfor %}""")
# Create prompt with conditionals
conditional_prompt = test_prompts_dir / "conditional.md"
conditional_prompt.write_text("""# Status Report
{% if error %}
⚠️ ERROR: {{ error }}
{% else %}
✅ All systems operational
{% endif %}
{% if items %}
Items to process ({{ items | length }}):
{% for item in items %}
{{ loop.index }}. {{ item | upper }}
{% endfor %}
{% else %}
No items to process.
{% endif %}
{% if debug %}
Debug info: {{ debug_data | tojson }}
{% endif %}""")
# Configure PromptManager
PromptManager.configure(path=test_prompts_dir)
print("=" * 60)
print("Testing Jinja2-Enhanced Prompt Management")
print("=" * 60)
# Test 1: Simple variable replacement
print("\n1. Simple variable replacement:")
print("-" * 40)
prompt = PromptManager.get_prompt("simple")
filled = prompt.fill(name="Alice", age=30)
print(filled)
# Test 2: List iteration
print("\n2. List iteration:")
print("-" * 40)
prompt = PromptManager.get_prompt("list_example")
filled = prompt.fill(tasks=["Write code", "Review PR", "Update documentation", "Deploy to staging"])
print(filled)
# Test 3: Complex data structures
print("\n3. Complex data structures:")
print("-" * 40)
prompt = PromptManager.get_prompt("complex")
filled = prompt.fill(
user={
"name": "Bob Smith",
"email": "bob@example.com",
"tasks": [
{"title": "Fix bug #123", "status": "", "priority": "High"},
{"title": "Implement feature X", "status": "", "priority": "Medium"},
{"title": "Code review", "status": "", "priority": "Low"}
]
},
skills=["Python", "JavaScript", "Docker", "Kubernetes"],
activities={
"2024-01-15": "Deployed v2.3.0",
"2024-01-14": "Fixed critical security issue",
"2024-01-13": "Merged 5 PRs"
}
)
print(filled)
# Test 4: Conditionals
print("\n4. Conditionals (with items):")
print("-" * 40)
prompt = PromptManager.get_prompt("conditional")
filled = prompt.fill(
error=None, # Provide None for optional conditional variables
items=["apple", "banana", "cherry"],
debug=True,
debug_data={"version": "1.0", "env": "dev"}
)
print(filled)
print("\n5. Conditionals (with error):")
print("-" * 40)
prompt = PromptManager.get_prompt("conditional")
filled = prompt.fill(
error="Connection timeout",
items=[],
debug=False,
debug_data={}
)
print(filled)
# Test 6: Pre-filled prompt
print("\n6. Pre-filled prompt on retrieval:")
print("-" * 40)
prompt = PromptManager.get_prompt("simple", name="Charlie", age=25)
print(prompt.prompt) # Should already be filled
# Test 7: Variable extraction
print("\n7. Variable extraction from complex template:")
print("-" * 40)
prompt = PromptManager.get_prompt("complex")
print(f"Required variables: {prompt.variables}")
print("\n" + "=" * 60)
print("All tests completed successfully!")
print("=" * 60)
# Cleanup
import shutil
shutil.rmtree(test_prompts_dir)