221 lines
6.2 KiB
Python
221 lines
6.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Example showcasing Jinja2 template features in prompt management"""
|
|
|
|
from pathlib import Path
|
|
from src.llmutils.prompt_manager import PromptManager
|
|
|
|
# Create example prompts directory
|
|
prompts_dir = Path("prompts")
|
|
prompts_dir.mkdir(exist_ok=True)
|
|
|
|
# Create a code generation prompt with lists
|
|
code_gen_prompt = prompts_dir / "code_generator.md"
|
|
code_gen_prompt.write_text("""Generate a Python class with the following specifications:
|
|
|
|
Class Name: {{ class_name }}
|
|
{% if parent_class %}
|
|
Inherits from: {{ parent_class }}
|
|
{% endif %}
|
|
|
|
## Attributes:
|
|
{% for attr in attributes %}
|
|
- {{ attr.name }}: {{ attr.type }}{% if attr.default %} = {{ attr.default }}{% endif %}
|
|
{% endfor %}
|
|
|
|
## Methods:
|
|
{% for method in methods %}
|
|
### {{ method.name }}({{ method.params | join(', ') }})
|
|
{{ method.description }}
|
|
Returns: {{ method.returns }}
|
|
{% endfor %}
|
|
|
|
## Example Usage:
|
|
```python
|
|
{% for example in examples %}
|
|
{{ example }}
|
|
{% endfor %}
|
|
```
|
|
|
|
{% if additional_notes %}
|
|
## Notes:
|
|
{% for note in additional_notes %}
|
|
- {{ note }}
|
|
{% endfor %}
|
|
{% endif %}""")
|
|
|
|
# Create an API documentation prompt
|
|
api_doc_prompt = prompts_dir / "api_documentation.md"
|
|
api_doc_prompt.write_text("""# API Documentation: {{ api_name }}
|
|
|
|
Base URL: `{{ base_url }}`
|
|
Version: {{ version }}
|
|
|
|
## Authentication
|
|
{{ auth_method }}
|
|
|
|
## Endpoints
|
|
|
|
{% for endpoint in endpoints %}
|
|
### {{ endpoint.method }} {{ endpoint.path }}
|
|
|
|
**Description:** {{ endpoint.description }}
|
|
|
|
{% if endpoint.params %}
|
|
**Parameters:**
|
|
{% for param in endpoint.params %}
|
|
- `{{ param.name }}` ({{ param.type }}): {{ param.description }}{% if param.required %} *[Required]*{% endif %}
|
|
{% endfor %}
|
|
{% endif %}
|
|
|
|
{% if endpoint.request_body %}
|
|
**Request Body:**
|
|
```json
|
|
{{ endpoint.request_body | tojson(indent=2) }}
|
|
```
|
|
{% endif %}
|
|
|
|
**Response:** `{{ endpoint.response_code }}`
|
|
```json
|
|
{{ endpoint.response_example | tojson(indent=2) }}
|
|
```
|
|
|
|
---
|
|
{% endfor %}
|
|
|
|
## Rate Limiting
|
|
{{ rate_limit }} requests per {{ rate_limit_window }}
|
|
|
|
## Error Codes
|
|
{% for error in error_codes %}
|
|
- `{{ error.code }}`: {{ error.message }}
|
|
{% endfor %}""")
|
|
|
|
# Configure PromptManager
|
|
PromptManager.configure(path=prompts_dir)
|
|
|
|
print("=" * 70)
|
|
print("Jinja2 Template Examples with Lists and Complex Data")
|
|
print("=" * 70)
|
|
|
|
# Example 1: Code Generation
|
|
print("\n1. CODE GENERATION PROMPT")
|
|
print("-" * 70)
|
|
|
|
prompt = PromptManager.get_prompt("code_generator")
|
|
filled = prompt.fill(
|
|
class_name="UserProfile",
|
|
parent_class="BaseModel",
|
|
attributes=[
|
|
{"name": "user_id", "type": "int"},
|
|
{"name": "username", "type": "str"},
|
|
{"name": "email", "type": "str"},
|
|
{"name": "created_at", "type": "datetime", "default": "datetime.now()"},
|
|
{"name": "is_active", "type": "bool", "default": "True"}
|
|
],
|
|
methods=[
|
|
{
|
|
"name": "validate_email",
|
|
"params": ["self"],
|
|
"description": "Validates the email format",
|
|
"returns": "bool"
|
|
},
|
|
{
|
|
"name": "update_profile",
|
|
"params": ["self", "**kwargs"],
|
|
"description": "Updates user profile with provided fields",
|
|
"returns": "None"
|
|
},
|
|
{
|
|
"name": "to_dict",
|
|
"params": ["self"],
|
|
"description": "Converts the profile to a dictionary",
|
|
"returns": "Dict[str, Any]"
|
|
}
|
|
],
|
|
examples=[
|
|
"user = UserProfile(user_id=1, username='alice', email='alice@example.com')",
|
|
"if user.validate_email():",
|
|
" user.update_profile(is_active=False)",
|
|
" print(user.to_dict())"
|
|
],
|
|
additional_notes=[
|
|
"Email validation should follow RFC 5322",
|
|
"All datetime values should be UTC",
|
|
"Profile updates should be logged"
|
|
]
|
|
)
|
|
print(filled)
|
|
|
|
# Example 2: API Documentation
|
|
print("\n2. API DOCUMENTATION PROMPT")
|
|
print("-" * 70)
|
|
|
|
prompt = PromptManager.get_prompt("api_documentation")
|
|
filled = prompt.fill(
|
|
api_name="User Management API",
|
|
base_url="https://api.example.com/v1",
|
|
version="1.0.0",
|
|
auth_method="Bearer token in Authorization header",
|
|
endpoints=[
|
|
{
|
|
"method": "GET",
|
|
"path": "/users",
|
|
"description": "List all users with optional filtering",
|
|
"params": [
|
|
{"name": "page", "type": "integer", "description": "Page number", "required": False},
|
|
{"name": "limit", "type": "integer", "description": "Items per page", "required": False},
|
|
{"name": "status", "type": "string", "description": "Filter by status", "required": False}
|
|
],
|
|
"response_code": "200 OK",
|
|
"response_example": {
|
|
"users": [
|
|
{"id": 1, "username": "alice", "status": "active"},
|
|
{"id": 2, "username": "bob", "status": "inactive"}
|
|
],
|
|
"total": 2,
|
|
"page": 1
|
|
}
|
|
},
|
|
{
|
|
"method": "POST",
|
|
"path": "/users",
|
|
"description": "Create a new user",
|
|
"request_body": {
|
|
"username": "string",
|
|
"email": "string",
|
|
"password": "string"
|
|
},
|
|
"response_code": "201 Created",
|
|
"response_example": {
|
|
"id": 3,
|
|
"username": "charlie",
|
|
"email": "charlie@example.com",
|
|
"created_at": "2024-01-15T10:30:00Z"
|
|
}
|
|
},
|
|
{
|
|
"method": "DELETE",
|
|
"path": "/users/{id}",
|
|
"description": "Delete a user by ID",
|
|
"params": [
|
|
{"name": "id", "type": "integer", "description": "User ID", "required": True}
|
|
],
|
|
"response_code": "204 No Content",
|
|
"response_example": {}
|
|
}
|
|
],
|
|
rate_limit=1000,
|
|
rate_limit_window="hour",
|
|
error_codes=[
|
|
{"code": "400", "message": "Bad Request - Invalid parameters"},
|
|
{"code": "401", "message": "Unauthorized - Invalid or missing token"},
|
|
{"code": "404", "message": "Not Found - Resource doesn't exist"},
|
|
{"code": "429", "message": "Too Many Requests - Rate limit exceeded"},
|
|
{"code": "500", "message": "Internal Server Error"}
|
|
]
|
|
)
|
|
print(filled)
|
|
|
|
print("\n" + "=" * 70)
|
|
print("Examples completed successfully!")
|
|
print("=" * 70) |