Spaces:
Runtime error
Runtime error
Scott Cogan commited on
Commit ·
79161d9
1
Parent(s): 3617c7c
fix: improve template processing to handle Jinja2 templates correctly
Browse files
app.py
CHANGED
|
@@ -85,42 +85,29 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 85 |
raise ValueError("prompt_templates must be a dictionary")
|
| 86 |
|
| 87 |
# Process templates to ensure they are valid Jinja2 templates
|
| 88 |
-
def process_template(
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
logger.debug(f"Processed template content: {value}")
|
| 112 |
-
except Exception as e:
|
| 113 |
-
logger.error(f"Error processing template string: {str(e)}")
|
| 114 |
-
raise
|
| 115 |
-
return value
|
| 116 |
-
elif isinstance(value, dict):
|
| 117 |
-
logger.debug(f"Processing template dictionary with keys: {list(value.keys())}")
|
| 118 |
-
try:
|
| 119 |
-
return {k: process_template(v, is_system_prompt=(k == 'system_prompt')) for k, v in value.items()}
|
| 120 |
-
except Exception as e:
|
| 121 |
-
logger.error(f"Error processing template dictionary: {str(e)}")
|
| 122 |
-
raise
|
| 123 |
-
return value
|
| 124 |
|
| 125 |
logger.debug("Starting template processing...")
|
| 126 |
try:
|
|
|
|
| 85 |
raise ValueError("prompt_templates must be a dictionary")
|
| 86 |
|
| 87 |
# Process templates to ensure they are valid Jinja2 templates
|
| 88 |
+
def process_template(template_content, is_system_prompt=False):
|
| 89 |
+
"""Process a template string to ensure it's properly formatted for Jinja2."""
|
| 90 |
+
if not isinstance(template_content, str):
|
| 91 |
+
return template_content
|
| 92 |
+
|
| 93 |
+
# For system prompts, we need to ensure they're properly formatted as templates
|
| 94 |
+
if is_system_prompt:
|
| 95 |
+
# If it's already a template (contains {{ or {%), return as is
|
| 96 |
+
if '{{' in template_content or '{%' in template_content:
|
| 97 |
+
return template_content
|
| 98 |
+
# Otherwise, wrap in double curly braces
|
| 99 |
+
return f"{{{{ {template_content} }}}}"
|
| 100 |
+
|
| 101 |
+
# For non-system prompts, if it contains no template expressions, return as is
|
| 102 |
+
if '{{' not in template_content and '{%' not in template_content:
|
| 103 |
+
return template_content
|
| 104 |
+
|
| 105 |
+
# If it's already wrapped in double curly braces, return as is
|
| 106 |
+
if template_content.startswith('{{') and template_content.endswith('}}'):
|
| 107 |
+
return template_content
|
| 108 |
+
|
| 109 |
+
# Otherwise, wrap in double curly braces
|
| 110 |
+
return f"{{{{ {template_content} }}}}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
logger.debug("Starting template processing...")
|
| 113 |
try:
|