Spaces:
Runtime error
Runtime error
Scott Cogan commited on
Commit ·
41caec1
1
Parent(s): a675830
fix: Enhance template processing and validation
Browse files
app.py
CHANGED
|
@@ -100,6 +100,13 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 100 |
# Add spaces around template expressions if missing
|
| 101 |
value = re.sub(r'{{([^{}]+)}}', r'{{ \1 }}', value)
|
| 102 |
value = re.sub(r'{%([^{}]+)%}', r'{% \1 %}', value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
logger.debug(f"Processed template string: {value[:100]}...")
|
| 104 |
else:
|
| 105 |
logger.debug("No template syntax found in string")
|
|
@@ -140,10 +147,22 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 140 |
for subkey, subvalue in value.items():
|
| 141 |
if isinstance(subvalue, str):
|
| 142 |
logger.debug(f"Validating template: {key}.{subkey}")
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
elif isinstance(value, str):
|
| 145 |
logger.debug(f"Validating template: {key}")
|
| 146 |
-
Template(value, undefined=StrictUndefined)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
logger.debug("Template validation completed successfully")
|
| 148 |
except Exception as e:
|
| 149 |
logger.error(f"Template validation failed: {str(e)}")
|
|
|
|
| 100 |
# Add spaces around template expressions if missing
|
| 101 |
value = re.sub(r'{{([^{}]+)}}', r'{{ \1 }}', value)
|
| 102 |
value = re.sub(r'{%([^{}]+)%}', r'{% \1 %}', value)
|
| 103 |
+
# Ensure proper spacing in template expressions
|
| 104 |
+
value = re.sub(r'{{([^{}]+)}}', lambda m: '{{ ' + m.group(1).strip() + ' }}', value)
|
| 105 |
+
value = re.sub(r'{%([^{}]+)%}', lambda m: '{% ' + m.group(1).strip() + ' %}', value)
|
| 106 |
+
# Remove any double spaces
|
| 107 |
+
value = re.sub(r'\s+', ' ', value)
|
| 108 |
+
# Ensure proper line breaks
|
| 109 |
+
value = value.replace('\\n', '\n')
|
| 110 |
logger.debug(f"Processed template string: {value[:100]}...")
|
| 111 |
else:
|
| 112 |
logger.debug("No template syntax found in string")
|
|
|
|
| 147 |
for subkey, subvalue in value.items():
|
| 148 |
if isinstance(subvalue, str):
|
| 149 |
logger.debug(f"Validating template: {key}.{subkey}")
|
| 150 |
+
# Test template compilation
|
| 151 |
+
template = Template(subvalue, undefined=StrictUndefined)
|
| 152 |
+
# Test template rendering with dummy data
|
| 153 |
+
try:
|
| 154 |
+
template.render(tools=[], task="test", name="test", final_answer="test", remaining_steps=1, answer_facts="test")
|
| 155 |
+
except Exception as e:
|
| 156 |
+
logger.error(f"Template render test failed for {key}.{subkey}: {str(e)}")
|
| 157 |
+
raise
|
| 158 |
elif isinstance(value, str):
|
| 159 |
logger.debug(f"Validating template: {key}")
|
| 160 |
+
template = Template(value, undefined=StrictUndefined)
|
| 161 |
+
try:
|
| 162 |
+
template.render(tools=[], task="test", name="test", final_answer="test", remaining_steps=1, answer_facts="test")
|
| 163 |
+
except Exception as e:
|
| 164 |
+
logger.error(f"Template render test failed for {key}: {str(e)}")
|
| 165 |
+
raise
|
| 166 |
logger.debug("Template validation completed successfully")
|
| 167 |
except Exception as e:
|
| 168 |
logger.error(f"Template validation failed: {str(e)}")
|