Spaces:
Runtime error
Runtime error
Scott Cogan commited on
Commit ·
aa7739f
1
Parent(s): 1e77542
fix: Rewrite template processing to handle code blocks separately
Browse files
app.py
CHANGED
|
@@ -91,28 +91,44 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 91 |
try:
|
| 92 |
# First, ensure we have valid template syntax
|
| 93 |
if '{{' in value or '{%' in value:
|
| 94 |
-
#
|
| 95 |
-
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
# Add
|
| 103 |
-
|
| 104 |
-
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
|
|
|
|
|
|
| 116 |
logger.debug(f"Processed template string: {value[:100]}...")
|
| 117 |
else:
|
| 118 |
logger.debug("No template syntax found in string")
|
|
@@ -153,19 +169,23 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 153 |
for subkey, subvalue in value.items():
|
| 154 |
if isinstance(subvalue, str):
|
| 155 |
logger.debug(f"Validating template: {key}.{subkey}")
|
|
|
|
| 156 |
# Test template compilation
|
| 157 |
template = Template(subvalue, undefined=StrictUndefined)
|
| 158 |
# Test template rendering with dummy data
|
| 159 |
try:
|
| 160 |
-
template.render(tools=[], task="test", name="test", final_answer="test", remaining_steps=1, answer_facts="test")
|
|
|
|
| 161 |
except Exception as e:
|
| 162 |
logger.error(f"Template render test failed for {key}.{subkey}: {str(e)}")
|
| 163 |
raise
|
| 164 |
elif isinstance(value, str):
|
| 165 |
logger.debug(f"Validating template: {key}")
|
|
|
|
| 166 |
template = Template(value, undefined=StrictUndefined)
|
| 167 |
try:
|
| 168 |
-
template.render(tools=[], task="test", name="test", final_answer="test", remaining_steps=1, answer_facts="test")
|
|
|
|
| 169 |
except Exception as e:
|
| 170 |
logger.error(f"Template render test failed for {key}: {str(e)}")
|
| 171 |
raise
|
|
|
|
| 91 |
try:
|
| 92 |
# First, ensure we have valid template syntax
|
| 93 |
if '{{' in value or '{%' in value:
|
| 94 |
+
# Split the string into parts: code blocks and non-code blocks
|
| 95 |
+
parts = []
|
| 96 |
+
current_pos = 0
|
| 97 |
+
code_block_pattern = r'```[^`]*```'
|
| 98 |
|
| 99 |
+
for match in re.finditer(code_block_pattern, value):
|
| 100 |
+
# Add text before code block
|
| 101 |
+
if match.start() > current_pos:
|
| 102 |
+
parts.append(('text', value[current_pos:match.start()]))
|
| 103 |
+
# Add code block
|
| 104 |
+
parts.append(('code', match.group(0)))
|
| 105 |
+
current_pos = match.end()
|
| 106 |
|
| 107 |
+
# Add remaining text
|
| 108 |
+
if current_pos < len(value):
|
| 109 |
+
parts.append(('text', value[current_pos:]))
|
| 110 |
|
| 111 |
+
# Process each part
|
| 112 |
+
processed_parts = []
|
| 113 |
+
for part_type, part_content in parts:
|
| 114 |
+
if part_type == 'text':
|
| 115 |
+
# Process template syntax in text parts
|
| 116 |
+
processed = part_content
|
| 117 |
+
# Ensure template variables are properly formatted
|
| 118 |
+
processed = processed.replace('{{', '{{ ').replace('}}', ' }}')
|
| 119 |
+
processed = processed.replace('{%', '{% ').replace('%}', ' %}')
|
| 120 |
+
# Add spaces around template expressions
|
| 121 |
+
processed = re.sub(r'{{([^{}]+)}}', lambda m: '{{ ' + m.group(1).strip() + ' }}', processed)
|
| 122 |
+
processed = re.sub(r'{%([^{}]+)%}', lambda m: '{% ' + m.group(1).strip() + ' %}', processed)
|
| 123 |
+
# Handle newlines
|
| 124 |
+
processed = processed.replace('\n', '\\n')
|
| 125 |
+
processed_parts.append(processed)
|
| 126 |
+
else:
|
| 127 |
+
# Keep code blocks as is
|
| 128 |
+
processed_parts.append(part_content)
|
| 129 |
|
| 130 |
+
# Join all parts
|
| 131 |
+
value = ''.join(processed_parts)
|
| 132 |
logger.debug(f"Processed template string: {value[:100]}...")
|
| 133 |
else:
|
| 134 |
logger.debug("No template syntax found in string")
|
|
|
|
| 169 |
for subkey, subvalue in value.items():
|
| 170 |
if isinstance(subvalue, str):
|
| 171 |
logger.debug(f"Validating template: {key}.{subkey}")
|
| 172 |
+
logger.debug(f"Template content: {subvalue[:200]}...")
|
| 173 |
# Test template compilation
|
| 174 |
template = Template(subvalue, undefined=StrictUndefined)
|
| 175 |
# Test template rendering with dummy data
|
| 176 |
try:
|
| 177 |
+
rendered = template.render(tools=[], task="test", name="test", final_answer="test", remaining_steps=1, answer_facts="test")
|
| 178 |
+
logger.debug(f"Template render test successful for {key}.{subkey}")
|
| 179 |
except Exception as e:
|
| 180 |
logger.error(f"Template render test failed for {key}.{subkey}: {str(e)}")
|
| 181 |
raise
|
| 182 |
elif isinstance(value, str):
|
| 183 |
logger.debug(f"Validating template: {key}")
|
| 184 |
+
logger.debug(f"Template content: {value[:200]}...")
|
| 185 |
template = Template(value, undefined=StrictUndefined)
|
| 186 |
try:
|
| 187 |
+
rendered = template.render(tools=[], task="test", name="test", final_answer="test", remaining_steps=1, answer_facts="test")
|
| 188 |
+
logger.debug(f"Template render test successful for {key}")
|
| 189 |
except Exception as e:
|
| 190 |
logger.error(f"Template render test failed for {key}: {str(e)}")
|
| 191 |
raise
|