Question Generation with GPT3 + Creative Prompting
Performing Some Task using GPT-3
!pip install openai
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])
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])
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])
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])
Important Note
- Signup for OpenAI to get your own API key !
- Engine choice: Davinci is not the only engine of choice
Play with the list using the above snippet and choose the one that best suits your case.# list engines engines = openai.Engine.list()
- 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