-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgpt_integration.py
More file actions
142 lines (117 loc) · 3.99 KB
/
gpt_integration.py
File metadata and controls
142 lines (117 loc) · 3.99 KB
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
138
139
140
141
142
import os
import openai
from jsonschema import validate
from utils import parse_json
from jsonschema.exceptions import ValidationError, SchemaError
openai.api_key = os.environ.get('OPEN_API_KEY')
if openai.api_key is None:
raise SystemError('Could not find OPEN_API_KEY environment variable')
model = 'gpt-3.5-turbo' # default
truefalsegeneration = True
def get_prompt(notes):
if truefalsegeneration:
return """
Please come up with a confusing mix of true and false difficult questions pairs based on the notes provided.
Notes:
""" + notes + """
Please only provide a RFC8259 compliant JSON response following this format without deviation.
[
{
"question": "",
"answer": "",
},
]
The JSON response:
"""
else:
return """
Create questions with their question and answer pairs based on the notes provided. Please create confusing questions that are useful for studying.
Notes:""" + notes + """
Please only provide a RFC8259 compliant JSON response following this format without deviation.
[
{
"question": "",
"answer": "",
},
]
The JSON response:
"""
def get_retry_prompt():
return """
Please format the response in JSON list with each object containing a 'question'
and 'answer' as follows:
[
{
"question": "",
"answer": "",
},
]
Create the question and answer pairs based on the following notes:
"""
def run_model(role, content):
return openai.ChatCompletion.create(model=model,
messages=[{"role": role, "content": content}])
def get_question_answers(text):
chat_response = run_model("user", get_prompt(text))
response_string = chat_response.choices[0].message.content
print(response_string)
response_json = parse_json(response_string)
try:
validate_response(response_json)
return response_json
except ValueError:
run_retry(text)
def run_retry(text):
retry_response = run_model("user", get_retry_prompt() + text)
response_string = retry_response.choices[0].message.content
response_json = parse_json(response_string)
try:
validate_response(response_json)
return response_json
except ValueError as e:
raise Exception("Unable to parse question and answer pairs from GPT response",
e)
def score_answer(question, user_answer, ai_answer):
role = 'You are a teacher who is grading a students answer'
formatted_input = f"""
The student answer is:
{user_answer}
The question is:
{question}
The answer key is:
{ai_answer}
Can you give the answer a score out of 5 with the reasoning?
"""
# can change this back to hardcode 'gpt-3.5-turbo' if there is no need for the
# user to control the model used for the grader
response = openai.ChatCompletion.create(
model=model,
messages=[
{"role": 'system', "content": role},
{"role": 'user', "content": formatted_input}
])
response_string = response.choices[0].message.content
print(response_string)
return response_string
def validate_response(response):
schema = {
"type": "array",
"items": {
"type": "object",
"properties": {
"question": {
"type": "string"
},
"answer": {
"type": "string"
}
},
"required": ["question", "answer"]
}
}
try:
validate(instance=response, schema=schema)
except ValidationError as e:
raise ValueError("Could not parse the AI response as a JSON file: ", e)
except SchemaError as e:
raise ValueError("Parsed AI response does not match the schema: ", e)