Spaces:
Runtime error
Runtime error
Scott Cogan commited on
Commit ·
8c1f404
1
Parent(s): 7c709cd
fix: Ensure all template content is properly formatted as Jinja2 templates
Browse files
app.py
CHANGED
|
@@ -89,9 +89,12 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 89 |
if isinstance(value, str):
|
| 90 |
logger.debug(f"Processing template string: {value[:100]}...")
|
| 91 |
try:
|
| 92 |
-
#
|
| 93 |
-
if '{{' in value or '{%' in value:
|
| 94 |
-
#
|
|
|
|
|
|
|
|
|
|
| 95 |
value = value.replace('{{', '{{ ').replace('}}', ' }}')
|
| 96 |
value = value.replace('{%', '{% ').replace('%}', ' %}')
|
| 97 |
|
|
@@ -101,10 +104,8 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 101 |
|
| 102 |
# Handle newlines
|
| 103 |
value = value.replace('\n', '\\n')
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
else:
|
| 107 |
-
logger.debug("No template syntax found in string")
|
| 108 |
except Exception as e:
|
| 109 |
logger.error(f"Error processing template string: {str(e)}")
|
| 110 |
raise
|
|
@@ -170,9 +171,12 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 170 |
# Pre-process the system prompt template
|
| 171 |
if 'system_prompt' in prompt_templates and 'text' in prompt_templates['system_prompt']:
|
| 172 |
system_prompt = prompt_templates['system_prompt']['text']
|
| 173 |
-
#
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
| 176 |
prompt_templates['system_prompt']['text'] = system_prompt
|
| 177 |
logger.debug("Pre-processed system prompt template")
|
| 178 |
|
|
|
|
| 89 |
if isinstance(value, str):
|
| 90 |
logger.debug(f"Processing template string: {value[:100]}...")
|
| 91 |
try:
|
| 92 |
+
# Ensure all content is wrapped in a template expression
|
| 93 |
+
if not ('{{' in value or '{%' in value):
|
| 94 |
+
# If no template syntax, wrap the entire string in a template expression
|
| 95 |
+
value = '{{ ' + value.replace('\n', '\\n') + ' }}'
|
| 96 |
+
else:
|
| 97 |
+
# Process existing template syntax
|
| 98 |
value = value.replace('{{', '{{ ').replace('}}', ' }}')
|
| 99 |
value = value.replace('{%', '{% ').replace('%}', ' %}')
|
| 100 |
|
|
|
|
| 104 |
|
| 105 |
# Handle newlines
|
| 106 |
value = value.replace('\n', '\\n')
|
| 107 |
+
|
| 108 |
+
logger.debug(f"Processed template string: {value[:100]}...")
|
|
|
|
|
|
|
| 109 |
except Exception as e:
|
| 110 |
logger.error(f"Error processing template string: {str(e)}")
|
| 111 |
raise
|
|
|
|
| 171 |
# Pre-process the system prompt template
|
| 172 |
if 'system_prompt' in prompt_templates and 'text' in prompt_templates['system_prompt']:
|
| 173 |
system_prompt = prompt_templates['system_prompt']['text']
|
| 174 |
+
# Ensure system prompt is a valid template
|
| 175 |
+
if not ('{{' in system_prompt or '{%' in system_prompt):
|
| 176 |
+
system_prompt = '{{ ' + system_prompt.replace('\n', '\\n') + ' }}'
|
| 177 |
+
else:
|
| 178 |
+
system_prompt = re.sub(r'{{([^{}]+)}}', lambda m: '{{ ' + m.group(1).strip() + ' }}', system_prompt)
|
| 179 |
+
system_prompt = re.sub(r'{%([^{}]+)%}', lambda m: '{% ' + m.group(1).strip() + ' %}', system_prompt)
|
| 180 |
prompt_templates['system_prompt']['text'] = system_prompt
|
| 181 |
logger.debug("Pre-processed system prompt template")
|
| 182 |
|