File size: 12,238 Bytes
d3bd386
d4557ee
 
 
f4505e9
 
 
1fa6961
f4505e9
 
d4557ee
f4505e9
1fa6961
f4505e9
 
 
 
 
d4557ee
f4505e9
d4557ee
f4505e9
 
 
 
 
 
8dc7ff2
 
a55679f
f4505e9
a55679f
d3bd386
0f81d99
f4505e9
 
d4557ee
f4505e9
d4557ee
 
 
a55679f
d4557ee
 
f4505e9
d4557ee
 
 
 
 
 
 
 
 
 
f4505e9
d4557ee
 
f4505e9
d4557ee
1fa6961
a55679f
d4557ee
 
 
1fa6961
d3bd386
f4505e9
d3bd386
c5a2611
f4505e9
d4557ee
 
f4505e9
d3bd386
 
 
 
 
 
 
 
 
 
f4505e9
 
8dc7ff2
d3bd386
f4505e9
 
 
 
 
d4557ee
f4505e9
d4557ee
f4505e9
d3bd386
 
f4505e9
 
 
 
 
8dc7ff2
d3bd386
f4505e9
 
 
a55679f
f4505e9
a55679f
 
 
 
 
 
 
 
 
 
 
 
f4505e9
d3bd386
 
f4505e9
 
 
d4557ee
f4505e9
a55679f
f4505e9
 
676363b
f4505e9
 
 
 
676363b
f4505e9
1fa6961
f4505e9
 
676363b
f4505e9
cc467c2
f4505e9
 
d3bd386
f4505e9
 
 
 
 
 
e750c5e
f4505e9
a55679f
f4505e9
 
72c7dbb
25c1140
d4557ee
 
a55679f
 
 
 
25c1140
d4557ee
0f81d99
f4505e9
9a3d597
72c7dbb
25c1140
a55679f
d4557ee
a55679f
 
 
 
25c1140
d4557ee
b1b6e20
d4557ee
 
f4505e9
d4557ee
e750c5e
f4505e9
a55679f
d4557ee
a55679f
 
 
 
 
 
d4557ee
f4505e9
 
 
 
a55679f
d4557ee
f4505e9
 
 
d4557ee
 
f4505e9
a55679f
f4505e9
d4557ee
 
a55679f
 
 
 
d4557ee
 
a55679f
 
 
 
 
d4557ee
a55679f
d4557ee
a55679f
 
 
 
 
 
e750c5e
 
 
 
a55679f
 
 
f4505e9
e750c5e
 
 
 
 
a55679f
 
f4505e9
e750c5e
 
 
a55679f
 
f4505e9
e750c5e
 
 
 
 
a55679f
 
d4557ee
e750c5e
a55679f
e750c5e
d4557ee
e750c5e
 
a55679f
 
 
 
e750c5e
 
 
 
 
 
0ab2059
 
72c7dbb
 
0ab2059
d4557ee
0ab2059
d4557ee
0f81d99
72c7dbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22c4422
72c7dbb
 
e750c5e
72c7dbb
 
6c0b312
cadb619
6c0b312
3be2c9c
 
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
"""Enhanced LangGraph + Agno Hybrid Agent System"""
import os
import time
import random
from dotenv import load_dotenv
from typing import List, Dict, Any, TypedDict, Annotated
import operator

# LangGraph imports
from langgraph.graph import START, StateGraph, MessagesState
from langgraph.prebuilt import tools_condition, ToolNode
from langgraph.checkpoint.memory import MemorySaver

# LangChain imports
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
from langchain_core.tools import tool
from langchain_groq import ChatGroq
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_nvidia_ai_endpoints import ChatNVIDIA, NVIDIAEmbeddings
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader, JSONLoader
from langchain_community.vectorstores import FAISS
from langchain.tools.retriever import create_retriever_tool
from langchain_text_splitters import RecursiveCharacterTextSplitter

# Agno imports
from agno.agent import Agent
from agno.models.groq import GroqChat
from agno.models.google import GeminiChat
from agno.tools.tavily import TavilyTools
from agno.memory.agent import AgentMemory
from agno.storage.sqlite import SqliteStorage
from agno.memory.v2.db.sqlite import SqliteMemoryDb # Correct import for memory DB

load_dotenv()

# Rate limiter with exponential backoff
class PerformanceRateLimiter:
    def __init__(self, rpm: int, name: str):
        self.rpm = rpm
        self.name = name
        self.times: List[float] = []
        self.failures = 0

    def wait_if_needed(self):
        now = time.time()
        self.times = [t for t in self.times if now - t < 60]
        if len(self.times) >= self.rpm:
            wait = 60 - (now - self.times[0]) + random.uniform(1, 3)
            time.sleep(wait)
        if self.failures:
            backoff = min(2 ** self.failures, 30) + random.uniform(0.5, 1.5)
            time.sleep(backoff)
        self.times.append(now)

    def record_success(self):
        self.failures = 0

    def record_failure(self):
        self.failures += 1

# Initialize rate limiters
gemini_limiter = PerformanceRateLimiter(28, "Gemini")
groq_limiter   = PerformanceRateLimiter(28, "Groq")
nvidia_limiter = PerformanceRateLimiter(4,  "NVIDIA")

# Create Agno agents with corrected SQLite storage and memory
def create_agno_agents():
    # 1. Storage for the agent's overall state (conversations, etc.)
    storage = SqliteStorage(
        table_name="agent_sessions",
        db_file="tmp/agent_sessions.db",
        auto_upgrade_schema=True
    )
    # 2. A separate database for the agent's memory
    memory_db = SqliteMemoryDb(db_file="tmp/agent_memory.db")

    # 3. The AgentMemory object, which uses the memory_db
    agent_memory = AgentMemory(
        db=memory_db, # Pass the SqliteMemoryDb here
        create_user_memories=True,
        create_session_summary=True
    )

    math_agent = Agent(
        name="MathSpecialist",
        model=GroqChat(
            model="llama-3.3-70b-versatile",
            api_key=os.getenv("GROQ_API_KEY"),
            temperature=0
        ),
        description="Expert mathematical problem solver",
        instructions=[
            "Solve math problems with precision",
            "Show step-by-step calculations",
            "Finish with: FINAL ANSWER: [result]"
        ],
        storage=storage,       # Use SqliteStorage for the agent's persistence
        memory=agent_memory,   # Use the configured AgentMemory
        show_tool_calls=False,
        markdown=False
    )
    research_agent = Agent(
        name="ResearchSpecialist",
        model=GeminiChat(
            model="gemini-2.0-flash-lite",
            api_key=os.getenv("GOOGLE_API_KEY"),
            temperature=0
        ),
        description="Expert research and information gathering specialist",
        instructions=[
            "Conduct thorough research using available tools",
            "Synthesize information from multiple sources",
            "Finish with: FINAL ANSWER: [answer]"
        ],
        tools=[
            TavilyTools(
                api_key=os.getenv("TAVILY_API_KEY"),
                search=True,
                max_tokens=6000,
                search_depth="advanced",
                format="markdown"
            )
        ],
        storage=storage,       # Use the same storage for persistence
        memory=agent_memory,   # Use the same memory configuration
        show_tool_calls=False,
        markdown=False
    )
    return {"math": math_agent, "research": research_agent}

# LangGraph tools
@tool
def multiply(a: int, b: int) -> int:
    """Multiply two numbers."""
    return a * b

@tool
def add(a: int, b: int) -> int:
    """Add two numbers."""
    return a + b

@tool
def subtract(a: int, b: int) -> int:
    """Subtract two numbers."""
    return a - b

@tool
def divide(a: int, b: int) -> float:
    """Divide two numbers."""
    if b == 0:
        raise ValueError("Cannot divide by zero.")
    return a / b

@tool
def modulus(a: int, b: int) -> int:
    """Return the remainder of a divided by b."""
    return a % b

@tool
def optimized_web_search(query: str) -> str:
    """Optimized Tavily web search."""
    try:
        time.sleep(random.uniform(1, 2))
        docs = TavilySearchResults(max_results=2).invoke(query=query)
        return "\n\n---\n\n".join(
            f"<Doc url='{d.get('url','')}'>{d.get('content','')[:500]}</Doc>"
            for d in docs
        )
    except Exception as e:
        return f"Web search failed: {e}"

@tool
def optimized_wiki_search(query: str) -> str:
    """Optimized Wikipedia search."""
    try:
        time.sleep(random.uniform(0.5, 1))
        docs = WikipediaLoader(query=query, load_max_docs=1).load()
        return "\n\n---\n\n".join(
            f"<Doc src='{d.metadata['source']}'>{d.page_content[:800]}</Doc>"
            for d in docs
        )
    except Exception as e:
        return f"Wikipedia search failed: {e}"

# FAISS setup
def setup_faiss():
    try:
        schema = """
        { page_content: .Question, metadata: { task_id: .task_id, Final_answer: ."Final answer" } }
        """
        loader = JSONLoader(file_path="metadata.jsonl", jq_schema=schema, json_lines=True, text_content=False)
        docs = loader.load()
        splitter = RecursiveCharacterTextSplitter(chunk_size=256, chunk_overlap=50)
        chunks = splitter.split_documents(docs)
        embeds = NVIDIAEmbeddings(
            model="nvidia/nv-embedqa-e5-v5",
            api_key=os.getenv("NVIDIA_API_KEY")
        )
        return FAISS.from_documents(chunks, embeds)
    except Exception as e:
        print(f"FAISS setup failed: {e}")
        return None

class EnhancedAgentState(TypedDict):
    messages: Annotated[List[HumanMessage|AIMessage], operator.add]
    query: str
    agent_type: str
    final_answer: str
    perf: Dict[str,Any]
    agno_resp: str

class HybridLangGraphAgnoSystem:
    def __init__(self):
        self.agno = create_agno_agents()
        self.store = setup_faiss()
        self.tools = [
            multiply, add, subtract, divide, modulus,
            optimized_web_search, optimized_wiki_search
        ]
        if self.store:
            retr = self.store.as_retriever(search_type="similarity", search_kwargs={"k":2})
            self.tools.append(create_retriever_tool(
                retriever=retr,
                name="Question_Search",
                description="Retrieve similar questions"
            ))
        self.graph = self._build_graph()

    def _build_graph(self):
        groq_llm = ChatGroq(model="llama-3.3-70b-versatile", temperature=0)
        gemini_llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash-lite", temperature=0)
        nvidia_llm = ChatNVIDIA(model="meta/llama-3.1-70b-instruct", temperature=0)

        def router(st: EnhancedAgentState) -> EnhancedAgentState:
            q = st["query"].lower()
            if any(k in q for k in ["calculate","math"]):          t="lg_math"
            elif any(k in q for k in ["research","analyze"]):      t="agno_research"
            elif any(k in q for k in ["what is","who is"]):        t="lg_retrieval"
            else:                                                  t="agno_general"
            return {**st, "agent_type": t}

        def lg_math(st: EnhancedAgentState) -> EnhancedAgentState:
            groq_limiter.wait_if_needed()
            t0=time.time()
            llm=groq_llm.bind_tools([multiply,add,subtract,divide,modulus])
            sys=SystemMessage(content="Fast calculator. FINAL ANSWER: [result]")
            res=llm.invoke([sys,HumanMessage(content=st["query"])])
            return {**st, "final_answer":res.content, "perf":{"time":time.time()-t0,"prov":"LG-Groq"}}

        def agno_research(st: EnhancedAgentState) -> EnhancedAgentState:
            gemini_limiter.wait_if_needed()
            t0=time.time()
            resp=self.agno["research"].run(st["query"],stream=False)
            return {**st, "final_answer":resp, "perf":{"time":time.time()-t0,"prov":"Agno-Gemini"}}

        def lg_retrieval(st: EnhancedAgentState) -> EnhancedAgentState:
            groq_limiter.wait_if_needed()
            t0=time.time()
            llm=groq_llm.bind_tools(self.tools)
            sys=SystemMessage(content="Retrieve. FINAL ANSWER: [answer]")
            res=llm.invoke([sys,HumanMessage(content=st["query"])])
            return {**st, "final_answer":res.content, "perf":{"time":time.time()-t0,"prov":"LG-Retrieval"}}

        def agno_general(st: EnhancedAgentState) -> EnhancedAgentState:
            nvidia_limiter.wait_if_needed()
            t0=time.time()
            if any(k in st["query"].lower() for k in ["calculate","compute"]):
                resp=self.agno["math"].run(st["query"],stream=False)
            else:
                resp=self.agno["research"].run(st["query"],stream=False)
            return {**st, "final_answer":resp, "perf":{"time":time.time()-t0,"prov":"Agno-General"}}

        def pick(st: EnhancedAgentState) -> str:
            return st["agent_type"]

        g=StateGraph(EnhancedAgentState)
        g.add_node("router",router)
        g.add_node("lg_math",lg_math)
        g.add_node("agno_research",agno_research)
        g.add_node("lg_retrieval",lg_retrieval)
        g.add_node("agno_general",agno_general)
        g.set_entry_point("router")
        g.add_conditional_edges("router",pick,{
            "lg_math":"lg_math","agno_research":"agno_research",
            "lg_retrieval":"lg_retrieval","agno_general":"agno_general"
        })
        for n in ["lg_math","agno_research","lg_retrieval","agno_general"]:
            g.add_edge(n,"END")
        return g.compile(checkpointer=MemorySaver())

    def process_query(self, q: str) -> Dict[str,Any]:
        state={
            "messages":[HumanMessage(content=q)],
            "query":q,"agent_type":"","final_answer":"",
            "perf":{},"agno_resp":""
        }
        cfg={"configurable":{"thread_id":f"hyb_{hash(q)}"}}
        try:
            out=self.graph.invoke(state,cfg)
            return {
                "answer":out["final_answer"],
                "performance_metrics":out["perf"],
                "provider_used":out["perf"].get("prov")
            }
        except Exception as e:
            return {"answer":f"Error: {e}","performance_metrics":{},"provider_used":"Error"}

def build_graph(provider: str = "hybrid"):
    """
    Build and return the StateGraph for the requested provider.
    - "hybrid", "groq", "google", and "nvidia" are all valid and
      will return the full HybridLangGraphAgnoSystem graph.
    """
    if provider in ("hybrid", "groq", "google", "nvidia"):
        return HybridLangGraphAgnoSystem().graph
    else:
        raise ValueError(f"Unsupported provider: '{provider}'. Please use 'hybrid', 'groq', 'google', or 'nvidia'.")

# Test
if __name__=="__main__":
    graph=build_graph()
    msgs=[HumanMessage(content="What are the names of the US presidents who were assassinated?")]
    res=graph.invoke({"messages":msgs},{"configurable":{"thread_id":"test"}})
    for m in res["messages"]:
        m.pretty_print()