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
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "480b7cf8",
   "metadata": {},
   "source": [
    "# Question Answering\n",
    "\n",
    "This notebook covers how to evaluate generic question answering problems. This is a situation where you have an example containing a question and its corresponding ground truth answer, and you want to measure how well the language model does at answering those questions."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78e3023b",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "For demonstration purposes, we will just evaluate a simple question answering system that only evaluates the model's internal knowledge. Please see other notebooks for examples where it evaluates how the model does at question answering over data not present in what the model was trained on."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "96710d50",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.chains import LLMChain\n",
    "from langchain.llms import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e33ccf00",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = PromptTemplate(template=\"Question: {question}\\nAnswer:\", input_variables=[\"question\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "172d993a",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = OpenAI(model_name=\"text-davinci-003\", temperature=0)\n",
    "chain = LLMChain(llm=llm, prompt=prompt)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c584440",
   "metadata": {},
   "source": [
    "## Examples\n",
    "For this purpose, we will just use two simple hardcoded examples, but see other notebooks for tips on how to get and/or generate these examples."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "87de1d84",
   "metadata": {},
   "outputs": [],
   "source": [
    "examples = [\n",
    "    {\n",
    "        \"question\": \"Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. How many tennis balls does he have now?\",\n",
    "        \"answer\": \"11\"\n",
    "    },\n",
    "    {\n",
    "        \"question\": 'Is the following sentence plausible? \"Joao Moutinho caught the screen pass in the NFC championship.\"',\n",
    "        \"answer\": \"No\"\n",
    "    }\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "143b1155",
   "metadata": {},
   "source": [
    "## Predictions\n",
    "\n",
    "We can now make and inspect the predictions for these questions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c7bd809c",
   "metadata": {},
   "outputs": [],
   "source": [
    "predictions = chain.apply(examples)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f06dceab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'text': ' 11 tennis balls'},\n",
       " {'text': ' No, this sentence is not plausible. Joao Moutinho is a professional soccer player, not an American football player, so it is not likely that he would be catching a screen pass in the NFC championship.'}]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predictions"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45cc2f9d",
   "metadata": {},
   "source": [
    "## Evaluation\n",
    "\n",
    "We can see that if we tried to just do exact match on the answer answers (`11` and `No`) they would not match what the language model answered. However, semantically the language model is correct in both cases. In order to account for this, we can use a language model itself to evaluate the answers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0cacc65a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.evaluation.qa import QAEvalChain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5aa6cd65",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = OpenAI(temperature=0)\n",
    "eval_chain = QAEvalChain.from_llm(llm)\n",
    "graded_outputs = eval_chain.evaluate(examples, predictions, question_key=\"question\", prediction_key=\"text\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "63780020",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 0:\n",
      "Question: Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. How many tennis balls does he have now?\n",
      "Real Answer: 11\n",
      "Predicted Answer:  11 tennis balls\n",
      "Predicted Grade:  CORRECT\n",
      "\n",
      "Example 1:\n",
      "Question: Is the following sentence plausible? \"Joao Moutinho caught the screen pass in the NFC championship.\"\n",
      "Real Answer: No\n",
      "Predicted Answer:  No, this sentence is not plausible. Joao Moutinho is a professional soccer player, not an American football player, so it is not likely that he would be catching a screen pass in the NFC championship.\n",
      "Predicted Grade:  CORRECT\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for i, eg in enumerate(examples):\n",
    "    print(f\"Example {i}:\")\n",
    "    print(\"Question: \" + eg['question'])\n",
    "    print(\"Real Answer: \" + eg['answer'])\n",
    "    print(\"Predicted Answer: \" + predictions[i]['text'])\n",
    "    print(\"Predicted Grade: \" + graded_outputs[i]['text'])\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "782ae8c8",
   "metadata": {},
   "source": [
    "## Customize Prompt\n",
    "\n",
    "You can also customize the prompt that is used. Here is an example prompting it using a score from 0 to 10.\n",
    "The custom prompt requires 3 input variables: \"query\", \"answer\" and \"result\". Where \"query\" is the question, \"answer\" is the ground truth answer, and \"result\" is the predicted answer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "153425c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.prompts.prompt import PromptTemplate\n",
    "\n",
    "_PROMPT_TEMPLATE = \"\"\"You are an expert professor specialized in grading students' answers to questions.\n",
    "You are grading the following question:\n",
    "{query}\n",
    "Here is the real answer:\n",
    "{answer}\n",
    "You are grading the following predicted answer:\n",
    "{result}\n",
    "What grade do you give from 0 to 10, where 0 is the lowest (very low similarity) and 10 is the highest (very high similarity)?\n",
    "\"\"\"\n",
    "\n",
    "PROMPT = PromptTemplate(input_variables=[\"query\", \"answer\", \"result\"], template=_PROMPT_TEMPLATE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a3b0fb7",
   "metadata": {},
   "outputs": [],
   "source": [
    "evalchain = QAEvalChain.from_llm(llm=llm,prompt=PROMPT)\n",
    "evalchain.evaluate(examples, predictions, question_key=\"question\", answer_key=\"answer\", prediction_key=\"text\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb1cf335",
   "metadata": {},
   "source": [
    "## Evaluation without Ground Truth\n",
    "Its possible to evaluate question answering systems without ground truth. You would need a `\"context\"` input that reflects what the information the LLM uses to answer the question. This context can be obtained by any retreival system. Here's an example of how it works:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c59293f",
   "metadata": {},
   "outputs": [],
   "source": [
    "context_examples = [\n",
    "    {\n",
    "        \"question\": \"How old am I?\",\n",
    "        \"context\": \"I am 30 years old. I live in New York and take the train to work everyday.\",\n",
    "    },\n",
    "    {\n",
    "        \"question\": 'Who won the NFC championship game in 2023?\"',\n",
    "        \"context\": \"NFC Championship Game 2023: Philadelphia Eagles 31, San Francisco 49ers 7\"\n",
    "    }\n",
    "]\n",
    "QA_PROMPT = \"Answer the question based on the  context\\nContext:{context}\\nQuestion:{question}\\nAnswer:\"\n",
    "template = PromptTemplate(input_variables=[\"context\", \"question\"], template=QA_PROMPT)\n",
    "qa_chain = LLMChain(llm=llm, prompt=template)\n",
    "predictions = qa_chain.apply(context_examples)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e500d0cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'text': 'You are 30 years old.'},\n",
       " {'text': ' The Philadelphia Eagles won the NFC championship game in 2023.'}]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6d8cbc1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.evaluation.qa import ContextQAEvalChain\n",
    "eval_chain = ContextQAEvalChain.from_llm(llm)\n",
    "graded_outputs = eval_chain.evaluate(context_examples, predictions, question_key=\"question\", prediction_key=\"text\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6c5262d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'text': ' CORRECT'}, {'text': ' CORRECT'}]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "graded_outputs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aaa61f0c",
   "metadata": {},
   "source": [
    "## Comparing to other evaluation metrics\n",
    "We can compare the evaluation results we get to other common evaluation metrics. To do this, let's load some evaluation metrics from HuggingFace's `evaluate` package."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d851453b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Some data munging to get the examples in the right format\n",
    "for i, eg in enumerate(examples):\n",
    "    eg['id'] = str(i)\n",
    "    eg['answers'] = {\"text\": [eg['answer']], \"answer_start\": [0]}\n",
    "    predictions[i]['id'] = str(i)\n",
    "    predictions[i]['prediction_text'] = predictions[i]['text']\n",
    "\n",
    "for p in predictions:\n",
    "    del p['text']\n",
    "\n",
    "new_examples = examples.copy()\n",
    "for eg in new_examples:\n",
    "    del eg ['question']\n",
    "    del eg['answer']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c38eb3e9",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from evaluate import load\n",
    "squad_metric = load(\"squad\")\n",
    "results = squad_metric.compute(\n",
    "    references=new_examples,\n",
    "    predictions=predictions,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "07d68f85",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'exact_match': 0.0, 'f1': 28.125}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b775150",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.16"
  },
  "vscode": {
   "interpreter": {
    "hash": "53f3bc57609c7a84333bb558594977aa5b4026b1d6070b93987956689e367341"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}