Spaces:
Runtime error
Runtime error
Scott Cogan commited on
Commit ·
24b6371
1
Parent(s): dfde1fc
fix: remove template processing to prevent double-wrapping of templates
Browse files
app.py
CHANGED
|
@@ -74,6 +74,20 @@ model = OpenAIModel(
|
|
| 74 |
# Import tool from Hub
|
| 75 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
# Load and validate templates from prompts.yaml
|
| 78 |
with open("prompts.yaml", 'r') as stream:
|
| 79 |
yaml_content = yaml.safe_load(stream)
|
|
@@ -87,26 +101,21 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 87 |
|
| 88 |
logger.debug("Starting template validation...")
|
| 89 |
try:
|
| 90 |
-
#
|
| 91 |
-
|
| 92 |
-
for key, value in prompt_templates.items():
|
| 93 |
-
if isinstance(value, dict):
|
| 94 |
-
logger.debug(f"{key}: {list(value.keys())}")
|
| 95 |
-
for subkey, subvalue in value.items():
|
| 96 |
-
logger.debug(f"{key}.{subkey}: {str(subvalue)[:200]}...")
|
| 97 |
-
else:
|
| 98 |
-
logger.debug(f"{key}: {str(value)[:200]}...")
|
| 99 |
-
|
| 100 |
-
# Validate templates
|
| 101 |
-
logger.debug("Validating templates...")
|
| 102 |
for key, value in prompt_templates.items():
|
| 103 |
if isinstance(value, dict):
|
|
|
|
| 104 |
for subkey, subvalue in value.items():
|
| 105 |
if isinstance(subvalue, str):
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
try:
|
| 109 |
-
template = Template(
|
| 110 |
rendered = template.render(
|
| 111 |
tools=[],
|
| 112 |
task="test",
|
|
@@ -116,15 +125,18 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 116 |
answer_facts="test"
|
| 117 |
)
|
| 118 |
logger.debug(f"Template render test successful for {key}.{subkey}")
|
| 119 |
-
logger.debug(f"Rendered content: {rendered[:200]}...")
|
| 120 |
except Exception as e:
|
| 121 |
logger.error(f"Template render test failed for {key}.{subkey}: {str(e)}")
|
| 122 |
raise
|
| 123 |
elif isinstance(value, str):
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
try:
|
| 127 |
-
template = Template(
|
| 128 |
rendered = template.render(
|
| 129 |
tools=[],
|
| 130 |
task="test",
|
|
@@ -134,7 +146,6 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 134 |
answer_facts="test"
|
| 135 |
)
|
| 136 |
logger.debug(f"Template render test successful for {key}")
|
| 137 |
-
logger.debug(f"Rendered content: {rendered[:200]}...")
|
| 138 |
except Exception as e:
|
| 139 |
logger.error(f"Template render test failed for {key}: {str(e)}")
|
| 140 |
raise
|
|
@@ -145,7 +156,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 145 |
logger.error(f"Error during template validation: {str(e)}")
|
| 146 |
raise
|
| 147 |
|
| 148 |
-
# Create the agent with the templates
|
| 149 |
agent = CodeAgent(
|
| 150 |
model=model,
|
| 151 |
tools=[final_answer, DuckDuckGoSearchTool(), calculate_min_price, extract_price_from_snippet, get_current_time_in_timezone],
|
|
@@ -155,7 +166,7 @@ agent = CodeAgent(
|
|
| 155 |
planning_interval=1,
|
| 156 |
name="question_answering_agent",
|
| 157 |
description="An agent specialized in answering various types of questions using available tools. The agent must use the final_answer tool to submit its answer.",
|
| 158 |
-
prompt_templates=
|
| 159 |
)
|
| 160 |
|
| 161 |
# Configure Gradio UI with sharing enabled
|
|
|
|
| 74 |
# Import tool from Hub
|
| 75 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 76 |
|
| 77 |
+
def process_template_content(content):
|
| 78 |
+
"""Process template content to ensure it's properly formatted for Jinja2."""
|
| 79 |
+
if not isinstance(content, str):
|
| 80 |
+
return content
|
| 81 |
+
|
| 82 |
+
# Unescape newlines
|
| 83 |
+
content = content.replace('\\n', '\n')
|
| 84 |
+
|
| 85 |
+
# Ensure proper spacing in template expressions
|
| 86 |
+
content = re.sub(r'{{([^{}]+)}}', lambda m: '{{ ' + m.group(1).strip() + ' }}', content)
|
| 87 |
+
content = re.sub(r'{%([^{}]+)%}', lambda m: '{% ' + m.group(1).strip() + ' %}', content)
|
| 88 |
+
|
| 89 |
+
return content
|
| 90 |
+
|
| 91 |
# Load and validate templates from prompts.yaml
|
| 92 |
with open("prompts.yaml", 'r') as stream:
|
| 93 |
yaml_content = yaml.safe_load(stream)
|
|
|
|
| 101 |
|
| 102 |
logger.debug("Starting template validation...")
|
| 103 |
try:
|
| 104 |
+
# Process and validate templates
|
| 105 |
+
processed_templates = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
for key, value in prompt_templates.items():
|
| 107 |
if isinstance(value, dict):
|
| 108 |
+
processed_templates[key] = {}
|
| 109 |
for subkey, subvalue in value.items():
|
| 110 |
if isinstance(subvalue, str):
|
| 111 |
+
processed_content = process_template_content(subvalue)
|
| 112 |
+
processed_templates[key][subkey] = processed_content
|
| 113 |
+
logger.debug(f"Processed template: {key}.{subkey}")
|
| 114 |
+
logger.debug(f"Processed content: {processed_content[:200]}...")
|
| 115 |
+
|
| 116 |
+
# Validate template
|
| 117 |
try:
|
| 118 |
+
template = Template(processed_content, undefined=StrictUndefined)
|
| 119 |
rendered = template.render(
|
| 120 |
tools=[],
|
| 121 |
task="test",
|
|
|
|
| 125 |
answer_facts="test"
|
| 126 |
)
|
| 127 |
logger.debug(f"Template render test successful for {key}.{subkey}")
|
|
|
|
| 128 |
except Exception as e:
|
| 129 |
logger.error(f"Template render test failed for {key}.{subkey}: {str(e)}")
|
| 130 |
raise
|
| 131 |
elif isinstance(value, str):
|
| 132 |
+
processed_content = process_template_content(value)
|
| 133 |
+
processed_templates[key] = processed_content
|
| 134 |
+
logger.debug(f"Processed template: {key}")
|
| 135 |
+
logger.debug(f"Processed content: {processed_content[:200]}...")
|
| 136 |
+
|
| 137 |
+
# Validate template
|
| 138 |
try:
|
| 139 |
+
template = Template(processed_content, undefined=StrictUndefined)
|
| 140 |
rendered = template.render(
|
| 141 |
tools=[],
|
| 142 |
task="test",
|
|
|
|
| 146 |
answer_facts="test"
|
| 147 |
)
|
| 148 |
logger.debug(f"Template render test successful for {key}")
|
|
|
|
| 149 |
except Exception as e:
|
| 150 |
logger.error(f"Template render test failed for {key}: {str(e)}")
|
| 151 |
raise
|
|
|
|
| 156 |
logger.error(f"Error during template validation: {str(e)}")
|
| 157 |
raise
|
| 158 |
|
| 159 |
+
# Create the agent with the processed templates
|
| 160 |
agent = CodeAgent(
|
| 161 |
model=model,
|
| 162 |
tools=[final_answer, DuckDuckGoSearchTool(), calculate_min_price, extract_price_from_snippet, get_current_time_in_timezone],
|
|
|
|
| 166 |
planning_interval=1,
|
| 167 |
name="question_answering_agent",
|
| 168 |
description="An agent specialized in answering various types of questions using available tools. The agent must use the final_answer tool to submit its answer.",
|
| 169 |
+
prompt_templates=processed_templates
|
| 170 |
)
|
| 171 |
|
| 172 |
# Configure Gradio UI with sharing enabled
|