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
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e975979-3b3d-4a8d-9db6-b7433cf0d8b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os, random, json\n",
    "import sqlite3\n",
    "\n",
    "import pandas as pd\n",
    "from openai import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98601634-bd9b-4566-b242-2b3c9d04b260",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "63db76a8-31de-4957-b7b9-291c2539f976",
   "metadata": {},
   "source": [
    "### Parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff4d40aa-a42e-4ad7-9ca9-d894653d205e",
   "metadata": {},
   "outputs": [],
   "source": [
    "db_path = \"../database/mock_qna.sqlite\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98a20c7e-b1dc-42d5-929b-62978959abda",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a11295d9-9bf0-4c9d-b5b2-0feec01bf640",
   "metadata": {},
   "outputs": [],
   "source": [
    "con = sqlite3.connect(db_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1c1e976-0d75-42e3-8c2e-5045ee0f2c4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "cur = con.cursor()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d78b0cc7-0238-41be-bc9f-688fcac71f73",
   "metadata": {},
   "outputs": [],
   "source": [
    "res = cur.execute(f\"\"\"SELECT COUNT(*)\n",
    "                      FROM qna_tbl\n",
    "                   \"\"\")\n",
    "table_size = res.fetchone()[0]\n",
    "print(f\"table size: {table_size}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "faaacff0-bc67-464d-bd7c-1d51b0901dd4",
   "metadata": {},
   "outputs": [],
   "source": [
    "res = cur.execute(f\"\"\"SELECT chapter, COUNT(*)\n",
    "                      FROM qna_tbl\n",
    "                      GROUP BY chapter\n",
    "                   \"\"\")\n",
    "chapter_counts = res.fetchall()\n",
    "print(chapter_counts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f83954ba-f92a-42ce-8d1c-758f4054b4c5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "117bbc79-5f58-4b31-9df1-dac75d7ef5a8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8dae73ca-845a-4d1e-8e1f-b1efb36dec8e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c4fddf3-6e7a-40c7-a6c2-2e06f976ec56",
   "metadata": {},
   "outputs": [],
   "source": [
    "id = random.randint(1, table_size)\n",
    "res = cur.execute(f\"\"\"SELECT question, option_1, option_2, option_3, option_4, correct_answer\n",
    "                      FROM qna_tbl\n",
    "                      WHERE id={id}\n",
    "                   \"\"\")\n",
    "result = res.fetchone()\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f55b4a21-45b1-42a6-8ad1-352174b78806",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c5ef430b-807c-4090-8ed2-969c43ba228e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_qna_question(chapter_n):\n",
    "    sql_string = f\"\"\"SELECT id, question, option_1, option_2, option_3, option_4, correct_answer\n",
    "                     FROM qna_tbl\n",
    "                     WHERE chapter='{chapter_n}'\n",
    "                  \"\"\"\n",
    "    res = cur.execute(sql_string)\n",
    "    result = res.fetchone()\n",
    "\n",
    "    id       = result[0]\n",
    "    question = result[1]\n",
    "    option_1 = result[2]\n",
    "    option_2 = result[3]\n",
    "    option_3 = result[4]\n",
    "    option_4 = result[5]\n",
    "    c_answer = result[6]\n",
    "\n",
    "    qna_str  = \"Question: \\n\" + \\\n",
    "               \"========= \\n\" + \\\n",
    "                question.replace(\"\\\\n\", \"\\n\") + \"\\n\" + \\\n",
    "               \"A) \" + option_1 + \"\\n\" + \\\n",
    "               \"B) \" + option_2 + \"\\n\" + \\\n",
    "               \"C) \" + option_3 + \"\\n\" + \\\n",
    "               \"D) \" + option_4\n",
    "    \n",
    "    return id, qna_str, c_answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b61cc8eb-5118-438a-b38f-e01fc92c7387",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13702036-6457-464d-bd32-0e20dd7050e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "qna_custom_functions = [\n",
    "    {\n",
    "        \"name\": \"get_qna_question\",\n",
    "        \"description\": \"\"\"\n",
    "                        Extract the chapter information from the body of the input text, the format looks as follow:\n",
    "                        The output should be in the format with `Chapter_` as prefix.\n",
    "                        Example 1: `Chapter_1` for first chapter\n",
    "                        Example 2: For chapter 12 of the textbook, you should return `Chapter_12`\n",
    "                        Example 3: `Chapter_5` for fifth chapter\n",
    "                        Thereafter, the chapter_n argument will be passed to the function for Q&A question retrieval.\n",
    "                       \"\"\",\n",
    "        \"parameters\": {\n",
    "            \"type\": \"object\",\n",
    "            \"properties\": {\n",
    "                \"chapter_n\": {\n",
    "                    \"type\": \"string\",\n",
    "                    \"description\": \"\"\"\n",
    "                        which chapter to extract, the format of this function argumet is with `Chapter_` as prefix, \n",
    "                        concatenated with chapter number in integer. For example, `Chapter_2`, `Chapter_10`.\n",
    "                    \"\"\"\n",
    "                }\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1bbb95af-dd82-443f-b23c-97c9a2777e11",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "957fe647-c1f7-4db5-8f31-fb5e1f546c0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "client = OpenAI()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "018fc414-d6df-408f-a14c-0a3857f4c52d",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = \"I am interested in chapter 13, can you test my understanding of this chapter?\"\n",
    "response = client.chat.completions.create(\n",
    "    model = 'gpt-3.5-turbo',\n",
    "    messages = [{'role': 'user', 'content': prompt}],\n",
    "    functions = qna_custom_functions,\n",
    "    function_call = 'auto'\n",
    ")\n",
    "json_response = json.loads(response.choices[0].message.function_call.arguments)\n",
    "print(json_response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2408c546-335c-478a-b1ea-9c0921a9b7a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37ec1b9a-2cdd-4838-ab02-8260d392483f",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = \"I am interested in chapter thirteen, can you test my understanding of this chapter?\"\n",
    "response = client.chat.completions.create(\n",
    "    model = 'gpt-3.5-turbo',\n",
    "    messages = [{'role': 'user', 'content': prompt}],\n",
    "    functions = qna_custom_functions,\n",
    "    function_call = 'auto'\n",
    ")\n",
    "json_response = json.loads(response.choices[0].message.function_call.arguments)\n",
    "print(json_response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b8e9f05-bb9a-429b-a1fb-abbaced23230",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18edebdd-2c7f-4589-8909-f816be5c4d1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = \"I am interested in 4th chapter, can you test my understanding of this chapter?\"\n",
    "response = client.chat.completions.create(\n",
    "    model = 'gpt-3.5-turbo',\n",
    "    messages = [{'role': 'user', 'content': prompt}],\n",
    "    functions = qna_custom_functions,\n",
    "    function_call = 'auto'\n",
    ")\n",
    "json_response = json.loads(response.choices[0].message.function_call.arguments)\n",
    "print(json_response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4325b3c-47d6-4d3f-a50a-45914b47a9c0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c558b722-4438-4485-98c0-b4117bc3d46e",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = \"\"\"There are 15 chapters in the Health Insurance text book, I want to study the last chapter, \n",
    "            can you test my understanding of this chapter?\n",
    "        \"\"\"\n",
    "response = client.chat.completions.create(\n",
    "    model = 'gpt-3.5-turbo',\n",
    "    messages = [{'role': 'user', 'content': prompt}],\n",
    "    functions = qna_custom_functions,\n",
    "    function_call = 'auto'\n",
    ")\n",
    "json_response = json.loads(response.choices[0].message.function_call.arguments)\n",
    "print(json_response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "049a28bf-abe5-4247-970f-615d1877a2c0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de49c61a-0b3e-4623-abcb-a7625ac4d0db",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = \"I am interested in 2nd chapter, can you test my understanding of this chapter?\"\n",
    "response = client.chat.completions.create(\n",
    "    model = 'gpt-3.5-turbo',\n",
    "    messages = [{'role': 'user', 'content': prompt}],\n",
    "    functions = qna_custom_functions,\n",
    "    function_call = 'auto'\n",
    ")\n",
    "json_response = json.loads(response.choices[0].message.function_call.arguments)\n",
    "print(json_response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "074229dc-82d9-4a2b-9a08-019228da78a1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "289fba25-f547-402a-bd13-0dc4ce7ddf8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "id, qna_str, answer = get_qna_question(chapter_n=json_response[\"chapter_n\"])\n",
    "print(qna_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "adc9f539-3654-4174-815b-e0939f513a20",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b6ad929-e6a5-4978-8678-519375ef62eb",
   "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.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}