-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt.py
50 lines (37 loc) · 1.63 KB
/
gpt.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
import openai
from dotenv import load_dotenv
import os
load_dotenv() # Load secrets
# Load the API KEY
api_key = os.getenv('API_KEY')
class GPT_3:
def __init__(self, api_key):
openai.api_key = api_key
self.completion = openai.Completion
self.options = {
'engine': 'text-davinci-002',
'temperature': 0.25,
'top_p': 1,
'frequency_penalty': 0,
'presence_penalty': 0,
'max_tokens': 512
}
def __call__(self, prompt, options=None):
return self.prediction(prompt, options)
def prediction(self, prompt, options=None):
if not options:
options = self.options
return self.completion.create(prompt=prompt, **options)['choices'][0]['text']
def teach(self, text, words):
prompt = f'explain the following topic to a UG student in a conventional way in about {words}.\n\n Topic: {text}'
return self.prediction(prompt=prompt)
def resources2(self, text):
prompt = f'Tell me resources to learn about {text} in the form of a list '
return self.prediction(prompt=prompt)
def translate(self, text, l):
prompt = f'Translate provided text into the {l} language !\n\n text: {text}'
return self.prediction(prompt=prompt)
format = '''["question": "Question 1","options": ["Option 1", "Option 2", "Option 3", "Option 4"], "correct_answer": "Option 1" ]"'''
def QuizMe(self, text, num):
prompt = f'please make an MCQ quiz of {num} questions related to {text} with all correct answers in a different line "'
return self.prediction(prompt=prompt)