-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact_llm_example.py
More file actions
56 lines (42 loc) · 1.53 KB
/
react_llm_example.py
File metadata and controls
56 lines (42 loc) · 1.53 KB
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
"""
ReAct Agent with LLM-Powered Tool Selection
Demonstrates:
- LLM decides when to use tools vs. answer directly
- Native function calling (no THOUGHT/ACTION text parsing)
- Sync run() and async arun()
- @function_tool for custom tools
Usage:
PYTHONPATH=. python examples/react_llm_example.py
"""
import asyncio
from dotenv import load_dotenv
load_dotenv()
from agentensemble.agents import ReActAgent
from agentensemble.tools import SearchTool, function_tool
@function_tool(description="Get current weather for a city")
def get_weather(city: str) -> str:
"""Fetch weather data."""
return f"Weather in {city}: 72°F, sunny"
async def main():
agent = ReActAgent(
name="research_agent",
tools=[SearchTool(), get_weather],
max_iterations=5,
)
# Query that needs search
print("=== Query requiring search ===")
result = await agent.arun("Who won the 2024 Nobel Prize in Physics?")
print(result["result"][:400] + "..." if len(result["result"]) > 400 else result["result"])
print(f"Metadata: {result['metadata']}\n")
# Query that doesn't need search (LLM answers directly)
print("=== Query requiring no search ===")
result = await agent.arun("What is 2 + 2?")
print(result["result"])
print(f"Metadata: {result['metadata']}\n")
# Capital of France
print("=== Capital of France ===")
result = await agent.arun("What is the capital of France?")
print(result["result"])
print(f"Metadata: {result['metadata']}")
if __name__ == "__main__":
asyncio.run(main())