File size: 11,212 Bytes
c99f4b7
139eea1
c99f4b7
 
 
 
139eea1
 
 
 
 
 
 
c99f4b7
139eea1
c99f4b7
 
139eea1
 
 
377bc4d
c99f4b7
 
 
139eea1
c99f4b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139eea1
c99f4b7
 
 
5bb3c4f
c99f4b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139eea1
c99f4b7
139eea1
 
c99f4b7
139eea1
 
 
 
c99f4b7
139eea1
 
 
 
c99f4b7
139eea1
 
 
c99f4b7
 
139eea1
 
 
 
 
 
c99f4b7
139eea1
 
 
 
c99f4b7
 
 
 
 
 
 
 
 
 
 
139eea1
 
 
c99f4b7
 
 
 
 
 
 
 
 
 
 
139eea1
 
c99f4b7
 
 
 
 
 
 
 
 
 
 
 
139eea1
c99f4b7
 
3afa28b
 
 
377bc4d
139eea1
c99f4b7
139eea1
 
 
 
 
 
 
 
 
 
c99f4b7
139eea1
 
5bb3c4f
c99f4b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139eea1
 
c99f4b7
 
139eea1
 
 
 
 
c99f4b7
 
 
 
 
 
 
139eea1
c99f4b7
139eea1
 
 
 
c99f4b7
139eea1
 
 
 
 
 
c99f4b7
 
 
 
 
 
 
 
 
 
 
 
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
"""LangGraph Agent with Direct Groq API and Custom Rate Limiting"""
import os
import time
import threading
from collections import deque
from typing import Dict, Any, List
from dotenv import load_dotenv
from langgraph.graph import START, StateGraph, MessagesState
from langgraph.prebuilt import tools_condition
from langgraph.prebuilt import ToolNode
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_community.document_loaders import WikipediaLoader
from langchain_community.document_loaders import ArxivLoader
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
from langchain_core.tools import tool
from groq import Groq, RateLimitError
import logging

load_dotenv()


# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class GroqRateLimiter:
    """Thread-safe rate limiter for direct Groq API calls"""
    
    def __init__(self, rpm: int = 20, tpm: int = 6000):
        self.rpm = rpm  # Requests per minute
        self.tpm = tpm  # Tokens per minute
        self.request_times = deque()
        self.token_usage = deque()  # (timestamp, token_count) tuples
        self.lock = threading.Lock()
        
    def _clean_old_records(self, current_time: float):
        """Remove records older than 1 minute"""
        minute_ago = current_time - 60
        
        while self.request_times and self.request_times[0] <= minute_ago:
            self.request_times.popleft()
            
        while self.token_usage and self.token_usage[0][0] <= minute_ago:
            self.token_usage.popleft()
    
    def can_make_request(self, estimated_tokens: int = 1000) -> tuple[bool, float]:
        """Check if request can be made, return (can_proceed, wait_time)"""
        with self.lock:
            current_time = time.time()
            self._clean_old_records(current_time)
            
            wait_time = 0
            
            # Check RPM limit
            if len(self.request_times) >= self.rpm:
                oldest_request = self.request_times[0]
                wait_time = max(wait_time, 60 - (current_time - oldest_request))
            
            # Check TPM limit
            current_tokens = sum(tokens for _, tokens in self.token_usage)
            if current_tokens + estimated_tokens > self.tpm:
                if self.token_usage:
                    oldest_token_time = self.token_usage[0][0]
                    wait_time = max(wait_time, 60 - (current_time - oldest_token_time))
            
            return wait_time <= 0, wait_time
    
    def record_request(self, token_count: int):
        """Record a successful request"""
        with self.lock:
            current_time = time.time()
            self.request_times.append(current_time)
            self.token_usage.append((current_time, token_count))

class GroqWrapper:
    """Wrapper for direct Groq API with rate limiting and error handling"""
    
    def __init__(self, model: str = "qwen/qwen3-32b", 
                 rpm: int = 30, tpm: int = 6000):
        self.client = Groq(api_key=os.getenv("GROQ_API_KEY"))
        self.model = model
        self.rate_limiter = GroqRateLimiter(rpm=rpm, tpm=tpm)
        
    def estimate_tokens(self, messages: List[Dict]) -> int:
        """Rough token estimation (4 chars ≈ 1 token)"""
        total_chars = sum(len(str(msg.get('content', ''))) for msg in messages)
        return max(total_chars // 4, 100)
    
    def invoke(self, messages: List[Dict], **kwargs) -> Dict:
        """Invoke Groq API with rate limiting and retry logic"""
        # Convert LangChain messages to Groq format if needed
        groq_messages = []
        for msg in messages:
            if hasattr(msg, 'content') and hasattr(msg, 'type'):
                # LangChain message object
                role = "user" if msg.type == "human" else "assistant" if msg.type == "ai" else "system"
                groq_messages.append({"role": role, "content": str(msg.content)})
            else:
                # Already in dict format
                groq_messages.append(msg)
        
        estimated_tokens = self.estimate_tokens(groq_messages)
        
        max_retries = 3
        for attempt in range(max_retries):
            try:
                # Check rate limits
                can_proceed, wait_time = self.rate_limiter.can_make_request(estimated_tokens)
                if not can_proceed:
                    logger.info(f"Rate limit: waiting {wait_time:.2f} seconds")
                    time.sleep(wait_time)
                
                # Make the API call
                response = self.client.chat.completions.create(
                    model=self.model,
                    messages=groq_messages,
                    **kwargs
                )
                
                # Record successful request
                actual_tokens = response.usage.total_tokens if hasattr(response, 'usage') else estimated_tokens
                self.rate_limiter.record_request(actual_tokens)
                
                # Convert back to LangChain format
                content = response.choices[0].message.content
                return AIMessage(content=content)
                
            except RateLimitError as e:
                if attempt == max_retries - 1:
                    raise e
                
                # Use retry-after header if available
                retry_after = getattr(e.response, 'headers', {}).get('retry-after')
                if retry_after:
                    delay = float(retry_after)
                else:
                    delay = 2 ** attempt  # Exponential backoff
                
                logger.warning(f"Rate limited. Retrying in {delay} seconds (attempt {attempt + 1})")
                time.sleep(delay)
                
            except Exception as e:
                logger.error(f"Groq API error: {e}")
                if attempt == max_retries - 1:
                    raise e
                time.sleep(2 ** attempt)
        
        raise Exception("Max retries exceeded")
    
    def bind_tools(self, tools):
        """Mock bind_tools method for compatibility"""
        self.tools = tools
        return self

# Your existing 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: float, b: float) -> float:
    """Divide two numbers."""
    if b == 0:
        raise ValueError("Cannot divide by zero.")
    return a / b

@tool
def modulus(a: int, b: int) -> int:
    """Get the modulus of two numbers."""
    return a % b

@tool
def wiki_search(query: str) -> str:
    """Search Wikipedia for a query and return maximum 2 results."""
    try:
        search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
        formatted_search_docs = "\n\n---\n\n".join(
            [
                f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
                for doc in search_docs
            ])
        return {"wiki_results": formatted_search_docs}
    except Exception as e:
        return {"wiki_results": f"Error: {str(e)}"}

@tool
def web_search(query: str) -> str:
    """Search Tavily for a query and return maximum 3 results."""
    try:
        search_docs = TavilySearchResults(max_results=3).invoke(query=query)
        formatted_search_docs = "\n\n---\n\n".join(
            [
                f'<Document source="{doc.get("url", "")}">\n{doc.get("content", "")}\n</Document>'
                for doc in search_docs
            ])
        return {"web_results": formatted_search_docs}
    except Exception as e:
        return {"web_results": f"Error: {str(e)}"}

@tool
def arxiv_search(query: str) -> str:
    """Search Arxiv for a query and return maximum 3 results."""
    try:
        search_docs = ArxivLoader(query=query, load_max_docs=3).load()
        formatted_search_docs = "\n\n---\n\n".join(
            [
                f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
                for doc in search_docs
            ])
        return {"arxiv_results": formatted_search_docs}
    except Exception as e:
        return {"arxiv_results": f"Error: {str(e)}"}

def load_system_prompt():
    """Load system prompt with error handling"""

    with open("system_prompt.txt", "r", encoding="utf-8") as f:
        return f.read()


system_prompt = load_system_prompt()
sys_msg = SystemMessage(content=system_prompt)

tools = [
    multiply,
    add,
    subtract,
    divide,
    modulus,
    wiki_search,
    web_search,
    arxiv_search,
]

def build_graph(provider: str = "direct_groq", model: str = "qwen/qwen3-32b"):
    """Build the graph with direct Groq API and custom rate limiting"""
    
    if provider == "direct_groq":
        # Use custom Groq wrapper with rate limiting
        llm = GroqWrapper(model=model, rpm=30, tpm=6000)  # Adjust based on your plan
        
    elif provider == "langchain_groq":
        # Use LangChain's ChatGroq with native rate limiting
        from langchain_core.rate_limiters import InMemoryRateLimiter
        
        rate_limiter = InMemoryRateLimiter(
            requests_per_second=0.5,  # 30 RPM
            check_every_n_seconds=0.1,
            max_bucket_size=5,
        )
        
        from langchain_groq import ChatGroq
        llm = ChatGroq(
            model=model,
            temperature=0,
            groq_api_key=os.getenv("GROQ_API_KEY"),
            rate_limiter=rate_limiter
        )
    else:
        raise ValueError("Choose 'direct_groq' or 'langchain_groq'")
    
    # Bind tools to LLM
    llm_with_tools = llm.bind_tools(tools)

    def assistant(state: MessagesState):
        """Assistant node"""
        try:
            response = llm_with_tools.invoke(state["messages"])
            return {"messages": [response]}
        except Exception as e:
            logger.error(f"Assistant failed: {e}")
            error_msg = AIMessage(content=f"I encountered an error: {str(e)}")
            return {"messages": [error_msg]}

    # Build the graph
    builder = StateGraph(MessagesState)
    builder.add_node("assistant", assistant)
    builder.add_node("tools", ToolNode(tools))
    builder.add_edge(START, "assistant")
    builder.add_conditional_edges("assistant", tools_condition)
    builder.add_edge("tools", "assistant")

    return builder.compile()

if __name__ == "__main__":
    question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
    
    try:
        # Test with direct Groq API
        graph = build_graph(provider="direct_groq")
        messages = [HumanMessage(content=question)]
        result = graph.invoke({"messages": messages})
        
        for m in result["messages"]:
            m.pretty_print()
            
    except Exception as e:
        logger.error(f"Test failed: {e}")