josondev commited on
Commit
0c69489
·
verified ·
1 Parent(s): 8dc7ff2

Update veryfinal.py

Browse files
Files changed (1) hide show
  1. veryfinal.py +121 -234
veryfinal.py CHANGED
@@ -1,4 +1,3 @@
1
- """Enhanced LangGraph + Agno Hybrid Agent System"""
2
  import os
3
  import time
4
  import random
@@ -6,160 +5,49 @@ from dotenv import load_dotenv
6
  from typing import List, Dict, Any, TypedDict, Annotated
7
  import operator
8
 
9
- # LangGraph imports
10
- from langgraph.graph import START, StateGraph, MessagesState
11
- from langgraph.prebuilt import tools_condition, ToolNode
12
- from langgraph.checkpoint.memory import MemorySaver
13
-
14
- # LangChain imports
15
- from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
16
  from langchain_core.tools import tool
17
- from langchain_groq import ChatGroq
18
- from langchain_google_genai import ChatGoogleGenerativeAI
19
- from langchain_nvidia_ai_endpoints import ChatNVIDIA, NVIDIAEmbeddings
20
  from langchain_community.tools.tavily_search import TavilySearchResults
21
- from langchain_community.document_loaders import WikipediaLoader, ArxivLoader, JSONLoader
22
- from langchain_community.vectorstores import FAISS
23
  from langchain.tools.retriever import create_retriever_tool
24
  from langchain_text_splitters import RecursiveCharacterTextSplitter
 
 
25
 
26
- # Agno imports
27
- from agno.agent import Agent
28
- from agno.models.groq import GroqChat
29
- from agno.models.google import GeminiChat
30
- from agno.tools.tavily import TavilyTools
31
- from agno.memory.agent import AgentMemory
32
- from agno.storage.sqlite import SqliteStorage
33
- from agno.memory.v2.db.sqlite import SqliteMemoryDb # Correct import for memory DB
34
-
35
- load_dotenv()
36
-
37
- # Rate limiter with exponential backoff
38
- class PerformanceRateLimiter:
39
- def __init__(self, rpm: int, name: str):
40
- self.rpm = rpm
41
- self.name = name
42
- self.times: List[float] = []
43
- self.failures = 0
44
-
45
- def wait_if_needed(self):
46
- now = time.time()
47
- self.times = [t for t in self.times if now - t < 60]
48
- if len(self.times) >= self.rpm:
49
- wait = 60 - (now - self.times[0]) + random.uniform(1, 3)
50
- time.sleep(wait)
51
- if self.failures:
52
- backoff = min(2 ** self.failures, 30) + random.uniform(0.5, 1.5)
53
- time.sleep(backoff)
54
- self.times.append(now)
55
-
56
- def record_success(self):
57
- self.failures = 0
58
-
59
- def record_failure(self):
60
- self.failures += 1
61
-
62
- # Initialize rate limiters
63
- gemini_limiter = PerformanceRateLimiter(28, "Gemini")
64
- groq_limiter = PerformanceRateLimiter(28, "Groq")
65
- nvidia_limiter = PerformanceRateLimiter(4, "NVIDIA")
66
-
67
- # Create Agno agents with corrected SQLite storage and memory
68
- def create_agno_agents():
69
- # 1. Storage for the agent's overall state (conversations, etc.)
70
- storage = SqliteStorage(
71
- table_name="agent_sessions",
72
- db_file="tmp/agent_sessions.db",
73
- auto_upgrade_schema=True
74
- )
75
- # 2. A separate database for the agent's memory
76
- memory_db = SqliteMemoryDb(db_file="tmp/agent_memory.db")
77
-
78
- # 3. The AgentMemory object, which uses the memory_db
79
- agent_memory = AgentMemory(
80
- db=memory_db, # Pass the SqliteMemoryDb here
81
- create_user_memories=True,
82
- create_session_summary=True
83
- )
84
-
85
- math_agent = Agent(
86
- name="MathSpecialist",
87
- model=GroqChat(
88
- model="llama-3.3-70b-versatile",
89
- api_key=os.getenv("GROQ_API_KEY"),
90
- temperature=0
91
- ),
92
- description="Expert mathematical problem solver",
93
- instructions=[
94
- "Solve math problems with precision",
95
- "Show step-by-step calculations",
96
- "Finish with: FINAL ANSWER: [result]"
97
- ],
98
- storage=storage, # Use SqliteStorage for the agent's persistence
99
- memory=agent_memory, # Use the configured AgentMemory
100
- show_tool_calls=False,
101
- markdown=False
102
- )
103
- research_agent = Agent(
104
- name="ResearchSpecialist",
105
- model=GeminiChat(
106
- model="gemini-2.0-flash-lite",
107
- api_key=os.getenv("GOOGLE_API_KEY"),
108
- temperature=0
109
- ),
110
- description="Expert research and information gathering specialist",
111
- instructions=[
112
- "Conduct thorough research using available tools",
113
- "Synthesize information from multiple sources",
114
- "Finish with: FINAL ANSWER: [answer]"
115
- ],
116
- tools=[
117
- TavilyTools(
118
- api_key=os.getenv("TAVILY_API_KEY"),
119
- search=True,
120
- max_tokens=6000,
121
- search_depth="advanced",
122
- format="markdown"
123
- )
124
- ],
125
- storage=storage, # Use the same storage for persistence
126
- memory=agent_memory, # Use the same memory configuration
127
- show_tool_calls=False,
128
- markdown=False
129
- )
130
- return {"math": math_agent, "research": research_agent}
131
 
132
- # LangGraph tools
133
  @tool
134
  def multiply(a: int, b: int) -> int:
135
- """Multiply two numbers."""
136
  return a * b
137
 
138
  @tool
139
  def add(a: int, b: int) -> int:
140
- """Add two numbers."""
141
  return a + b
142
 
143
  @tool
144
  def subtract(a: int, b: int) -> int:
145
- """Subtract two numbers."""
146
  return a - b
147
 
148
  @tool
149
  def divide(a: int, b: int) -> float:
150
- """Divide two numbers."""
151
  if b == 0:
152
  raise ValueError("Cannot divide by zero.")
153
  return a / b
154
 
155
  @tool
156
  def modulus(a: int, b: int) -> int:
157
- """Return the remainder of a divided by b."""
158
  return a % b
159
 
160
  @tool
161
  def optimized_web_search(query: str) -> str:
162
- """Optimized Tavily web search."""
163
  try:
164
  time.sleep(random.uniform(1, 2))
165
  docs = TavilySearchResults(max_results=2).invoke(query=query)
@@ -172,7 +60,7 @@ def optimized_web_search(query: str) -> str:
172
 
173
  @tool
174
  def optimized_wiki_search(query: str) -> str:
175
- """Optimized Wikipedia search."""
176
  try:
177
  time.sleep(random.uniform(0.5, 1))
178
  docs = WikipediaLoader(query=query, load_max_docs=1).load()
@@ -183,24 +71,45 @@ def optimized_wiki_search(query: str) -> str:
183
  except Exception as e:
184
  return f"Wikipedia search failed: {e}"
185
 
186
- # FAISS setup
187
- def setup_faiss():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  try:
189
- schema = """
190
- { page_content: .Question, metadata: { task_id: .task_id, Final_answer: ."Final answer" } }
191
- """
192
- loader = JSONLoader(file_path="metadata.jsonl", jq_schema=schema, json_lines=True, text_content=False)
193
- docs = loader.load()
194
- splitter = RecursiveCharacterTextSplitter(chunk_size=256, chunk_overlap=50)
195
- chunks = splitter.split_documents(docs)
196
- embeds = NVIDIAEmbeddings(
197
- model="nvidia/nv-embedqa-e5-v5",
198
- api_key=os.getenv("NVIDIA_API_KEY")
199
- )
200
- return FAISS.from_documents(chunks, embeds)
 
201
  except Exception as e:
202
- print(f"FAISS setup failed: {e}")
203
- return None
 
204
 
205
  class EnhancedAgentState(TypedDict):
206
  messages: Annotated[List[HumanMessage|AIMessage], operator.add]
@@ -210,117 +119,95 @@ 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()
216
- self.store = setup_faiss()
217
  self.tools = [
218
  multiply, add, subtract, divide, modulus,
219
  optimized_web_search, optimized_wiki_search
220
  ]
221
- if self.store:
222
- retr = self.store.as_retriever(search_type="similarity", search_kwargs={"k":2})
223
- self.tools.append(create_retriever_tool(
224
- retriever=retr,
225
- name="Question_Search",
226
- description="Retrieve similar questions"
227
- ))
228
  self.graph = self._build_graph()
229
 
230
  def _build_graph(self):
231
- groq_llm = ChatGroq(model="llama-3.3-70b-versatile", temperature=0)
232
- gemini_llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash-lite", temperature=0)
233
- nvidia_llm = ChatNVIDIA(model="meta/llama-3.1-70b-instruct", temperature=0)
234
 
235
  def router(st: EnhancedAgentState) -> EnhancedAgentState:
236
  q = st["query"].lower()
237
- if any(k in q for k in ["calculate","math"]): t="lg_math"
238
- elif any(k in q for k in ["research","analyze"]): t="agno_research"
239
- elif any(k in q for k in ["what is","who is"]): t="lg_retrieval"
240
- else: t="agno_general"
 
 
241
  return {**st, "agent_type": t}
242
 
243
- def lg_math(st: EnhancedAgentState) -> EnhancedAgentState:
244
- groq_limiter.wait_if_needed()
245
- t0=time.time()
246
- llm=groq_llm.bind_tools([multiply,add,subtract,divide,modulus])
247
- sys=SystemMessage(content="Fast calculator. FINAL ANSWER: [result]")
248
- res=llm.invoke([sys,HumanMessage(content=st["query"])])
249
- return {**st, "final_answer":res.content, "perf":{"time":time.time()-t0,"prov":"LG-Groq"}}
250
-
251
- def agno_research(st: EnhancedAgentState) -> EnhancedAgentState:
252
- gemini_limiter.wait_if_needed()
253
- t0=time.time()
254
- resp=self.agno["research"].run(st["query"],stream=False)
255
- return {**st, "final_answer":resp, "perf":{"time":time.time()-t0,"prov":"Agno-Gemini"}}
256
-
257
- def lg_retrieval(st: EnhancedAgentState) -> EnhancedAgentState:
258
- groq_limiter.wait_if_needed()
259
- t0=time.time()
260
- llm=groq_llm.bind_tools(self.tools)
261
- sys=SystemMessage(content="Retrieve. FINAL ANSWER: [answer]")
262
- res=llm.invoke([sys,HumanMessage(content=st["query"])])
263
- return {**st, "final_answer":res.content, "perf":{"time":time.time()-t0,"prov":"LG-Retrieval"}}
264
-
265
- def agno_general(st: EnhancedAgentState) -> EnhancedAgentState:
266
- nvidia_limiter.wait_if_needed()
267
- t0=time.time()
268
- if any(k in st["query"].lower() for k in ["calculate","compute"]):
269
- resp=self.agno["math"].run(st["query"],stream=False)
270
- else:
271
- resp=self.agno["research"].run(st["query"],stream=False)
272
- return {**st, "final_answer":resp, "perf":{"time":time.time()-t0,"prov":"Agno-General"}}
273
 
274
  def pick(st: EnhancedAgentState) -> str:
275
  return st["agent_type"]
276
 
277
- g=StateGraph(EnhancedAgentState)
278
- g.add_node("router",router)
279
- g.add_node("lg_math",lg_math)
280
- g.add_node("agno_research",agno_research)
281
- g.add_node("lg_retrieval",lg_retrieval)
282
- g.add_node("agno_general",agno_general)
 
283
  g.set_entry_point("router")
284
- g.add_conditional_edges("router",pick,{
285
- "lg_math":"lg_math","agno_research":"agno_research",
286
- "lg_retrieval":"lg_retrieval","agno_general":"agno_general"
 
 
 
287
  })
288
- for n in ["lg_math","agno_research","lg_retrieval","agno_general"]:
289
- g.add_edge(n,"END")
290
  return g.compile(checkpointer=MemorySaver())
291
 
292
- def process_query(self, q: str) -> Dict[str,Any]:
293
- state={
294
- "messages":[HumanMessage(content=q)],
295
- "query":q,"agent_type":"","final_answer":"",
296
- "perf":{},"agno_resp":""
 
 
 
297
  }
298
- cfg={"configurable":{"thread_id":f"hyb_{hash(q)}"}}
299
- try:
300
- out=self.graph.invoke(state,cfg)
301
- return {
302
- "answer":out["final_answer"],
303
- "performance_metrics":out["perf"],
304
- "provider_used":out["perf"].get("prov")
305
- }
306
- except Exception as e:
307
- return {"answer":f"Error: {e}","performance_metrics":{},"provider_used":"Error"}
308
-
309
- def build_graph(provider: str = "hybrid"):
310
- """
311
- Build and return the StateGraph for the requested provider.
312
- - "hybrid", "groq", "google", and "nvidia" are all valid and
313
- will return the full HybridLangGraphAgnoSystem graph.
314
- """
315
- if provider in ("hybrid", "groq", "google", "nvidia"):
316
- return HybridLangGraphAgnoSystem().graph
317
- else:
318
- raise ValueError(f"Unsupported provider: '{provider}'. Please use 'hybrid', 'groq', 'google', or 'nvidia'.")
319
-
320
- # Test
321
- if __name__=="__main__":
322
- graph=build_graph()
323
- msgs=[HumanMessage(content="What are the names of the US presidents who were assassinated?")]
324
- res=graph.invoke({"messages":msgs},{"configurable":{"thread_id":"test"}})
325
- for m in res["messages"]:
326
- m.pretty_print()
 
 
1
  import os
2
  import time
3
  import random
 
5
  from typing import List, Dict, Any, TypedDict, Annotated
6
  import operator
7
 
 
 
 
 
 
 
 
8
  from langchain_core.tools import tool
 
 
 
9
  from langchain_community.tools.tavily_search import TavilySearchResults
10
+ from langchain_community.document_loaders import WikipediaLoader
11
+ from langchain_community.vectorstores import Chroma
12
  from langchain.tools.retriever import create_retriever_tool
13
  from langchain_text_splitters import RecursiveCharacterTextSplitter
14
+ from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
15
+ from langchain_community.embeddings import SentenceTransformerEmbeddings
16
 
17
+ from langgraph.graph import StateGraph, START, END
18
+ from langgraph.checkpoint.memory import MemorySaver
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ # ---- Tool Definitions ----
21
  @tool
22
  def multiply(a: int, b: int) -> int:
23
+ """Multiply two integers and return the product."""
24
  return a * b
25
 
26
  @tool
27
  def add(a: int, b: int) -> int:
28
+ """Add two integers and return the sum."""
29
  return a + b
30
 
31
  @tool
32
  def subtract(a: int, b: int) -> int:
33
+ """Subtract the second integer from the first and return the difference."""
34
  return a - b
35
 
36
  @tool
37
  def divide(a: int, b: int) -> float:
38
+ """Divide the first integer by the second and return the quotient."""
39
  if b == 0:
40
  raise ValueError("Cannot divide by zero.")
41
  return a / b
42
 
43
  @tool
44
  def modulus(a: int, b: int) -> int:
45
+ """Return the remainder of the division of the first integer by the second."""
46
  return a % b
47
 
48
  @tool
49
  def optimized_web_search(query: str) -> str:
50
+ """Perform an optimized web search using TavilySearchResults and return concatenated document snippets."""
51
  try:
52
  time.sleep(random.uniform(1, 2))
53
  docs = TavilySearchResults(max_results=2).invoke(query=query)
 
60
 
61
  @tool
62
  def optimized_wiki_search(query: str) -> str:
63
+ """Perform an optimized Wikipedia search and return concatenated document snippets."""
64
  try:
65
  time.sleep(random.uniform(0.5, 1))
66
  docs = WikipediaLoader(query=query, load_max_docs=1).load()
 
71
  except Exception as e:
72
  return f"Wikipedia search failed: {e}"
73
 
74
+ # ---- LLM Integrations ----
75
+ load_dotenv()
76
+
77
+ # Groq (Llama 3, DeepSeek, etc. via LangChain integration)
78
+ from langchain_groq import ChatGroq
79
+
80
+ # NVIDIA NIM (LangChain integration)
81
+ from langchain_nvidia_ai_endpoints import ChatNVIDIA
82
+
83
+ from google import genai
84
+
85
+ # DeepSeek (via Ollama or API)
86
+ import requests
87
+
88
+ # Baidu ERNIE (assume open source API, use requests as placeholder)
89
+ def baidu_ernie_generate(prompt, api_key=None):
90
+ """Call Baidu ERNIE open source API (pseudo-code, replace with actual endpoint and params)."""
91
+ # Example endpoint and payload for demonstration purposes only:
92
+ url = "https://api.baidu.com/ernie/v1/generate"
93
+ headers = {"Authorization": f"Bearer {api_key}"}
94
+ data = {"model": "ernie-4.5", "prompt": prompt}
95
  try:
96
+ resp = requests.post(url, headers=headers, json=data, timeout=30)
97
+ return resp.json().get("result", "")
98
+ except Exception as e:
99
+ return f"ERNIE API error: {e}"
100
+
101
+ def deepseek_generate(prompt, api_key=None):
102
+ """Call DeepSeek open source API (pseudo-code, replace with actual endpoint and params)."""
103
+ url = "https://api.deepseek.com/v1/chat/completions"
104
+ headers = {"Authorization": f"Bearer {api_key}"}
105
+ data = {"model": "deepseek-chat", "messages": [{"role": "user", "content": prompt}]}
106
+ try:
107
+ resp = requests.post(url, headers=headers, json=data, timeout=30)
108
+ return resp.json().get("choices", [{}])[0].get("message", {}).get("content", "")
109
  except Exception as e:
110
+ return f"DeepSeek API error: {e}"
111
+
112
+ # ---- Graph State and System ----
113
 
114
  class EnhancedAgentState(TypedDict):
115
  messages: Annotated[List[HumanMessage|AIMessage], operator.add]
 
119
  perf: Dict[str,Any]
120
  agno_resp: str
121
 
122
+ class HybridLangGraphMultiLLMSystem:
123
  def __init__(self):
 
 
124
  self.tools = [
125
  multiply, add, subtract, divide, modulus,
126
  optimized_web_search, optimized_wiki_search
127
  ]
 
 
 
 
 
 
 
128
  self.graph = self._build_graph()
129
 
130
  def _build_graph(self):
131
+ groq_llm = ChatGroq(model="llama3-70b-8192", temperature=0, api_key=os.getenv("GROQ_API_KEY"))
132
+ nvidia_llm = ChatNVIDIA(model="meta/llama3-70b-instruct", temperature=0, api_key=os.getenv("NVIDIA_API_KEY"))
 
133
 
134
  def router(st: EnhancedAgentState) -> EnhancedAgentState:
135
  q = st["query"].lower()
136
+ if "groq" in q: t = "groq"
137
+ elif "nvidia" in q: t = "nvidia"
138
+ elif "gemini" in q or "google" in q: t = "gemini"
139
+ elif "deepseek" in q: t = "deepseek"
140
+ elif "ernie" in q or "baidu" in q: t = "baidu"
141
+ else: t = "groq" # default
142
  return {**st, "agent_type": t}
143
 
144
+ def groq_node(st: EnhancedAgentState) -> EnhancedAgentState:
145
+ t0 = time.time()
146
+ sys = SystemMessage(content="Answer as an expert.")
147
+ res = groq_llm.invoke([sys, HumanMessage(content=st["query"])])
148
+ return {**st, "final_answer": res.content, "perf": {"time": time.time() - t0, "prov": "Groq"}}
149
+
150
+ def nvidia_node(st: EnhancedAgentState) -> EnhancedAgentState:
151
+ t0 = time.time()
152
+ sys = SystemMessage(content="Answer as an expert.")
153
+ res = nvidia_llm.invoke([sys, HumanMessage(content=st["query"])])
154
+ return {**st, "final_answer": res.content, "perf": {"time": time.time() - t0, "prov": "NVIDIA"}}
155
+
156
+ def gemini_node(st: EnhancedAgentState) -> EnhancedAgentState:
157
+ t0 = time.time()
158
+ genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
159
+ model = genai.GenerativeModel("gemini-1.5-pro-latest")
160
+ res = model.generate_content(st["query"])
161
+ return {**st, "final_answer": res.text, "perf": {"time": time.time() - t0, "prov": "Gemini"}}
162
+
163
+ def deepseek_node(st: EnhancedAgentState) -> EnhancedAgentState:
164
+ t0 = time.time()
165
+ resp = deepseek_generate(st["query"], api_key=os.getenv("DEEPSEEK_API_KEY"))
166
+ return {**st, "final_answer": resp, "perf": {"time": time.time() - t0, "prov": "DeepSeek"}}
167
+
168
+ def baidu_node(st: EnhancedAgentState) -> EnhancedAgentState:
169
+ t0 = time.time()
170
+ resp = baidu_ernie_generate(st["query"], api_key=os.getenv("BAIDU_API_KEY"))
171
+ return {**st, "final_answer": resp, "perf": {"time": time.time() - t0, "prov": "ERNIE"}}
 
 
172
 
173
  def pick(st: EnhancedAgentState) -> str:
174
  return st["agent_type"]
175
 
176
+ g = StateGraph(EnhancedAgentState)
177
+ g.add_node("router", router)
178
+ g.add_node("groq", groq_node)
179
+ g.add_node("nvidia", nvidia_node)
180
+ g.add_node("gemini", gemini_node)
181
+ g.add_node("deepseek", deepseek_node)
182
+ g.add_node("baidu", baidu_node)
183
  g.set_entry_point("router")
184
+ g.add_conditional_edges("router", pick, {
185
+ "groq": "groq",
186
+ "nvidia": "nvidia",
187
+ "gemini": "gemini",
188
+ "deepseek": "deepseek",
189
+ "baidu": "baidu"
190
  })
191
+ for n in ["groq", "nvidia", "gemini", "deepseek", "baidu"]:
192
+ g.add_edge(n, END)
193
  return g.compile(checkpointer=MemorySaver())
194
 
195
+ def process_query(self, q: str) -> str:
196
+ state = {
197
+ "messages": [HumanMessage(content=q)],
198
+ "query": q,
199
+ "agent_type": "",
200
+ "final_answer": "",
201
+ "perf": {},
202
+ "agno_resp": ""
203
  }
204
+ cfg = {"configurable": {"thread_id": f"hyb_{hash(q)}"}}
205
+ out = self.graph.invoke(state, cfg)
206
+ raw_answer = out["final_answer"]
207
+ parts = raw_answer.split('\n\n', 1)
208
+ answer_part = parts[1].strip() if len(parts) > 1 else raw_answer.strip()
209
+ return answer_part
210
+
211
+ if __name__ == "__main__":
212
+ query = "What are the names of the US presidents who were assassinated? (groq)"
213
+ print("LangGraph Hybrid:", HybridLangGraphMultiLLMSystem().process_query(query))