Scott Cogan commited on
Commit
4481e03
·
1 Parent(s): 2304ec1

fix: simplify template processing to prevent double-wrapping and ensure proper spacing

Browse files
Files changed (1) hide show
  1. app.py +12 -46
app.py CHANGED
@@ -90,23 +90,15 @@ with open("prompts.yaml", 'r') as stream:
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...")
@@ -199,38 +191,12 @@ with open("prompts.yaml", 'r') as stream:
199
  agent_templates[key] = {}
200
  for subkey, subvalue in value.items():
201
  if isinstance(subvalue, str):
202
- # Special handling for system prompt
203
- if key == 'system_prompt' and subkey == 'text':
204
- # Keep system prompt as is, only ensure proper spacing in template expressions
205
- content = subvalue
206
- content = re.sub(r'{{([^{}]+)}}', r'{{ \1 }}', content)
207
- content = re.sub(r'{%([^{}]+)%}', r'{% \1 %}', content)
208
- else:
209
- # For other templates, only wrap if not already a template
210
- content = subvalue
211
- if '{{' not in content and '{%' not in content:
212
- content = f'{{{{ "{content}" }}}}'
213
- else:
214
- # Ensure proper spacing in template expressions
215
- content = re.sub(r'{{([^{}]+)}}', r'{{ \1 }}', content)
216
- content = re.sub(r'{%([^{}]+)%}', r'{% \1 %}', content)
217
  agent_templates[key][subkey] = content
218
  elif isinstance(value, str):
219
- # Special handling for system prompt
220
- if key == 'system_prompt':
221
- # Keep system prompt as is, only ensure proper spacing in template expressions
222
- content = value
223
- content = re.sub(r'{{([^{}]+)}}', r'{{ \1 }}', content)
224
- content = re.sub(r'{%([^{}]+)%}', r'{% \1 %}', content)
225
- else:
226
- # For other templates, only wrap if not already a template
227
- content = value
228
- if '{{' not in content and '{%' not in content:
229
- content = f'{{{{ "{content}" }}}}'
230
- else:
231
- # Ensure proper spacing in template expressions
232
- content = re.sub(r'{{([^{}]+)}}', r'{{ \1 }}', content)
233
- content = re.sub(r'{%([^{}]+)%}', r'{% \1 %}', content)
234
  agent_templates[key] = content
235
  else:
236
  agent_templates[key] = value
 
90
  if not isinstance(template_content, str):
91
  return template_content
92
 
93
+ # If it's already a template (contains {{ or {%), return as is
94
+ if '{{' in template_content or '{%' in template_content:
95
+ # Just ensure proper spacing in template expressions
96
+ content = template_content
97
+ content = re.sub(r'{{([^{}]+)}}', r'{{ \1 }}', content)
98
+ content = re.sub(r'{%([^{}]+)%}', r'{% \1 %}', content)
99
+ return content
100
 
101
+ # For non-template strings, wrap in double curly braces
 
 
 
 
 
 
 
 
102
  return f"{{{{ {template_content} }}}}"
103
 
104
  logger.debug("Starting template processing...")
 
191
  agent_templates[key] = {}
192
  for subkey, subvalue in value.items():
193
  if isinstance(subvalue, str):
194
+ # Process template with proper spacing
195
+ content = process_template(subvalue, key == 'system_prompt')
 
 
 
 
 
 
 
 
 
 
 
 
 
196
  agent_templates[key][subkey] = content
197
  elif isinstance(value, str):
198
+ # Process template with proper spacing
199
+ content = process_template(value, key == 'system_prompt')
 
 
 
 
 
 
 
 
 
 
 
 
 
200
  agent_templates[key] = content
201
  else:
202
  agent_templates[key] = value