Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -342,27 +342,8 @@ class MagAgent:
|
|
| 342 |
max_tokens=8192
|
| 343 |
)
|
| 344 |
|
| 345 |
-
# Add explicit managed_agent template validation
|
| 346 |
-
required_managed_agent_vars = {'task', 'question_analysis', 'subtasks', 'validation_rules'}
|
| 347 |
-
if missing_vars := required_managed_agent_vars - set(self.prompt_templates["managed_agent"]["variables"]):
|
| 348 |
-
raise ValueError(f"Missing required variables in managed_agent template: {missing_vars}")
|
| 349 |
-
|
| 350 |
-
# Verify template contains actual task placeholder
|
| 351 |
-
if "{task}" not in self.prompt_templates["managed_agent"]["template"]:
|
| 352 |
-
raise ConfigurationError("Managed agent template missing {task} placeholder")
|
| 353 |
-
|
| 354 |
-
print("Managed Agent Template:", self.prompt_templates["managed_agent"]["template"])
|
| 355 |
|
| 356 |
-
|
| 357 |
-
UniversalLoader(),
|
| 358 |
-
CrossVerifiedSearch(), # Replaces individual search tools
|
| 359 |
-
ValidatedExcelReader(),
|
| 360 |
-
VisitWebpageTool(),
|
| 361 |
-
DownloadTaskAttachmentTool(),
|
| 362 |
-
SpeechToTextTool(),
|
| 363 |
-
]
|
| 364 |
-
|
| 365 |
-
# Initialize with default prompts as nested dictionaries
|
| 366 |
self.prompt_templates = {
|
| 367 |
"system_prompt": {
|
| 368 |
"template": "You are Magus...",
|
|
@@ -385,12 +366,13 @@ class MagAgent:
|
|
| 385 |
"final_answer": {
|
| 386 |
"template": "Final Verified Answer...",
|
| 387 |
"variables": ["sources", "answer"]
|
| 388 |
-
|
| 389 |
}
|
| 390 |
|
|
|
|
| 391 |
try:
|
| 392 |
with open("prompts.yaml") as f:
|
| 393 |
-
user_prompts = yaml.safe_load(f)
|
| 394 |
|
| 395 |
if isinstance(user_prompts, dict):
|
| 396 |
for key in self.prompt_templates:
|
|
@@ -401,6 +383,7 @@ class MagAgent:
|
|
| 401 |
if k in ['template', 'variables']
|
| 402 |
})
|
| 403 |
|
|
|
|
| 404 |
for template_key in user_prompts[key].keys():
|
| 405 |
if template_key not in ['template', 'variables']:
|
| 406 |
self.prompt_templates[key][template_key] = user_prompts[key][template_key]
|
|
@@ -410,16 +393,27 @@ class MagAgent:
|
|
| 410 |
except Exception as e:
|
| 411 |
print(f"Error loading prompts.yaml: {str(e)}. Using default templates")
|
| 412 |
|
|
|
|
| 413 |
# Final validation
|
| 414 |
required_keys = {'system_prompt', 'managed_agent', 'planning', 'final_answer'}
|
| 415 |
if missing := required_keys - self.prompt_templates.keys():
|
| 416 |
raise ValueError(f"Missing required prompt templates: {missing}")
|
| 417 |
|
| 418 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 419 |
required_planning_templates = {'initial_plan', 'initial_facts'}
|
| 420 |
if missing := required_planning_templates - self.prompt_templates['planning'].keys():
|
| 421 |
-
raise ValueError(f"Missing required planning templates: {missing}")
|
| 422 |
-
|
|
|
|
|
|
|
| 423 |
print("Loaded prompt templates:") # Debug line
|
| 424 |
for name, template in self.prompt_templates.items():
|
| 425 |
print(f"{name}:")
|
|
@@ -427,6 +421,16 @@ class MagAgent:
|
|
| 427 |
if 'sub_templates' in template:
|
| 428 |
print(f"Sub-templates: {list(template['sub_templates'].keys())}")
|
| 429 |
print("---")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 430 |
|
| 431 |
self.agent = CodeAgent(
|
| 432 |
model=self.model,
|
|
|
|
| 342 |
max_tokens=8192
|
| 343 |
)
|
| 344 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
|
| 346 |
+
# Initialize with default prompts FIRST
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
self.prompt_templates = {
|
| 348 |
"system_prompt": {
|
| 349 |
"template": "You are Magus...",
|
|
|
|
| 366 |
"final_answer": {
|
| 367 |
"template": "Final Verified Answer...",
|
| 368 |
"variables": ["sources", "answer"]
|
| 369 |
+
}
|
| 370 |
}
|
| 371 |
|
| 372 |
+
# THEN load custom prompts
|
| 373 |
try:
|
| 374 |
with open("prompts.yaml") as f:
|
| 375 |
+
user_prompts = yaml.safe_load(f)
|
| 376 |
|
| 377 |
if isinstance(user_prompts, dict):
|
| 378 |
for key in self.prompt_templates:
|
|
|
|
| 383 |
if k in ['template', 'variables']
|
| 384 |
})
|
| 385 |
|
| 386 |
+
# Merge additional keys
|
| 387 |
for template_key in user_prompts[key].keys():
|
| 388 |
if template_key not in ['template', 'variables']:
|
| 389 |
self.prompt_templates[key][template_key] = user_prompts[key][template_key]
|
|
|
|
| 393 |
except Exception as e:
|
| 394 |
print(f"Error loading prompts.yaml: {str(e)}. Using default templates")
|
| 395 |
|
| 396 |
+
# NOW perform validations
|
| 397 |
# Final validation
|
| 398 |
required_keys = {'system_prompt', 'managed_agent', 'planning', 'final_answer'}
|
| 399 |
if missing := required_keys - self.prompt_templates.keys():
|
| 400 |
raise ValueError(f"Missing required prompt templates: {missing}")
|
| 401 |
|
| 402 |
+
# Managed agent validation
|
| 403 |
+
required_managed_agent_vars = {'task', 'question_analysis', 'subtasks', 'validation_rules'}
|
| 404 |
+
if missing_vars := required_managed_agent_vars - set(self.prompt_templates["managed_agent"]["variables"]):
|
| 405 |
+
raise ValueError(f"Missing required variables in managed_agent template: {missing_vars}")
|
| 406 |
+
|
| 407 |
+
if "{task}" not in self.prompt_templates["managed_agent"]["template"]:
|
| 408 |
+
raise ValueError("Managed agent template missing {task} placeholder")
|
| 409 |
+
|
| 410 |
+
# Planning validation
|
| 411 |
required_planning_templates = {'initial_plan', 'initial_facts'}
|
| 412 |
if missing := required_planning_templates - self.prompt_templates['planning'].keys():
|
| 413 |
+
raise ValueError(f"Missing required planning templates: {missing}")
|
| 414 |
+
|
| 415 |
+
print("Managed Agent Template:", self.prompt_templates["managed_agent"]["template"])
|
| 416 |
+
|
| 417 |
print("Loaded prompt templates:") # Debug line
|
| 418 |
for name, template in self.prompt_templates.items():
|
| 419 |
print(f"{name}:")
|
|
|
|
| 421 |
if 'sub_templates' in template:
|
| 422 |
print(f"Sub-templates: {list(template['sub_templates'].keys())}")
|
| 423 |
print("---")
|
| 424 |
+
|
| 425 |
+
# THEN initialize tools and agent
|
| 426 |
+
self.tools = [
|
| 427 |
+
UniversalLoader(),
|
| 428 |
+
CrossVerifiedSearch(),
|
| 429 |
+
ValidatedExcelReader(),
|
| 430 |
+
VisitWebpageTool(),
|
| 431 |
+
DownloadTaskAttachmentTool(),
|
| 432 |
+
SpeechToTextTool(),
|
| 433 |
+
]
|
| 434 |
|
| 435 |
self.agent = CodeAgent(
|
| 436 |
model=self.model,
|