Spaces:
Runtime error
Runtime error
Scott Cogan commited on
Commit ·
97f78cf
1
Parent(s): aa7739f
fix: Convert non-template content to Jinja2 comments
Browse files
app.py
CHANGED
|
@@ -91,44 +91,21 @@ 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 |
-
code_block_pattern = r'```[^`]*```'
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 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
|
| 108 |
-
|
| 109 |
-
|
| 110 |
|
| 111 |
-
#
|
| 112 |
-
|
| 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")
|
|
@@ -194,6 +171,14 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 194 |
logger.error(f"Template validation failed: {str(e)}")
|
| 195 |
raise
|
| 196 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
agent = CodeAgent(
|
| 198 |
model=model,
|
| 199 |
tools=[final_answer, DuckDuckGoSearchTool(), calculate_min_price, extract_price_from_snippet, get_current_time_in_timezone],
|
|
|
|
| 91 |
try:
|
| 92 |
# First, ensure we have valid template syntax
|
| 93 |
if '{{' in value or '{%' in value:
|
| 94 |
+
# Pre-process the template to ensure it's valid Jinja2
|
| 95 |
+
# Replace any non-template content with a comment
|
| 96 |
+
value = re.sub(r'(?<!{{|{%)([^{}]+)(?!}}|%})', lambda m: '{# ' + m.group(1) + ' #}', value)
|
|
|
|
| 97 |
|
| 98 |
+
# Ensure template variables are properly formatted
|
| 99 |
+
value = value.replace('{{', '{{ ').replace('}}', ' }}')
|
| 100 |
+
value = value.replace('{%', '{% ').replace('%}', ' %}')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
# Add spaces around template expressions
|
| 103 |
+
value = re.sub(r'{{([^{}]+)}}', lambda m: '{{ ' + m.group(1).strip() + ' }}', value)
|
| 104 |
+
value = re.sub(r'{%([^{}]+)%}', lambda m: '{% ' + m.group(1).strip() + ' %}', value)
|
| 105 |
|
| 106 |
+
# Handle newlines
|
| 107 |
+
value = value.replace('\n', '\\n')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
|
|
|
|
|
|
| 109 |
logger.debug(f"Processed template string: {value[:100]}...")
|
| 110 |
else:
|
| 111 |
logger.debug("No template syntax found in string")
|
|
|
|
| 171 |
logger.error(f"Template validation failed: {str(e)}")
|
| 172 |
raise
|
| 173 |
|
| 174 |
+
# Pre-process the system prompt template
|
| 175 |
+
if 'system_prompt' in prompt_templates and 'text' in prompt_templates['system_prompt']:
|
| 176 |
+
system_prompt = prompt_templates['system_prompt']['text']
|
| 177 |
+
# Convert non-template content to comments
|
| 178 |
+
system_prompt = re.sub(r'(?<!{{|{%)([^{}]+)(?!}}|%})', lambda m: '{# ' + m.group(1) + ' #}', system_prompt)
|
| 179 |
+
prompt_templates['system_prompt']['text'] = system_prompt
|
| 180 |
+
logger.debug("Pre-processed system prompt template")
|
| 181 |
+
|
| 182 |
agent = CodeAgent(
|
| 183 |
model=model,
|
| 184 |
tools=[final_answer, DuckDuckGoSearchTool(), calculate_min_price, extract_price_from_snippet, get_current_time_in_timezone],
|