├── README.md ├── Response.json ├── StreamlitAPP.py ├── src ├── __init__.py ├── mcqgenerator │ ├── __init__.py │ ├── __pycache__ │ │ ├── logger.cpython-38.pyc │ │ └── __init__.cpython-38.pyc │ ├── logger.py │ ├── utils.py │ └── MCQGenerator.py └── __pycache__ │ └── __init__.cpython-38.pyc ├── .gitignore ├── mcqgenrator.egg-info ├── top_level.txt ├── dependency_links.txt ├── requires.txt ├── PKG-INFO └── SOURCES.txt ├── requirements.txt ├── logs └── 12_12_2023_15_47_13.log ├── test.py ├── setup.py ├── experiment ├── machinelearning.csv └── mcq.ipynb └── data.txt /README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Response.json: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /StreamlitAPP.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | env 2 | .env -------------------------------------------------------------------------------- /src/mcqgenerator/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /mcqgenrator.egg-info/top_level.txt: -------------------------------------------------------------------------------- 1 | src 2 | -------------------------------------------------------------------------------- /mcqgenrator.egg-info/dependency_links.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | openai 2 | langchain 3 | streamlit 4 | python-dotenv 5 | PyPDF2 6 | 7 | 8 | -e . -------------------------------------------------------------------------------- /mcqgenrator.egg-info/requires.txt: -------------------------------------------------------------------------------- 1 | openai 2 | langchain 3 | streamlit 4 | python-dotenv 5 | PyPDF2 6 | -------------------------------------------------------------------------------- /logs/12_12_2023_15_47_13.log: -------------------------------------------------------------------------------- 1 | [2023-12-12 15:47:13,186] 3 root - INFO - hi, i am going to start my excution... 2 | -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | from src.mcqgenerator.logger import logging 2 | 3 | logging.info("hi, i am going to start my excution...") -------------------------------------------------------------------------------- /mcqgenrator.egg-info/PKG-INFO: -------------------------------------------------------------------------------- 1 | Metadata-Version: 2.1 2 | Name: mcqgenrator 3 | Version: 0.0.1 4 | Author: sunny savita 5 | Author-email: sunny.savita@ineuron.ai 6 | -------------------------------------------------------------------------------- /src/__pycache__/__init__.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sunnysavita10/Automated-MCQ-Generator-Using-Langchain-OpenAI-API/HEAD/src/__pycache__/__init__.cpython-38.pyc -------------------------------------------------------------------------------- /src/mcqgenerator/__pycache__/logger.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sunnysavita10/Automated-MCQ-Generator-Using-Langchain-OpenAI-API/HEAD/src/mcqgenerator/__pycache__/logger.cpython-38.pyc -------------------------------------------------------------------------------- /src/mcqgenerator/__pycache__/__init__.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sunnysavita10/Automated-MCQ-Generator-Using-Langchain-OpenAI-API/HEAD/src/mcqgenerator/__pycache__/__init__.cpython-38.pyc -------------------------------------------------------------------------------- /mcqgenrator.egg-info/SOURCES.txt: -------------------------------------------------------------------------------- 1 | README.md 2 | setup.py 3 | mcqgenrator.egg-info/PKG-INFO 4 | mcqgenrator.egg-info/SOURCES.txt 5 | mcqgenrator.egg-info/dependency_links.txt 6 | mcqgenrator.egg-info/requires.txt 7 | mcqgenrator.egg-info/top_level.txt 8 | src/__init__.py 9 | src/mcqgenerator/__init__.py -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import find_packages,setup 2 | 3 | setup( 4 | name='mcqgenrator', 5 | version='0.0.1', 6 | author='sunny savita', 7 | author_email='sunny.savita@ineuron.ai', 8 | install_requires=["openai","langchain","streamlit","python-dotenv","PyPDF2"], 9 | packages=find_packages() 10 | ) -------------------------------------------------------------------------------- /src/mcqgenerator/logger.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import os 3 | from datetime import datetime 4 | 5 | 6 | LOG_FILE=f"{datetime.now().strftime('%m_%d_%Y_%H_%M_%S')}.log" 7 | 8 | log_path=os.path.join(os.getcwd(),"logs") 9 | 10 | os.makedirs(log_path,exist_ok=True) 11 | 12 | 13 | LOG_FILEPATH=os.path.join(log_path,LOG_FILE) 14 | 15 | 16 | logging.basicConfig(level=logging.INFO, 17 | filename=LOG_FILEPATH, 18 | format="[%(asctime)s] %(lineno)d %(name)s - %(levelname)s - %(message)s" 19 | ) 20 | -------------------------------------------------------------------------------- /experiment/machinelearning.csv: -------------------------------------------------------------------------------- 1 | MCQ,Choices,Correct 2 | Who coined the term machine learning?,a: Donald Hebb | b: Arthur Samuel | c: Walter Pitts | d: Warren McCulloch,b 3 | What was the earliest machine learning model introduced by Arthur Samuel?,a: Speech recognition | b: Image classification | c: Checkers game | d: Pattern recognition,c 4 | Which book introduced the Hebbian theory?,a: The Organization of Behavior | b: Learning Machines | c: Computing Machinery and Intelligence | d: The History of Machine Learning,a 5 | "In the 1960s, a learning machine called Cybertron was developed to analyze which of the following?",a: Sonar signals | b: Speech patterns | c: Electrocardiograms | d: All of the above,d 6 | "According to Tom M. Mitchell, what is the definition of machine learning?",a: Improving computer performance | b: Learning from experience to improve task performance | c: Analyzing cognitive processes | d: Developing neural networks,b 7 | -------------------------------------------------------------------------------- /data.txt: -------------------------------------------------------------------------------- 1 | Biology is the scientific study of life.[1][2][3] It is a natural science with a broad scope but has several unifying themes that tie it together as a single, coherent field.[1][2][3] For instance, all organisms are made up of cells that process hereditary information encoded in genes, which can be transmitted to future generations. Another major theme is evolution, which explains the unity and diversity of life.[1][2][3] Energy processing is also important to life as it allows organisms to move, grow, and reproduce.[1][2][3] Finally, all organisms are able to regulate their own internal environments.[1][2][3][4][5] 2 | 3 | Biologists are able to study life at multiple levels of organization,[1] from the molecular biology of a cell to the anatomy and physiology of plants and animals, and evolution of populations.[1][6] Hence, there are multiple subdisciplines within biology, each defined by the nature of their research questions and the tools that they use.[7][8][9] Like other scientists, biologists use the scientific method to make observations, pose questions, generate hypotheses, perform experiments, and form conclusions about the world around them.[1] 4 | 5 | Life on Earth, which emerged more than 3.7 billion years ago,[10] is immensely diverse. Biologists have sought to study and classify the various forms of life, from prokaryotic organisms such as archaea and bacteria to eukaryotic organisms such as protists, fungi, plants, and animals. These various organisms contribute to the biodiversity of an ecosystem, where they play specialized roles in the cycling of nutrients and energy through their biophysical environment. -------------------------------------------------------------------------------- /src/mcqgenerator/utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | import PyPDF2 3 | import json 4 | import traceback 5 | 6 | 7 | def read_file(file): 8 | if file.name.endswith(".pdf"): 9 | try: 10 | pdf_reader=PyPDF2.PdfFileReader(file) 11 | text="" 12 | for page in pdf_reader.pages: 13 | text+=page.extract_text() 14 | return text 15 | 16 | except Exception as e: 17 | raise Exception("error reading the PDF file") 18 | 19 | elif file.name.endswith(".txt"): 20 | return file.read().decode("utf-8") 21 | 22 | else: 23 | raise Exception( 24 | "unsupported file format only pdf and text file suppoted" 25 | ) 26 | 27 | def get_table_data(quiz_str): 28 | try: 29 | # convert the quiz from a str to dict 30 | quiz_dict=json.loads(quiz_str) 31 | quiz_table_data=[] 32 | 33 | # iterate over the quiz dictionary and extract the required information 34 | for key,value in quiz_dict.items(): 35 | mcq=value["mcq"] 36 | options=" || ".join( 37 | [ 38 | f"{option}-> {option_value}" for option, option_value in value["options"].items() 39 | 40 | ] 41 | ) 42 | 43 | correct=value["correct"] 44 | quiz_table_data.append({"MCQ": mcq,"Choices": options, "Correct": correct}) 45 | 46 | return quiz_table_data 47 | 48 | except Exception as e: 49 | traceback.print_exception(type(e), e, e.__traceback__) 50 | return False 51 | 52 | 53 | -------------------------------------------------------------------------------- /src/mcqgenerator/MCQGenerator.py: -------------------------------------------------------------------------------- 1 | import os 2 | import json 3 | import traceback 4 | import pandas as pd 5 | from dotenv import load_dotenv 6 | from src.mcqgenerator.utils import read_file,get_table_data 7 | from src.mcqgenerator.logger import logging 8 | 9 | #imporing necessary packages packages from langchain 10 | from langchain.chat_models import ChatOpenAI 11 | from langchain.prompts import PromptTemplate 12 | from langchain.chains import LLMChain 13 | from langchain.chains import SequentialChain 14 | 15 | 16 | # Load environment variables from the .env file 17 | load_dotenv() 18 | 19 | # Access the environment variables just like you would with os.environ 20 | key=os.getenv("OPENAI_API_KEY") 21 | 22 | 23 | llm = ChatOpenAI(openai_api_key=key,model_name="gpt-3.5-turbo", temperature=0.7) 24 | 25 | template=""" 26 | Text:{text} 27 | You are an expert MCQ maker. Given the above text, it is your job to \ 28 | create a quiz of {number} multiple choice questions for {subject} students in {tone} tone. 29 | Make sure the questions are not repeated and check all the questions to be conforming the text as well. 30 | Make sure to format your response like RESPONSE_JSON below and use it as a guide. \ 31 | Ensure to make {number} MCQs 32 | ### RESPONSE_JSON 33 | {response_json} 34 | 35 | """ 36 | 37 | quiz_generation_prompt = PromptTemplate( 38 | input_variables=["text", "number", "subject", "tone", "response_json"], 39 | template=template) 40 | 41 | 42 | quiz_chain=LLMChain(llm=llm,prompt=quiz_generation_prompt,output_key="quiz",verbose=True) 43 | 44 | 45 | template2=""" 46 | You are an expert english grammarian and writer. Given a Multiple Choice Quiz for {subject} students.\ 47 | You need to evaluate the complexity of the question and give a complete analysis of the quiz. Only use at max 50 words for complexity analysis. 48 | if the quiz is not at per with the cognitive and analytical abilities of the students,\ 49 | update the quiz questions which needs to be changed and change the tone such that it perfectly fits the student abilities 50 | Quiz_MCQs: 51 | {quiz} 52 | 53 | Check from an expert English Writer of the above quiz: 54 | """ 55 | 56 | 57 | quiz_evaluation_prompt=PromptTemplate(input_variables=["subject", "quiz"], template=template2) 58 | 59 | review_chain=LLMChain(llm=llm, prompt=quiz_evaluation_prompt, output_key="review", verbose=True) 60 | 61 | 62 | # This is an Overall Chain where we run the two chains in Sequence 63 | generate_evaluate_chain=SequentialChain(chains=[quiz_chain, review_chain], input_variables=["text", "number", "subject", "tone", "response_json"], 64 | output_variables=["quiz", "review"], verbose=True,) 65 | -------------------------------------------------------------------------------- /experiment/mcq.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import os\n", 10 | "import json\n", 11 | "import pandas as pd\n", 12 | "import traceback" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 4, 18 | "metadata": {}, 19 | "outputs": [], 20 | "source": [ 21 | "from langchain.chat_models import ChatOpenAI\n" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 5, 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "data": { 31 | "text/plain": [ 32 | "True" 33 | ] 34 | }, 35 | "execution_count": 5, 36 | "metadata": {}, 37 | "output_type": "execute_result" 38 | } 39 | ], 40 | "source": [ 41 | "from dotenv import load_dotenv\n", 42 | "\n", 43 | "load_dotenv() # take environment variables from .env." 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 6, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "KEY=os.getenv(\"OPENAI_API_KEY\")" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 8, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "llm=ChatOpenAI(openai_api_key=KEY,model_name=\"gpt-3.5-turbo\", temperature=0.5)" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 9, 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "data": { 71 | "text/plain": [ 72 | "ChatOpenAI(client=, async_client=, temperature=0.5, openai_api_key='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', openai_proxy='')" 73 | ] 74 | }, 75 | "execution_count": 9, 76 | "metadata": {}, 77 | "output_type": "execute_result" 78 | } 79 | ], 80 | "source": [ 81 | "llm" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 10, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [ 95 | "from langchain.llms import OpenAI\n", 96 | "from langchain.prompts import PromptTemplate\n", 97 | "from langchain.chains import LLMChain\n", 98 | "from langchain.chains import SequentialChain\n", 99 | "from langchain.callbacks import get_openai_callback\n", 100 | "import PyPDF2\n" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 11, 106 | "metadata": {}, 107 | "outputs": [], 108 | "source": [ 109 | "RESPONSE_JSON = {\n", 110 | " \"1\": {\n", 111 | " \"mcq\": \"multiple choice question\",\n", 112 | " \"options\": {\n", 113 | " \"a\": \"choice here\",\n", 114 | " \"b\": \"choice here\",\n", 115 | " \"c\": \"choice here\",\n", 116 | " \"d\": \"choice here\",\n", 117 | " },\n", 118 | " \"correct\": \"correct answer\",\n", 119 | " },\n", 120 | " \"2\": {\n", 121 | " \"mcq\": \"multiple choice question\",\n", 122 | " \"options\": {\n", 123 | " \"a\": \"choice here\",\n", 124 | " \"b\": \"choice here\",\n", 125 | " \"c\": \"choice here\",\n", 126 | " \"d\": \"choice here\",\n", 127 | " },\n", 128 | " \"correct\": \"correct answer\",\n", 129 | " },\n", 130 | " \"3\": {\n", 131 | " \"mcq\": \"multiple choice question\",\n", 132 | " \"options\": {\n", 133 | " \"a\": \"choice here\",\n", 134 | " \"b\": \"choice here\",\n", 135 | " \"c\": \"choice here\",\n", 136 | " \"d\": \"choice here\",\n", 137 | " },\n", 138 | " \"correct\": \"correct answer\",\n", 139 | " },\n", 140 | "}\n" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 12, 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [ 149 | "TEMPLATE=\"\"\"\n", 150 | "Text:{text}\n", 151 | "You are an expert MCQ maker. Given the above text, it is your job to \\\n", 152 | "create a quiz of {number} multiple choice questions for {subject} students in {tone} tone. \n", 153 | "Make sure the questions are not repeated and check all the questions to be conforming the text as well.\n", 154 | "Make sure to format your response like RESPONSE_JSON below and use it as a guide. \\\n", 155 | "Ensure to make {number} MCQs\n", 156 | "### RESPONSE_JSON\n", 157 | "{response_json}\n", 158 | "\n", 159 | "\"\"\"" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 13, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "quiz_generation_prompt = PromptTemplate(\n", 169 | " input_variables=[\"text\", \"number\", \"subject\", \"tone\", \"response_json\"],\n", 170 | " template=TEMPLATE\n", 171 | " )\n" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 14, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [ 180 | "quiz_chain=LLMChain(llm=llm, prompt=quiz_generation_prompt, output_key=\"quiz\", verbose=True)" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 15, 186 | "metadata": {}, 187 | "outputs": [], 188 | "source": [ 189 | "TEMPLATE2=\"\"\"\n", 190 | "You are an expert english grammarian and writer. Given a Multiple Choice Quiz for {subject} students.\\\n", 191 | "You need to evaluate the complexity of the question and give a complete analysis of the quiz. Only use at max 50 words for complexity analysis. \n", 192 | "if the quiz is not at per with the cognitive and analytical abilities of the students,\\\n", 193 | "update the quiz questions which needs to be changed and change the tone such that it perfectly fits the student abilities\n", 194 | "Quiz_MCQs:\n", 195 | "{quiz}\n", 196 | "\n", 197 | "Check from an expert English Writer of the above quiz:\n", 198 | "\"\"\"" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 16, 204 | "metadata": {}, 205 | "outputs": [], 206 | "source": [ 207 | "quiz_evaluation_prompt=PromptTemplate(input_variables=[\"subject\", \"quiz\"], template=TEMPLATE)" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 17, 213 | "metadata": {}, 214 | "outputs": [], 215 | "source": [ 216 | "review_chain=LLMChain(llm=llm, prompt=quiz_evaluation_prompt, output_key=\"review\", verbose=True)" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": 18, 222 | "metadata": {}, 223 | "outputs": [], 224 | "source": [ 225 | "generate_evaluate_chain=SequentialChain(chains=[quiz_chain, review_chain], input_variables=[\"text\", \"number\", \"subject\", \"tone\", \"response_json\"],\n", 226 | " output_variables=[\"quiz\", \"review\"], verbose=True,)" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 19, 232 | "metadata": {}, 233 | "outputs": [], 234 | "source": [ 235 | "file_path=r\"C:\\Users\\sunny\\mcqgen\\data.txt\"" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 20, 241 | "metadata": {}, 242 | "outputs": [ 243 | { 244 | "data": { 245 | "text/plain": [ 246 | "'C:\\\\Users\\\\sunny\\\\mcqgen\\\\data.txt'" 247 | ] 248 | }, 249 | "execution_count": 20, 250 | "metadata": {}, 251 | "output_type": "execute_result" 252 | } 253 | ], 254 | "source": [ 255 | "file_path" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 39, 261 | "metadata": {}, 262 | "outputs": [], 263 | "source": [ 264 | "with open(file_path, 'r') as file:\n", 265 | " TEXT = file.read()" 266 | ] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "execution_count": 40, 271 | "metadata": {}, 272 | "outputs": [ 273 | { 274 | "name": "stdout", 275 | "output_type": "stream", 276 | "text": [ 277 | "Biology is the scientific study of life.[1][2][3] It is a natural science with a broad scope but has several unifying themes that tie it together as a single, coherent field.[1][2][3] For instance, all organisms are made up of cells that process hereditary information encoded in genes, which can be transmitted to future generations. Another major theme is evolution, which explains the unity and diversity of life.[1][2][3] Energy processing is also important to life as it allows organisms to move, grow, and reproduce.[1][2][3] Finally, all organisms are able to regulate their own internal environments.[1][2][3][4][5]\n", 278 | "\n", 279 | "Biologists are able to study life at multiple levels of organization,[1] from the molecular biology of a cell to the anatomy and physiology of plants and animals, and evolution of populations.[1][6] Hence, there are multiple subdisciplines within biology, each defined by the nature of their research questions and the tools that they use.[7][8][9] Like other scientists, biologists use the scientific method to make observations, pose questions, generate hypotheses, perform experiments, and form conclusions about the world around them.[1]\n", 280 | "\n", 281 | "Life on Earth, which emerged more than 3.7 billion years ago,[10] is immensely diverse. Biologists have sought to study and classify the various forms of life, from prokaryotic organisms such as archaea and bacteria to eukaryotic organisms such as protists, fungi, plants, and animals. These various organisms contribute to the biodiversity of an ecosystem, where they play specialized roles in the cycling of nutrients and energy through their biophysical environment.\n" 282 | ] 283 | } 284 | ], 285 | "source": [ 286 | "print(TEXT)" 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": 42, 292 | "metadata": {}, 293 | "outputs": [ 294 | { 295 | "data": { 296 | "text/plain": [ 297 | "'{\"1\": {\"mcq\": \"multiple choice question\", \"options\": {\"a\": \"choice here\", \"b\": \"choice here\", \"c\": \"choice here\", \"d\": \"choice here\"}, \"correct\": \"correct answer\"}, \"2\": {\"mcq\": \"multiple choice question\", \"options\": {\"a\": \"choice here\", \"b\": \"choice here\", \"c\": \"choice here\", \"d\": \"choice here\"}, \"correct\": \"correct answer\"}, \"3\": {\"mcq\": \"multiple choice question\", \"options\": {\"a\": \"choice here\", \"b\": \"choice here\", \"c\": \"choice here\", \"d\": \"choice here\"}, \"correct\": \"correct answer\"}}'" 298 | ] 299 | }, 300 | "execution_count": 42, 301 | "metadata": {}, 302 | "output_type": "execute_result" 303 | } 304 | ], 305 | "source": [ 306 | "# Serialize the Python dictionary into a JSON-formatted string\n", 307 | "json.dumps(RESPONSE_JSON)" 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": 41, 313 | "metadata": {}, 314 | "outputs": [], 315 | "source": [ 316 | "NUMBER=5 \n", 317 | "SUBJECT=\"biology\"\n", 318 | "TONE=\"simple\"\n" 319 | ] 320 | }, 321 | { 322 | "cell_type": "code", 323 | "execution_count": 43, 324 | "metadata": {}, 325 | "outputs": [ 326 | { 327 | "name": "stdout", 328 | "output_type": "stream", 329 | "text": [ 330 | "\n", 331 | "\n", 332 | "\u001b[1m> Entering new SequentialChain chain...\u001b[0m\n", 333 | "\n", 334 | "\n", 335 | "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", 336 | "Prompt after formatting:\n", 337 | "\u001b[32;1m\u001b[1;3m\n", 338 | "Text:Biology is the scientific study of life.[1][2][3] It is a natural science with a broad scope but has several unifying themes that tie it together as a single, coherent field.[1][2][3] For instance, all organisms are made up of cells that process hereditary information encoded in genes, which can be transmitted to future generations. Another major theme is evolution, which explains the unity and diversity of life.[1][2][3] Energy processing is also important to life as it allows organisms to move, grow, and reproduce.[1][2][3] Finally, all organisms are able to regulate their own internal environments.[1][2][3][4][5]\n", 339 | "\n", 340 | "Biologists are able to study life at multiple levels of organization,[1] from the molecular biology of a cell to the anatomy and physiology of plants and animals, and evolution of populations.[1][6] Hence, there are multiple subdisciplines within biology, each defined by the nature of their research questions and the tools that they use.[7][8][9] Like other scientists, biologists use the scientific method to make observations, pose questions, generate hypotheses, perform experiments, and form conclusions about the world around them.[1]\n", 341 | "\n", 342 | "Life on Earth, which emerged more than 3.7 billion years ago,[10] is immensely diverse. Biologists have sought to study and classify the various forms of life, from prokaryotic organisms such as archaea and bacteria to eukaryotic organisms such as protists, fungi, plants, and animals. These various organisms contribute to the biodiversity of an ecosystem, where they play specialized roles in the cycling of nutrients and energy through their biophysical environment.\n", 343 | "You are an expert MCQ maker. Given the above text, it is your job to create a quiz of 5 multiple choice questions for biology students in simple tone. \n", 344 | "Make sure the questions are not repeated and check all the questions to be conforming the text as well.\n", 345 | "Make sure to format your response like RESPONSE_JSON below and use it as a guide. Ensure to make 5 MCQs\n", 346 | "### RESPONSE_JSON\n", 347 | "{\"1\": {\"mcq\": \"multiple choice question\", \"options\": {\"a\": \"choice here\", \"b\": \"choice here\", \"c\": \"choice here\", \"d\": \"choice here\"}, \"correct\": \"correct answer\"}, \"2\": {\"mcq\": \"multiple choice question\", \"options\": {\"a\": \"choice here\", \"b\": \"choice here\", \"c\": \"choice here\", \"d\": \"choice here\"}, \"correct\": \"correct answer\"}, \"3\": {\"mcq\": \"multiple choice question\", \"options\": {\"a\": \"choice here\", \"b\": \"choice here\", \"c\": \"choice here\", \"d\": \"choice here\"}, \"correct\": \"correct answer\"}}\n", 348 | "\n", 349 | "\u001b[0m\n", 350 | "\n", 351 | "\u001b[1m> Finished chain.\u001b[0m\n", 352 | "\n", 353 | "\n", 354 | "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", 355 | "Prompt after formatting:\n", 356 | "\u001b[32;1m\u001b[1;3m\n", 357 | "Text:Biology is the scientific study of life.[1][2][3] It is a natural science with a broad scope but has several unifying themes that tie it together as a single, coherent field.[1][2][3] For instance, all organisms are made up of cells that process hereditary information encoded in genes, which can be transmitted to future generations. Another major theme is evolution, which explains the unity and diversity of life.[1][2][3] Energy processing is also important to life as it allows organisms to move, grow, and reproduce.[1][2][3] Finally, all organisms are able to regulate their own internal environments.[1][2][3][4][5]\n", 358 | "\n", 359 | "Biologists are able to study life at multiple levels of organization,[1] from the molecular biology of a cell to the anatomy and physiology of plants and animals, and evolution of populations.[1][6] Hence, there are multiple subdisciplines within biology, each defined by the nature of their research questions and the tools that they use.[7][8][9] Like other scientists, biologists use the scientific method to make observations, pose questions, generate hypotheses, perform experiments, and form conclusions about the world around them.[1]\n", 360 | "\n", 361 | "Life on Earth, which emerged more than 3.7 billion years ago,[10] is immensely diverse. Biologists have sought to study and classify the various forms of life, from prokaryotic organisms such as archaea and bacteria to eukaryotic organisms such as protists, fungi, plants, and animals. These various organisms contribute to the biodiversity of an ecosystem, where they play specialized roles in the cycling of nutrients and energy through their biophysical environment.\n", 362 | "You are an expert MCQ maker. Given the above text, it is your job to create a quiz of 5 multiple choice questions for biology students in simple tone. \n", 363 | "Make sure the questions are not repeated and check all the questions to be conforming the text as well.\n", 364 | "Make sure to format your response like RESPONSE_JSON below and use it as a guide. Ensure to make 5 MCQs\n", 365 | "### RESPONSE_JSON\n", 366 | "{\"1\": {\"mcq\": \"multiple choice question\", \"options\": {\"a\": \"choice here\", \"b\": \"choice here\", \"c\": \"choice here\", \"d\": \"choice here\"}, \"correct\": \"correct answer\"}, \"2\": {\"mcq\": \"multiple choice question\", \"options\": {\"a\": \"choice here\", \"b\": \"choice here\", \"c\": \"choice here\", \"d\": \"choice here\"}, \"correct\": \"correct answer\"}, \"3\": {\"mcq\": \"multiple choice question\", \"options\": {\"a\": \"choice here\", \"b\": \"choice here\", \"c\": \"choice here\", \"d\": \"choice here\"}, \"correct\": \"correct answer\"}}\n", 367 | "\n", 368 | "\u001b[0m\n" 369 | ] 370 | }, 371 | { 372 | "ename": "AuthenticationError", 373 | "evalue": "Error code: 401 - {'error': {'message': 'Incorrect API key provided: sk-jey3R***************************************FFpj. You can find your API key at https://platform.openai.com/account/api-keys.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_api_key'}}", 374 | "output_type": "error", 375 | "traceback": [ 376 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 377 | "\u001b[1;31mAuthenticationError\u001b[0m Traceback (most recent call last)", 378 | "\u001b[1;32mc:\\Users\\sunny\\mcqgen\\experiment\\mcq.ipynb Cell 23\u001b[0m line \u001b[0;36m5\n\u001b[0;32m 1\u001b[0m \u001b[39m#https://python.langchain.com/docs/modules/model_io/llms/token_usage_tracking\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \n\u001b[0;32m 3\u001b[0m \u001b[39m#How to setup Token Usage Tracking in LangChain\u001b[39;00m\n\u001b[0;32m 4\u001b[0m \u001b[39mwith\u001b[39;00m get_openai_callback() \u001b[39mas\u001b[39;00m cb:\n\u001b[1;32m----> 5\u001b[0m response\u001b[39m=\u001b[39mgenerate_evaluate_chain(\n\u001b[0;32m 6\u001b[0m {\n\u001b[0;32m 7\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mtext\u001b[39;49m\u001b[39m\"\u001b[39;49m: TEXT,\n\u001b[0;32m 8\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mnumber\u001b[39;49m\u001b[39m\"\u001b[39;49m: NUMBER,\n\u001b[0;32m 9\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39msubject\u001b[39;49m\u001b[39m\"\u001b[39;49m:SUBJECT,\n\u001b[0;32m 10\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mtone\u001b[39;49m\u001b[39m\"\u001b[39;49m: TONE,\n\u001b[0;32m 11\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mresponse_json\u001b[39;49m\u001b[39m\"\u001b[39;49m: json\u001b[39m.\u001b[39;49mdumps(RESPONSE_JSON)\n\u001b[0;32m 12\u001b[0m }\n\u001b[0;32m 13\u001b[0m )\n", 379 | "File \u001b[1;32mc:\\Users\\sunny\\mcqgen\\env\\lib\\site-packages\\langchain\\chains\\base.py:312\u001b[0m, in \u001b[0;36mChain.__call__\u001b[1;34m(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)\u001b[0m\n\u001b[0;32m 310\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mBaseException\u001b[39;00m \u001b[39mas\u001b[39;00m e:\n\u001b[0;32m 311\u001b[0m run_manager\u001b[39m.\u001b[39mon_chain_error(e)\n\u001b[1;32m--> 312\u001b[0m \u001b[39mraise\u001b[39;00m e\n\u001b[0;32m 313\u001b[0m run_manager\u001b[39m.\u001b[39mon_chain_end(outputs)\n\u001b[0;32m 314\u001b[0m final_outputs: Dict[\u001b[39mstr\u001b[39m, Any] \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mprep_outputs(\n\u001b[0;32m 315\u001b[0m inputs, outputs, return_only_outputs\n\u001b[0;32m 316\u001b[0m )\n", 380 | "File \u001b[1;32mc:\\Users\\sunny\\mcqgen\\env\\lib\\site-packages\\langchain\\chains\\base.py:306\u001b[0m, in \u001b[0;36mChain.__call__\u001b[1;34m(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)\u001b[0m\n\u001b[0;32m 299\u001b[0m run_manager \u001b[39m=\u001b[39m callback_manager\u001b[39m.\u001b[39mon_chain_start(\n\u001b[0;32m 300\u001b[0m dumpd(\u001b[39mself\u001b[39m),\n\u001b[0;32m 301\u001b[0m inputs,\n\u001b[0;32m 302\u001b[0m name\u001b[39m=\u001b[39mrun_name,\n\u001b[0;32m 303\u001b[0m )\n\u001b[0;32m 304\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m 305\u001b[0m outputs \u001b[39m=\u001b[39m (\n\u001b[1;32m--> 306\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_call(inputs, run_manager\u001b[39m=\u001b[39;49mrun_manager)\n\u001b[0;32m 307\u001b[0m \u001b[39mif\u001b[39;00m new_arg_supported\n\u001b[0;32m 308\u001b[0m \u001b[39melse\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_call(inputs)\n\u001b[0;32m 309\u001b[0m )\n\u001b[0;32m 310\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mBaseException\u001b[39;00m \u001b[39mas\u001b[39;00m e:\n\u001b[0;32m 311\u001b[0m run_manager\u001b[39m.\u001b[39mon_chain_error(e)\n", 381 | "File \u001b[1;32mc:\\Users\\sunny\\mcqgen\\env\\lib\\site-packages\\langchain\\chains\\sequential.py:105\u001b[0m, in \u001b[0;36mSequentialChain._call\u001b[1;34m(self, inputs, run_manager)\u001b[0m\n\u001b[0;32m 103\u001b[0m \u001b[39mfor\u001b[39;00m i, chain \u001b[39min\u001b[39;00m \u001b[39menumerate\u001b[39m(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mchains):\n\u001b[0;32m 104\u001b[0m callbacks \u001b[39m=\u001b[39m _run_manager\u001b[39m.\u001b[39mget_child()\n\u001b[1;32m--> 105\u001b[0m outputs \u001b[39m=\u001b[39m chain(known_values, return_only_outputs\u001b[39m=\u001b[39;49m\u001b[39mTrue\u001b[39;49;00m, callbacks\u001b[39m=\u001b[39;49mcallbacks)\n\u001b[0;32m 106\u001b[0m known_values\u001b[39m.\u001b[39mupdate(outputs)\n\u001b[0;32m 107\u001b[0m \u001b[39mreturn\u001b[39;00m {k: known_values[k] \u001b[39mfor\u001b[39;00m k \u001b[39min\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39moutput_variables}\n", 382 | "File \u001b[1;32mc:\\Users\\sunny\\mcqgen\\env\\lib\\site-packages\\langchain\\chains\\base.py:312\u001b[0m, in \u001b[0;36mChain.__call__\u001b[1;34m(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)\u001b[0m\n\u001b[0;32m 310\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mBaseException\u001b[39;00m \u001b[39mas\u001b[39;00m e:\n\u001b[0;32m 311\u001b[0m run_manager\u001b[39m.\u001b[39mon_chain_error(e)\n\u001b[1;32m--> 312\u001b[0m \u001b[39mraise\u001b[39;00m e\n\u001b[0;32m 313\u001b[0m run_manager\u001b[39m.\u001b[39mon_chain_end(outputs)\n\u001b[0;32m 314\u001b[0m final_outputs: Dict[\u001b[39mstr\u001b[39m, Any] \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mprep_outputs(\n\u001b[0;32m 315\u001b[0m inputs, outputs, return_only_outputs\n\u001b[0;32m 316\u001b[0m )\n", 383 | "File \u001b[1;32mc:\\Users\\sunny\\mcqgen\\env\\lib\\site-packages\\langchain\\chains\\base.py:306\u001b[0m, in \u001b[0;36mChain.__call__\u001b[1;34m(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)\u001b[0m\n\u001b[0;32m 299\u001b[0m run_manager \u001b[39m=\u001b[39m callback_manager\u001b[39m.\u001b[39mon_chain_start(\n\u001b[0;32m 300\u001b[0m dumpd(\u001b[39mself\u001b[39m),\n\u001b[0;32m 301\u001b[0m inputs,\n\u001b[0;32m 302\u001b[0m name\u001b[39m=\u001b[39mrun_name,\n\u001b[0;32m 303\u001b[0m )\n\u001b[0;32m 304\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m 305\u001b[0m outputs \u001b[39m=\u001b[39m (\n\u001b[1;32m--> 306\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_call(inputs, run_manager\u001b[39m=\u001b[39;49mrun_manager)\n\u001b[0;32m 307\u001b[0m \u001b[39mif\u001b[39;00m new_arg_supported\n\u001b[0;32m 308\u001b[0m \u001b[39melse\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_call(inputs)\n\u001b[0;32m 309\u001b[0m )\n\u001b[0;32m 310\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mBaseException\u001b[39;00m \u001b[39mas\u001b[39;00m e:\n\u001b[0;32m 311\u001b[0m run_manager\u001b[39m.\u001b[39mon_chain_error(e)\n", 384 | "File \u001b[1;32mc:\\Users\\sunny\\mcqgen\\env\\lib\\site-packages\\langchain\\chains\\llm.py:103\u001b[0m, in \u001b[0;36mLLMChain._call\u001b[1;34m(self, inputs, run_manager)\u001b[0m\n\u001b[0;32m 98\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m_call\u001b[39m(\n\u001b[0;32m 99\u001b[0m \u001b[39mself\u001b[39m,\n\u001b[0;32m 100\u001b[0m inputs: Dict[\u001b[39mstr\u001b[39m, Any],\n\u001b[0;32m 101\u001b[0m run_manager: Optional[CallbackManagerForChainRun] \u001b[39m=\u001b[39m \u001b[39mNone\u001b[39;00m,\n\u001b[0;32m 102\u001b[0m ) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m Dict[\u001b[39mstr\u001b[39m, \u001b[39mstr\u001b[39m]:\n\u001b[1;32m--> 103\u001b[0m response \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mgenerate([inputs], run_manager\u001b[39m=\u001b[39;49mrun_manager)\n\u001b[0;32m 104\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mcreate_outputs(response)[\u001b[39m0\u001b[39m]\n", 385 | "File \u001b[1;32mc:\\Users\\sunny\\mcqgen\\env\\lib\\site-packages\\langchain\\chains\\llm.py:115\u001b[0m, in \u001b[0;36mLLMChain.generate\u001b[1;34m(self, input_list, run_manager)\u001b[0m\n\u001b[0;32m 113\u001b[0m callbacks \u001b[39m=\u001b[39m run_manager\u001b[39m.\u001b[39mget_child() \u001b[39mif\u001b[39;00m run_manager \u001b[39melse\u001b[39;00m \u001b[39mNone\u001b[39;00m\n\u001b[0;32m 114\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39misinstance\u001b[39m(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mllm, BaseLanguageModel):\n\u001b[1;32m--> 115\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mllm\u001b[39m.\u001b[39;49mgenerate_prompt(\n\u001b[0;32m 116\u001b[0m prompts,\n\u001b[0;32m 117\u001b[0m stop,\n\u001b[0;32m 118\u001b[0m callbacks\u001b[39m=\u001b[39;49mcallbacks,\n\u001b[0;32m 119\u001b[0m \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49m\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mllm_kwargs,\n\u001b[0;32m 120\u001b[0m )\n\u001b[0;32m 121\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m 122\u001b[0m results \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mllm\u001b[39m.\u001b[39mbind(stop\u001b[39m=\u001b[39mstop, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mllm_kwargs)\u001b[39m.\u001b[39mbatch(\n\u001b[0;32m 123\u001b[0m cast(List, prompts), {\u001b[39m\"\u001b[39m\u001b[39mcallbacks\u001b[39m\u001b[39m\"\u001b[39m: callbacks}\n\u001b[0;32m 124\u001b[0m )\n", 386 | "File \u001b[1;32mc:\\Users\\sunny\\mcqgen\\env\\lib\\site-packages\\langchain_core\\language_models\\chat_models.py:491\u001b[0m, in \u001b[0;36mBaseChatModel.generate_prompt\u001b[1;34m(self, prompts, stop, callbacks, **kwargs)\u001b[0m\n\u001b[0;32m 483\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mgenerate_prompt\u001b[39m(\n\u001b[0;32m 484\u001b[0m \u001b[39mself\u001b[39m,\n\u001b[0;32m 485\u001b[0m prompts: List[PromptValue],\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 488\u001b[0m \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs: Any,\n\u001b[0;32m 489\u001b[0m ) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m LLMResult:\n\u001b[0;32m 490\u001b[0m prompt_messages \u001b[39m=\u001b[39m [p\u001b[39m.\u001b[39mto_messages() \u001b[39mfor\u001b[39;00m p \u001b[39min\u001b[39;00m prompts]\n\u001b[1;32m--> 491\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mgenerate(prompt_messages, stop\u001b[39m=\u001b[39;49mstop, callbacks\u001b[39m=\u001b[39;49mcallbacks, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n", 387 | "File \u001b[1;32mc:\\Users\\sunny\\mcqgen\\env\\lib\\site-packages\\langchain_core\\language_models\\chat_models.py:378\u001b[0m, in \u001b[0;36mBaseChatModel.generate\u001b[1;34m(self, messages, stop, callbacks, tags, metadata, run_name, **kwargs)\u001b[0m\n\u001b[0;32m 376\u001b[0m \u001b[39mif\u001b[39;00m run_managers:\n\u001b[0;32m 377\u001b[0m run_managers[i]\u001b[39m.\u001b[39mon_llm_error(e, response\u001b[39m=\u001b[39mLLMResult(generations\u001b[39m=\u001b[39m[]))\n\u001b[1;32m--> 378\u001b[0m \u001b[39mraise\u001b[39;00m e\n\u001b[0;32m 379\u001b[0m flattened_outputs \u001b[39m=\u001b[39m [\n\u001b[0;32m 380\u001b[0m LLMResult(generations\u001b[39m=\u001b[39m[res\u001b[39m.\u001b[39mgenerations], llm_output\u001b[39m=\u001b[39mres\u001b[39m.\u001b[39mllm_output)\n\u001b[0;32m 381\u001b[0m \u001b[39mfor\u001b[39;00m res \u001b[39min\u001b[39;00m results\n\u001b[0;32m 382\u001b[0m ]\n\u001b[0;32m 383\u001b[0m llm_output \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_combine_llm_outputs([res\u001b[39m.\u001b[39mllm_output \u001b[39mfor\u001b[39;00m res \u001b[39min\u001b[39;00m results])\n", 388 | "File \u001b[1;32mc:\\Users\\sunny\\mcqgen\\env\\lib\\site-packages\\langchain_core\\language_models\\chat_models.py:368\u001b[0m, in \u001b[0;36mBaseChatModel.generate\u001b[1;34m(self, messages, stop, callbacks, tags, metadata, run_name, **kwargs)\u001b[0m\n\u001b[0;32m 365\u001b[0m \u001b[39mfor\u001b[39;00m i, m \u001b[39min\u001b[39;00m \u001b[39menumerate\u001b[39m(messages):\n\u001b[0;32m 366\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m 367\u001b[0m results\u001b[39m.\u001b[39mappend(\n\u001b[1;32m--> 368\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_generate_with_cache(\n\u001b[0;32m 369\u001b[0m m,\n\u001b[0;32m 370\u001b[0m stop\u001b[39m=\u001b[39;49mstop,\n\u001b[0;32m 371\u001b[0m run_manager\u001b[39m=\u001b[39;49mrun_managers[i] \u001b[39mif\u001b[39;49;00m run_managers \u001b[39melse\u001b[39;49;00m \u001b[39mNone\u001b[39;49;00m,\n\u001b[0;32m 372\u001b[0m \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs,\n\u001b[0;32m 373\u001b[0m )\n\u001b[0;32m 374\u001b[0m )\n\u001b[0;32m 375\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mBaseException\u001b[39;00m \u001b[39mas\u001b[39;00m e:\n\u001b[0;32m 376\u001b[0m \u001b[39mif\u001b[39;00m run_managers:\n", 389 | "File \u001b[1;32mc:\\Users\\sunny\\mcqgen\\env\\lib\\site-packages\\langchain_core\\language_models\\chat_models.py:524\u001b[0m, in \u001b[0;36mBaseChatModel._generate_with_cache\u001b[1;34m(self, messages, stop, run_manager, **kwargs)\u001b[0m\n\u001b[0;32m 520\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\n\u001b[0;32m 521\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mAsked to cache, but no cache found at `langchain.cache`.\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m 522\u001b[0m )\n\u001b[0;32m 523\u001b[0m \u001b[39mif\u001b[39;00m new_arg_supported:\n\u001b[1;32m--> 524\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_generate(\n\u001b[0;32m 525\u001b[0m messages, stop\u001b[39m=\u001b[39;49mstop, run_manager\u001b[39m=\u001b[39;49mrun_manager, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs\n\u001b[0;32m 526\u001b[0m )\n\u001b[0;32m 527\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m 528\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_generate(messages, stop\u001b[39m=\u001b[39mstop, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n", 390 | "File \u001b[1;32mc:\\Users\\sunny\\mcqgen\\env\\lib\\site-packages\\langchain\\chat_models\\openai.py:426\u001b[0m, in \u001b[0;36mChatOpenAI._generate\u001b[1;34m(self, messages, stop, run_manager, stream, **kwargs)\u001b[0m\n\u001b[0;32m 420\u001b[0m message_dicts, params \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_create_message_dicts(messages, stop)\n\u001b[0;32m 421\u001b[0m params \u001b[39m=\u001b[39m {\n\u001b[0;32m 422\u001b[0m \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mparams,\n\u001b[0;32m 423\u001b[0m \u001b[39m*\u001b[39m\u001b[39m*\u001b[39m({\u001b[39m\"\u001b[39m\u001b[39mstream\u001b[39m\u001b[39m\"\u001b[39m: stream} \u001b[39mif\u001b[39;00m stream \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39melse\u001b[39;00m {}),\n\u001b[0;32m 424\u001b[0m \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs,\n\u001b[0;32m 425\u001b[0m }\n\u001b[1;32m--> 426\u001b[0m response \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mcompletion_with_retry(\n\u001b[0;32m 427\u001b[0m messages\u001b[39m=\u001b[39;49mmessage_dicts, run_manager\u001b[39m=\u001b[39;49mrun_manager, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mparams\n\u001b[0;32m 428\u001b[0m )\n\u001b[0;32m 429\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_create_chat_result(response)\n", 391 | "File \u001b[1;32mc:\\Users\\sunny\\mcqgen\\env\\lib\\site-packages\\langchain\\chat_models\\openai.py:344\u001b[0m, in \u001b[0;36mChatOpenAI.completion_with_retry\u001b[1;34m(self, run_manager, **kwargs)\u001b[0m\n\u001b[0;32m 342\u001b[0m \u001b[39m\u001b[39m\u001b[39m\"\"\"Use tenacity to retry the completion call.\"\"\"\u001b[39;00m\n\u001b[0;32m 343\u001b[0m \u001b[39mif\u001b[39;00m is_openai_v1():\n\u001b[1;32m--> 344\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mclient\u001b[39m.\u001b[39;49mcreate(\u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n\u001b[0;32m 346\u001b[0m retry_decorator \u001b[39m=\u001b[39m _create_retry_decorator(\u001b[39mself\u001b[39m, run_manager\u001b[39m=\u001b[39mrun_manager)\n\u001b[0;32m 348\u001b[0m \u001b[39m@retry_decorator\u001b[39m\n\u001b[0;32m 349\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m_completion_with_retry\u001b[39m(\u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs: Any) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m Any:\n", 392 | "File \u001b[1;32mc:\\Users\\sunny\\mcqgen\\env\\lib\\site-packages\\openai\\_utils\\_utils.py:303\u001b[0m, in \u001b[0;36mrequired_args..inner..wrapper\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m 301\u001b[0m msg \u001b[39m=\u001b[39m \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mMissing required argument: \u001b[39m\u001b[39m{\u001b[39;00mquote(missing[\u001b[39m0\u001b[39m])\u001b[39m}\u001b[39;00m\u001b[39m\"\u001b[39m\n\u001b[0;32m 302\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mTypeError\u001b[39;00m(msg)\n\u001b[1;32m--> 303\u001b[0m \u001b[39mreturn\u001b[39;00m func(\u001b[39m*\u001b[39;49margs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n", 393 | "File \u001b[1;32mc:\\Users\\sunny\\mcqgen\\env\\lib\\site-packages\\openai\\resources\\chat\\completions.py:598\u001b[0m, in \u001b[0;36mCompletions.create\u001b[1;34m(self, messages, model, frequency_penalty, function_call, functions, logit_bias, max_tokens, n, presence_penalty, response_format, seed, stop, stream, temperature, tool_choice, tools, top_p, user, extra_headers, extra_query, extra_body, timeout)\u001b[0m\n\u001b[0;32m 551\u001b[0m \u001b[39m@required_args\u001b[39m([\u001b[39m\"\u001b[39m\u001b[39mmessages\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m\"\u001b[39m\u001b[39mmodel\u001b[39m\u001b[39m\"\u001b[39m], [\u001b[39m\"\u001b[39m\u001b[39mmessages\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m\"\u001b[39m\u001b[39mmodel\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m\"\u001b[39m\u001b[39mstream\u001b[39m\u001b[39m\"\u001b[39m])\n\u001b[0;32m 552\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mcreate\u001b[39m(\n\u001b[0;32m 553\u001b[0m \u001b[39mself\u001b[39m,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 596\u001b[0m timeout: \u001b[39mfloat\u001b[39m \u001b[39m|\u001b[39m httpx\u001b[39m.\u001b[39mTimeout \u001b[39m|\u001b[39m \u001b[39mNone\u001b[39;00m \u001b[39m|\u001b[39m NotGiven \u001b[39m=\u001b[39m NOT_GIVEN,\n\u001b[0;32m 597\u001b[0m ) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m ChatCompletion \u001b[39m|\u001b[39m Stream[ChatCompletionChunk]:\n\u001b[1;32m--> 598\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_post(\n\u001b[0;32m 599\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39m/chat/completions\u001b[39;49m\u001b[39m\"\u001b[39;49m,\n\u001b[0;32m 600\u001b[0m body\u001b[39m=\u001b[39;49mmaybe_transform(\n\u001b[0;32m 601\u001b[0m {\n\u001b[0;32m 602\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mmessages\u001b[39;49m\u001b[39m\"\u001b[39;49m: messages,\n\u001b[0;32m 603\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mmodel\u001b[39;49m\u001b[39m\"\u001b[39;49m: model,\n\u001b[0;32m 604\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mfrequency_penalty\u001b[39;49m\u001b[39m\"\u001b[39;49m: frequency_penalty,\n\u001b[0;32m 605\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mfunction_call\u001b[39;49m\u001b[39m\"\u001b[39;49m: function_call,\n\u001b[0;32m 606\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mfunctions\u001b[39;49m\u001b[39m\"\u001b[39;49m: functions,\n\u001b[0;32m 607\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mlogit_bias\u001b[39;49m\u001b[39m\"\u001b[39;49m: logit_bias,\n\u001b[0;32m 608\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mmax_tokens\u001b[39;49m\u001b[39m\"\u001b[39;49m: max_tokens,\n\u001b[0;32m 609\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mn\u001b[39;49m\u001b[39m\"\u001b[39;49m: n,\n\u001b[0;32m 610\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mpresence_penalty\u001b[39;49m\u001b[39m\"\u001b[39;49m: presence_penalty,\n\u001b[0;32m 611\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mresponse_format\u001b[39;49m\u001b[39m\"\u001b[39;49m: response_format,\n\u001b[0;32m 612\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mseed\u001b[39;49m\u001b[39m\"\u001b[39;49m: seed,\n\u001b[0;32m 613\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mstop\u001b[39;49m\u001b[39m\"\u001b[39;49m: stop,\n\u001b[0;32m 614\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mstream\u001b[39;49m\u001b[39m\"\u001b[39;49m: stream,\n\u001b[0;32m 615\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mtemperature\u001b[39;49m\u001b[39m\"\u001b[39;49m: temperature,\n\u001b[0;32m 616\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mtool_choice\u001b[39;49m\u001b[39m\"\u001b[39;49m: tool_choice,\n\u001b[0;32m 617\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mtools\u001b[39;49m\u001b[39m\"\u001b[39;49m: tools,\n\u001b[0;32m 618\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mtop_p\u001b[39;49m\u001b[39m\"\u001b[39;49m: top_p,\n\u001b[0;32m 619\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39muser\u001b[39;49m\u001b[39m\"\u001b[39;49m: user,\n\u001b[0;32m 620\u001b[0m },\n\u001b[0;32m 621\u001b[0m completion_create_params\u001b[39m.\u001b[39;49mCompletionCreateParams,\n\u001b[0;32m 622\u001b[0m ),\n\u001b[0;32m 623\u001b[0m options\u001b[39m=\u001b[39;49mmake_request_options(\n\u001b[0;32m 624\u001b[0m extra_headers\u001b[39m=\u001b[39;49mextra_headers, extra_query\u001b[39m=\u001b[39;49mextra_query, extra_body\u001b[39m=\u001b[39;49mextra_body, timeout\u001b[39m=\u001b[39;49mtimeout\n\u001b[0;32m 625\u001b[0m ),\n\u001b[0;32m 626\u001b[0m cast_to\u001b[39m=\u001b[39;49mChatCompletion,\n\u001b[0;32m 627\u001b[0m stream\u001b[39m=\u001b[39;49mstream \u001b[39mor\u001b[39;49;00m \u001b[39mFalse\u001b[39;49;00m,\n\u001b[0;32m 628\u001b[0m stream_cls\u001b[39m=\u001b[39;49mStream[ChatCompletionChunk],\n\u001b[0;32m 629\u001b[0m )\n", 394 | "File \u001b[1;32mc:\\Users\\sunny\\mcqgen\\env\\lib\\site-packages\\openai\\_base_client.py:1086\u001b[0m, in \u001b[0;36mSyncAPIClient.post\u001b[1;34m(self, path, cast_to, body, options, files, stream, stream_cls)\u001b[0m\n\u001b[0;32m 1072\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mpost\u001b[39m(\n\u001b[0;32m 1073\u001b[0m \u001b[39mself\u001b[39m,\n\u001b[0;32m 1074\u001b[0m path: \u001b[39mstr\u001b[39m,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 1081\u001b[0m stream_cls: \u001b[39mtype\u001b[39m[_StreamT] \u001b[39m|\u001b[39m \u001b[39mNone\u001b[39;00m \u001b[39m=\u001b[39m \u001b[39mNone\u001b[39;00m,\n\u001b[0;32m 1082\u001b[0m ) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m ResponseT \u001b[39m|\u001b[39m _StreamT:\n\u001b[0;32m 1083\u001b[0m opts \u001b[39m=\u001b[39m FinalRequestOptions\u001b[39m.\u001b[39mconstruct(\n\u001b[0;32m 1084\u001b[0m method\u001b[39m=\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mpost\u001b[39m\u001b[39m\"\u001b[39m, url\u001b[39m=\u001b[39mpath, json_data\u001b[39m=\u001b[39mbody, files\u001b[39m=\u001b[39mto_httpx_files(files), \u001b[39m*\u001b[39m\u001b[39m*\u001b[39moptions\n\u001b[0;32m 1085\u001b[0m )\n\u001b[1;32m-> 1086\u001b[0m \u001b[39mreturn\u001b[39;00m cast(ResponseT, \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mrequest(cast_to, opts, stream\u001b[39m=\u001b[39;49mstream, stream_cls\u001b[39m=\u001b[39;49mstream_cls))\n", 395 | "File \u001b[1;32mc:\\Users\\sunny\\mcqgen\\env\\lib\\site-packages\\openai\\_base_client.py:846\u001b[0m, in \u001b[0;36mSyncAPIClient.request\u001b[1;34m(self, cast_to, options, remaining_retries, stream, stream_cls)\u001b[0m\n\u001b[0;32m 837\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mrequest\u001b[39m(\n\u001b[0;32m 838\u001b[0m \u001b[39mself\u001b[39m,\n\u001b[0;32m 839\u001b[0m cast_to: Type[ResponseT],\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 844\u001b[0m stream_cls: \u001b[39mtype\u001b[39m[_StreamT] \u001b[39m|\u001b[39m \u001b[39mNone\u001b[39;00m \u001b[39m=\u001b[39m \u001b[39mNone\u001b[39;00m,\n\u001b[0;32m 845\u001b[0m ) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m ResponseT \u001b[39m|\u001b[39m _StreamT:\n\u001b[1;32m--> 846\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_request(\n\u001b[0;32m 847\u001b[0m cast_to\u001b[39m=\u001b[39;49mcast_to,\n\u001b[0;32m 848\u001b[0m options\u001b[39m=\u001b[39;49moptions,\n\u001b[0;32m 849\u001b[0m stream\u001b[39m=\u001b[39;49mstream,\n\u001b[0;32m 850\u001b[0m stream_cls\u001b[39m=\u001b[39;49mstream_cls,\n\u001b[0;32m 851\u001b[0m remaining_retries\u001b[39m=\u001b[39;49mremaining_retries,\n\u001b[0;32m 852\u001b[0m )\n", 396 | "File \u001b[1;32mc:\\Users\\sunny\\mcqgen\\env\\lib\\site-packages\\openai\\_base_client.py:898\u001b[0m, in \u001b[0;36mSyncAPIClient._request\u001b[1;34m(self, cast_to, options, remaining_retries, stream, stream_cls)\u001b[0m\n\u001b[0;32m 895\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m err\u001b[39m.\u001b[39mresponse\u001b[39m.\u001b[39mis_closed:\n\u001b[0;32m 896\u001b[0m err\u001b[39m.\u001b[39mresponse\u001b[39m.\u001b[39mread()\n\u001b[1;32m--> 898\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_make_status_error_from_response(err\u001b[39m.\u001b[39mresponse) \u001b[39mfrom\u001b[39;00m \u001b[39mNone\u001b[39;00m\n\u001b[0;32m 899\u001b[0m \u001b[39mexcept\u001b[39;00m httpx\u001b[39m.\u001b[39mTimeoutException \u001b[39mas\u001b[39;00m err:\n\u001b[0;32m 900\u001b[0m \u001b[39mif\u001b[39;00m response \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n", 397 | "\u001b[1;31mAuthenticationError\u001b[0m: Error code: 401 - {'error': {'message': 'Incorrect API key provided: sk-jey3R***************************************FFpj. You can find your API key at https://platform.openai.com/account/api-keys.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_api_key'}}" 398 | ] 399 | } 400 | ], 401 | "source": [ 402 | "#https://python.langchain.com/docs/modules/model_io/llms/token_usage_tracking\n", 403 | "\n", 404 | "#How to setup Token Usage Tracking in LangChain\n", 405 | "with get_openai_callback() as cb:\n", 406 | " response=generate_evaluate_chain(\n", 407 | " {\n", 408 | " \"text\": TEXT,\n", 409 | " \"number\": NUMBER,\n", 410 | " \"subject\":SUBJECT,\n", 411 | " \"tone\": TONE,\n", 412 | " \"response_json\": json.dumps(RESPONSE_JSON)\n", 413 | " }\n", 414 | " )" 415 | ] 416 | }, 417 | { 418 | "cell_type": "code", 419 | "execution_count": null, 420 | "metadata": {}, 421 | "outputs": [], 422 | "source": [] 423 | }, 424 | { 425 | "cell_type": "code", 426 | "execution_count": 27, 427 | "metadata": {}, 428 | "outputs": [ 429 | { 430 | "name": "stdout", 431 | "output_type": "stream", 432 | "text": [ 433 | "Total Tokens:2452\n", 434 | "Prompt Tokens:1808\n", 435 | "Completion Tokens:644\n", 436 | "Total Cost:0.004\n" 437 | ] 438 | } 439 | ], 440 | "source": [ 441 | "print(f\"Total Tokens:{cb.total_tokens}\")\n", 442 | "print(f\"Prompt Tokens:{cb.prompt_tokens}\")\n", 443 | "print(f\"Completion Tokens:{cb.completion_tokens}\")\n", 444 | "print(f\"Total Cost:{cb.total_cost}\")" 445 | ] 446 | }, 447 | { 448 | "cell_type": "code", 449 | "execution_count": 28, 450 | "metadata": {}, 451 | "outputs": [ 452 | { 453 | "data": { 454 | "text/plain": [ 455 | "{'text': 'The term machine learning was coined in 1959 by Arthur Samuel, an IBM employee and pioneer in the field of computer gaming and artificial intelligence.[9][10] The synonym self-teaching computers was also used in this time period.[11][12]\\n\\nAlthough the earliest machine learning model was introduced in the 1950s when Arthur Samuel invented a program that calculated the winning chance in checkers for each side, the history of machine learning roots back to decades of human desire and effort to study human cognitive processes.[13] In 1949, Donald Hebb, a Canadian psychologist, published a book titled The Organization of Behavior where he introduces the Hebbian theory, discussing the neural structure or synapses between the nerve cells.[14] Hebb’s model of neurons interacting with one another sets a groundwork for how AIs and machine learning algorithms work under nodes, or artificial neurons used by computers to communicate data.[13] Other researchers who have studied on human cognitive systems contributed to the modern machine learning technologies as well including logician Walter Pitts and Warren McCulloch, who proposed the early mathematical models of neural networks to come up with algorithms that mirror human thought processes.[13]\\n\\nBy the early 1960s an experimental \"learning machine\" with punched tape memory, called Cybertron, had been developed by Raytheon Company to analyze sonar signals, electrocardiograms, and speech patterns using rudimentary reinforcement learning. It was repetitively \"trained\" by a human operator/teacher to recognize patterns and equipped with a \"goof\" button to cause it to re-evaluate incorrect decisions.[15] A representative book on research into machine learning during the 1960s was Nilsson\\'s book on Learning Machines, dealing mostly with machine learning for pattern classification.[16] Interest related to pattern recognition continued into the 1970s, as described by Duda and Hart in 1973.[17] In 1981 a report was given on using teaching strategies so that a neural network learns to recognize 40 characters (26 letters, 10 digits, and 4 special symbols) from a computer terminal.[18]\\n\\nTom M. Mitchell provided a widely quoted, more formal definition of the algorithms studied in the machine learning field: \"A computer program is said to learn from experience E with respect to some class of tasks T and performance measure P if its performance at tasks in T, as measured by P, improves with experience E.\"[19] This definition of the tasks in which machine learning is concerned offers a fundamentally operational definition rather than defining the field in cognitive terms. This follows Alan Turing\\'s proposal in his paper \"Computing Machinery and Intelligence\", in which the question \"Can machines think?\" is replaced with the question \"Can machines do what we (as thinking entities) can do?\".[20]\\n\\nModern-day machine learning has two objectives, one is to classify data based on models which have been developed, the other purpose is to make predictions for future outcomes based on these models. A hypothetical algorithm specific to classifying data may use computer vision of moles coupled with supervised learning in order to train it to classify the cancerous moles. A machine learning algorithm for stock trading may inform the trader of future potential predictions.[21]',\n", 456 | " 'number': 5,\n", 457 | " 'subject': 'machine learning',\n", 458 | " 'tone': 'simple',\n", 459 | " 'response_json': '{\"1\": {\"mcq\": \"multiple choice question\", \"options\": {\"a\": \"choice here\", \"b\": \"choice here\", \"c\": \"choice here\", \"d\": \"choice here\"}, \"correct\": \"correct answer\"}, \"2\": {\"mcq\": \"multiple choice question\", \"options\": {\"a\": \"choice here\", \"b\": \"choice here\", \"c\": \"choice here\", \"d\": \"choice here\"}, \"correct\": \"correct answer\"}, \"3\": {\"mcq\": \"multiple choice question\", \"options\": {\"a\": \"choice here\", \"b\": \"choice here\", \"c\": \"choice here\", \"d\": \"choice here\"}, \"correct\": \"correct answer\"}}',\n", 460 | " 'quiz': '{\"1\": {\"mcq\": \"Who coined the term machine learning?\", \"options\": {\"a\": \"Donald Hebb\", \"b\": \"Arthur Samuel\", \"c\": \"Walter Pitts\", \"d\": \"Warren McCulloch\"}, \"correct\": \"b\"}, \"2\": {\"mcq\": \"What was the earliest machine learning model introduced by Arthur Samuel?\", \"options\": {\"a\": \"Speech recognition\", \"b\": \"Image classification\", \"c\": \"Checkers game\", \"d\": \"Pattern recognition\"}, \"correct\": \"c\"}, \"3\": {\"mcq\": \"Which book introduced the Hebbian theory?\", \"options\": {\"a\": \"The Organization of Behavior\", \"b\": \"Learning Machines\", \"c\": \"Computing Machinery and Intelligence\", \"d\": \"The History of Machine Learning\"}, \"correct\": \"a\"}, \"4\": {\"mcq\": \"In the 1960s, a learning machine called Cybertron was developed to analyze which of the following?\", \"options\": {\"a\": \"Sonar signals\", \"b\": \"Speech patterns\", \"c\": \"Electrocardiograms\", \"d\": \"All of the above\"}, \"correct\": \"d\"}, \"5\": {\"mcq\": \"According to Tom M. Mitchell, what is the definition of machine learning?\", \"options\": {\"a\": \"Improving computer performance\", \"b\": \"Learning from experience to improve task performance\", \"c\": \"Analyzing cognitive processes\", \"d\": \"Developing neural networks\"}, \"correct\": \"b\"}}',\n", 461 | " 'review': '{\"1\": {\"mcq\": \"Who coined the term \\'machine learning\\'?\", \"options\": {\"a\": \"Donald Hebb\", \"b\": \"Arthur Samuel\", \"c\": \"Walter Pitts\", \"d\": \"Warren McCulloch\"}, \"correct\": \"b\"}, \"2\": {\"mcq\": \"What was the earliest machine learning model introduced by Arthur Samuel?\", \"options\": {\"a\": \"Speech recognition\", \"b\": \"Image classification\", \"c\": \"Checkers game\", \"d\": \"Pattern recognition\"}, \"correct\": \"c\"}, \"3\": {\"mcq\": \"Which book introduced the Hebbian theory?\", \"options\": {\"a\": \"The Organization of Behavior\", \"b\": \"Learning Machines\", \"c\": \"Computing Machinery and Intelligence\", \"d\": \"Cybertron\"}, \"correct\": \"a\"}, \"4\": {\"mcq\": \"What is the purpose of modern-day machine learning?\", \"options\": {\"a\": \"To classify data based on developed models\", \"b\": \"To predict future outcomes based on models\", \"c\": \"Both a and b\", \"d\": \"Neither a nor b\"}, \"correct\": \"c\"}, \"5\": {\"mcq\": \"What is the operational definition of machine learning according to Tom M. Mitchell?\", \"options\": {\"a\": \"Improving performance with experience\", \"b\": \"Classifying data based on models\", \"c\": \"Making predictions for future outcomes\", \"d\": \"Performing tasks that humans can do\"}, \"correct\": \"a\"}}'}" 462 | ] 463 | }, 464 | "execution_count": 28, 465 | "metadata": {}, 466 | "output_type": "execute_result" 467 | } 468 | ], 469 | "source": [ 470 | "response" 471 | ] 472 | }, 473 | { 474 | "cell_type": "code", 475 | "execution_count": 30, 476 | "metadata": {}, 477 | "outputs": [], 478 | "source": [ 479 | "quiz=response.get(\"quiz\")" 480 | ] 481 | }, 482 | { 483 | "cell_type": "code", 484 | "execution_count": 33, 485 | "metadata": {}, 486 | "outputs": [], 487 | "source": [ 488 | "quiz=json.loads(quiz)" 489 | ] 490 | }, 491 | { 492 | "cell_type": "code", 493 | "execution_count": 34, 494 | "metadata": {}, 495 | "outputs": [], 496 | "source": [ 497 | "quiz_table_data = []\n", 498 | "for key, value in quiz.items():\n", 499 | " mcq = value[\"mcq\"]\n", 500 | " options = \" | \".join(\n", 501 | " [\n", 502 | " f\"{option}: {option_value}\"\n", 503 | " for option, option_value in value[\"options\"].items()\n", 504 | " ]\n", 505 | " )\n", 506 | " correct = value[\"correct\"]\n", 507 | " quiz_table_data.append({\"MCQ\": mcq, \"Choices\": options, \"Correct\": correct})" 508 | ] 509 | }, 510 | { 511 | "cell_type": "code", 512 | "execution_count": 35, 513 | "metadata": {}, 514 | "outputs": [ 515 | { 516 | "data": { 517 | "text/plain": [ 518 | "[{'MCQ': 'Who coined the term machine learning?',\n", 519 | " 'Choices': 'a: Donald Hebb | b: Arthur Samuel | c: Walter Pitts | d: Warren McCulloch',\n", 520 | " 'Correct': 'b'},\n", 521 | " {'MCQ': 'What was the earliest machine learning model introduced by Arthur Samuel?',\n", 522 | " 'Choices': 'a: Speech recognition | b: Image classification | c: Checkers game | d: Pattern recognition',\n", 523 | " 'Correct': 'c'},\n", 524 | " {'MCQ': 'Which book introduced the Hebbian theory?',\n", 525 | " 'Choices': 'a: The Organization of Behavior | b: Learning Machines | c: Computing Machinery and Intelligence | d: The History of Machine Learning',\n", 526 | " 'Correct': 'a'},\n", 527 | " {'MCQ': 'In the 1960s, a learning machine called Cybertron was developed to analyze which of the following?',\n", 528 | " 'Choices': 'a: Sonar signals | b: Speech patterns | c: Electrocardiograms | d: All of the above',\n", 529 | " 'Correct': 'd'},\n", 530 | " {'MCQ': 'According to Tom M. Mitchell, what is the definition of machine learning?',\n", 531 | " 'Choices': 'a: Improving computer performance | b: Learning from experience to improve task performance | c: Analyzing cognitive processes | d: Developing neural networks',\n", 532 | " 'Correct': 'b'}]" 533 | ] 534 | }, 535 | "execution_count": 35, 536 | "metadata": {}, 537 | "output_type": "execute_result" 538 | } 539 | ], 540 | "source": [ 541 | "quiz_table_data" 542 | ] 543 | }, 544 | { 545 | "cell_type": "code", 546 | "execution_count": 37, 547 | "metadata": {}, 548 | "outputs": [], 549 | "source": [ 550 | "quiz=pd.DataFrame(quiz_table_data)" 551 | ] 552 | }, 553 | { 554 | "cell_type": "code", 555 | "execution_count": 38, 556 | "metadata": {}, 557 | "outputs": [], 558 | "source": [ 559 | "quiz.to_csv(\"machinelearning.csv\",index=False)" 560 | ] 561 | }, 562 | { 563 | "cell_type": "code", 564 | "execution_count": 2, 565 | "metadata": {}, 566 | "outputs": [ 567 | { 568 | "data": { 569 | "text/plain": [ 570 | "'12_12_2023_15_35_09'" 571 | ] 572 | }, 573 | "execution_count": 2, 574 | "metadata": {}, 575 | "output_type": "execute_result" 576 | } 577 | ], 578 | "source": [ 579 | "from datetime import datetime\n", 580 | "datetime.now().strftime('%m_%d_%Y_%H_%M_%S')" 581 | ] 582 | }, 583 | { 584 | "cell_type": "code", 585 | "execution_count": null, 586 | "metadata": {}, 587 | "outputs": [], 588 | "source": [] 589 | } 590 | ], 591 | "metadata": { 592 | "kernelspec": { 593 | "display_name": "Python 3", 594 | "language": "python", 595 | "name": "python3" 596 | }, 597 | "language_info": { 598 | "codemirror_mode": { 599 | "name": "ipython", 600 | "version": 3 601 | }, 602 | "file_extension": ".py", 603 | "mimetype": "text/x-python", 604 | "name": "python", 605 | "nbconvert_exporter": "python", 606 | "pygments_lexer": "ipython3", 607 | "version": "3.8.18" 608 | } 609 | }, 610 | "nbformat": 4, 611 | "nbformat_minor": 2 612 | } 613 | --------------------------------------------------------------------------------