OpenAI can create simple Quiz

2023.06.06

Introduction

Yes, OpenAI can help you create a Quiz, let's see how this can be achieved using OpenAI's Text Completion API. As a prerequisite, you should have an OpenAI account, API key, and Juypiter environment to work with. For a detailed step-by-step explanation of the mentioned prerequisite check out this blog from steps 1 to 4.

Now let's get into the action

Step 1: Create a Prompt

The prompt is the most important attribute of OpenAI parameters. Now let's decide what is required in a prompt for the quiz, Of Course name of the topic, the number of questions, and a number of options for each question. Well let's make all these params dynamically imbibed in a function.

def create_test_prompt(topic, num_questions, num_options):
   prompt = f"Create a multiple choice quiz on the topic of {topic} consisting of {num_questions} questions. " \
               + f"Each question should have {num_options} options. "\
               + f"Also include the correct answer for each question using the starting string 'Correct Answer: '."
   return prompt

The above example prompt has simple and direct instructions to create a certain number of multiple-choice quizzes on a particular topic, including correct answers.

Step 2: Call OpenAI API

openai.Completion.create is a method provided by the OpenAI Python client library that allows you to make requests to the OpenAI Completion API.

In the below example, we are calling the Completion API with certain parameters. In the prompt, we are requesting the quiz on IOT with 4 questions each with 4 options. Max_tokens are set to 256, and temperature to 0.7. Temperature is set to a higher value than the default value to allow the API to be creative while making the quiz.

response = openai.Completion.create(engine='text-davinci-003',
                                    prompt=create_test_prompt('IOT',4,4),
                                    max_tokens=256,
                                    temperature=0.7)

Step 3: Response

Hola! We have the quiz ready

Happy Learning!!