Scott Cogan commited on
Commit
e2642c3
·
1 Parent(s): 17177b2

fix: Handle system prompt template separately from other templates

Browse files
Files changed (1) hide show
  1. app.py +16 -12
app.py CHANGED
@@ -85,23 +85,27 @@ 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):
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
- # Ensure proper spacing in template expressions
96
- value = re.sub(r'{{([^{}]+)}}', lambda m: '{{ ' + m.group(1).strip() + ' }}', value)
97
- value = re.sub(r'{%([^{}]+)%}', lambda m: '{% ' + m.group(1).strip() + ' %}', value)
98
-
99
- # Handle newlines
100
- value = value.replace('\n', '\\n')
101
-
102
- # If no template syntax exists, wrap in a simple template expression
103
- if '{{' not in value and '{%' not in value:
104
- value = f'{{{{ "{value}" }}}}'
 
 
 
 
105
 
106
  logger.debug(f"Processed template content: {value}")
107
  except Exception as e:
@@ -111,7 +115,7 @@ with open("prompts.yaml", 'r') as stream:
111
  elif isinstance(value, dict):
112
  logger.debug(f"Processing template dictionary with keys: {list(value.keys())}")
113
  try:
114
- return {k: process_template(v) for k, v in value.items()}
115
  except Exception as e:
116
  logger.error(f"Error processing template dictionary: {str(e)}")
117
  raise
 
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, only ensure proper spacing in template expressions
96
+ if is_system_prompt:
97
+ value = re.sub(r'{{([^{}]+)}}', lambda m: '{{ ' + m.group(1).strip() + ' }}', value)
98
+ value = re.sub(r'{%([^{}]+)%}', lambda m: '{% ' + m.group(1).strip() + ' %}', value)
99
+ value = value.replace('\n', '\\n')
100
+ else:
101
+ # For other templates, wrap non-template content
102
+ if '{{' not in value and '{%' not in value:
103
+ value = f'{{{{ "{value}" }}}}'
104
+ else:
105
+ # Ensure proper spacing in template expressions
106
+ value = re.sub(r'{{([^{}]+)}}', lambda m: '{{ ' + m.group(1).strip() + ' }}', value)
107
+ value = re.sub(r'{%([^{}]+)%}', lambda m: '{% ' + m.group(1).strip() + ' %}', value)
108
+ value = value.replace('\n', '\\n')
109
 
110
  logger.debug(f"Processed template content: {value}")
111
  except Exception as e:
 
115
  elif isinstance(value, dict):
116
  logger.debug(f"Processing template dictionary with keys: {list(value.keys())}")
117
  try:
118
+ return {k: process_template(v, is_system_prompt=(k == 'system_prompt')) for k, v in value.items()}
119
  except Exception as e:
120
  logger.error(f"Error processing template dictionary: {str(e)}")
121
  raise