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.
- 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
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 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. โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
| 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 |
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
pip install -r requirements.txt
# Set API keys
export OPENAI_API_KEY=sk-...
export SERPAPI_KEY=... # Optional, for web searchInteractive 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" --planAPI Server:
python main.py --mode api --port 8000# 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"}'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)- "What's 15% of 847.50?"
- "Convert 100 kilometers to miles"
- "Calculate compound interest on $5000 at 4% for 5 years"
- "What's the current price of Bitcoin?"
- "Latest news about artificial intelligence"
- "Weather in London today"
- "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"
- "What is the refund policy?" (from knowledge base)
- "Compare Python and JavaScript for web development"
- "Explain quantum computing in simple terms"
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
- Tool Failure: Agent tries alternative approaches
- Invalid Input: Graceful error messages
- Max Iterations: Prevents infinite loops
- Timeout Protection: Limits execution time
# 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)- Modularity: Each tool is independent and replaceable
- Transparency: All reasoning steps are visible
- Robustness: Handles failures gracefully
- Extensibility: Easy to add new tools
- Efficiency: Minimal tool calls to solve tasks
- 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
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()MIT License