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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
"""LLM integration with support for Groq API and local Ollama."""
from typing import List, Dict, Optional, AsyncIterator
import time
from groq import Groq
import asyncio
from datetime import datetime, timedelta
from collections import deque
import os
import requests
import json


class RateLimiter:
    """Rate limiter for API calls to respect RPM limits."""
    
    def __init__(self, max_requests_per_minute: int = 30):
        """Initialize rate limiter.
        
        Args:
            max_requests_per_minute: Maximum requests allowed per minute (default: 30)
        """
        self.max_requests = max_requests_per_minute
        self.request_times = deque()
        self.lock = asyncio.Lock()
        # Calculate minimum delay between requests to avoid hitting limit
        # E.g., 30 RPM = 2.0 seconds minimum delay, but we add safety margin
        self.min_interval = 60.0 / max_requests_per_minute
    
    async def acquire(self):
        """Acquire permission to make a request (async version)."""
        async with self.lock:
            now = datetime.now()
            
            # Remove requests older than 1 minute
            while self.request_times and (now - self.request_times[0]) > timedelta(minutes=1):
                self.request_times.popleft()
            
            # If at limit, wait
            if len(self.request_times) >= self.max_requests:
                # Calculate how long to wait
                oldest_request = self.request_times[0]
                wait_time = 60 - (now - oldest_request).total_seconds()
                
                if wait_time > 0:
                    print(f"[RATE LIMIT] At {self.max_requests} RPM limit. Waiting {wait_time:.2f}s...")
                    await asyncio.sleep(wait_time)
                    # Recursive call after waiting
                    return await self.acquire()
            
            # Record this request
            self.request_times.append(now)
    
    def acquire_sync(self):
        """Synchronous version of acquire (for blocking code)."""
        now = datetime.now()
        
        # Remove requests older than 1 minute
        while self.request_times and (now - self.request_times[0]) > timedelta(minutes=1):
            self.request_times.popleft()
        
        # Calculate current request rate
        current_rpm = len(self.request_times)
        
        # If at limit, wait
        if len(self.request_times) >= self.max_requests:
            oldest_request = self.request_times[0]
            wait_time = 60 - (now - oldest_request).total_seconds()
            
            if wait_time > 0:
                print(f"[RATE LIMIT] At {self.max_requests} RPM limit. Waiting {wait_time:.2f}s before next request...")
                time.sleep(wait_time)
                return self.acquire_sync()
        
        # Record this request
        self.request_times.append(now)
        
        # Log current rate
        if current_rpm > 0:
            print(f"[RATE LIMIT] Current: {current_rpm} requests in last minute (Limit: {self.max_requests} RPM)")


class GroqLLMClient:
    """Client for Groq LLM API with rate limiting and API key rotation."""
    
    def __init__(
        self,
        api_key: str,
        model_name: str = "llama-3.1-8b-instant",
        max_rpm: int = 30,
        rate_limit_delay: float = 2.0,
        api_keys: list = None,
        max_retries: int = 3,
        retry_delay: float = 60.0
    ):
        """Initialize Groq client with optional API key rotation.
        
        Args:
            api_key: Primary Groq API key
            model_name: Name of the LLM model
            max_rpm: Maximum requests per minute
            rate_limit_delay: Additional delay between requests (seconds)
            api_keys: List of API keys for rotation (optional)
            max_retries: Maximum retries on rate limit errors
            retry_delay: Delay before retry on rate limit error
        """
        # Setup API key rotation
        self.api_keys = api_keys if api_keys else [api_key]
        self.current_key_index = 0
        self.api_key = self.api_keys[self.current_key_index]
        
        self.client = Groq(api_key=self.api_key)
        self.model_name = model_name
        self.rate_limiter = RateLimiter(max_rpm)
        self.rate_limit_delay = rate_limit_delay
        self.max_retries = max_retries
        self.retry_delay = retry_delay
        
        # Track requests per key for smart rotation
        self.requests_per_key = {key: 0 for key in self.api_keys}
        
        # Available models
        self.available_models = [
            "meta-llama/llama-4-maverick-17b-128e-instruct",
            "llama-3.1-8b-instant",
            "openai/gpt-oss-120b"
        ]
        
        if len(self.api_keys) > 1:
            print(f"[API KEYS] Initialized with {len(self.api_keys)} API keys for rotation")
    
    def rotate_api_key(self):
        """Rotate to the next API key."""
        if len(self.api_keys) <= 1:
            return False
        
        self.current_key_index = (self.current_key_index + 1) % len(self.api_keys)
        self.api_key = self.api_keys[self.current_key_index]
        self.client = Groq(api_key=self.api_key)
        print(f"[API KEYS] Rotated to API key {self.current_key_index + 1}/{len(self.api_keys)}")
        return True
    
    def set_model(self, model_name: str):
        """Set the LLM model.
        
        Args:
            model_name: Name of the model
        """
        if model_name not in self.available_models:
            print(f"Warning: {model_name} not in available models. Using anyway...")
        self.model_name = model_name
    
    def generate(
        self,
        prompt: str,
        max_tokens: int = 1024,
        temperature: float = 0.7,
        system_prompt: Optional[str] = None
    ) -> str:
        """Generate text using Groq LLM with rate limiting and retry logic.
        
        Args:
            prompt: Input prompt
            max_tokens: Maximum tokens to generate
            temperature: Sampling temperature
            system_prompt: System prompt
            
        Returns:
            Generated text
        """
        # Apply rate limiting to respect 30 RPM limit
        print(f"[RATE LIMIT] Applying rate limiting (RPM limit: {self.rate_limiter.max_requests}, delay: {self.rate_limit_delay}s)")
        self.rate_limiter.acquire_sync()
        
        # Prepare messages
        messages = []
        if system_prompt:
            messages.append({
                "role": "system",
                "content": system_prompt
            })
        messages.append({
            "role": "user",
            "content": prompt
        })
        
        # Retry logic with API key rotation
        last_error = None
        for attempt in range(self.max_retries):
            try:
                # Make API call
                response = self.client.chat.completions.create(
                    model=self.model_name,
                    messages=messages,
                    max_tokens=max_tokens,
                    temperature=temperature
                )
                
                # Track successful request
                self.requests_per_key[self.api_key] = self.requests_per_key.get(self.api_key, 0) + 1
                
                # Add additional delay for safety margin below RPM limit
                print(f"[RATE LIMIT] Adding safety delay: {self.rate_limit_delay}s")
                time.sleep(self.rate_limit_delay)
                
                return response.choices[0].message.content
                
            except Exception as e:
                last_error = e
                error_str = str(e).lower()
                
                # Check if it's a rate limit error
                if "rate" in error_str or "limit" in error_str or "429" in error_str or "quota" in error_str:
                    print(f"[RATE LIMIT ERROR] Hit rate limit on attempt {attempt + 1}/{self.max_retries}")
                    
                    # Try rotating to another API key
                    if self.rotate_api_key():
                        print(f"[API KEYS] Trying with different API key...")
                        continue
                    
                    # If no more keys or rotation failed, wait and retry
                    if attempt < self.max_retries - 1:
                        print(f"[RATE LIMIT] Waiting {self.retry_delay}s before retry...")
                        time.sleep(self.retry_delay)
                        continue
                else:
                    # Non-rate-limit error
                    print(f"[ERROR] API error: {str(e)}")
                    break
        
        print(f"[ERROR] Failed after {self.max_retries} attempts: {str(last_error)}")
        return f"Error: {str(last_error)}"
    
    async def generate_async(
        self,
        prompt: str,
        max_tokens: int = 1024,
        temperature: float = 0.7,
        system_prompt: Optional[str] = None
    ) -> str:
        """Asynchronous version of generate.
        
        Args:
            prompt: Input prompt
            max_tokens: Maximum tokens to generate
            temperature: Sampling temperature
            system_prompt: System prompt
            
        Returns:
            Generated text
        """
        # Apply rate limiting
        await self.rate_limiter.acquire()
        
        # Prepare messages
        messages = []
        if system_prompt:
            messages.append({
                "role": "system",
                "content": system_prompt
            })
        messages.append({
            "role": "user",
            "content": prompt
        })
        
        try:
            # Make API call (synchronous client used in async context)
            response = self.client.chat.completions.create(
                model=self.model_name,
                messages=messages,
                max_tokens=max_tokens,
                temperature=temperature
            )
            
            # Add delay
            await asyncio.sleep(self.rate_limit_delay)
            
            return response.choices[0].message.content
            
        except Exception as e:
            print(f"Error generating response: {str(e)}")
            return f"Error: {str(e)}"
    
    def generate_with_context(
        self,
        query: str,
        context_documents: List[str],
        max_tokens: int = 1024,
        temperature: float = 0.7
    ) -> str:
        """Generate response with retrieved context.
        
        Args:
            query: User query
            context_documents: List of retrieved documents
            max_tokens: Maximum tokens to generate
            temperature: Sampling temperature
            
        Returns:
            Generated response
        """
        # Build context
        context = "\n\n".join([
            f"Document {i+1}: {doc}"
            for i, doc in enumerate(context_documents)
        ])
        
        # Build prompt
        prompt = f"""Answer the following question based on the provided context.

Context:
{context}

Question: {query}

Answer:"""
        
        system_prompt = "You are a helpful AI assistant. Answer questions based on the provided context. If the answer is not in the context, say so."
        
        return self.generate(prompt, max_tokens, temperature, system_prompt)
    
    def batch_generate(
        self,
        prompts: List[str],
        max_tokens: int = 1024,
        temperature: float = 0.7,
        system_prompt: Optional[str] = None
    ) -> List[str]:
        """Generate responses for multiple prompts.
        
        Args:
            prompts: List of prompts
            max_tokens: Maximum tokens to generate
            temperature: Sampling temperature
            system_prompt: System prompt
            
        Returns:
            List of generated responses
        """
        responses = []
        for i, prompt in enumerate(prompts):
            print(f"Processing prompt {i+1}/{len(prompts)}")
            response = self.generate(prompt, max_tokens, temperature, system_prompt)
            responses.append(response)
        
        return responses


class OllamaLLMClient:
    """Client for local Ollama LLM - no rate limits, unlimited usage."""
    
    def __init__(
        self,
        host: str = "http://localhost:11434",
        model_name: str = "gemma3:12b"
    ):
        """Initialize Ollama client.
        
        Args:
            host: Ollama server URL (default: http://localhost:11434)
            model_name: Name of the model (e.g., gemma3:12b, llama3.3)
        """
        self.host = host.rstrip("/")
        self.model_name = model_name
        
        # Available models for Ollama
        self.available_models = [
            "gemma3:12b",
            "llama3.3"
        ]
        
        print(f"[OLLAMA] Initialized with model: {model_name} at {host}")
    
    def check_connection(self) -> bool:
        """Check if Ollama server is running.
        
        Returns:
            True if connected, False otherwise
        """
        try:
            response = requests.get(f"{self.host}/api/tags", timeout=5)
            return response.status_code == 200
        except Exception as e:
            print(f"[OLLAMA] Connection error: {e}")
            return False
    
    def list_models(self) -> List[str]:
        """List available models on Ollama server.
        
        Returns:
            List of model names
        """
        try:
            response = requests.get(f"{self.host}/api/tags", timeout=10)
            if response.status_code == 200:
                data = response.json()
                return [model["name"] for model in data.get("models", [])]
            return []
        except Exception as e:
            print(f"[OLLAMA] Error listing models: {e}")
            return []
    
    def set_model(self, model_name: str):
        """Set the LLM model.
        
        Args:
            model_name: Name of the model
        """
        self.model_name = model_name
        print(f"[OLLAMA] Model set to: {model_name}")
    
    def generate(
        self,
        prompt: str,
        max_tokens: int = 1024,
        temperature: float = 0.7,
        system_prompt: Optional[str] = None
    ) -> str:
        """Generate text using local Ollama LLM.
        
        Args:
            prompt: Input prompt
            max_tokens: Maximum tokens to generate
            temperature: Sampling temperature
            system_prompt: System prompt
            
        Returns:
            Generated text
        """
        print(f"[OLLAMA] Generating with model: {self.model_name} (no rate limits)")
        
        # Build full prompt with system prompt
        full_prompt = prompt
        if system_prompt:
            full_prompt = f"{system_prompt}\n\n{prompt}"
        
        try:
            # Make API call to Ollama
            response = requests.post(
                f"{self.host}/api/generate",
                json={
                    "model": self.model_name,
                    "prompt": full_prompt,
                    "options": {
                        "num_predict": max_tokens,
                        "temperature": temperature
                    },
                    "stream": False
                },
                timeout=600  # Longer timeout for local inference
            )
            
            if response.status_code == 200:
                data = response.json()
                return data.get("response", "")
            else:
                error_msg = f"Ollama API error: {response.status_code} - {response.text}"
                print(f"[OLLAMA ERROR] {error_msg}")
                return f"Error: {error_msg}"
                
        except requests.exceptions.ConnectionError:
            error_msg = f"Cannot connect to Ollama at {self.host}. Is Ollama running?"
            print(f"[OLLAMA ERROR] {error_msg}")
            return f"Error: {error_msg}"
        except Exception as e:
            error_msg = f"Ollama error: {str(e)}"
            print(f"[OLLAMA ERROR] {error_msg}")
            return f"Error: {error_msg}"
    
    def generate_with_context(
        self,
        query: str,
        context_documents: List[str],
        max_tokens: int = 1024,
        temperature: float = 0.7
    ) -> str:
        """Generate response with retrieved context.
        
        Args:
            query: User query
            context_documents: List of retrieved documents
            max_tokens: Maximum tokens to generate
            temperature: Sampling temperature
            
        Returns:
            Generated response
        """
        # Build context
        context = "\n\n".join([
            f"Document {i+1}: {doc}"
            for i, doc in enumerate(context_documents)
        ])
        
        # Build prompt
        prompt = f"""Answer the following question based on the provided context.

Context:
{context}

Question: {query}

Answer:"""
        
        system_prompt = "You are a helpful AI assistant. Answer questions based on the provided context. If the answer is not in the context, say so."
        
        return self.generate(prompt, max_tokens, temperature, system_prompt)
    
    def batch_generate(
        self,
        prompts: List[str],
        max_tokens: int = 1024,
        temperature: float = 0.7,
        system_prompt: Optional[str] = None
    ) -> List[str]:
        """Generate responses for multiple prompts.
        
        Args:
            prompts: List of prompts
            max_tokens: Maximum tokens to generate
            temperature: Sampling temperature
            system_prompt: System prompt
            
        Returns:
            List of generated responses
        """
        responses = []
        for i, prompt in enumerate(prompts):
            print(f"[OLLAMA] Processing prompt {i+1}/{len(prompts)}")
            response = self.generate(prompt, max_tokens, temperature, system_prompt)
            responses.append(response)
        
        return responses


def create_llm_client(
    provider: str = "groq",
    api_key: str = "",
    api_keys: list = None,
    model_name: str = None,
    ollama_host: str = "http://localhost:11434",
    max_rpm: int = 30,
    rate_limit_delay: float = 2.0,
    max_retries: int = 3,
    retry_delay: float = 60.0
):
    """Factory function to create LLM client based on provider.
    
    Args:
        provider: "groq" or "ollama"
        api_key: Groq API key (for groq provider)
        api_keys: List of Groq API keys for rotation
        model_name: Model name (auto-detected if not provided)
        ollama_host: Ollama server URL
        max_rpm: Maximum requests per minute (for groq)
        rate_limit_delay: Delay between requests (for groq)
        max_retries: Max retries on error (for groq)
        retry_delay: Delay before retry (for groq)
        
    Returns:
        LLM client instance (GroqLLMClient or OllamaLLMClient)
    """
    if provider.lower() == "ollama":
        if model_name is None:
            model_name = "gemma3:12b"
        print(f"[LLM FACTORY] Creating Ollama client with model: {model_name}")
        return OllamaLLMClient(host=ollama_host, model_name=model_name)
    else:
        if model_name is None:
            model_name = "llama-3.1-8b-instant"
        print(f"[LLM FACTORY] Creating Groq client with model: {model_name}")
        return GroqLLMClient(
            api_key=api_key,
            model_name=model_name,
            max_rpm=max_rpm,
            rate_limit_delay=rate_limit_delay,
            api_keys=api_keys,
            max_retries=max_retries,
            retry_delay=retry_delay
        )


class RAGPipeline:
    """Complete RAG pipeline with LLM and vector store."""
    
    def __init__(
        self,
        llm_client,
        vector_store_manager
    ):
        """Initialize RAG pipeline.
        
        Args:
            llm_client: LLM client (GroqLLMClient or OllamaLLMClient)
            vector_store_manager: ChromaDB manager
        """
        self.llm = llm_client
        self.vector_store = vector_store_manager
        self.chat_history = []
    
    def query(
        self,
        query: str,
        n_results: int = 5,
        max_tokens: int = 1024,
        temperature: float = 0.7
    ) -> Dict:
        """Query the RAG system.
        
        Args:
            query: User query
            n_results: Number of documents to retrieve
            max_tokens: Maximum tokens to generate
            temperature: Sampling temperature
            
        Returns:
            Dictionary with response and retrieved documents
        """
        # Retrieve documents
        retrieved_docs = self.vector_store.get_retrieved_documents(query, n_results)
        
        # Extract document texts
        doc_texts = [doc["document"] for doc in retrieved_docs]
        
        # Generate response
        response = self.llm.generate_with_context(
            query,
            doc_texts,
            max_tokens,
            temperature
        )
        
        # Store in chat history
        self.chat_history.append({
            "query": query,
            "response": response,
            "retrieved_docs": retrieved_docs,
            "timestamp": datetime.now().isoformat()
        })
        
        return {
            "query": query,
            "response": response,
            "retrieved_documents": retrieved_docs
        }
    
    def get_chat_history(self) -> List[Dict]:
        """Get chat history.
        
        Returns:
            List of chat history entries
        """
        return self.chat_history
    
    def clear_history(self):
        """Clear chat history."""
        self.chat_history = []