{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "view-in-github" }, "source": [ "\"Open" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "kfO7rE64ZTI_" }, "outputs": [], "source": [ "!pip install openai" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "f26sZpe-MCCj" }, "outputs": [], "source": [ "import json\n", "import openai\n", "import os\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 614 }, "id": "BVTr_mR0XIJI", "outputId": "897e41a0-d5e1-4b5f-d254-0a6e0f6aa3fa" }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
timestampauthormessage
02023-06-07 08:16:00+00:00assistantQuestion 1:\\nWhich of the following statements...
12023-06-07 08:16:30+00:00userC
22023-06-07 08:17:00+00:00assistantCorrect! Option C is the correct answer...
32023-06-07 08:17:30+00:00assistantQuestion 2:\\nWhich of the following expenses a...
42023-06-07 08:18:00+00:00userA
52023-06-07 08:18:30+00:00assistantI'm sorry, but your answer is incorrect...
62023-06-07 08:19:00+00:00assistantQuestion 2 (Revised):\\nWhich of the following ...
72023-06-07 08:19:30+00:00userD
82023-06-07 08:20:00+00:00assistantCorrect! Option D is the correct answer...
92023-06-07 08:20:30+00:00assistantQuestion 3:\\nWhat is the purpose of capitalizi...
102023-06-07 08:21:00+00:00userC
112023-06-07 08:21:30+00:00assistantCorrect! Option C is the correct answer...
122023-06-07 08:22:00+00:00assistantQuestion 4:\\nWhich financial statement provide...
132023-06-07 08:22:30+00:00userC
142023-06-07 08:23:00+00:00assistantCorrect! Option C is the correct answer...
152023-06-07 08:23:30+00:00assistantQuestion 5:\\nWhat is the purpose of the matchi...
162023-06-07 08:24:00+00:00userB
172023-06-07 08:24:30+00:00assistantCorrect! Option B is the correct answer...
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ], "text/plain": [ " timestamp author \\\n", "0 2023-06-07 08:16:00+00:00 assistant \n", "1 2023-06-07 08:16:30+00:00 user \n", "2 2023-06-07 08:17:00+00:00 assistant \n", "3 2023-06-07 08:17:30+00:00 assistant \n", "4 2023-06-07 08:18:00+00:00 user \n", "5 2023-06-07 08:18:30+00:00 assistant \n", "6 2023-06-07 08:19:00+00:00 assistant \n", "7 2023-06-07 08:19:30+00:00 user \n", "8 2023-06-07 08:20:00+00:00 assistant \n", "9 2023-06-07 08:20:30+00:00 assistant \n", "10 2023-06-07 08:21:00+00:00 user \n", "11 2023-06-07 08:21:30+00:00 assistant \n", "12 2023-06-07 08:22:00+00:00 assistant \n", "13 2023-06-07 08:22:30+00:00 user \n", "14 2023-06-07 08:23:00+00:00 assistant \n", "15 2023-06-07 08:23:30+00:00 assistant \n", "16 2023-06-07 08:24:00+00:00 user \n", "17 2023-06-07 08:24:30+00:00 assistant \n", "\n", " message \n", "0 Question 1:\\nWhich of the following statements... \n", "1 C \n", "2 Correct! Option C is the correct answer... \n", "3 Question 2:\\nWhich of the following expenses a... \n", "4 A \n", "5 I'm sorry, but your answer is incorrect... \n", "6 Question 2 (Revised):\\nWhich of the following ... \n", "7 D \n", "8 Correct! Option D is the correct answer... \n", "9 Question 3:\\nWhat is the purpose of capitalizi... \n", "10 C \n", "11 Correct! Option C is the correct answer... \n", "12 Question 4:\\nWhich financial statement provide... \n", "13 C \n", "14 Correct! Option C is the correct answer... \n", "15 Question 5:\\nWhat is the purpose of the matchi... \n", "16 B \n", "17 Correct! Option B is the correct answer... " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_json('demo_json.json')\n", "pd.read_json('demo_json.json')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "anSNlvqlXh6i" }, "outputs": [], "source": [ "openai.api_key = \"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\"" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 627 }, "id": "udujJrX6SryU", "outputId": "9b182162-7c1c-4d5a-be56-16947ddcda33" }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
QuestionCorrect AnswerUser AnswerEvaluationScore
0Question 1:\\nWhich of the following statements...CCcorrect.1
1Question 2 (Revised):\\nWhich of the following ...DDincorrect. the correct answer is d, software d...1
2Question 3:\\nWhat is the purpose of capitalizi...CCincorrect. the correct answer is b.1
3Question 4:\\nWhich financial statement provide...CCcorrect2
4Question 5:\\nWhat is the purpose of the matchi...BBcorrect3
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ], "text/plain": [ " Question Correct Answer \\\n", "0 Question 1:\\nWhich of the following statements... C \n", "1 Question 2 (Revised):\\nWhich of the following ... D \n", "2 Question 3:\\nWhat is the purpose of capitalizi... C \n", "3 Question 4:\\nWhich financial statement provide... C \n", "4 Question 5:\\nWhat is the purpose of the matchi... B \n", "\n", " User Answer Evaluation Score \n", "0 C correct. 1 \n", "1 D incorrect. the correct answer is d, software d... 1 \n", "2 C incorrect. the correct answer is b. 1 \n", "3 C correct 2 \n", "4 B correct 3 " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Initialize necessary variables\n", "prompt = \"\"\n", "question = \"\"\n", "correct_answer = \"\"\n", "user_answer = \"\"\n", "\n", "# Initialize score\n", "score = 0\n", "\n", "# Initialize an empty list to hold row data\n", "row_data = []\n", "\n", "for index, row in df.iterrows():\n", " author = row['author']\n", " message = row['message']\n", "\n", " # Choose the appropriate prompt based on the author\n", " if author == 'assistant':\n", " if 'Question' in message:\n", " question = message\n", " user_answer = '' # Reset user_answer after a new question\n", " elif 'Correct! Option' in message:\n", " correct_answer = message.split('Option ')[1][0]\n", " if user_answer: # If user_answer exists, make the API call\n", " prompt = f\"Given the following question:\\n{question}\\nThe student responded with: {user_answer}\\nIs the student's response correct or incorrect?\"\n", "\n", " # Make an API call to OpenAI\n", " api_response = openai.Completion.create(\n", " engine='text-davinci-003',\n", " prompt=prompt,\n", " max_tokens=100,\n", " temperature=0.7,\n", " n=1,\n", " stop=None\n", " )\n", "\n", " # Extract and evaluate the generated response\n", " generated_response = api_response.choices[0].text.strip().lower()\n", "\n", " # Update score based on generated_response\n", " if 'correct' in generated_response and 'incorrect' not in generated_response:\n", " score += 1\n", "\n", " # Create a dictionary for the current row\n", " row_dict = {\n", " 'Question': question,\n", " 'Correct Answer': correct_answer,\n", " 'User Answer': user_answer,\n", " 'Evaluation': generated_response,\n", " 'Score': score\n", " }\n", " # Append the row dictionary to row_data\n", " row_data.append(row_dict)\n", "\n", " elif author == 'user':\n", " user_answer = message\n", "\n", "# Create a DataFrame from row_data\n", "output_df = pd.DataFrame(row_data)\n", "output_df\n" ] } ], "metadata": { "colab": { "authorship_tag": "ABX9TyOn+FniXzrkHNKH5uAKgyUD", "include_colab_link": true, "provenance": [] }, "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.8.16" } }, "nbformat": 4, "nbformat_minor": 4 }