· 2 years ago · Dec 23, 2022, 02:30 AM
1import openai
2import csv
3import json
4
5key = open("key", "r")
6
7
8# Set the API key
9openai.api_key = key.readline()
10
11key.close()
12
13# Set the model to use
14model_engine = "text-davinci-003"
15topics = ["Consumer Healthcare", "Fitness", "Wellness"]
16content_count = []
17industry = []
18
19
20for t in topics:
21 print("How many quizes do you want to generate about topic:"+t)
22 count = int(input())
23 content_count.append(count)
24
25
26with open("quiz.csv", "a") as file:
27 reader = csv.writer(file)
28 t = 0
29 for topic in topics:
30 c = 0
31 for count in range(content_count[c]):
32 print("Feed in content to write about for"+topic)
33 content = input()
34 print(
35 "Write a brief summary of your content. Or press enter to let the ai generate it for you")
36 summary = input()
37 if(summary == ""):
38 summaryprompt = "Write a brief summary of the followign content: "+content
39 summaryresult = openai.Completion.create(
40 engine=model_engine, prompt=summaryprompt, max_tokens=1024, n=1, stop=None, temperature=0.5)
41 # Print the generated text
42 summary = summaryresult.text
43 print("Summary:"+summary)
44 print("How many questions do you want to write?")
45 question_count = input()
46 print("Answer count?")
47 answer_count = input()
48
49 # Set the prompt to use as input
50 prompt = "Given the following text, write exactly "+str(question_count)+"multiple choice quiz questions each with "+str(answer_count) + " possible answers and write down whether each answer is wrong or right and why. in the 'questions' column questions column should be enclosed in square brackets separated by a comma. answers column should have the answers enclosed in square brackets separated by comma, right_answer should be an array of correct answer indexes for each of the questions. In the descriptions column write descriptions of each answer choice separated by comma enclosed in square brackets for the column descriptions for each answer separate by comma and enclose in square brackets. For right answer index, write down why the answer is correct, for all other indexes write down why the answer is wrong. In descriptions, enclose in square brackets descriptions of the wrong answers and description of the right answers separated by commas. Do not enclose any values in quotes. \n" + \
51 " Your response should be in CSV format in the order of 'questions','answers','right_answer','descriptions': reply only in this csv format." + \
52 """[Question 1,Question 2,Question 3,Question 4],[["a","b","c","d"],["a","b","c","d"]],[3,3],[[incorrect because,incorrect because,correct because, incorrect beacuse],[incorrect because,incorrect because,correct because, incorrect beacuse]]""" + content
53 # Generate text using the GPT-3 model
54 response = openai.Completion.create(
55 engine=model_engine, prompt=prompt, max_tokens=1024, n=1, stop=None, temperature=0.5)
56 # Print the generated text
57 print(response["choices"][0].text)
58
59 reader.writerow([response["choices"][0].text])
60 c += 1
61