-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_agent.py
More file actions
185 lines (145 loc) · 5.94 KB
/
test_agent.py
File metadata and controls
185 lines (145 loc) · 5.94 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
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
"""Test script for the React Agent."""
import asyncio
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
async def test_basic_functionality():
"""Test basic agent functionality without external APIs."""
print("🧪 Testing React Agent Basic Functionality")
print("=" * 50)
try:
from agent import ReactAgent
# Test with mock/local tools only
agent = ReactAgent(verbose=True)
# Test 1: Calculator tool
print("\n📊 Test 1: Calculator Tool")
print("-" * 30)
response = await agent.run("What is 15 * 8 + 32?")
print(f"Success: {response['success']}")
if response['success']:
print(f"Answer: {response['output']}")
else:
print(f"Error: {response['error']}")
# Test 2: Database tool
print("\n💾 Test 2: Database Tool")
print("-" * 30)
response = await agent.run("Store my favorite color as blue in the database")
print(f"Success: {response['success']}")
if response['success']:
print(f"Answer: {response['output']}")
else:
print(f"Error: {response['error']}")
# Test 3: Database retrieval
print("\n🔍 Test 3: Database Retrieval")
print("-" * 30)
response = await agent.run("List all the keys in the database")
print(f"Success: {response['success']}")
if response['success']:
print(f"Answer: {response['output']}")
else:
print(f"Error: {response['error']}")
# Test 4: Complex reasoning
print("\n🧠 Test 4: Complex Reasoning")
print("-" * 30)
response = await agent.run("Calculate the square root of 144, then store that result in the database as 'sqrt_result'")
print(f"Success: {response['success']}")
if response['success']:
print(f"Answer: {response['output']}")
else:
print(f"Error: {response['error']}")
print("\n✅ Basic functionality tests completed!")
except Exception as e:
print(f"❌ Test failed: {str(e)}")
import traceback
traceback.print_exc()
async def test_with_external_apis():
"""Test with external APIs if available."""
print("\n🌐 Testing External API Integration")
print("=" * 50)
# Check if API keys are available
google_api_key = os.getenv("GOOGLE_API_KEY")
serper_api_key = os.getenv("SERPER_API_KEY")
if not google_api_key:
print("⚠️ GOOGLE_API_KEY not found. Skipping external API tests.")
print(" Set GOOGLE_API_KEY in your .env file to test with Gemini.")
return
try:
from agent import ReactAgent
agent = ReactAgent(verbose=True)
# Test Wikipedia (should work without additional API key)
print("\n📚 Test: Wikipedia Tool")
print("-" * 30)
response = await agent.run("Tell me about artificial intelligence from Wikipedia")
print(f"Success: {response['success']}")
if response['success']:
print(f"Answer: {response['output'][:200]}...") # Truncate for readability
else:
print(f"Error: {response['error']}")
# Test web search if API key is available
if serper_api_key:
print("\n🔍 Test: Web Search Tool")
print("-" * 30)
response = await agent.run("Search for the latest news about artificial intelligence")
print(f"Success: {response['success']}")
if response['success']:
print(f"Answer: {response['output'][:200]}...")
else:
print(f"Error: {response['error']}")
else:
print("\n⚠️ SERPER_API_KEY not found. Skipping web search test.")
print("\n✅ External API tests completed!")
except Exception as e:
print(f"❌ External API test failed: {str(e)}")
import traceback
traceback.print_exc()
async def test_multiagent_framework():
"""Test the multiagent framework."""
print("\n🤖 Testing Multiagent Framework")
print("=" * 50)
try:
from extensions.multiagent_framework import (
MultiAgentSystem,
create_coordinator_agent,
create_researcher_agent,
create_analyst_agent
)
# Create multiagent system
system = MultiAgentSystem()
# Add agents
coordinator = create_coordinator_agent("coord_1")
researcher = create_researcher_agent("research_1")
analyst = create_analyst_agent("analyst_1")
system.add_agent(coordinator)
system.add_agent(researcher)
system.add_agent(analyst)
# Test system stats
stats = system.get_system_stats()
print(f"System stats: {stats}")
# Test task distribution
research_task = {
"type": "research",
"query": "What is machine learning?"
}
print(f"\n📋 Distributing research task...")
result = await system.distribute_task(research_task)
print(f"Task result: {result['success'] if 'success' in result else 'completed'}")
print("\n✅ Multiagent framework tests completed!")
except Exception as e:
print(f"❌ Multiagent test failed: {str(e)}")
import traceback
traceback.print_exc()
async def main():
"""Run all tests."""
print("🚀 React Agent Test Suite")
print("=" * 60)
# Test basic functionality (should work without external APIs)
await test_basic_functionality()
# Test external APIs if available
await test_external_apis()
# Test multiagent framework
await test_multiagent_framework()
print("\n🎉 All tests completed!")
print("=" * 60)
if __name__ == "__main__":
asyncio.run(main())