-
Notifications
You must be signed in to change notification settings - Fork 2
/
openAI.py
137 lines (109 loc) · 12.9 KB
/
openAI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import requests
import wikipedia
import re
import os
from pathlib import Path
from dotenv import load_dotenv
def puppyTest():
url = 'https://api.openai.com/v1/answers'
openAI = os.environ['OPEN_AI']
headers = {'Authorization': f'Bearer {openAI}'}
payload = {
"documents": ["Puppy A is happy.", "Puppy B is sad."],
"question": "which puppy is happy?",
"search_model": "ada",
"model": "curie",
"examples_context": "In 2017, U.S. life expectancy was 78.6 years.",
"examples": [["What is human life expectancy in the United States?", "78 years."]],
"max_tokens": 5,
"stop": ["\n", "<|endoftext|>"]
}
response = requests.post(url, headers=headers, json=payload)
return (response.json()["answers"])
# print(puppyTest())
def toHackTest(question: str):
url = 'https://api.openai.com/v1/answers'
openAI = os.environ['OPEN_AI']
headers = {'Authorization': f'Bearer {openAI}'}
payload = {
"documents": ["TOHacks is a student-run organization affiliated with the University of Toronto.",
"TOHacks hosted a virtual hackathon for participants to compete in on May 8th and May 9th in 2021.",
"Shwetha Sivakumar and Andy Tran are the Co-Chairs and Co-Founders of TOHacks.",
"Spandhana Gade is the Vice President of HR Strategy at TOHacks and Joy Lin is the Vice President of Operations at TOHacks.",
"Four people are allowed per team for the hackathon, and participants of all levels of experience are welcome.",
"The official website for TOHacks is https://www.tohacks.ca/.",
"Questions about TOHacks can be emailed to [email protected]."
"TOHacks is on Facebook at https://www.facebook.com/hackathontohacks/ and on LinkedIn at https://www.linkedin.com/company/tohacks/."],
"question": question,
"search_model": "ada",
"model": "curie",
"examples_context": "In 2017, U.S. life expectancy was 78.6 years. Atul Gawande has written about the decline of end of life health care in America.",
"examples": [["What is human life expectancy in the United States?", "78 years."], ["Who wrote books about health care in America?", "Atul Gawande."]],
"max_tokens": 50,
"stop": ["\n", "<|endoftext|>"]
}
response = requests.post(url, headers=headers, json=payload)
return (response.json()["answers"])
# print(toHackTest("What is TOHacks?"))
# print(toHackTest("Who is the founder of TOHacks?"))
# print(toHackTest("Who is allowed to participate in TOHacks?"))
# print(toHackTest("When is TOHacks Hackathon happening?"))
# print(toHackTest("Where can I learn more about TOHacks?"))
def storyTest(question: str):
story = "Once upon a time there was a dear little girl who was loved by every one who looked at her, but most of all by her grandmother, and there was nothing that she would not have given to the child. Once she gave her a little cap of red velvet, which suited her so well that she would never wear anything else. So she was always called Little Red Riding Hood. One day her mother said to her, \"Come, Little Red Riding Hood, here is a piece of cake and a bottle of wine. Take them to your grandmother, she is ill and weak, and they will do her good. Set out before it gets hot, and when you are going, walk nicely and quietly and do not run off the path, or you may fall and break the bottle, and then your grandmother will get nothing. And when you go into her room, don't forget to say, good-morning, and don't peep into every corner before you do it.\" I will take great care, said Little Red Riding Hood to her mother, and gave her hand on it. The grandmother lived out in the wood, half a league from the village, and just as Little Red Riding Hood entered the wood, a wolf met her. Little Red Riding Hood did not know what a wicked creature he was, and was not at all afraid of him. \"Good-day, Little Red Riding Hood, \" said he. \"Thank you kindly, wolf.\" \"Whither away so early, Little Red Riding Hood?\" \"To my grandmother's.\" \"What have you got in your apron?\" \"Cake and wine. Yesterday was baking-day, so poor sick grandmother is to have something good, to make her stronger." "Where does your grandmother live, Little Red Riding Hood?" "A good quarter of a league farther on in the wood. Her house stands under the three large oak-trees, the nut-trees are just below. You surely must know it,\" replied Little Red Riding Hood. The wolf thought to himself, \"What a tender young creature. What a nice plump mouthful, she will be better to eat than the old woman. I must act craftily, so as to catch both.\" So he walked for a short time by the side of Little Red Riding Hood, and then he said, \"see Little Red Riding Hood, how pretty the flowers are about here. Why do you not look round. I believe, too, that you do not hear how sweetly the little birds are singing. You walk gravely along as if you were going to school, while everything else out here in the wood is merry.\" Little Red Riding Hood raised her eyes, and when she saw the sunbeams dancing here and there through the trees, and pretty flowers growing everywhere, she thought, suppose I take grandmother a fresh nosegay. That would please her too. It is so early in the day that I shall still get there in good time. And so she ran from the path into the wood to look for flowers. And whenever she had picked one, she fancied that she saw a still prettier one farther on, and ran after it, and so got deeper and deeper into the wood. Meanwhile the wolf ran straight to the grandmother's house and knocked at the door. \"Who is there?" "Little Red Riding Hood,\" replied the wolf. \"She is bringing cake and wine. Open the door." "Lift the latch,\" called out the grandmother, \"I am too weak, and cannot get up.\" The wolf lifted the latch, the door sprang open, and without saying a word he went straight to the grandmother's bed, and devoured her. Then he put on her clothes, dressed himself in her cap, laid himself in bed and drew the curtains. Little Red Riding Hood, however, had been running about picking flowers, and when she had gathered so many that she could carry no more, she remembered her grandmother, and set out on the way to her. She was surprised to find the cottage-door standing open, and when she went into the room, she had such a strange feeling that she said to herself, oh dear, how uneasy I feel to-day, and at other times I like being with grandmother so much. She called out, \"Good morning,\" but received no answer. So she went to the bed and drew back the curtains. There lay her grandmother with her cap pulled far over her face, and looking very strange. \"Oh, grandmother,\" she said, \"what big ears you have." "The better to hear you with, my child,\" was the reply. \"But, grandmother, what big eyes you have,\" she said. \"The better to see you with, my dear." "But, grandmother, what large hands you have." "The better to hug you with." "Oh, but, grandmother, what a terrible big mouth you have." "The better to eat you with.\" And scarcely had the wolf said this, than with one bound he was out of bed and swallowed up Little Red Riding Hood. When the wolf had appeased his appetite, he lay down again in the bed, fell asleep and began to snore very loud. The huntsman was just passing the house, and thought to himself, how the old woman is snoring. I must just see if she wants anything. So he went into the room, and when he came to the bed, he saw that the wolf was lying in it. \"Do I find you here, you old sinner,\" said he. \"I have long sought you.\" Then just as he was going to fire at him, it occurred to him that the wolf might have devoured the grandmother, and that she might still be saved, so he did not fire, but took a pair of scissors, and began to cut open the stomach of the sleeping wolf. When he had made two snips, he saw the Little Red Riding Hood shining, and then he made two snips more, and the little girl sprang out, crying, \"Ah, how frightened I have been. How dark it was inside the wolf.\" And after that the aged grandmother came out alive also, but scarcely able to breathe. Little Red Riding Hood, however, quickly fetched great stones with which they filled the wolf\'s belly, and when he awoke, he wanted to run away, but the stones were so heavy that he collapsed at once, and fell dead. Then all three were delighted. The huntsman drew off the wolf's skin and went home with it. The grandmother ate the cake and drank the wine which Little Red Riding Hood had brought, and revived, but Little Red Riding Hood thought to herself, as long as I live, I will never by myself leave the path, to run into the wood, when my mother has forbidden me to do so. It is also related that once when Little Red Riding Hood was again taking cakes to the old grandmother, another wolf spoke to her, and tried to entice her from the path. Little Red Riding Hood, however, was on her guard, and went straight forward on her way, and told her grandmother that she had met the wolf, and that he had said good-morning to her, but with such a wicked look in his eyes, that if they had not been on the public road she was certain he would have eaten her up. \"Well,\" said the grandmother, \"we will shut the door, that he may not come in.\" Soon afterwards the wolf knocked, and cried, \"open the door, grandmother, I am Little Red Riding Hood, and am bringing you some cakes.\" But they did not speak, or open the door, so the grey-beard stole twice or thrice round the house, and at last jumped on the roof, intending to wait until Little Red Riding Hood went home in the evening, and then to steal after her and devour her in the darkness. But the grandmother saw what was in his thoughts. In front of the house was a great stone trough, so she said to the child, take the pail, Little Red Riding Hood. I made some sausages yesterday, so carry the water in which I boiled them to the trough. Little Red Riding Hood carried until the great trough was quite full. Then the smell of the sausages reached the wolf, and he sniffed and peeped down, and at last stretched out his neck so far that he could no longer keep his footing and began to slip, and slipped down from the roof straight into the great trough, and was drowned. But Little Red Riding Hood went joyously home, and no one ever did anything to harm her again."
url = 'https://api.openai.com/v1/answers'
openAI = os.environ['OPEN_AI']
headers = {'Authorization': f'Bearer {openAI}'}
payload = {
"documents": [story],
"question": question,
"search_model": "ada",
"model": "curie",
"examples_context": "In 2017, U.S. life expectancy was 78.6 years. Atul Gawande has written about the decline of end of life health care in America.",
"examples": [["What is human life expectancy in the United States?", "78 years."], ["Who wrote books about health care in America?", "Atul Gawande."]],
"max_tokens": 50,
"stop": ["\n", "<|endoftext|>"]
}
response = requests.post(url, headers=headers, json=payload)
return (response.json()["answers"])
# print(storyTest("What color is the main character's hood?"))
# print(storyTest("Who ate little red riding hood's grandmother?"))
# print(storyTest("What food was little red riding hood bringing her grandmother?"))
# print(storyTest("How did the wolf die?"))
def parseWikiText(wikiTitle: str):
try:
wikiObject = wikipedia.page(wikiTitle)
text = wikiObject.content
text = re.sub(r'==.*?==+', '', text)
text = text.replace('\n', '')
# print(text)
except:
return 999
return text
def getOpenAIAnswer(question: str, wikiPrompt: str):
openAI = os.environ['OPEN_AI']
headers = {'Authorization': f'Bearer {openAI}'}
url = 'https://api.openai.com/v1/answers'
docs = parseWikiText(wikiPrompt)
payload = {
"documents": [docs],
"question": question,
"search_model": "ada",
"model": "curie",
"examples_context": "In 2017, U.S. life expectancy was 78.6 years. Atul Gawande has written about the decline of end of life health care in America.",
"examples": [["What is human life expectancy in the United States?", "78 years."], ["Who wrote books about health care in America?", "Atul Gawande."]],
"max_tokens": 50,
"stop": ["\n", "<|endoftext|>"]
}
response = requests.post(url, headers=headers, json=payload)
return (response.json()["answers"])
# print(getOpenAIAnswer("What does Python code emphasize?",
# "Python (programming language)"))
# print(getOpenAIAnswer("When was Python 3.0 released?",
# "Python (programming language)"))
# print(getOpenAIAnswer("How does Python delimit blocks of code?",
# "Python (programming language)"))
# print(getOpenAIAnswer("How do I write a Hello World function in Python?",
# "Python (programming language)"))
# print(getOpenAIAnswer("Does Python check for type errors at compile time?",
# "Python (programming language)"))