Skip to content

Shameendra/MRKL_Agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

3 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿค– Multi-Tool MRKL Agent

A sophisticated reasoning agent based on the MRKL (Modular Reasoning, Knowledge and Language) architecture. This agent dynamically selects and chains tools to solve complex, multi-step problems.

๐ŸŽฏ Key Features

  • Dynamic Tool Selection: Automatically chooses the right tool for each subtask
  • Chain-of-Thought Reasoning: Explicit reasoning steps for transparency
  • Multi-Step Problem Solving: Breaks complex tasks into manageable steps
  • Self-Correction: Handles tool failures gracefully
  • Planning Mode: Optional explicit planning before execution
  • Streaming Output: Real-time visibility into agent reasoning

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                        User Query                               โ”‚
โ”‚           "What's the weather in Tokyo and convert              โ”‚
โ”‚            the temperature to Fahrenheit?"                      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                          โ”‚
                          โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    Planning Phase (Optional)                    โ”‚
โ”‚  1. Search for Tokyo weather                                    โ”‚
โ”‚  2. Extract temperature value                                   โ”‚
โ”‚  3. Convert Celsius to Fahrenheit using calculator              โ”‚
โ”‚  4. Format final answer                                         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                          โ”‚
                          โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    Reasoning Loop                               โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚  Thought: I need to find the current weather in Tokyo      โ”‚ โ”‚
โ”‚  โ”‚  Action: web_search                                        โ”‚ โ”‚
โ”‚  โ”‚  Action Input: "Tokyo weather today"                       โ”‚ โ”‚
โ”‚  โ”‚  Observation: Temperature: 22ยฐC, Partly cloudy             โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚                          โ”‚                                      โ”‚
โ”‚                          โ–ผ                                      โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚  Thought: Now I need to convert 22ยฐC to Fahrenheit         โ”‚ โ”‚
โ”‚  โ”‚  Action: calculator                                        โ”‚ โ”‚
โ”‚  โ”‚  Action Input: "22 * 9/5 + 32"                             โ”‚ โ”‚
โ”‚  โ”‚  Observation: Result: 71.6                                 โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚                          โ”‚                                      โ”‚
โ”‚                          โ–ผ                                      โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚  Thought: I have all the information needed                โ”‚ โ”‚
โ”‚  โ”‚  Final Answer: Tokyo is currently 22ยฐC (71.6ยฐF)            โ”‚ โ”‚
โ”‚  โ”‚               with partly cloudy conditions.               โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ”ง Available Tools

Tool Description Use Case
web_search Search the internet Current information, facts
calculator Math operations Calculations, conversions
python_executor Run Python code Data processing, algorithms
wolfram_alpha Scientific computing Complex math, science data
news_search Recent news Current events
sql_query Database queries Structured data
datetime_tool Date/time operations Scheduling, time zones
knowledge_base Internal docs Company-specific info
logical_reasoning Logic problems Puzzles, deductions

๐Ÿ“ Project Structure

mrkl-agent/
โ”œโ”€โ”€ config.py           # Configuration
โ”œโ”€โ”€ tools.py            # Tool implementations
โ”œโ”€โ”€ agent.py            # MRKL agent core
โ”œโ”€โ”€ main.py             # CLI and API
โ”œโ”€โ”€ requirements.txt    # Dependencies
โ””โ”€โ”€ README.md           # Documentation

๐Ÿš€ Quick Start

Installation

pip install -r requirements.txt

# Set API keys
export OPENAI_API_KEY=sk-...
export SERPAPI_KEY=...  # Optional, for web search

Usage

Interactive CLI:

python main.py

# Example queries:
# "What's 25% of the current Bitcoin price?"
# "How many days until New Year and what day will it be?"
# "Search for recent AI news and summarize the top story"

Single Query:

python main.py --query "Calculate the compound interest on $10,000 at 5% for 10 years"

With Planning:

python main.py --query "Compare the populations of Tokyo and New York" --plan

API Server:

python main.py --mode api --port 8000

API Usage

# Query endpoint
curl -X POST "http://localhost:8000/query" \
  -H "Content-Type: application/json" \
  -d '{"question": "What is 15% tip on $84.50?", "with_planning": false}'

# Stream execution
curl -X POST "http://localhost:8000/query/stream" \
  -H "Content-Type: application/json" \
  -d '{"question": "Find the latest stock price of Apple"}'

# Generate plan only
curl -X POST "http://localhost:8000/plan" \
  -H "Content-Type: application/json" \
  -d '{"question": "Plan a trip from NYC to LA"}'

Python API

from agent import MRKLAgent, ask_agent

# Simple usage
result = ask_agent("What's the capital of France and its population?")
print(result["answer"])

# With planning
result = ask_agent("Calculate monthly payments for a $300k mortgage at 7% for 30 years", with_planning=True)
print("Plan:", result["plan"])
print("Answer:", result["answer"])

# Streaming
agent = MRKLAgent()
for event in agent.stream("How old is the universe in seconds?"):
    print(event)

๐ŸŽฎ Example Queries

Math & Calculations

  • "What's 15% of 847.50?"
  • "Convert 100 kilometers to miles"
  • "Calculate compound interest on $5000 at 4% for 5 years"

Current Information

  • "What's the current price of Bitcoin?"
  • "Latest news about artificial intelligence"
  • "Weather in London today"

Multi-Step Reasoning

  • "How many seconds are there until New Year 2025?"
  • "What's 20% tip on a $85 dinner bill, split 4 ways?"
  • "Find Apple's stock price and calculate its market cap"

Knowledge & Research

  • "What is the refund policy?" (from knowledge base)
  • "Compare Python and JavaScript for web development"
  • "Explain quantum computing in simple terms"

๐Ÿ”„ Reasoning Flow

1. RECEIVE QUERY
   โ†“
2. THINK: Analyze what's needed
   โ†“
3. SELECT TOOL: Choose appropriate tool
   โ†“
4. EXECUTE: Run tool with input
   โ†“
5. OBSERVE: Process tool output
   โ†“
6. ITERATE: Need more info? โ†’ Go to step 2
   โ†“
7. ANSWER: Formulate final response

๐Ÿ›ก๏ธ Error Handling

  • Tool Failure: Agent tries alternative approaches
  • Invalid Input: Graceful error messages
  • Max Iterations: Prevents infinite loops
  • Timeout Protection: Limits execution time

๐Ÿ”ง Configuration

# config.py
MAX_ITERATIONS = 10        # Max reasoning steps
MAX_EXECUTION_TIME = 120   # Timeout in seconds
VERBOSE = True             # Show reasoning steps
TEMPERATURE = 0.1          # LLM temperature (low for consistency)

๐ŸŽฏ Design Principles

  1. Modularity: Each tool is independent and replaceable
  2. Transparency: All reasoning steps are visible
  3. Robustness: Handles failures gracefully
  4. Extensibility: Easy to add new tools
  5. Efficiency: Minimal tool calls to solve tasks

๐Ÿ“Š Performance Tips

  • Use specific queries for better tool selection
  • Enable planning for complex multi-step tasks
  • Provide context when needed
  • Check tool availability for your use case

๐Ÿค Adding Custom Tools

from langchain_core.tools import tool
from pydantic import BaseModel, Field

class MyToolInput(BaseModel):
    param: str = Field(description="Parameter description")

@tool("my_tool", args_schema=MyToolInput)
def my_tool(param: str) -> str:
    """Tool description for the agent."""
    # Implementation
    return result

# Add to tools.py get_all_tools()

๐Ÿ“ License

MIT License


About

A sophisticated reasoning agent based on the MRKL (Modular Reasoning, Knowledge and Language) architecture.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages