{ "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 }