Scott Cogan commited on
Commit
79161d9
·
1 Parent(s): 3617c7c

fix: improve template processing to handle Jinja2 templates correctly

Browse files
Files changed (1) hide show
  1. app.py +23 -36
app.py CHANGED
@@ -85,42 +85,29 @@ with open("prompts.yaml", 'r') as stream:
85
  raise ValueError("prompt_templates must be a dictionary")
86
 
87
  # Process templates to ensure they are valid Jinja2 templates
88
- def process_template(value, is_system_prompt=False):
89
- if isinstance(value, str):
90
- logger.debug(f"Processing template string: {value[:100]}...")
91
- try:
92
- # Log the exact content before processing
93
- logger.debug(f"Raw template content: {value}")
94
-
95
- # For system prompt, ensure proper spacing in template expressions
96
- if is_system_prompt:
97
- # Ensure proper spacing in template expressions
98
- value = re.sub(r'{{([^{}]+)}}', lambda m: '{{ ' + m.group(1).strip() + ' }}', value)
99
- value = re.sub(r'{%([^{}]+)%}', lambda m: '{% ' + m.group(1).strip() + ' %}', value)
100
- value = value.replace('\n', '\\n')
101
- else:
102
- # For other templates, only wrap non-template content
103
- if '{{' not in value and '{%' not in value:
104
- # Don't wrap in double curly braces, just return the string as is
105
- value = value
106
- else:
107
- # Ensure proper spacing in template expressions
108
- value = re.sub(r'{{([^{}]+)}}', lambda m: '{{ ' + m.group(1).strip() + ' }}', value)
109
- value = re.sub(r'{%([^{}]+)%}', lambda m: '{% ' + m.group(1).strip() + ' %}', value)
110
-
111
- logger.debug(f"Processed template content: {value}")
112
- except Exception as e:
113
- logger.error(f"Error processing template string: {str(e)}")
114
- raise
115
- return value
116
- elif isinstance(value, dict):
117
- logger.debug(f"Processing template dictionary with keys: {list(value.keys())}")
118
- try:
119
- return {k: process_template(v, is_system_prompt=(k == 'system_prompt')) for k, v in value.items()}
120
- except Exception as e:
121
- logger.error(f"Error processing template dictionary: {str(e)}")
122
- raise
123
- return value
124
 
125
  logger.debug("Starting template processing...")
126
  try:
 
85
  raise ValueError("prompt_templates must be a dictionary")
86
 
87
  # Process templates to ensure they are valid Jinja2 templates
88
+ def process_template(template_content, is_system_prompt=False):
89
+ """Process a template string to ensure it's properly formatted for Jinja2."""
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...")
113
  try: