Install OpenAI Python SDK

!pip install openai
Collecting openai
  Downloading openai-0.11.5.tar.gz (466 kB)
     |████████████████████████████████| 466 kB 11.7 MB/s 
Requirement already satisfied: requests>=2.20 in /usr/local/lib/python3.7/dist-packages (from openai) (2.23.0)
Requirement already satisfied: tqdm in /usr/local/lib/python3.7/dist-packages (from openai) (4.62.3)
Collecting pandas>=1.2.3
  Downloading pandas-1.3.5-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.3 MB)
     |████████████████████████████████| 11.3 MB 42.7 MB/s 
Collecting pandas-stubs>=1.1.0.11
  Downloading pandas_stubs-1.2.0.40-py3-none-any.whl (161 kB)
     |████████████████████████████████| 161 kB 48.3 MB/s 
Collecting openpyxl>=3.0.7
  Downloading openpyxl-3.0.9-py2.py3-none-any.whl (242 kB)
     |████████████████████████████████| 242 kB 52.7 MB/s 
Requirement already satisfied: et-xmlfile in /usr/local/lib/python3.7/dist-packages (from openpyxl>=3.0.7->openai) (1.1.0)
Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=1.2.3->openai) (2.8.2)
Requirement already satisfied: numpy>=1.17.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=1.2.3->openai) (1.19.5)
Requirement already satisfied: pytz>=2017.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=1.2.3->openai) (2018.9)
Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.7/dist-packages (from pandas-stubs>=1.1.0.11->openai) (3.10.0.2)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.7.3->pandas>=1.2.3->openai) (1.15.0)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests>=2.20->openai) (2.10)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests>=2.20->openai) (3.0.4)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests>=2.20->openai) (2021.10.8)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests>=2.20->openai) (1.24.3)
Building wheels for collected packages: openai
  Building wheel for openai (setup.py) ... done
  Created wheel for openai: filename=openai-0.11.5-py3-none-any.whl size=161464 sha256=604ae3e818b3dc26bade5925151a4404156543dd2fee70dd437372e651bb86ee
  Stored in directory: /root/.cache/pip/wheels/5a/52/66/06c234d9748d81b9622d0b9765c3bd10e99d059ed93f13b0b3
Successfully built openai
Installing collected packages: pandas-stubs, pandas, openpyxl, openai
  Attempting uninstall: pandas
    Found existing installation: pandas 1.1.5
    Uninstalling pandas-1.1.5:
      Successfully uninstalled pandas-1.1.5
  Attempting uninstall: openpyxl
    Found existing installation: openpyxl 2.5.9
    Uninstalling openpyxl-2.5.9:
      Successfully uninstalled openpyxl-2.5.9
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
google-colab 1.0.0 requires pandas~=1.1.0; python_version >= "3.0", but you have pandas 1.3.5 which is incompatible.
Successfully installed openai-0.11.5 openpyxl-3.0.9 pandas-1.3.5 pandas-stubs-1.2.0.40

Few-shot prompting with question templates


The Idea

For each question type (see below), Create nice reusable question templates with few examples.

  • Fill in the blanks
  • MCQs
  • True or False / Boolean
  • Multi-choice/Multiple Fill in the blanks

Question templates are a combination of prompt tokens and question phrases. For the question generation task "Few" in the phrase "few examples" depends on the question type. For instance you will witness for Fill in the lanks and MCQs 2 examples works just fine. For True or False you could just go with 1 example and May be in your experiments you may discover Zero-short can be put to best use for your needs.

Note:

  • Here examples simply mean string templates of some static knowledge put together to match the desired Question (and answer) style.
  • Having categories of templates can help, because the closer your templates are with the actual paragraphs you are using to generate question in terms of domains the stronger will be the generation results.

Disclaimer:

  • The strength and quality of your generations are a function of your Prompt Tokens and Example phrases. Choose creative yet relevant prompt tokens.
  • PROMPT PROGRAMMING is a SKILL you can improve purely by TRIAL AND ERROR with your ENGLISH knowledge.

1. Fill in the blanks


Below is a sample prompt template for Fill in the blank questions As said before the examples are based on the some static knowledge. Here we have 2 <Paragraph/Ques-Ans> pairs. You can have as many example as you want. In this example Paragraph:, Question:, Answer: are the 3 prompt tokens.

  • Line 1. Paragraph: Jesus, according to some biblical sources, was born in this town some two millennia ago in Bethlehem.
  • Line 2. Question: Where was Jesus born __ ? Answer: Bethlehem
  • Line 3. Paragraph: Sachin Tendulkar was born in India. He debuted for Indian cricket in 1988.
  • Line 4. Question: In __ Sachin started playing cricket? Answer: 1988
import openai
openai.api_key = "<your_openai_api_key>"
prompt_template= """
Paragraph: Jesus, according to some biblical sources, was born in this town some two millennia ago in Bethlehem. 
Question: Where was Jesus born ______ ? Answer: Bethlehem
Paragraph: Sachin Tendulkar was born in India. He debuted for Indian cricket in 1988. 
Question: In ______ Sachin started playing cricket. Answer: 1988
"""

custom_prompt="""
Paragraph: Elon Musk was a born in South Africa in 1971 and he joined Tesla in 2004.
"""

prompt = prompt_template + custom_prompt
completion = openai.Completion.create(engine="davinci", prompt=prompt, max_tokens=32, temperature=0.7)

print("Generated Question..")
generated = completion.choices[0].text
if "Paragraph" in generated:
   ind = generated.index("Paragraph") 
   print(generated[:ind])
Generated Question..
Question: In ______ Elon Musk joined Tesla.
Answer: 2004


2. MCQ - Multiple Choice Questions


Below is a sample prompt template for MCQs As said before the examples are based on the some static knowledge. Here we have 2 <Paragraph/Ques-Choice-Ans> pairs. You can have as many as you want to make the generation strong.

  • Line 1. Paragraph: Jesus, according to some biblical sources, was born in this town some two millennia ago in Bethlehem. The story begins with wise men who come to the city of Jerusalem after seeing a star that they interpreted as signaling the birth of a new king.
  • Line 2. Question: Where was Jesus born ? A) Jerusalem, B) Palestine, C) Bethlehem. D) Tel-Aviv Answer: C
  • Line 3. Paragraph: Sachin Tendulkar was born in India 1972. He debuted for Indian cricket in 1988 and retired in 2011.
  • Line 4. Question: In what year Sachin started playing cricket? A) 1972, B) 1988, C) 2011, D) 2001. Answer: B
prompt_template= """
Paragraph: Jesus, according to some biblical sources, was born in this town some two millennia ago in Bethlehem. The story begins with wise men who come to the city of Jerusalem after seeing a star that they interpreted as signaling the birth of a new king.
Question: Where was Jesus born ? A) Jerusalem, B) Palestine, C) Bethlehem. D) Tel-Aviv Answer: C
Paragraph: Sachin Tendulkar was born in India 1972. He debuted for Indian cricket in 1988 and retired in 2011.
Question: In what year Sachin started playing cricket? A) 1972, B) 1988, C) 2011, D) 2001. Answer: B
"""

custom_prompt="""
Paragraph: Elon Musk was a born in South Africa in 1971 and he joined Tesla in 2004.
"""

prompt = prompt_template + custom_prompt
completion = openai.Completion.create(engine="davinci", prompt=prompt, max_tokens=32, temperature=0.7)

print("Generated Question..")
generated = completion.choices[0].text
if "Paragraph" in generated:
   ind = generated.index("Paragraph") 
   print(generated[:ind])
Generated Question..
Question: In what year did Elon join Tesla? A) 1971, B) 2004, C) 1992, D) 2012. Answer: B

3. True or False / Boolean Questions


Here we have ONLY 1 <Paragraph/Ques-Ans> pair. But be warned generation might suffer. More the examples in your template Stronger the generations.

prompt_template= """
Paragraph: Jesus, according to some biblical sources, was born in this town some two millennia ago in Bethlehem. The story begins with wise men who come to the city of Jerusalem after seeing a star that they interpreted as signaling the birth of a new king.
Question: Jesus was born in Jerusalem. Answer: False
"""

custom_prompt="""
Paragraph: Elon Musk was a born in South Africa in 1971 and he joined Tesla in 2004.
"""

prompt = prompt_template + custom_prompt
completion = openai.Completion.create(engine="davinci", prompt=prompt, max_tokens=32, temperature=0.7)

print("Generated Question..")
generated = completion.choices[0].text
if "Paragraph" in generated:
   ind = generated.index("Paragraph") 
   print(generated[:ind])
Generated Question..
Question: Elon Musk was born in Cape Town.
Answer: True


4. (Just for fun) Lets try generating higher-order probing questions which either needs common sense or deeper inference.


Note how we changed the prompt strings to <fact/probe-inference> pairs

prompt_template= """
Fact: Jesus, according to some biblical sources, was born in this town some two millennia ago in Bethlehem. The story begins with wise men who come to the city of Jerusalem after seeing a star that they interpreted as signaling the birth of a new king.
Probe: Is Jesus a human being? Inference: No he is a God.
Fact: Sachin Tendulkar was born in India 1972. He debuted for Indian cricket in 1988 and retired in 2011.
Probe: How many years Sachin played cricket? Inference: 23 years.
"""

custom_prompt="""
Fact: Elon Musk started Tesla Motors since 2004. He is the CEO and product architect of Tesla and he is also the Chairman of Musk Foundation, an organization supporting research on renewable energy, human space exploration and pediatrics. At age 12, sold his code for a video game called “Blastar” to a computer magazine for $500.
"""

prompt = prompt_template + custom_prompt
completion = openai.Completion.create(engine="davinci", prompt=prompt, max_tokens=32, temperature=0.7)

print("Generated Probe..")
generated = completion.choices[0].text
if "Fact" in generated:
   ind = generated.index("Fact") 
   print(generated[:ind])
Generated Probe..
Probe: What is Musk's current age? Inference: 41 years.


Important Note

  • Signup for OpenAI to get your own API key !
  • Engine choice: Davinci is not the only engine of choice
    # list engines
    engines = openai.Engine.list()
    Play with the list using the above snippet and choose the one that best suits your case.
  • Number of generations - As you can see, I have limited my generations to only 1. You could iterate over many generations with single prompt
  • Play with misc Parameters like max_tokens=32, temperature=0.7 Refer this link -https://beta.openai.com/docs/api-reference/completions/create
  • Feel free to use your creativity and rxpand to other tasks