File size: 24,019 Bytes
162ee47 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 |
"""
Reasoning System for GAIA-Ready AI Agent
This module provides advanced reasoning capabilities for the AI agent,
implementing the ReAct approach (Reasoning + Acting) and supporting
the Think-Act-Observe workflow.
"""
import os
import json
from typing import List, Dict, Any, Optional, Union, Tuple
from datetime import datetime
import traceback
import re
try:
from smolagents import Agent, InferenceClientModel, Tool
except ImportError:
import subprocess
subprocess.check_call(["pip", "install", "smolagents"])
from smolagents import Agent, InferenceClientModel, Tool
class ReasoningSystem:
"""
Advanced reasoning system implementing the ReAct approach
and supporting the Think-Act-Observe workflow.
"""
def __init__(self, agent, memory_manager):
self.agent = agent
self.memory_manager = memory_manager
self.max_reasoning_depth = 5
self.reasoning_templates = self._load_reasoning_templates()
def _load_reasoning_templates(self) -> Dict[str, str]:
"""Load reasoning templates for different stages of the workflow"""
return {
"think": """
# Task Analysis and Planning
## Task
{query}
## Relevant Context
{context}
## Analysis
Let me analyze this task step by step:
1. What is being asked?
2. What information do I need?
3. What challenges might I encounter?
## Plan
Based on my analysis, here's my plan:
1. [First step]
2. [Second step]
3. [Third step]
...
## Tools Needed
To accomplish this task, I'll need:
- [Tool 1]: For [purpose]
- [Tool 2]: For [purpose]
...
## Expected Outcome
If successful, I expect to:
[Description of expected outcome]
""",
"act": """
# Action Execution
## Current Task
{query}
## Current Plan
{plan}
## Previous Results
{previous_results}
## Next Action
Based on my plan and previous results, I'll now:
1. Use the [tool name] tool
2. With parameters: [parameters]
3. Purpose: [why this action is needed]
## Execution
[Detailed description of how I'll execute this action]
""",
"observe": """
# Result Analysis
## Current Task
{query}
## Action Taken
{action}
## Results Obtained
{results}
## Analysis
Let me analyze these results:
1. What did I learn?
2. Does this answer the original question?
3. Are there any inconsistencies or gaps?
## Next Steps
Based on my analysis:
- [Next step recommendation]
- [Alternative approach if needed]
## Progress Assessment
Task completion status: [percentage]%
[Explanation of current progress]
"""
}
def think(self, query: str) -> Dict[str, Any]:
"""
Analyze the task and plan an approach (Think phase)
Args:
query: The user's query or task
Returns:
Dictionary containing analysis and plan
"""
# Retrieve relevant memories
relevant_memories = self.memory_manager.get_relevant_memories(query)
# Format context from relevant memories
context = ""
if relevant_memories:
context_items = []
for memory in relevant_memories:
memory_type = memory.get("type", "general")
content = memory.get("content", "")
relevance = memory.get("relevance_score", 0)
context_items.append(f"- [{memory_type.upper()}] (Relevance: {relevance:.2f}): {content}")
context = "\n".join(context_items)
else:
context = "No relevant prior knowledge found."
# Apply the thinking template
thinking_template = self.reasoning_templates["think"]
thinking_prompt = thinking_template.format(
query=query,
context=context
)
# Use the agent to generate a plan
try:
response = self.agent.chat(thinking_prompt)
# Store the thinking in memory
self.memory_manager.add_to_short_term({
"type": "thinking",
"content": response,
"timestamp": datetime.now().isoformat()
})
# Parse the response to extract structured information
analysis = self._extract_section(response, "Analysis")
plan = self._extract_section(response, "Plan")
tools_needed = self._extract_section(response, "Tools Needed")
expected_outcome = self._extract_section(response, "Expected Outcome")
return {
"raw_response": response,
"analysis": analysis,
"plan": plan,
"tools_needed": tools_needed,
"expected_outcome": expected_outcome
}
except Exception as e:
error_msg = f"Error during thinking phase: {str(e)}\n{traceback.format_exc()}"
print(error_msg)
# Store the error in memory
self.memory_manager.add_to_short_term({
"type": "error",
"content": error_msg,
"timestamp": datetime.now().isoformat()
})
# Return a basic plan despite the error
return {
"raw_response": "Error occurred during thinking phase.",
"analysis": "Could not analyze the task due to an error.",
"plan": "1. Try a simpler approach\n2. Break down the task into smaller steps",
"tools_needed": "web_search: To find basic information",
"expected_outcome": "Partial answer to the query"
}
def act(self, plan: Dict[str, Any], query: str, previous_results: str = "") -> Dict[str, Any]:
"""
Execute actions based on the plan (Act phase)
Args:
plan: The plan generated by the think step
query: The original query
previous_results: Results from previous actions
Returns:
Dictionary containing action details and results
"""
# Apply the action template
action_template = self.reasoning_templates["act"]
action_prompt = action_template.format(
query=query,
plan=plan.get("plan", "No plan available."),
previous_results=previous_results if previous_results else "No previous results."
)
try:
# Use the agent to determine the next action
action_response = self.agent.chat(action_prompt)
# Store the action planning in memory
self.memory_manager.add_to_short_term({
"type": "action_planning",
"content": action_response,
"timestamp": datetime.now().isoformat()
})
# Parse the action response to extract tool and parameters
tool_info = self._extract_tool_info(action_response)
if not tool_info:
# If no tool was identified, try a more direct approach
direct_prompt = f"""
Based on the task "{query}" and the plan:
{plan.get('plan', 'No plan available.')}
Which specific tool should I use next and with what parameters?
Respond in this format:
TOOL: [tool name]
PARAMETERS: [parameter1=value1, parameter2=value2, ...]
"""
direct_response = self.agent.chat(direct_prompt)
tool_info = self._extract_tool_info(direct_response)
if tool_info:
tool_name = tool_info["tool"]
tool_params = tool_info["parameters"]
# Find the matching tool
matching_tool = None
for tool in self.agent.tools:
if tool.name == tool_name:
matching_tool = tool
break
if matching_tool:
# Execute the tool
try:
if isinstance(tool_params, dict):
result = matching_tool.function(**tool_params)
else:
result = matching_tool.function(tool_params)
# Store the successful action result in memory
self.memory_manager.add_to_short_term({
"type": "action_result",
"content": f"Tool: {tool_name}\nParameters: {tool_params}\nResult: {result}",
"timestamp": datetime.now().isoformat()
})
return {
"tool": tool_name,
"parameters": tool_params,
"result": result,
"success": True,
"error": None
}
except Exception as e:
error_msg = f"Error executing tool {tool_name}: {str(e)}\n{traceback.format_exc()}"
print(error_msg)
# Store the error in memory
self.memory_manager.add_to_short_term({
"type": "error",
"content": error_msg,
"timestamp": datetime.now().isoformat()
})
return {
"tool": tool_name,
"parameters": tool_params,
"result": f"Error: {str(e)}",
"success": False,
"error": str(e)
}
else:
error_msg = f"Tool '{tool_name}' not found."
print(error_msg)
# Store the error in memory
self.memory_manager.add_to_short_term({
"type": "error",
"content": error_msg,
"timestamp": datetime.now().isoformat()
})
return {
"tool": tool_name,
"parameters": tool_params,
"result": f"Error: Tool '{tool_name}' not found.",
"success": False,
"error": "Tool not found"
}
else:
error_msg = "Could not determine which tool to use."
print(error_msg)
# Store the error in memory
self.memory_manager.add_to_short_term({
"type": "error",
"content": error_msg,
"timestamp": datetime.now().isoformat()
})
# Default to web search as a fallback
try:
web_search_tool = None
for tool in self.agent.tools:
if tool.name == "web_search":
web_search_tool = tool
break
if web_search_tool:
result = web_search_tool.function(query)
return {
"tool": "web_search",
"parameters": query,
"result": result,
"success": True,
"error": None,
"fallback": True
}
else:
return {
"tool": "none",
"parameters": "none",
"result": "Could not determine which tool to use and web_search fallback not available.",
"success": False,
"error": "No tool selected"
}
except Exception as e:
return {
"tool": "web_search",
"parameters": query,
"result": f"Error in fallback web search: {str(e)}",
"success": False,
"error": str(e),
"fallback": True
}
except Exception as e:
error_msg = f"Error during action phase: {str(e)}\n{traceback.format_exc()}"
print(error_msg)
# Store the error in memory
self.memory_manager.add_to_short_term({
"type": "error",
"content": error_msg,
"timestamp": datetime.now().isoformat()
})
return {
"tool": "none",
"parameters": "none",
"result": f"Error during action planning: {str(e)}",
"success": False,
"error": str(e)
}
def observe(self, action_result: Dict[str, Any], plan: Dict[str, Any], query: str) -> Dict[str, Any]:
"""
Analyze the results of actions and determine next steps (Observe phase)
Args:
action_result: Results from the act step
plan: The original plan
query: The original query
Returns:
Dictionary containing observation and next steps
"""
# Apply the observation template
observation_template = self.reasoning_templates["observe"]
observation_prompt = observation_template.format(
query=query,
action=f"Tool: {action_result.get('tool', 'none')}\nParameters: {action_result.get('parameters', 'none')}",
results=action_result.get('result', 'No results.')
)
try:
# Use the agent to analyze the results
observation_response = self.agent.chat(observation_prompt)
# Store the observation in memory
self.memory_manager.add_to_short_term({
"type": "observation",
"content": observation_response,
"timestamp": datetime.now().isoformat()
})
# Parse the observation to extract structured information
analysis = self._extract_section(observation_response, "Analysis")
next_steps = self._extract_section(observation_response, "Next Steps")
progress = self._extract_section(observation_response, "Progress Assessment")
# Determine if we need to continue with more actions
continue_execution = True
# Check for completion indicators
completion_phrases = [
"task complete", "question answered", "fully answered",
"100%", "task is complete", "fully resolved"
]
if any(phrase in observation_response.lower() for phrase in completion_phrases):
continue_execution = False
# Store the final answer in long-term memory
self.memory_manager.add_to_long_term({
"type": "final_answer",
"query": query,
"content": observation_response,
"timestamp": datetime.now().isoformat(),
"importance": 0.8 # High importance for final answers
})
return {
"raw_response": observation_response,
"analysis": analysis,
"next_steps": next_steps,
"progress": progress,
"continue": continue_execution
}
except Exception as e:
error_msg = f"Error during observation phase: {str(e)}\n{traceback.format_exc()}"
print(error_msg)
# Store the error in memory
self.memory_manager.add_to_short_term({
"type": "error",
"content": error_msg,
"timestamp": datetime.now().isoformat()
})
# Default observation with continuation
return {
"raw_response": f"Error occurred during observation phase: {str(e)}",
"analysis": "Could not analyze the results due to an error.",
"next_steps": "Try a different approach or tool.",
"progress": "Unknown due to error.",
"continue": True # Continue by default on error
}
def _extract_section(self, text: str, section_name: str) -> str:
"""Extract a section from the response text"""
pattern = rf"(?:^|\n)(?:#+\s*{re.escape(section_name)}:?|\*\*{re.escape(section_name)}:?\*\*|{re.escape(section_name)}:?)\s*(.*?)(?:\n(?:#+\s*|$)|\Z)"
match = re.search(pattern, text, re.DOTALL | re.IGNORECASE)
if match:
content = match.group(1).strip()
return content
# Try a more lenient approach if the first one fails
pattern = rf"{re.escape(section_name)}:?\s*(.*?)(?:\n\n|\n[A-Z]|\Z)"
match = re.search(pattern, text, re.DOTALL | re.IGNORECASE)
if match:
content = match.group(1).strip()
return content
return f"No {section_name.lower()} found."
def _extract_tool_info(self, text: str) -> Optional[Dict[str, Any]]:
"""Extract tool name and parameters from the response text"""
# Try to find tool name
tool_pattern = r"(?:TOOL|Tool|tool):\s*(\w+)"
tool_match = re.search(tool_pattern, text)
if not tool_match:
return None
tool_name = tool_match.group(1).strip()
# Try to find parameters
params_pattern = r"(?:PARAMETERS|Parameters|parameters):\s*(.*?)(?:\n\n|\n[A-Z]|\Z)"
params_match = re.search(params_pattern, text, re.DOTALL)
if params_match:
params_text = params_match.group(1).strip()
# Check if parameters are in key=value format
if "=" in params_text:
# Parse as dictionary
params_dict = {}
param_pairs = re.findall(r"(\w+)\s*=\s*([^,\n]+)", params_text)
for key, value in param_pairs:
params_dict[key.strip()] = value.strip()
return {
"tool": tool_name,
"parameters": params_dict
}
else:
# Treat as a single string parameter
return {
"tool": tool_name,
"parameters": params_text
}
else:
# No parameters found, use empty dict
return {
"tool": tool_name,
"parameters": {}
}
def execute_reasoning_cycle(self, query: str, max_iterations: int = 5) -> str:
"""
Execute a complete Think-Act-Observe reasoning cycle
Args:
query: The user's query or task
max_iterations: Maximum number of iterations
Returns:
Final answer to the query
"""
# Store the query in memory
self.memory_manager.add_to_short_term({
"type": "query",
"content": query,
"timestamp": datetime.now().isoformat()
})
# Initialize the workflow
iteration = 0
final_answer = None
all_results = []
while iteration < max_iterations:
print(f"Iteration {iteration + 1}/{max_iterations}")
# Think
print("Thinking...")
plan = self.think(query)
# Act
print("Acting...")
previous_results = "\n".join([r.get("result", "") for r in all_results])
action_result = self.act(plan, query, previous_results)
all_results.append(action_result)
# Observe
print("Observing...")
observation = self.observe(action_result, plan, query)
# Check if we have a final answer
if not observation["continue"]:
# Generate final answer
final_answer_prompt = f"""
TASK: {query}
REASONING PROCESS:
{plan.get('raw_response', 'No thinking process available.')}
ACTIONS TAKEN:
{', '.join([f"{r.get('tool', 'unknown')}({r.get('parameters', '')})" for r in all_results])}
RESULTS:
{previous_results}
{action_result.get('result', '')}
OBSERVATION:
{observation.get('raw_response', 'No observation available.')}
Based on all the above, provide a comprehensive final answer to the original task.
"""
final_answer = self.agent.chat(final_answer_prompt)
# Store the final answer in long-term memory
self.memory_manager.add_to_long_term({
"type": "final_answer",
"query": query,
"content": final_answer,
"timestamp": datetime.now().isoformat(),
"importance": 0.9 # Very high importance
})
break
# Update the query with the observation for the next iteration
query = f"""
Original task: {query}
Progress so far:
{observation.get('raw_response', 'No observation available.')}
Please continue solving this task.
"""
iteration += 1
# If we reached max iterations without a final answer
if final_answer is None:
final_answer = f"""
I've spent {max_iterations} iterations trying to solve this task.
Here's my best answer based on what I've learned:
{observation.get('raw_response', 'No final observation available.')}
Note: This answer may be incomplete as I reached the maximum number of iterations.
"""
# Store the partial answer in long-term memory
self.memory_manager.add_to_long_term({
"type": "partial_answer",
"query": query,
"content": final_answer,
"timestamp": datetime.now().isoformat(),
"importance": 0.6 # Medium importance for partial answers
})
return final_answer
# Example usage
if __name__ == "__main__":
# This would be imported from your agent.py
from smolagents import Agent, InferenceClientModel, Tool
# Mock agent for testing
class MockAgent:
def __init__(self):
self.tools = [
Tool(name="web_search", description="Search the web", function=lambda x: f"Search results for: {x}"),
Tool(name="calculator", description="Calculate", function=lambda x: f"Result: {eval(x)}")
]
def chat(self, message):
return f"Response to: {message[:50]}..."
# Mock memory manager
class MockMemoryManager:
def add_to_short_term(self, item):
print(f"Added to short-term: {item['type']}")
def add_to_long_term(self, item):
print(f"Added to long-term: {item['type']}")
def get_relevant_memories(self, query):
return []
# Test the reasoning system
agent = MockAgent()
memory_manager = MockMemoryManager()
reasoning = ReasoningSystem(agent, memory_manager)
result = reasoning.execute_reasoning_cycle("What is 2+2?")
print(f"\nFinal result: {result}")
|