Spaces:
Runtime error
Runtime error
Scott Cogan commited on
Commit ·
655b2f6
1
Parent(s): c7acf7c
fix: remove template processing and use templates directly from prompts.yaml
Browse files
app.py
CHANGED
|
@@ -74,20 +74,6 @@ model = OpenAIModel(
|
|
| 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,21 +87,17 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 101 |
|
| 102 |
logger.debug("Starting template validation...")
|
| 103 |
try:
|
| 104 |
-
#
|
| 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 |
-
|
| 112 |
-
|
| 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(
|
| 119 |
rendered = template.render(
|
| 120 |
tools=[],
|
| 121 |
task="test",
|
|
@@ -129,14 +111,12 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 129 |
logger.error(f"Template render test failed for {key}.{subkey}: {str(e)}")
|
| 130 |
raise
|
| 131 |
elif isinstance(value, str):
|
| 132 |
-
|
| 133 |
-
|
| 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(
|
| 140 |
rendered = template.render(
|
| 141 |
tools=[],
|
| 142 |
task="test",
|
|
@@ -156,7 +136,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 156 |
logger.error(f"Error during template validation: {str(e)}")
|
| 157 |
raise
|
| 158 |
|
| 159 |
-
# Create the agent with the
|
| 160 |
agent = CodeAgent(
|
| 161 |
model=model,
|
| 162 |
tools=[final_answer, DuckDuckGoSearchTool(), calculate_min_price, extract_price_from_snippet, get_current_time_in_timezone],
|
|
@@ -166,7 +146,7 @@ agent = CodeAgent(
|
|
| 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=
|
| 170 |
)
|
| 171 |
|
| 172 |
# 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 |
# Load and validate templates from prompts.yaml
|
| 78 |
with open("prompts.yaml", 'r') as stream:
|
| 79 |
yaml_content = yaml.safe_load(stream)
|
|
|
|
| 87 |
|
| 88 |
logger.debug("Starting template validation...")
|
| 89 |
try:
|
| 90 |
+
# Validate templates
|
|
|
|
| 91 |
for key, value in prompt_templates.items():
|
| 92 |
if isinstance(value, dict):
|
|
|
|
| 93 |
for subkey, subvalue in value.items():
|
| 94 |
if isinstance(subvalue, str):
|
| 95 |
+
logger.debug(f"Validating template: {key}.{subkey}")
|
| 96 |
+
logger.debug(f"Template content: {subvalue[:200]}...")
|
|
|
|
|
|
|
| 97 |
|
| 98 |
# Validate template
|
| 99 |
try:
|
| 100 |
+
template = Template(subvalue, undefined=StrictUndefined)
|
| 101 |
rendered = template.render(
|
| 102 |
tools=[],
|
| 103 |
task="test",
|
|
|
|
| 111 |
logger.error(f"Template render test failed for {key}.{subkey}: {str(e)}")
|
| 112 |
raise
|
| 113 |
elif isinstance(value, str):
|
| 114 |
+
logger.debug(f"Validating template: {key}")
|
| 115 |
+
logger.debug(f"Template content: {value[:200]}...")
|
|
|
|
|
|
|
| 116 |
|
| 117 |
# Validate template
|
| 118 |
try:
|
| 119 |
+
template = Template(value, undefined=StrictUndefined)
|
| 120 |
rendered = template.render(
|
| 121 |
tools=[],
|
| 122 |
task="test",
|
|
|
|
| 136 |
logger.error(f"Error during template validation: {str(e)}")
|
| 137 |
raise
|
| 138 |
|
| 139 |
+
# Create the agent with the templates
|
| 140 |
agent = CodeAgent(
|
| 141 |
model=model,
|
| 142 |
tools=[final_answer, DuckDuckGoSearchTool(), calculate_min_price, extract_price_from_snippet, get_current_time_in_timezone],
|
|
|
|
| 146 |
planning_interval=1,
|
| 147 |
name="question_answering_agent",
|
| 148 |
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.",
|
| 149 |
+
prompt_templates=prompt_templates
|
| 150 |
)
|
| 151 |
|
| 152 |
# Configure Gradio UI with sharing enabled
|