Spaces:
Sleeping
Sleeping
Update veryfinal.py
Browse files- veryfinal.py +58 -70
veryfinal.py
CHANGED
|
@@ -26,8 +26,8 @@ from langchain_text_splitters import RecursiveCharacterTextSplitter
|
|
| 26 |
|
| 27 |
# Agno imports
|
| 28 |
from agno.agent import Agent
|
| 29 |
-
from agno.models.groq import
|
| 30 |
-
from agno.models.google import
|
| 31 |
from agno.tools.tavily import TavilyTools
|
| 32 |
from agno.memory.agent import AgentMemory
|
| 33 |
from agno.storage.sqlite import SqliteStorage
|
|
@@ -144,19 +144,19 @@ def subtract(a: int, b: int) -> int:
|
|
| 144 |
|
| 145 |
@tool
|
| 146 |
def divide(a: int, b: int) -> float:
|
| 147 |
-
"""Divide two numbers."""
|
| 148 |
if b == 0:
|
| 149 |
raise ValueError("Cannot divide by zero.")
|
| 150 |
return a / b
|
| 151 |
|
| 152 |
@tool
|
| 153 |
def modulus(a: int, b: int) -> int:
|
| 154 |
-
"""
|
| 155 |
return a % b
|
| 156 |
|
| 157 |
@tool
|
| 158 |
def optimized_web_search(query: str) -> str:
|
| 159 |
-
"""Optimized Tavily web search."""
|
| 160 |
try:
|
| 161 |
time.sleep(random.uniform(1, 2))
|
| 162 |
docs = TavilySearchResults(max_results=2).invoke(query=query)
|
|
@@ -169,7 +169,7 @@ def optimized_web_search(query: str) -> str:
|
|
| 169 |
|
| 170 |
@tool
|
| 171 |
def optimized_wiki_search(query: str) -> str:
|
| 172 |
-
"""Optimized Wikipedia search."""
|
| 173 |
try:
|
| 174 |
time.sleep(random.uniform(0.5, 1))
|
| 175 |
docs = WikipediaLoader(query=query, load_max_docs=1).load()
|
|
@@ -184,10 +184,7 @@ def optimized_wiki_search(query: str) -> str:
|
|
| 184 |
def setup_faiss():
|
| 185 |
try:
|
| 186 |
schema = """
|
| 187 |
-
{
|
| 188 |
-
page_content: .Question,
|
| 189 |
-
metadata: { task_id: .task_id, Final_answer: ."Final answer" }
|
| 190 |
-
}
|
| 191 |
"""
|
| 192 |
loader = JSONLoader(file_path="metadata.jsonl", jq_schema=schema, json_lines=True, text_content=False)
|
| 193 |
docs = loader.load()
|
|
@@ -202,6 +199,7 @@ def setup_faiss():
|
|
| 202 |
print(f"FAISS setup failed: {e}")
|
| 203 |
return None
|
| 204 |
|
|
|
|
| 205 |
class EnhancedAgentState(TypedDict):
|
| 206 |
messages: Annotated[List[HumanMessage|AIMessage], operator.add]
|
| 207 |
query: str
|
|
@@ -210,6 +208,7 @@ class EnhancedAgentState(TypedDict):
|
|
| 210 |
perf: Dict[str,Any]
|
| 211 |
agno_resp: str
|
| 212 |
|
|
|
|
| 213 |
class HybridLangGraphAgnoSystem:
|
| 214 |
def __init__(self):
|
| 215 |
self.agno = create_agno_agents()
|
|
@@ -234,98 +233,87 @@ class HybridLangGraphAgnoSystem:
|
|
| 234 |
|
| 235 |
def router(st: EnhancedAgentState) -> EnhancedAgentState:
|
| 236 |
q = st["query"].lower()
|
| 237 |
-
if any(k in q for k in ["calculate","math"]):
|
| 238 |
-
|
| 239 |
-
elif any(k in q for k in ["
|
| 240 |
-
|
| 241 |
-
elif any(k in q for k in ["what is","who is"]):
|
| 242 |
-
t = "lg_retrieval"
|
| 243 |
-
else:
|
| 244 |
-
t = "agno_general"
|
| 245 |
return {**st, "agent_type": t}
|
| 246 |
|
| 247 |
def lg_math(st: EnhancedAgentState) -> EnhancedAgentState:
|
| 248 |
groq_limiter.wait_if_needed()
|
| 249 |
-
t0
|
| 250 |
-
llm
|
| 251 |
-
sys
|
| 252 |
-
res
|
| 253 |
-
return {**st, "final_answer":
|
| 254 |
|
| 255 |
def agno_research(st: EnhancedAgentState) -> EnhancedAgentState:
|
| 256 |
gemini_limiter.wait_if_needed()
|
| 257 |
-
t0
|
| 258 |
-
resp
|
| 259 |
-
return {**st, "final_answer":
|
| 260 |
|
| 261 |
def lg_retrieval(st: EnhancedAgentState) -> EnhancedAgentState:
|
| 262 |
groq_limiter.wait_if_needed()
|
| 263 |
-
t0
|
| 264 |
-
llm
|
| 265 |
-
sys
|
| 266 |
-
res
|
| 267 |
-
return {**st, "final_answer":
|
| 268 |
|
| 269 |
def agno_general(st: EnhancedAgentState) -> EnhancedAgentState:
|
| 270 |
nvidia_limiter.wait_if_needed()
|
| 271 |
-
t0
|
| 272 |
if any(k in st["query"].lower() for k in ["calculate","compute"]):
|
| 273 |
-
resp
|
| 274 |
else:
|
| 275 |
-
resp
|
| 276 |
-
return {**st, "final_answer":
|
| 277 |
|
| 278 |
def pick(st: EnhancedAgentState) -> str:
|
| 279 |
return st["agent_type"]
|
| 280 |
|
| 281 |
-
g
|
| 282 |
-
g.add_node("router",
|
| 283 |
-
g.add_node("lg_math",
|
| 284 |
-
g.add_node("agno_research",
|
| 285 |
-
g.add_node("lg_retrieval",
|
| 286 |
-
g.add_node("agno_general",
|
| 287 |
g.set_entry_point("router")
|
| 288 |
-
g.add_conditional_edges("router",
|
| 289 |
-
"lg_math":"lg_math",
|
| 290 |
-
"
|
| 291 |
-
"lg_retrieval":"lg_retrieval",
|
| 292 |
-
"agno_general":"agno_general"
|
| 293 |
})
|
| 294 |
for n in ["lg_math","agno_research","lg_retrieval","agno_general"]:
|
| 295 |
-
g.add_edge(n,
|
| 296 |
return g.compile(checkpointer=MemorySaver())
|
| 297 |
|
| 298 |
def process_query(self, q: str) -> Dict[str,Any]:
|
| 299 |
-
state
|
| 300 |
"messages":[HumanMessage(content=q)],
|
| 301 |
-
"query":q,
|
|
|
|
| 302 |
}
|
| 303 |
-
cfg
|
| 304 |
try:
|
| 305 |
-
out
|
| 306 |
return {
|
| 307 |
-
"answer":
|
| 308 |
-
"performance_metrics":
|
| 309 |
-
"provider_used":
|
| 310 |
}
|
| 311 |
except Exception as e:
|
| 312 |
-
return {"answer":f"Error: {e}",
|
| 313 |
|
| 314 |
-
def build_graph(provider: str
|
| 315 |
-
if provider
|
| 316 |
return HybridLangGraphAgnoSystem().graph
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
if __name__ == "__main__":
|
| 327 |
-
graph = build_graph()
|
| 328 |
-
msgs = [HumanMessage(content="What are the names of the US presidents who were assassinated?")]
|
| 329 |
-
res = graph.invoke({"messages":msgs},{"configurable":{"thread_id":"test"}})
|
| 330 |
for m in res["messages"]:
|
| 331 |
m.pretty_print()
|
|
|
|
| 26 |
|
| 27 |
# Agno imports
|
| 28 |
from agno.agent import Agent
|
| 29 |
+
from agno.models.groq import GroqChat
|
| 30 |
+
from agno.models.google import GeminiChat
|
| 31 |
from agno.tools.tavily import TavilyTools
|
| 32 |
from agno.memory.agent import AgentMemory
|
| 33 |
from agno.storage.sqlite import SqliteStorage
|
|
|
|
| 144 |
|
| 145 |
@tool
|
| 146 |
def divide(a: int, b: int) -> float:
|
| 147 |
+
"""Divide two numbers; errors if divisor is zero."""
|
| 148 |
if b == 0:
|
| 149 |
raise ValueError("Cannot divide by zero.")
|
| 150 |
return a / b
|
| 151 |
|
| 152 |
@tool
|
| 153 |
def modulus(a: int, b: int) -> int:
|
| 154 |
+
"""Return the remainder of a divided by b."""
|
| 155 |
return a % b
|
| 156 |
|
| 157 |
@tool
|
| 158 |
def optimized_web_search(query: str) -> str:
|
| 159 |
+
"""Optimized Tavily web search for speed."""
|
| 160 |
try:
|
| 161 |
time.sleep(random.uniform(1, 2))
|
| 162 |
docs = TavilySearchResults(max_results=2).invoke(query=query)
|
|
|
|
| 169 |
|
| 170 |
@tool
|
| 171 |
def optimized_wiki_search(query: str) -> str:
|
| 172 |
+
"""Optimized Wikipedia search for speed."""
|
| 173 |
try:
|
| 174 |
time.sleep(random.uniform(0.5, 1))
|
| 175 |
docs = WikipediaLoader(query=query, load_max_docs=1).load()
|
|
|
|
| 184 |
def setup_faiss():
|
| 185 |
try:
|
| 186 |
schema = """
|
| 187 |
+
{ page_content: .Question, metadata: { task_id: .task_id, Final_answer: ."Final answer" } }
|
|
|
|
|
|
|
|
|
|
| 188 |
"""
|
| 189 |
loader = JSONLoader(file_path="metadata.jsonl", jq_schema=schema, json_lines=True, text_content=False)
|
| 190 |
docs = loader.load()
|
|
|
|
| 199 |
print(f"FAISS setup failed: {e}")
|
| 200 |
return None
|
| 201 |
|
| 202 |
+
# State definition
|
| 203 |
class EnhancedAgentState(TypedDict):
|
| 204 |
messages: Annotated[List[HumanMessage|AIMessage], operator.add]
|
| 205 |
query: str
|
|
|
|
| 208 |
perf: Dict[str,Any]
|
| 209 |
agno_resp: str
|
| 210 |
|
| 211 |
+
# Hybrid system combining LangGraph and Agno
|
| 212 |
class HybridLangGraphAgnoSystem:
|
| 213 |
def __init__(self):
|
| 214 |
self.agno = create_agno_agents()
|
|
|
|
| 233 |
|
| 234 |
def router(st: EnhancedAgentState) -> EnhancedAgentState:
|
| 235 |
q = st["query"].lower()
|
| 236 |
+
if any(k in q for k in ["calculate","math"]): t="lg_math"
|
| 237 |
+
elif any(k in q for k in ["research","analyze"]): t="agno_research"
|
| 238 |
+
elif any(k in q for k in ["what is","who is"]): t="lg_retrieval"
|
| 239 |
+
else: t="agno_general"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
return {**st, "agent_type": t}
|
| 241 |
|
| 242 |
def lg_math(st: EnhancedAgentState) -> EnhancedAgentState:
|
| 243 |
groq_limiter.wait_if_needed()
|
| 244 |
+
t0=time.time()
|
| 245 |
+
llm=groq_llm.bind_tools([multiply,add,subtract,divide,modulus])
|
| 246 |
+
sys=SystemMessage(content="Fast calculator. FINAL ANSWER: [result]")
|
| 247 |
+
res=llm.invoke([sys,HumanMessage(content=st["query"])])
|
| 248 |
+
return {**st, "final_answer":res.content, "perf":{"time":time.time()-t0,"prov":"LG-Groq"}}
|
| 249 |
|
| 250 |
def agno_research(st: EnhancedAgentState) -> EnhancedAgentState:
|
| 251 |
gemini_limiter.wait_if_needed()
|
| 252 |
+
t0=time.time()
|
| 253 |
+
resp=self.agno["research"].run(st["query"],stream=False)
|
| 254 |
+
return {**st, "final_answer":resp, "perf":{"time":time.time()-t0,"prov":"Agno-Gemini"}}
|
| 255 |
|
| 256 |
def lg_retrieval(st: EnhancedAgentState) -> EnhancedAgentState:
|
| 257 |
groq_limiter.wait_if_needed()
|
| 258 |
+
t0=time.time()
|
| 259 |
+
llm=groq_llm.bind_tools(self.tools)
|
| 260 |
+
sys=SystemMessage(content="Retrieve. FINAL ANSWER: [answer]")
|
| 261 |
+
res=llm.invoke([sys,HumanMessage(content=st["query"])])
|
| 262 |
+
return {**st, "final_answer":res.content, "perf":{"time":time.time()-t0,"prov":"LG-Retrieval"}}
|
| 263 |
|
| 264 |
def agno_general(st: EnhancedAgentState) -> EnhancedAgentState:
|
| 265 |
nvidia_limiter.wait_if_needed()
|
| 266 |
+
t0=time.time()
|
| 267 |
if any(k in st["query"].lower() for k in ["calculate","compute"]):
|
| 268 |
+
resp=self.agno["math"].run(st["query"],stream=False)
|
| 269 |
else:
|
| 270 |
+
resp=self.agno["research"].run(st["query"],stream=False)
|
| 271 |
+
return {**st, "final_answer":resp, "perf":{"time":time.time()-t0,"prov":"Agno-General"}}
|
| 272 |
|
| 273 |
def pick(st: EnhancedAgentState) -> str:
|
| 274 |
return st["agent_type"]
|
| 275 |
|
| 276 |
+
g=StateGraph(EnhancedAgentState)
|
| 277 |
+
g.add_node("router",router)
|
| 278 |
+
g.add_node("lg_math",lg_math)
|
| 279 |
+
g.add_node("agno_research",agno_research)
|
| 280 |
+
g.add_node("lg_retrieval",lg_retrieval)
|
| 281 |
+
g.add_node("agno_general",agno_general)
|
| 282 |
g.set_entry_point("router")
|
| 283 |
+
g.add_conditional_edges("router",pick,{
|
| 284 |
+
"lg_math":"lg_math","agno_research":"agno_research",
|
| 285 |
+
"lg_retrieval":"lg_retrieval","agno_general":"agno_general"
|
|
|
|
|
|
|
| 286 |
})
|
| 287 |
for n in ["lg_math","agno_research","lg_retrieval","agno_general"]:
|
| 288 |
+
g.add_edge(n,"END")
|
| 289 |
return g.compile(checkpointer=MemorySaver())
|
| 290 |
|
| 291 |
def process_query(self, q: str) -> Dict[str,Any]:
|
| 292 |
+
state={
|
| 293 |
"messages":[HumanMessage(content=q)],
|
| 294 |
+
"query":q,"agent_type":"","final_answer":"",
|
| 295 |
+
"perf":{},"agno_resp":""
|
| 296 |
}
|
| 297 |
+
cfg={"configurable":{"thread_id":f"hyb_{hash(q)}"}}
|
| 298 |
try:
|
| 299 |
+
out=self.graph.invoke(state,cfg)
|
| 300 |
return {
|
| 301 |
+
"answer":out["final_answer"],
|
| 302 |
+
"performance_metrics":out["perf"],
|
| 303 |
+
"provider_used":out["perf"].get("prov")
|
| 304 |
}
|
| 305 |
except Exception as e:
|
| 306 |
+
return {"answer":f"Error: {e}","performance_metrics":{},"provider_used":"Error"}
|
| 307 |
|
| 308 |
+
def build_graph(provider: str="hybrid"):
|
| 309 |
+
if provider=="hybrid":
|
| 310 |
return HybridLangGraphAgnoSystem().graph
|
| 311 |
+
raise ValueError("Only 'hybrid' supported")
|
| 312 |
+
|
| 313 |
+
# Test
|
| 314 |
+
if __name__=="__main__":
|
| 315 |
+
graph=build_graph()
|
| 316 |
+
msgs=[HumanMessage(content="What are the names of the US presidents who were assassinated?")]
|
| 317 |
+
res=graph.invoke({"messages":msgs},{"configurable":{"thread_id":"test"}})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
for m in res["messages"]:
|
| 319 |
m.pretty_print()
|