File size: 19,730 Bytes
1d10b0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Evaluation: LLM Calling & Query Processing Flow

## High-Level Overview

```
EVALUATION PROCESS:
β”‚
β”œβ”€ Load Test Data from Dataset
β”‚  └─ Questions + Ground Truth Answers
β”‚
β”œβ”€ FOR EACH TEST QUESTION:
β”‚  β”‚
β”‚  β”œβ”€ 1. RETRIEVE DOCUMENTS (Vector Search)
β”‚  β”‚   β”‚
β”‚  β”‚   └─ query_text β†’ embed_query() β†’ semantic search β†’ get_retrieved_documents()
β”‚  β”‚
β”‚  β”œβ”€ 2. GENERATE RESPONSE (LLM Call)
β”‚  β”‚   β”‚
β”‚  β”‚   └─ query + documents β†’ LLM β†’ response
β”‚  β”‚
β”‚  └─ 3. STORE TEST CASE (For Evaluation)
β”‚      └─ {query, response, documents, ground_truth}
β”‚
β”œβ”€ COMPUTE TRACe METRICS
β”‚  └─ utilization, relevance, adherence, completeness
β”‚
└─ DISPLAY RESULTS
```

---

## Detailed Flow: Query Processing in Evaluation

### **Step 1: Test Sample Loop** (streamlit_app.py, Line 723)

```python
for i, sample in enumerate(test_data):
    # sample = {"question": "...", "answer": "...", ...}
    
    # Step 2: Call RAG pipeline with the question
    result = st.session_state.rag_pipeline.query(
        sample["question"],     # ← Query string
        n_results=5             # ← How many docs to retrieve
    )
```

**Input**: 
- `sample["question"]` = User question from RAGBench dataset
  - Example: "What is machine learning?"
- `n_results=5` = Retrieve top 5 most similar documents

---

### **Step 2: RAG Pipeline Query** (llm_client.py, Line 295)

```python
class RAGPipeline:
    def query(self, query: str, n_results: int = 5) -> Dict:
        
        # β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        # β”‚ PHASE 1: RETRIEVAL (Vector Search)                  β”‚
        # β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        
        # STEP 1: Call vector store to retrieve documents
        retrieved_docs = self.vector_store.get_retrieved_documents(
            query,        # "What is machine learning?"
            n_results=5   # Top 5 documents
        )
        # Result: [
        #   {"document": "ML is...", "metadata": {...}, "distance": 0.12},
        #   {"document": "Machine learning uses...", "metadata": {...}, "distance": 0.15},
        #   ...
        # ]
        
        # Extract document texts
        doc_texts = [doc["document"] for doc in retrieved_docs]
        # doc_texts = ["ML is...", "Machine learning uses...", ...]
        
        # β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        # β”‚ PHASE 2: GENERATION (LLM Call)                      β”‚
        # β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        
        # STEP 2: Call LLM with query + retrieved documents
        response = self.llm.generate_with_context(
            query,          # "What is machine learning?"
            doc_texts,      # ["ML is...", "Machine learning uses...", ...]
            max_tokens=1024,
            temperature=0.7
        )
        # response = "Machine learning is a subset of artificial intelligence..."
        
        # STEP 3: Package results
        return {
            "query": query,
            "response": response,
            "retrieved_documents": retrieved_docs
        }
```

---

### **Step 3A: Document Retrieval (Vector Store)** (vector_store.py, Line 321)

```
Query Processing:

USER QUESTION:
"What is machine learning?"
        β”‚
        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 1. Embed the Query                  β”‚
β”‚ ──────────────────────────────────  β”‚
β”‚ embedding_model.embed_query(query)  β”‚
β”‚                                     β”‚
β”‚ Model: sentence-transformers/       β”‚
β”‚        all-mpnet-base-v2            β”‚
β”‚                                     β”‚
β”‚ Query String (tokens):              β”‚
β”‚   "What" β†’ [0.1, 0.2, ...]        β”‚
β”‚   "is" β†’ [0.3, 0.4, ...]          β”‚
β”‚   "machine" β†’ [0.5, 0.6, ...]     β”‚
β”‚   "learning" β†’ [0.7, 0.8, ...]    β”‚
β”‚                                     β”‚
β”‚ Output: Query Vector [768-dim]      β”‚
β”‚         ↓                           β”‚
β”‚   [0.15, 0.32, 0.51, ..., 0.89]   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚
        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 2. Semantic Search in ChromaDB           β”‚
β”‚ ──────────────────────────────────────── β”‚
β”‚                                          β”‚
β”‚ collection.query(                        β”‚
β”‚     query_embeddings=[query_vector],     β”‚
β”‚     n_results=5,                         β”‚
β”‚     where=None                           β”‚
β”‚ )                                        β”‚
β”‚                                          β”‚
β”‚ Compare query_vector against all doc     β”‚
β”‚ vectors in the collection using          β”‚
β”‚ cosine similarity                        β”‚
β”‚                                          β”‚
β”‚ Scoring: similarity = dot_product/       β”‚
β”‚          (norm_a * norm_b)               β”‚
β”‚                                          β”‚
β”‚ Top 5 Results (sorted by similarity):    β”‚
β”‚  β€’ Doc 1: "ML is a field..." (sim: 0.92) β”‚
β”‚  β€’ Doc 2: "Deep learning..." (sim: 0.89) β”‚
β”‚  β€’ Doc 3: "Neural networks..." (sim: 0.87)
β”‚  β€’ Doc 4: "AI overview..." (sim: 0.81)   β”‚
β”‚  β€’ Doc 5: "Training data..." (sim: 0.78) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚
        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 3. Format Retrieved Documents       β”‚
β”‚ ──────────────────────────────────  β”‚
β”‚ retrieved_docs = [                  β”‚
β”‚   {                                 β”‚
β”‚     "document": "ML is a field...",β”‚
β”‚     "metadata": {...},              β”‚
β”‚     "distance": 0.08                β”‚
β”‚   },                                β”‚
β”‚   {...},                            β”‚
β”‚   ...                               β”‚
β”‚ ]                                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚
        β–Ό
   RETURNED TO RAGPipeline
```

---

### **Step 3B: LLM Response Generation** (llm_client.py, Line 215)

```
Retrieved Documents:
β”‚
β”œβ”€ Doc1: "ML is a field of AI that..."
β”œβ”€ Doc2: "Machine learning uses algorithms..."
β”œβ”€ Doc3: "Neural networks process data..."
β”œβ”€ Doc4: "Training data is essential..."
└─ Doc5: "Deep learning is a subset..."
        β”‚
        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 1. BUILD PROMPT                                        β”‚
β”‚ ────────────────────────────────────────────────────── β”‚
β”‚                                                        β”‚
β”‚ context = """                                          β”‚
β”‚   Document 1: ML is a field of AI that...             β”‚
β”‚   Document 2: Machine learning uses algorithms...     β”‚
β”‚   Document 3: Neural networks process data...         β”‚
β”‚   Document 4: Training data is essential...           β”‚
β”‚   Document 5: Deep learning is a subset...            β”‚
β”‚ """                                                    β”‚
β”‚                                                        β”‚
β”‚ prompt = """                                           β”‚
β”‚ Answer the following question based on the provided   β”‚
β”‚ context.                                              β”‚
β”‚                                                        β”‚
β”‚ Context:                                              β”‚
β”‚ {context}                                             β”‚
β”‚                                                        β”‚
β”‚ Question: What is machine learning?                   β”‚
β”‚                                                        β”‚
β”‚ Answer:                                               β”‚
β”‚ """                                                    β”‚
β”‚                                                        β”‚
β”‚ system_prompt = "You are a helpful AI assistant..."  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚
        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 2. LLM API CALL (Groq)                                 β”‚
β”‚ ────────────────────────────────────────────────────── β”‚
β”‚                                                        β”‚
β”‚ Client: Groq (groq.com)                               β”‚
β”‚ Model: llama-3.1-8b-instant (or selected model)      β”‚
β”‚ API Endpoint: https://api.groq.com/v1/chat/           β”‚
β”‚             completions                               β”‚
β”‚                                                        β”‚
β”‚ Request:                                              β”‚
β”‚ {                                                      β”‚
β”‚   "model": "llama-3.1-8b-instant",                    β”‚
β”‚   "messages": [                                        β”‚
β”‚     {                                                  β”‚
β”‚       "role": "system",                               β”‚
β”‚       "content": "You are a helpful..."               β”‚
β”‚     },                                                β”‚
β”‚     {                                                  β”‚
β”‚       "role": "user",                                 β”‚
β”‚       "content": "[full prompt above]"                β”‚
β”‚     }                                                  β”‚
β”‚   ],                                                   β”‚
β”‚   "max_tokens": 1024,                                 β”‚
β”‚   "temperature": 0.7                                  β”‚
β”‚ }                                                      β”‚
β”‚                                                        β”‚
β”‚ Where is the LLM processing happening:                β”‚
β”‚  β†’ Groq's GPU servers (not local)                     β”‚
β”‚  β†’ Model processes entire prompt                      β”‚
β”‚  β†’ Generates response token-by-token                  β”‚
β”‚  β†’ Returns complete response                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚
        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 3. PARSE LLM RESPONSE                                  β”‚
β”‚ ────────────────────────────────────────────────────── β”‚
β”‚                                                        β”‚
β”‚ Response Text:                                        β”‚
β”‚ "Machine learning is a field of artificial            β”‚
β”‚  intelligence that enables computers to learn from    β”‚
β”‚  data without being explicitly programmed..."         β”‚
β”‚                                                        β”‚
β”‚ Extract: response.choices[0].message.content          β”‚
β”‚ Return: Final Answer String                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚
        β–Ό
   RETURNED TO RAGPipeline
```

---

## Complete Code Flow for One Evaluation Query

### **File: streamlit_app.py** (Line 723-730)

```python
# FOR EACH TEST QUESTION IN THE DATASET:
for i, sample in enumerate(test_data):
    # sample["question"] = "What is machine learning?"
    # sample["answer"] = "ML is a subset of AI..."
    
    # β˜… STEP 1: CALL RAG PIPELINE β˜…
    result = st.session_state.rag_pipeline.query(
        sample["question"],     # Pass question
        n_results=5             # Get top 5 docs
    )
    # Returns:
    # {
    #   "query": "What is machine learning?",
    #   "response": "Machine learning is...",
    #   "retrieved_documents": [
    #     {"document": "...", "metadata": {...}, ...},
    #     ...
    #   ]
    # }
    
    # β˜… STEP 2: EXTRACT RESULTS β˜…
    test_cases.append({
        "query": sample["question"],
        "response": result["response"],
        "retrieved_documents": [
            doc["document"] for doc in result["retrieved_documents"]
        ],
        "ground_truth": sample.get("answer", "")
    })
```

### **File: llm_client.py** (RAGPipeline class, Line 295-340)

```python
class RAGPipeline:
    def query(self, query: str, n_results: int = 5) -> Dict:
        
        # β˜… STEP 2A: RETRIEVE DOCUMENTS β˜…
        # Where: vector_store.py β†’ get_retrieved_documents()
        retrieved_docs = self.vector_store.get_retrieved_documents(
            query,          # "What is machine learning?"
            n_results=5
        )
        
        # β˜… STEP 2B: EXTRACT DOCUMENT TEXTS β˜…
        doc_texts = [doc["document"] for doc in retrieved_docs]
        # doc_texts = [
        #   "Machine learning is a subset of AI...",
        #   "Deep learning uses neural networks...",
        #   ...
        # ]
        
        # β˜… STEP 2C: CALL LLM β˜…
        # Where: llm_client.py β†’ generate_with_context()
        response = self.llm.generate_with_context(
            query,          # "What is machine learning?"
            doc_texts,      # [retrieved document texts]
            max_tokens=1024,
            temperature=0.7
        )
        # response = "Machine learning is a field of AI..."
        
        # β˜… STEP 2D: RETURN RESULTS β˜…
        return {
            "query": query,
            "response": response,
            "retrieved_documents": retrieved_docs
        }
```

### **File: vector_store.py** (ChromaDBManager class, Line 370-400)

```python
def get_retrieved_documents(self, query_text: str, n_results: int = 5):
    # β˜… STEP 3A-1: QUERY THE COLLECTION β˜…
    # Where: vector_store.py β†’ query()
    results = self.query(query_text, n_results)
    # results = {
    #   'documents': [[doc1, doc2, doc3, doc4, doc5]],
    #   'metadatas': [[meta1, meta2, ...]],
    #   'distances': [[dist1, dist2, ...]],
    #   'ids': [[id1, id2, ...]]
    # }
    
    # β˜… STEP 3A-2: FORMAT RESULTS β˜…
    retrieved_docs = []
    for i in range(len(results['documents'][0])):
        retrieved_docs.append({
            "document": results['documents'][0][i],
            "metadata": results['metadatas'][0][i],
            "distance": results['distances'][0][i]
        })
    # retrieved_docs = [
    #   {"document": "ML is...", "metadata": {...}, "distance": 0.08},
    #   {"document": "Deep...", "metadata": {...}, "distance": 0.11},
    #   ...
    # ]
    
    return retrieved_docs
```

### **File: llm_client.py** (GroqLLMClient class, Line 215-250)

```python
def generate_with_context(self, query: str, context_documents: List[str]):
    # β˜… STEP 3B-1: BUILD CONTEXT STRING β˜…
    context = "\n\n".join([
        f"Document {i+1}: {doc}"
        for i, doc in enumerate(context_documents)
    ])
    # context = """
    # Document 1: ML is a field of AI that...
    # Document 2: Machine learning uses algorithms...
    # ...
    # """
    
    # β˜… STEP 3B-2: 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..."
    
    # β˜… STEP 3B-3: CALL LLM (GROQ API) β˜…
    # Where: llm_client.py β†’ generate()
    return self.generate(prompt, max_tokens=1024, temperature=0.7, system_prompt)
```

### **File: llm_client.py** (GroqLLMClient.generate(), Line 110-155)

```python
def generate(self, prompt: str, max_tokens: int, temperature: float, system_prompt: str):
    # β˜… STEP 3B-4: PREPARE GROQ API CALL β˜…
    
    # Apply rate limiting (max 30 requests per minute)
    self.rate_limiter.acquire_sync()
    
    # Build messages for Groq API
    messages = []
    if system_prompt:
        messages.append({
            "role": "system",
            "content": system_prompt
        })
    messages.append({
        "role": "user",
        "content": prompt
    })
    
    # β˜… STEP 3B-5: MAKE GROQ API REQUEST β˜…
    try:
        response = self.client.chat.completions.create(
            model=self.model_name,          # e.g., "llama-3.1-8b-instant"
            messages=messages,
            max_tokens=max_tokens,          # 1024
            temperature=temperature         # 0.7
        )
        
        # β˜… STEP 3B-6: EXTRACT RESPONSE β˜…
        return response.choices[0].message.content
        # Returns: "Machine learning is a field of artificial intelligence..."
        
    except Exception as e:
        return f"Error: {str(e)}"
```

---

## Summary of Query Processing in Evaluation

| Step | Component | Input | Process | Output |
|------|-----------|-------|---------|--------|
| 1 | Streamlit UI | Test sample | Load from dataset | Question |
| 2 | RAGPipeline | Question | Orchestrate RAG | Response |
| 2A | ChromaDB | Question | Embed & search | 5 documents |
| 2B | Embedding Model | Question text | Convert to vector | 768-dim vector |
| 2C | Groq LLM | Q + 5 docs | API call | Generated answer |
| 3 | TRACEEvaluator | Q, response, docs | Compute metrics | TRACe scores |

---

## Where LLM Gets Called

**PRIMARY LLM CALL LOCATION**: `llm_client.py`, function `GroqLLMClient.generate()` (Line 110)

**TRIGGERED BY**:
1. Chat interface: `Chat tab β†’ query β†’ generate()`
2. Evaluation: `run_evaluation() β†’ rag_pipeline.query() β†’ generate_with_context() β†’ generate()`

**DURING EVALUATION SPECIFICALLY**:
- Called **once per test question** (e.g., 10 times for 10 test samples)
- Each call:
  - Gets a unique question
  - Retrieves 5 relevant documents
  - Asks Groq LLM to answer using those documents
  - Stores result for TRACe metric computation

**LLM MODEL USED**:
- Default: `llama-3.1-8b-instant` (can be switched in UI)
- Also available: `meta-llama/llama-4-maverick-17b-128e-instruct`, `openai/gpt-oss-120b`
- Provider: **Groq** (cloud-based GPU inference)