Update veryfinal.py
Browse files- veryfinal.py +26 -44
veryfinal.py
CHANGED
@@ -26,7 +26,7 @@ class AdvancedRateLimiter:
|
|
26 |
self.token_usage = []
|
27 |
self.consecutive_failures = 0
|
28 |
|
29 |
-
|
30 |
current_time = time.time()
|
31 |
|
32 |
# Clean old requests (older than 1 minute)
|
@@ -36,7 +36,7 @@ class AdvancedRateLimiter:
|
|
36 |
# Calculate wait time for requests (SILENT)
|
37 |
if len(self.request_times) >= self.requests_per_minute:
|
38 |
wait_time = 60 - (current_time - self.request_times[0]) + random.uniform(2, 8)
|
39 |
-
|
40 |
|
41 |
# Record this request
|
42 |
self.request_times.append(current_time)
|
@@ -52,12 +52,12 @@ class AdvancedRateLimiter:
|
|
52 |
# Initialize rate limiters for free tiers
|
53 |
groq_limiter = AdvancedRateLimiter(requests_per_minute=30, tokens_per_minute=6000)
|
54 |
gemini_limiter = AdvancedRateLimiter(requests_per_minute=2, tokens_per_minute=32000)
|
55 |
-
tavily_limiter = AdvancedRateLimiter(requests_per_minute=50)
|
56 |
|
57 |
# Initialize Tavily client
|
58 |
tavily_client = TavilyClient(os.getenv("TAVILY_API_KEY"))
|
59 |
|
60 |
-
# Custom tool functions
|
61 |
def multiply_tool(a: float, b: float) -> float:
|
62 |
"""Multiply two numbers."""
|
63 |
return a * b
|
@@ -76,10 +76,10 @@ def divide_tool(a: float, b: float) -> float:
|
|
76 |
raise ValueError("Cannot divide by zero.")
|
77 |
return a / b
|
78 |
|
79 |
-
|
80 |
-
"""Search using Tavily with rate limiting."""
|
81 |
try:
|
82 |
-
|
83 |
response = tavily_client.search(
|
84 |
query=query,
|
85 |
max_results=3,
|
@@ -97,20 +97,20 @@ async def tavily_search_tool(query: str) -> str:
|
|
97 |
except Exception as e:
|
98 |
return f"Tavily search failed: {str(e)}"
|
99 |
|
100 |
-
|
101 |
-
"""Search Wikipedia with rate limiting."""
|
102 |
try:
|
103 |
-
|
104 |
loader = WikipediaLoader(query=query, load_max_docs=1)
|
105 |
data = loader.load()
|
106 |
return "\n\n---\n\n".join([doc.page_content[:1000] for doc in data])
|
107 |
except Exception as e:
|
108 |
return f"Wikipedia search failed: {str(e)}"
|
109 |
|
110 |
-
|
111 |
-
"""Search ArXiv with rate limiting."""
|
112 |
try:
|
113 |
-
|
114 |
search_docs = ArxivLoader(query=query, load_max_docs=2).load()
|
115 |
return "\n\n---\n\n".join([doc.page_content[:800] for doc in search_docs])
|
116 |
except Exception as e:
|
@@ -148,7 +148,7 @@ def create_agno_agents():
|
|
148 |
api_key=os.getenv("GOOGLE_API_KEY"),
|
149 |
temperature=0
|
150 |
),
|
151 |
-
tools=[tavily_search_tool, wiki_search_tool, arxiv_search_tool], #
|
152 |
instructions=[
|
153 |
"You are a research specialist with access to multiple search tools.",
|
154 |
"Use Tavily search for current web information, Wikipedia for encyclopedic content, and ArXiv for academic papers.",
|
@@ -168,7 +168,7 @@ def create_agno_agents():
|
|
168 |
api_key=os.getenv("GROQ_API_KEY"),
|
169 |
temperature=0
|
170 |
),
|
171 |
-
tools=[tavily_search_tool, wiki_search_tool], #
|
172 |
instructions=[
|
173 |
"You are the main coordinator agent.",
|
174 |
"Analyze queries and provide comprehensive responses.",
|
@@ -185,7 +185,7 @@ def create_agno_agents():
|
|
185 |
"coordinator": coordinator_agent
|
186 |
}
|
187 |
|
188 |
-
# Main Agno multi-agent system (
|
189 |
class AgnoMultiAgentSystem:
|
190 |
"""Agno multi-agent system with comprehensive rate limiting"""
|
191 |
|
@@ -194,8 +194,8 @@ class AgnoMultiAgentSystem:
|
|
194 |
self.request_count = 0
|
195 |
self.last_request_time = time.time()
|
196 |
|
197 |
-
|
198 |
-
"""Process query using Agno agents with
|
199 |
|
200 |
# Global rate limiting (SILENT)
|
201 |
current_time = time.time()
|
@@ -207,7 +207,7 @@ class AgnoMultiAgentSystem:
|
|
207 |
|
208 |
# Add delay between requests (SILENT)
|
209 |
if self.request_count > 1:
|
210 |
-
|
211 |
|
212 |
for attempt in range(max_retries):
|
213 |
try:
|
@@ -228,7 +228,7 @@ class AgnoMultiAgentSystem:
|
|
228 |
|
229 |
if any(keyword in error_msg for keyword in ['rate limit', '429', 'quota', 'too many requests']):
|
230 |
wait_time = (2 ** attempt) + random.uniform(15, 45)
|
231 |
-
|
232 |
continue
|
233 |
|
234 |
elif attempt == max_retries - 1:
|
@@ -239,28 +239,15 @@ class AgnoMultiAgentSystem:
|
|
239 |
|
240 |
else:
|
241 |
wait_time = (2 ** attempt) + random.uniform(2, 8)
|
242 |
-
|
243 |
|
244 |
return "Maximum retries exceeded. Please try again later."
|
245 |
|
246 |
-
#
|
247 |
-
async def main_async(query: str) -> str:
|
248 |
-
"""Async main function compatible with Jupyter notebooks (SILENT)"""
|
249 |
-
agno_system = AgnoMultiAgentSystem()
|
250 |
-
return await agno_system.process_query(query)
|
251 |
-
|
252 |
def main(query: str) -> str:
|
253 |
-
"""Main function using Agno multi-agent system (
|
254 |
-
|
255 |
-
|
256 |
-
if loop.is_running():
|
257 |
-
import nest_asyncio
|
258 |
-
nest_asyncio.apply()
|
259 |
-
return asyncio.run(main_async(query))
|
260 |
-
else:
|
261 |
-
return asyncio.run(main_async(query))
|
262 |
-
except RuntimeError:
|
263 |
-
return asyncio.run(main_async(query))
|
264 |
|
265 |
def get_final_answer(query: str) -> str:
|
266 |
"""Extract only the FINAL ANSWER from the response"""
|
@@ -272,12 +259,7 @@ def get_final_answer(query: str) -> str:
|
|
272 |
else:
|
273 |
return full_response.strip()
|
274 |
|
275 |
-
# For Jupyter notebooks
|
276 |
-
async def run_query(query: str) -> str:
|
277 |
-
"""Direct async function for Jupyter notebooks (SILENT)"""
|
278 |
-
return await main_async(query)
|
279 |
-
|
280 |
if __name__ == "__main__":
|
281 |
-
# Test the Agno system
|
282 |
result = get_final_answer("What are the names of the US presidents who were assassinated?")
|
283 |
print(result)
|
|
|
26 |
self.token_usage = []
|
27 |
self.consecutive_failures = 0
|
28 |
|
29 |
+
def wait_if_needed(self, estimated_tokens: int = 1000):
|
30 |
current_time = time.time()
|
31 |
|
32 |
# Clean old requests (older than 1 minute)
|
|
|
36 |
# Calculate wait time for requests (SILENT)
|
37 |
if len(self.request_times) >= self.requests_per_minute:
|
38 |
wait_time = 60 - (current_time - self.request_times[0]) + random.uniform(2, 8)
|
39 |
+
time.sleep(wait_time) # Changed from asyncio.sleep to time.sleep
|
40 |
|
41 |
# Record this request
|
42 |
self.request_times.append(current_time)
|
|
|
52 |
# Initialize rate limiters for free tiers
|
53 |
groq_limiter = AdvancedRateLimiter(requests_per_minute=30, tokens_per_minute=6000)
|
54 |
gemini_limiter = AdvancedRateLimiter(requests_per_minute=2, tokens_per_minute=32000)
|
55 |
+
tavily_limiter = AdvancedRateLimiter(requests_per_minute=50)
|
56 |
|
57 |
# Initialize Tavily client
|
58 |
tavily_client = TavilyClient(os.getenv("TAVILY_API_KEY"))
|
59 |
|
60 |
+
# Custom tool functions - ALL SYNCHRONOUS (SILENT)
|
61 |
def multiply_tool(a: float, b: float) -> float:
|
62 |
"""Multiply two numbers."""
|
63 |
return a * b
|
|
|
76 |
raise ValueError("Cannot divide by zero.")
|
77 |
return a / b
|
78 |
|
79 |
+
def tavily_search_tool(query: str) -> str:
|
80 |
+
"""Search using Tavily with rate limiting - SYNCHRONOUS."""
|
81 |
try:
|
82 |
+
tavily_limiter.wait_if_needed()
|
83 |
response = tavily_client.search(
|
84 |
query=query,
|
85 |
max_results=3,
|
|
|
97 |
except Exception as e:
|
98 |
return f"Tavily search failed: {str(e)}"
|
99 |
|
100 |
+
def wiki_search_tool(query: str) -> str:
|
101 |
+
"""Search Wikipedia with rate limiting - SYNCHRONOUS."""
|
102 |
try:
|
103 |
+
time.sleep(random.uniform(1, 3)) # Changed from asyncio.sleep
|
104 |
loader = WikipediaLoader(query=query, load_max_docs=1)
|
105 |
data = loader.load()
|
106 |
return "\n\n---\n\n".join([doc.page_content[:1000] for doc in data])
|
107 |
except Exception as e:
|
108 |
return f"Wikipedia search failed: {str(e)}"
|
109 |
|
110 |
+
def arxiv_search_tool(query: str) -> str:
|
111 |
+
"""Search ArXiv with rate limiting - SYNCHRONOUS."""
|
112 |
try:
|
113 |
+
time.sleep(random.uniform(1, 4)) # Changed from asyncio.sleep
|
114 |
search_docs = ArxivLoader(query=query, load_max_docs=2).load()
|
115 |
return "\n\n---\n\n".join([doc.page_content[:800] for doc in search_docs])
|
116 |
except Exception as e:
|
|
|
148 |
api_key=os.getenv("GOOGLE_API_KEY"),
|
149 |
temperature=0
|
150 |
),
|
151 |
+
tools=[tavily_search_tool, wiki_search_tool, arxiv_search_tool], # All synchronous now
|
152 |
instructions=[
|
153 |
"You are a research specialist with access to multiple search tools.",
|
154 |
"Use Tavily search for current web information, Wikipedia for encyclopedic content, and ArXiv for academic papers.",
|
|
|
168 |
api_key=os.getenv("GROQ_API_KEY"),
|
169 |
temperature=0
|
170 |
),
|
171 |
+
tools=[tavily_search_tool, wiki_search_tool], # All synchronous now
|
172 |
instructions=[
|
173 |
"You are the main coordinator agent.",
|
174 |
"Analyze queries and provide comprehensive responses.",
|
|
|
185 |
"coordinator": coordinator_agent
|
186 |
}
|
187 |
|
188 |
+
# Main Agno multi-agent system (SIMPLIFIED - NO ASYNC)
|
189 |
class AgnoMultiAgentSystem:
|
190 |
"""Agno multi-agent system with comprehensive rate limiting"""
|
191 |
|
|
|
194 |
self.request_count = 0
|
195 |
self.last_request_time = time.time()
|
196 |
|
197 |
+
def process_query(self, query: str, max_retries: int = 5) -> str:
|
198 |
+
"""Process query using Agno agents with rate limiting (SYNCHRONOUS)"""
|
199 |
|
200 |
# Global rate limiting (SILENT)
|
201 |
current_time = time.time()
|
|
|
207 |
|
208 |
# Add delay between requests (SILENT)
|
209 |
if self.request_count > 1:
|
210 |
+
time.sleep(random.uniform(3, 10)) # Changed from asyncio.sleep
|
211 |
|
212 |
for attempt in range(max_retries):
|
213 |
try:
|
|
|
228 |
|
229 |
if any(keyword in error_msg for keyword in ['rate limit', '429', 'quota', 'too many requests']):
|
230 |
wait_time = (2 ** attempt) + random.uniform(15, 45)
|
231 |
+
time.sleep(wait_time) # Changed from asyncio.sleep
|
232 |
continue
|
233 |
|
234 |
elif attempt == max_retries - 1:
|
|
|
239 |
|
240 |
else:
|
241 |
wait_time = (2 ** attempt) + random.uniform(2, 8)
|
242 |
+
time.sleep(wait_time) # Changed from asyncio.sleep
|
243 |
|
244 |
return "Maximum retries exceeded. Please try again later."
|
245 |
|
246 |
+
# SIMPLIFIED main function (NO ASYNC)
|
|
|
|
|
|
|
|
|
|
|
247 |
def main(query: str) -> str:
|
248 |
+
"""Main function using Agno multi-agent system (SYNCHRONOUS)"""
|
249 |
+
agno_system = AgnoMultiAgentSystem()
|
250 |
+
return agno_system.process_query(query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
251 |
|
252 |
def get_final_answer(query: str) -> str:
|
253 |
"""Extract only the FINAL ANSWER from the response"""
|
|
|
259 |
else:
|
260 |
return full_response.strip()
|
261 |
|
|
|
|
|
|
|
|
|
|
|
262 |
if __name__ == "__main__":
|
263 |
+
# Test the Agno system - CLEAN OUTPUT ONLY
|
264 |
result = get_final_answer("What are the names of the US presidents who were assassinated?")
|
265 |
print(result)
|