File size: 22,408 Bytes
de38977
 
 
 
82b35ca
30b40e4
 
 
 
 
 
 
 
 
 
82b35ca
de38977
 
82b35ca
 
 
 
 
 
 
 
 
 
de38977
 
 
30b40e4
82b35ca
 
de38977
 
 
 
 
82b35ca
de38977
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82b35ca
de38977
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82b35ca
de38977
 
 
 
 
 
 
82b35ca
30b40e4
82b35ca
 
 
30b40e4
82b35ca
 
 
30b40e4
82b35ca
 
de38977
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82b35ca
 
 
 
 
 
 
 
 
 
 
 
de38977
 
30b40e4
82b35ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de38977
 
 
 
 
 
 
 
 
 
 
 
 
 
82b35ca
 
 
 
 
30b40e4
 
 
 
 
 
82b35ca
30b40e4
 
 
 
 
 
82b35ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30b40e4
 
 
 
 
 
82b35ca
30b40e4
 
 
 
 
 
82b35ca
 
 
 
30b40e4
 
 
 
 
 
82b35ca
30b40e4
 
 
 
 
 
82b35ca
 
de38977
 
 
 
 
 
82b35ca
de38977
 
 
 
 
 
82b35ca
 
 
 
 
de38977
 
 
 
 
 
82b35ca
de38977
 
 
 
 
 
82b35ca
 
 
 
de38977
 
 
 
 
 
82b35ca
de38977
 
 
 
 
 
82b35ca
 
 
de38977
 
 
 
 
 
82b35ca
de38977
 
 
 
 
 
82b35ca
de38977
 
 
 
 
 
82b35ca
de38977
 
 
 
 
 
 
 
 
 
 
 
 
82b35ca
de38977
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82b35ca
de38977
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82b35ca
 
 
de38977
 
82b35ca
 
 
de38977
 
82b35ca
 
 
de38977
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cb2ff14b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('..')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6bb3bb7d",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "f:\\Dissertation\\prod-rag-chat\\.venv\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "from datasets import Dataset\n",
    "from app import retrieve_relevant_documents, emb_text, model, embedding_model\n",
    "from langchain_classic.chains.combine_documents import create_stuff_documents_chain\n",
    "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
    "from langchain_core.runnables import RunnableLambda\n",
    "from langchain_core.documents import Document"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e572fb31",
   "metadata": {},
   "outputs": [],
   "source": [
    "def setup_standalone_rag_chain():\n",
    "    \"\"\"Setup a standalone RAG chain for testing without Chainlit session.\"\"\"\n",
    "    \n",
    "    def get_context_and_history(inputs):\n",
    "        \"\"\"Retrieve context without session history.\"\"\"\n",
    "        query = inputs[\"question\"]\n",
    "        relevant_docs = retrieve_relevant_documents(query, limit=5)\n",
    "        print(\"Relevant documents:\", relevant_docs[0] if relevant_docs else \"No documents found\")\n",
    "        \n",
    "        # Convert dictionaries to Document objects for LangChain\n",
    "        doc_objects = []\n",
    "        for doc in relevant_docs:\n",
    "            doc_obj = Document(\n",
    "                page_content=doc.get('text', ''),\n",
    "                metadata=doc.get('metadata', {})\n",
    "            )\n",
    "            doc_objects.append(doc_obj)\n",
    "\n",
    "        return {\n",
    "            \"question\": query,\n",
    "            \"context\": doc_objects,\n",
    "            \"history\": []  # Empty history for testing\n",
    "        }\n",
    "    \n",
    "    system_prompt = \"\"\"You are a helpful assistant specialising in developing non-discriminatory competence standards and disability support, reasonable adjustments, and equality legislation.\n",
    "\n",
    "When answering questions, you should:\n",
    "1. Use the provided context documents to inform your response\n",
    "2. Be accurate and helpful\n",
    "3. If the context doesn't contain relevant information, say so clearly\n",
    "4. Always reply in English\n",
    "5. Provide clear recommendations and examples wherever applicable\n",
    "6. Do not make assumptions about the user's knowledge or background\n",
    "7. If the user asks for a specific law or regulation, provide a brief explanation and cite relevant documents if available.\n",
    "8. Do not overemphasize disability in your responses, but rather focus on the support and adjustments that can be made to ensure equality and inclusivity.\n",
    "9. If the user query explicitly asks for a disability-related topic, provide a well-informed response based on the context documents.\n",
    "\n",
    "Context documents:\n",
    "{context} \n",
    "\n",
    "Please provide a clear response using the above context\n",
    "\"\"\"\n",
    "\n",
    "    prompt = ChatPromptTemplate.from_messages([\n",
    "        (\"system\", system_prompt),\n",
    "        MessagesPlaceholder(variable_name=\"history\"),\n",
    "        (\"human\", \"{question}\"),\n",
    "    ])\n",
    "\n",
    "    question_answer_chain = create_stuff_documents_chain(model, prompt)\n",
    "    \n",
    "    # Use a custom chain that properly handles our context and history\n",
    "    def process_input_and_format(inputs):\n",
    "        context_data = get_context_and_history(inputs)\n",
    "        return {\n",
    "            \"context\": context_data[\"context\"],\n",
    "            \"question\": context_data[\"question\"],\n",
    "            \"history\": context_data[\"history\"]\n",
    "        }\n",
    "    \n",
    "    chain = RunnableLambda(process_input_and_format) | question_answer_chain\n",
    "    \n",
    "    return chain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "330ee35d",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# Setup the RAG chain\n",
    "rag_chain = setup_standalone_rag_chain()\n",
    "\n",
    "questions = [\"What are Provisions, Criteria and Practices?\", \n",
    "             \"What is 'reasonable'?\",\n",
    "             \"What is 'substantial disadvantage'?\",\n",
    "            ]\n",
    "ground_truths = [\n",
    "    \"\"\"The Equality and Human Rights Commission (EHRC) interprets PCPs as including:3 \n",
    "+ arrangements (for example, for deciding who to admit)  \n",
    "+ the way that education, or access to any benefit, service or facility is offered or provided  \n",
    "+ one-off or discretionary decisions  \n",
    "+ proposals or directions to do something in a particular way  \n",
    "+ formal and informal policies  \n",
    "+ rules\"\"\",\n",
    "\n",
    "    \"\"\"There are two key considerations of 'reasonableness' which can help when thinking through \n",
    "when an adjustment may be reasonable:4  \n",
    "+ Could the adjustment be practicable in its application (is it possible)? \n",
    "+ Could the adjustment be effective in achieving its aim (will it work)? \n",
    "There is no need to prove that the adjustment is practicable and effective in advance, just \n",
    "that it might be. An adjustment should not be considered unreasonable if it does not remove \n",
    "the disadvantage fully; an adjustment which partially removes or reduces substantial \n",
    "disadvantage is also likely to be reasonable.\"\"\",\n",
    "\n",
    "    \"\"\"'Substantial' is defined in the Act as 'more than minor or trivial'. \n",
    "Examples of disadvantage recognised by the EHRC include: \n",
    "+ The additional time and effort expended by a disabled student \n",
    "+ The inconvenience, indignity, discomfort, or perceived disadvantage suffered by a \n",
    "disabled student \n",
    "+ The loss of opportunity or diminished progress experienced by a disabled student. \"\"\"]\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ba3810dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-12-20 19:42:27 - HTTP Request: POST https://api.studio.nebius.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "Relevant documents: {'text': 'What is a provision, criterion or practice? The phrase β€˜provision, criterion or practice’ is not defined by the Act. These concepts should be construed widely so as to include, for example, any formal or informal policies, rules, practices, arrangements, criteria, procedures, activities or provisions. They can cover one-off decisions and actions. In simple terms, they are about the way an education provider does things. Example:', 'metadata': {'source': 'data\\\\technical-guidance-further-higher-education.docx', 'file_directory': 'data', 'filename': 'technical-guidance-further-higher-education.docx', 'last_modified': '2025-07-02T21:00:50', 'page_number': 95, 'languages': ['eng'], 'filetype': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'category': 'CompositeElement', 'element_id': '3ae881ad6f81487213a9e234debf0921'}, 'score': 0.7807720899581909}\n",
      "2025-12-20 19:42:29 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:42:32 - HTTP Request: POST https://api.studio.nebius.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:42:32 - HTTP Request: POST https://api.studio.nebius.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "Relevant documents: {'text': 'β€˜Reasonable’ means having regard to all of the circumstances including the nature of the act and how obviously discriminatory it is, the authority of the person making the statement and the knowledge that the helper has or ought to have.', 'metadata': {'source': 'data\\\\technical-guidance-further-higher-education.docx', 'file_directory': 'data', 'filename': 'technical-guidance-further-higher-education.docx', 'last_modified': '2025-07-02T21:00:50', 'page_number': 36, 'languages': ['eng'], 'filetype': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'category': 'CompositeElement', 'element_id': 'c5e3a60e2a6ccc88e0eff961f645a962'}, 'score': 0.7367081046104431}\n",
      "2025-12-20 19:42:33 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:42:36 - HTTP Request: POST https://api.studio.nebius.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:42:37 - HTTP Request: POST https://api.studio.nebius.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "Relevant documents: {'text': 'The Act states that disadvantage must be substantial, which is defined as more than minor or trivial. Whether such a disadvantage exists in a particular case is a question of fact, and is assessed on an objective basis. s212(1)', 'metadata': {'source': 'data\\\\technical-guidance-further-higher-education.docx', 'file_directory': 'data', 'filename': 'technical-guidance-further-higher-education.docx', 'last_modified': '2025-07-02T21:00:50', 'page_number': 89, 'languages': ['eng'], 'filetype': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'category': 'CompositeElement', 'element_id': 'b9e8ef04daf9150c9f7e32736b53df5b'}, 'score': 0.8376985788345337}\n",
      "2025-12-20 19:42:38 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:42:40 - HTTP Request: POST https://api.studio.nebius.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n"
     ]
    }
   ],
   "source": [
    "answers = []\n",
    "contexts = []\n",
    "\n",
    "def clean_answer(answer):\n",
    "    \"\"\"Remove <think></think> tags and content from the answer.\"\"\"\n",
    "    import re\n",
    "    # Remove everything between <think> and </think> tags, including the tags themselves\n",
    "    cleaned = re.sub(r'<think>.*?</think>\\s*', '', answer, flags=re.DOTALL)\n",
    "    return cleaned.strip()\n",
    "\n",
    "# Inference\n",
    "for query in questions:\n",
    "    # Get answer from the RAG chain\n",
    "    answer = rag_chain.invoke({\"question\": query})\n",
    "    # Clean the answer to remove thinking content\n",
    "    cleaned_answer = clean_answer(answer)\n",
    "    answers.append(cleaned_answer)\n",
    "    \n",
    "    # Get relevant documents for context\n",
    "    relevant_docs = retrieve_relevant_documents(query, limit=5)\n",
    "    context_texts = [doc['text'] for doc in relevant_docs]\n",
    "    contexts.append(context_texts)\n",
    "\n",
    "# To dict\n",
    "data = {\n",
    "    \"question\": questions,\n",
    "    \"answer\": answers,\n",
    "    \"contexts\": contexts,\n",
    "    \"reference\": ground_truths\n",
    "}\n",
    "\n",
    "# Convert dict to dataset\n",
    "dataset = Dataset.from_dict(data)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "25c00c40",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_nebius import ChatNebius\n",
    "from pydantic import SecretStr\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2994b26e",
   "metadata": {},
   "outputs": [],
   "source": [
    "eval_model = ChatNebius(\n",
    "    model=\"Qwen/Qwen3-235B-A22B-Instruct-2507\",\n",
    "    streaming=False,  # <--- CRITICAL: Must be False for Ragas\n",
    "    temperature=0,  # Lower temperature for the judge is better\n",
    "    max_tokens=8192,\n",
    "    top_p=0.95,\n",
    "    api_key=SecretStr(os.getenv(\"OPENAI_API_KEY\")),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3e016be2",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Evaluating:   0%|          | 0/12 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-12-20 19:42:46 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:42:48 - HTTP Request: POST https://api.studio.nebius.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:42:49 - HTTP Request: POST https://api.studio.nebius.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:42:49 - HTTP Request: POST https://api.studio.nebius.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:42:49 - HTTP Request: POST https://api.studio.nebius.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Evaluating:   8%|β–Š         | 1/12 [00:06<01:08,  6.19s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-12-20 19:42:50 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:42:50 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:42:50 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:42:50 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:42:50 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:42:50 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:42:50 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:42:55 - HTTP Request: POST https://api.studio.nebius.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:42:56 - HTTP Request: POST https://api.studio.nebius.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:42:57 - HTTP Request: POST https://api.studio.nebius.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:42:57 - HTTP Request: POST https://api.studio.nebius.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:42:59 - HTTP Request: POST https://api.studio.nebius.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:43:00 - HTTP Request: POST https://api.studio.nebius.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:43:00 - HTTP Request: POST https://api.studio.nebius.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:43:01 - HTTP Request: POST https://api.studio.nebius.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Evaluating:  17%|β–ˆβ–‹        | 2/12 [00:20<01:49, 10.98s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-12-20 19:43:05 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:43:05 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:43:05 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:43:05 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Evaluating:  42%|β–ˆβ–ˆβ–ˆβ–ˆβ–     | 5/12 [00:26<00:32,  4.60s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-12-20 19:43:11 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:43:11 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Evaluating:  50%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ     | 6/12 [00:28<00:22,  3.83s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-12-20 19:43:17 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:43:18 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:43:18 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:43:22 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:43:22 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Evaluating:  58%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Š    | 7/12 [00:41<00:31,  6.21s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-12-20 19:43:25 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:43:25 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:43:25 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:43:28 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Evaluating:  67%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‹   | 8/12 [00:45<00:23,  5.78s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-12-20 19:43:31 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:43:31 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2025-12-20 19:43:31 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Evaluating:  75%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Œ  | 9/12 [00:52<00:17,  5.95s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-12-20 19:43:36 - HTTP Request: POST https://api.studio.nebius.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Evaluating: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 12/12 [00:53<00:00,  4.47s/it]\n"
     ]
    }
   ],
   "source": [
    "from ragas import evaluate\n",
    "from ragas.metrics import (\n",
    "    faithfulness,\n",
    "    answer_relevancy,\n",
    "    context_recall,\n",
    "    context_precision,\n",
    ")\n",
    "\n",
    "result = evaluate(\n",
    "    llm=eval_model,\n",
    "    embeddings=embedding_model,\n",
    "    dataset = dataset, \n",
    "    metrics=[\n",
    "        context_precision,\n",
    "        context_recall,\n",
    "        faithfulness,\n",
    "        answer_relevancy,\n",
    "    ],\n",
    ")\n",
    "\n",
    "df = result.to_pandas()\n",
    "\n",
    "# evaluation_results = result.to_pandas()\n",
    "\n",
    "# display_columns = ['user_input', 'answer_relevancy', 'faithfulness', 'context_precision', 'context_recall']\n",
    "# formatted_results = evaluation_results[display_columns].to_markdown(index=False, numalign=\"left\", stralign=\"left\")\n",
    "\n",
    "# print(formatted_results)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d8514ff3",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                     user_input  \\\n",
      "0  What are Provisions, Criteria and Practices?   \n",
      "1                         What is 'reasonable'?   \n",
      "2           What is 'substantial disadvantage'?   \n",
      "\n",
      "                                  retrieved_contexts  \\\n",
      "0  [What is a provision, criterion or practice? T...   \n",
      "1  [β€˜Reasonable’ means having regard to all of th...   \n",
      "2  [The Act states that disadvantage must be subs...   \n",
      "\n",
      "                                            response  \\\n",
      "0  **Provisions, Criteria and Practices (PCPs)** ...   \n",
      "1  The term **'reasonable'** in the context of di...   \n",
      "2  'Substantial disadvantage' refers to a disadva...   \n",
      "\n",
      "                                           reference  context_precision  \\\n",
      "0  The Equality and Human Rights Commission (EHRC...           1.000000   \n",
      "1  There are two key considerations of 'reasonabl...           0.533333   \n",
      "2  'Substantial' is defined in the Act as 'more t...           0.866667   \n",
      "\n",
      "   context_recall  faithfulness  answer_relevancy  \n",
      "0        0.857143      0.714286          0.704690  \n",
      "1        0.333333      0.774194          0.716996  \n",
      "2        0.400000      0.875000          0.834463  \n"
     ]
    }
   ],
   "source": [
    "print(df)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}