Spaces:
Runtime error
Runtime error
Scott Cogan commited on
Commit ·
a675830
1
Parent(s): 875f8d9
feat: Add comprehensive logging and template validation
Browse files
app.py
CHANGED
|
@@ -7,9 +7,15 @@ import yaml
|
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
import re
|
| 9 |
import os
|
|
|
|
|
|
|
| 10 |
|
| 11 |
from Gradio_UI import GradioUI
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 14 |
@tool
|
| 15 |
def calculate_min_price(prices: list[float])-> str: #it's import to specify the return type
|
|
@@ -78,15 +84,70 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 78 |
if not isinstance(prompt_templates, dict):
|
| 79 |
raise ValueError("prompt_templates must be a dictionary")
|
| 80 |
|
| 81 |
-
#
|
| 82 |
def process_template(value):
|
| 83 |
if isinstance(value, str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
return value
|
| 85 |
elif isinstance(value, dict):
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
return value
|
| 88 |
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
agent = CodeAgent(
|
| 92 |
model=model,
|
|
|
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
import re
|
| 9 |
import os
|
| 10 |
+
import logging
|
| 11 |
+
from jinja2 import Template, StrictUndefined
|
| 12 |
|
| 13 |
from Gradio_UI import GradioUI
|
| 14 |
|
| 15 |
+
# Configure logging
|
| 16 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 17 |
+
logger = logging.getLogger(__name__)
|
| 18 |
+
|
| 19 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 20 |
@tool
|
| 21 |
def calculate_min_price(prices: list[float])-> str: #it's import to specify the return type
|
|
|
|
| 84 |
if not isinstance(prompt_templates, dict):
|
| 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 |
+
# First, ensure we have valid template syntax
|
| 93 |
+
if '{{' in value or '{%' in value:
|
| 94 |
+
# Ensure template variables are properly formatted
|
| 95 |
+
value = value.replace('{{', '{{ ').replace('}}', ' }}')
|
| 96 |
+
# Ensure template blocks are properly formatted
|
| 97 |
+
value = value.replace('{%', '{% ').replace('%}', ' %}')
|
| 98 |
+
# Ensure newlines are properly handled
|
| 99 |
+
value = value.replace('\n', '\\n')
|
| 100 |
+
# Add spaces around template expressions if missing
|
| 101 |
+
value = re.sub(r'{{([^{}]+)}}', r'{{ \1 }}', value)
|
| 102 |
+
value = re.sub(r'{%([^{}]+)%}', r'{% \1 %}', value)
|
| 103 |
+
logger.debug(f"Processed template string: {value[:100]}...")
|
| 104 |
+
else:
|
| 105 |
+
logger.debug("No template syntax found in string")
|
| 106 |
+
except Exception as e:
|
| 107 |
+
logger.error(f"Error processing template string: {str(e)}")
|
| 108 |
+
raise
|
| 109 |
return value
|
| 110 |
elif isinstance(value, dict):
|
| 111 |
+
logger.debug(f"Processing template dictionary with keys: {list(value.keys())}")
|
| 112 |
+
try:
|
| 113 |
+
return {k: process_template(v) for k, v in value.items()}
|
| 114 |
+
except Exception as e:
|
| 115 |
+
logger.error(f"Error processing template dictionary: {str(e)}")
|
| 116 |
+
raise
|
| 117 |
return value
|
| 118 |
|
| 119 |
+
logger.debug("Starting template processing...")
|
| 120 |
+
try:
|
| 121 |
+
prompt_templates = process_template(prompt_templates)
|
| 122 |
+
logger.debug("Template processing completed")
|
| 123 |
+
|
| 124 |
+
# Log the final processed templates
|
| 125 |
+
logger.debug("Final processed templates:")
|
| 126 |
+
for key, value in prompt_templates.items():
|
| 127 |
+
if isinstance(value, dict):
|
| 128 |
+
logger.debug(f"{key}: {list(value.keys())}")
|
| 129 |
+
else:
|
| 130 |
+
logger.debug(f"{key}: {str(value)[:100]}...")
|
| 131 |
+
except Exception as e:
|
| 132 |
+
logger.error(f"Error during template processing: {str(e)}")
|
| 133 |
+
raise
|
| 134 |
+
|
| 135 |
+
# Validate templates before creating agent
|
| 136 |
+
logger.debug("Validating templates...")
|
| 137 |
+
try:
|
| 138 |
+
for key, value in prompt_templates.items():
|
| 139 |
+
if isinstance(value, dict):
|
| 140 |
+
for subkey, subvalue in value.items():
|
| 141 |
+
if isinstance(subvalue, str):
|
| 142 |
+
logger.debug(f"Validating template: {key}.{subkey}")
|
| 143 |
+
Template(subvalue, undefined=StrictUndefined)
|
| 144 |
+
elif isinstance(value, str):
|
| 145 |
+
logger.debug(f"Validating template: {key}")
|
| 146 |
+
Template(value, undefined=StrictUndefined)
|
| 147 |
+
logger.debug("Template validation completed successfully")
|
| 148 |
+
except Exception as e:
|
| 149 |
+
logger.error(f"Template validation failed: {str(e)}")
|
| 150 |
+
raise
|
| 151 |
|
| 152 |
agent = CodeAgent(
|
| 153 |
model=model,
|