trace_and_evaluate_rag_using_parea.ipynb 17.1 KB
Newer Older
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# RAG Powered by SGLang & Chroma Evaluated using Parea\n",
    "\n",
    "In this notebook, we will build a simple RAG pipeline using SGLang to execute our LLM calls, Chroma as vector database for retrieval and [Parea](https://www.parea.ai) for tracing and evaluation. We will then evaluate the performance of our RAG pipeline. The dataset we will use was created by [Virat](https://twitter.com/virattt) and contains 100 questions, contexts and answers from the Airbnb 2023 10k filing.\n",
    "\n",
    "The RAG pipeline consists of two steps:\n",
    "1. Retrieval: Given a question, we retrieve the relevant context from all provided contexts.\n",
    "2. Generation: Given the question and the retrieved context, we generate an answer.\n",
    "\n",
    "ℹ️ This notebook requires an OpenAI API key.\n",
    "\n",
    "ℹ️ This notebook requires a Parea API key, which can be created [here](https://docs.parea.ai/api-reference/authentication#parea-api-key)."
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Setting up the environment\n",
    "\n",
    "We will first install the necessary packages: `sglang`, `parea` and `chromadb`."
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# note, if you use a Mac M1 chip, you might need to install grpcio 1.59.0 first such that installing chromadb works\n",
    "# !pip install grpcio==1.59.0\n",
    "\n",
    "!pip install sglang[openai] parea chromadb"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Create a Parea API key as outlined [here](https://docs.parea.ai/api-reference/authentication#parea-api-key) and save it in a `.env` file as `PAREA_API_KEY=your-api-key`."
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Indexing the data\n",
    "\n",
    "Now it's time to download the data & index it! For that, we create a collection called `contexts` in Chroma and add the contexts as documents."
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "\n",
    "import chromadb\n",
    "\n",
    "path_qca = \"airbnb-2023-10k-qca.json\"\n",
    "\n",
    "if not os.path.exists(path_qca):\n",
    "    !wget https://virattt.github.io/datasets/abnb-2023-10k.json -O airbnb-2023-10k-qca.json\n",
    "\n",
    "with open(path_qca, 'r') as f:\n",
    "    question_context_answers = json.load(f)\n",
    "\n",
    "chroma_client = chromadb.PersistentClient()\n",
    "collection = chroma_client.get_or_create_collection(name=\"contexts\")\n",
    "if collection.count() == 0:\n",
    "    collection.add(\n",
    "        documents=[qca[\"context\"] for qca in question_context_answers],\n",
    "        ids=[str(i) for i in range(len(question_context_answers))]\n",
    "    )"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Defining the RAG pipeline\n",
    "\n",
    "We will start with importing the necessary packages, setting up tracing of OpenAI calls via Parea and setting OpenAI as the default backend for SGLang."
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "import os\n",
    "import time\n",
    "\n",
    "from dotenv import load_dotenv\n",
    "\n",
    "from sglang import function, user, assistant, gen, set_default_backend, OpenAI\n",
    "from sglang.lang.interpreter import ProgramState\n",
    "from parea import Parea, trace\n",
    "\n",
    "\n",
    "load_dotenv()\n",
    "\n",
    "os.environ['TOKENIZERS_PARALLELISM'] = \"false\"\n",
    "\n",
    "p = Parea(api_key=os.getenv(\"PAREA_API_KEY\"), project_name=\"rag_sglang\")\n",
    "p.integrate_with_sglang()\n",
    "\n",
    "set_default_backend(OpenAI(\"gpt-3.5-turbo\"))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Now we can define our retrieval step shown below. Notice, the `trace` decorator which will automatically trace inputs, output, latency, etc. of that call."
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "@trace\n",
    "def retrieval(question: str) -> list[str]:\n",
    "    return collection.query(\n",
    "        query_texts=[question],\n",
    "        n_results=1\n",
    "    )['documents'][0]"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Next we will define the generation step which uses SGLang to execute the LLM call."
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "@function\n",
    "def generation_sglang(s, question: str, *context: str):\n",
    "    context = \"\\n\".join(context)\n",
    "    s += user(f'Given this question:\\n{question}\\n\\nAnd this context:\\n{context}\\n\\nAnswer the question.')\n",
    "    s += assistant(gen(\"answer\"))\n",
    "\n",
    "\n",
    "@trace\n",
    "def generation(question: str, *context):\n",
    "    state: ProgramState = generation_sglang.run(question, *context)\n",
    "    while not state.stream_executor.is_finished:\n",
    "        time.sleep(1)\n",
    "    return state.stream_executor.variables[\"answer\"]"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Finally, we can tie it together and execute a sample query."
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": "'The World Health Organization formally declared an end to the COVID-19 global health emergency'"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "@trace\n",
    "def rag_pipeline(question: str) -> str:\n",
    "    contexts = retrieval(question)\n",
    "    return generation(question, *contexts)\n",
    "\n",
    "\n",
    "rag_pipeline(\"When did the World Health Organization formally declare an end to the COVID-19 global health emergency?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Debug Trace\n",
    "\n",
    "The output is unfortunately wrong! Using the traced pipeline, we can see that\n",
    "\n",
    "- the context is relevant to the question and contains the correct information\n",
    "- but, the generation step is cut off as max tokens is set to 16\n",
    "\n",
    "When opening the generation step in the playground and rerunning the prompt with max. tokens set to 1000, the correct answer is produced.\n",
    "\n",
    "![RAG Trace](https://drive.google.com/uc?id=1QI243ogGjzbO01tUrR72g9rFoGzUJqVH)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Evaluating RAG Pipelines\n",
    "\n",
    "Before we apply above's fix, let's dive into evaluating RAG pipelines.\n",
    "\n",
    "RAG pipelines consist of a retrieval step to fetch relevant information and a generation step to generate a response to a users question. A RAG pipeline can fail at either step. E.g. the retrieval step can fail to find relevant information which makes generating the correct impossible. Another failure mode is that the generation step doesn't leverage the retrieved information correctly. We will apply the following evaluation metrics to understand different failure modes:\n",
    "\n",
    "- `context_relevancy`: measures how relevant the context is given the question\n",
    "- `percent_target_supported_by_context`: measures how much of the target answer is supported by the context; this will give an upper ceiling of how well the generation step can perform\n",
    "- `answer_context_faithfulness`: measures how much the generated answer utilizes the context\n",
    "- `answer_matches_target`: measures how well the generated answer matches the target answer judged by a LLM and gives a sense of accuracy of our entire pipeline\n",
    "\n",
    "To use these evaluation metrics, we can import them from  `parea.evals.rag` and `parea.evals.general` and apply them to a function by specifying in the `trace` decorator which evaluation metrics to use. The `@trace` decorator will automatically log the results of the evaluation metrics to the Parea dashboard.\n",
    "\n",
    "Applying them to the retrieval step:"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "from parea.evals.rag import context_query_relevancy_factory, percent_target_supported_by_context_factory\n",
    "\n",
    "\n",
    "context_relevancy_eval = context_query_relevancy_factory()\n",
    "percent_target_supported_by_context = percent_target_supported_by_context_factory()\n",
    "\n",
    "\n",
    "@trace(eval_funcs=[context_relevancy_eval, percent_target_supported_by_context])\n",
    "def retrieval(question: str) -> list[str]:\n",
    "    return collection.query(\n",
    "        query_texts=[question],\n",
    "        n_results=1\n",
    "    )['documents'][0]"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Now we can apply `answer_context_faithfulness` and `answer_matches_target` to the generation step."
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "from parea.evals.general import answer_matches_target_llm_grader_factory\n",
    "from parea.evals.rag import answer_context_faithfulness_statement_level_factory\n",
    "\n",
    "\n",
    "answer_context_faithfulness = answer_context_faithfulness_statement_level_factory()\n",
    "answer_matches_target_llm_grader = answer_matches_target_llm_grader_factory()\n",
    "\n",
    "@function\n",
    "def generation_sglang(s, question: str, *context: str):\n",
    "    context = \"\\n\".join(context)\n",
    "    s += user(f'Given this question:\\n{question}\\n\\nAnd this context:\\n{context}\\n\\nAnswer the question.')\n",
    "    s += assistant(gen(\"answer\", max_tokens=1_000))\n",
    "\n",
    "\n",
    "@trace(eval_funcs=[answer_context_faithfulness, answer_matches_target_llm_grader])\n",
    "def generation(question: str, *context):\n",
    "    state: ProgramState = generation_sglang.run(question, *context)\n",
    "    while not state.stream_executor.is_finished:\n",
    "        time.sleep(1)\n",
    "    return state.stream_executor.variables[\"answer\"]"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Finally, we tie them together & execute the original sample query."
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "'The World Health Organization formally declared an end to the COVID-19 global health emergency in May 2023.'"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "@trace\n",
    "def rag_pipeline(question: str) -> str:\n",
    "    contexts = retrieval(question)\n",
    "    return generation(question, *contexts)\n",
    "\n",
    "\n",
    "rag_pipeline(\"When did the World Health Organization formally declare an end to the COVID-19 global health emergency?\")"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Great, the answer is correct! Can you spot the line where we fixed the output truncation issue?\n",
    "\n",
    "The evaluation scores appear in the bottom right of the logs (screenshot below). Note, that there is no score for `answer_matches_target_llm_grader` and `percent_target_supported_by_context` as these evals are automatically skipped if the target answer is not provided.\n",
    "\n",
    "![Fixed Max. Tokens](./images/rag/max-tokens-fixed-rag-trace.png)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Running an experiment\n",
    "\n",
    "Now we are (almost) ready to evaluate the performance of our RAG pipeline on the entire dataset. First, we will need to apply the `nest_asyncio` package to avoid issues with the Jupyter notebook event loop."
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: nest-asyncio in /Users/joschkabraun/miniconda3/envs/sglang/lib/python3.10/site-packages (1.6.0)\r\n"
     ]
    }
   ],
   "source": [
    "!pip install nest-asyncio\n",
    "import nest_asyncio\n",
    "nest_asyncio.apply()"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Running the actual experiment is straight-forward. For that we use `p.experiment` to initialize the experiment with a name, the data (list of key-value pairs fed into our entry function) and the entry function. We then call `run` on the experiment to execute it. Note, that `target` is a reserved key in the data dictionary and will be used as the target answer for evaluation."
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Run name set to: sneak-weal, since a name was not provided.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 100/100 [00:27<00:00,  3.63it/s]\n",
      "Waiting for evaluations to finish: 100%|██████████| 19/19 [00:10<00:00,  1.89it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Experiment RAG Run sneak-weal stats:\n",
      "{\n",
      "  \"latency\": \"2.69\",\n",
      "  \"input_tokens\": \"61.26\",\n",
      "  \"output_tokens\": \"75.88\",\n",
      "  \"total_tokens\": \"137.14\",\n",
      "  \"cost\": \"0.00\",\n",
      "  \"answer_context_faithfulness_statement_level\": \"0.26\",\n",
      "  \"answer_matches_target_llm_grader\": \"0.22\",\n",
      "  \"context_query_relevancy\": \"0.27\",\n",
      "  \"percent_target_supported_by_context\": \"0.40\"\n",
      "}\n",
      "\n",
      "\n",
      "View experiment & traces at: https://app.parea.ai/experiments/RAG/30f0244a-d56c-44ff-bdfb-8f47626304b6\n",
      "\n"
     ]
    }
   ],
   "source": [
    "e = p.experiment(\n",
    "    'RAG',\n",
    "    data=[\n",
    "        {\n",
    "            \"question\": qca[\"question\"],\n",
    "            \"target\": qca[\"answer\"],\n",
    "        }\n",
    "        for qca in question_context_answers\n",
    "    ],\n",
    "    func=rag_pipeline\n",
    ").run()"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Analyzing the results\n",
    "\n",
    "When opening above experiment, we will see an overview of the experiment as shown below. The upper half shows a summary of the statistics on the left and charts to investigate the distribution and relationships of scores on the right. The lower half is a table with the individual traces which we can use to debug individual samples.\n",
    "\n",
    "When looking at the statistics, we can see that the accuracy of our RAG pipeline is 22% as measured by `answer_matches_target_llm_grader`. Though when checking the quality of our retrieval step (`context_query_relevancy`), we can see that our retrival step is fetching relevant information in only 27% of all samples. As shown in the GIF, we investigate the relationship between the two and see the two scores have 95% agreement. This confirms that the retrieval step is a major bottleneck for our RAG pipeline. So, now it's your turn to improve the retrieval step!\n",
    "\n",
    "Note, above link isn't publicly accessible but the experiment can be accessed through [here](https://app.parea.ai/public-experiments/parea/rag_sglang/30f0244a-d56c-44ff-bdfb-8f47626304b6).\n",
    "\n",
    "![Experiment Results](https://drive.google.com/uc?id=1KMtJBU47nPB02Pvv3SPPTK7RnHRh5YdA)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}