-
Notifications
You must be signed in to change notification settings - Fork 0
/
livehint_ai_client.py
242 lines (210 loc) · 10.4 KB
/
livehint_ai_client.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import os
import requests
from dotenv import load_dotenv
from enum import Enum
import json
from urllib.parse import quote
class APIExceptionType(Enum):
TIMEOUT = 1
HTTP_ERROR = 2
CONNECTION_ERROR = 3
REQUEST_ERROR = 4
UNEXPECTED_ERROR = 5
class APIException(Exception):
def __init__(self, exception_type, message):
self.exception_type = exception_type
self.message = message
def __str__(self):
return f"{self.exception_type.name}: {self.message}"
load_dotenv()
API_BASE_URL = os.getenv("API_BASE_URL")
API_BEARER_TOKEN = os.getenv("API_BEARER_TOKEN")
API_TIMEOUT= 3
API_TUTORBOT_TYPE = "solver_bot"
API_VALID_APP_CONTEXTS = ["mathbook_tx", "mathstream"]
API_DEFAULT_APP_CONTEXT = "mathbook_tx"
API_SUPPORTED_MODELS = ["gpt-4o", "gpt-4-0125-preview", "gpt-4-1106-preview", "gpt-4-0613", "claude-3-opus-20240229", "claude-3-sonnet-20240229", "claude-3-haiku-20240307"]
API_DEFAULT_MODEL = "gpt-4-0613"
API_DEFAULT_TEMPERATURE = 0.3
API_DEFAULT_STREAM = True
if API_BASE_URL is None:
raise ValueError("API_BASE_URL is not defined")
if API_BEARER_TOKEN is None:
raise ValueError("API_BEARER_TOKEN is not defined")
def get_problem_info(app_context, course, module, page, question, item_id):
params = {
"app_context": app_context,
"qr_course": course,
"qr_module": module,
"qr_page": page,
"qr_question": question,
"item_id": item_id
}
headers = {"Authorization": f"Bearer {API_BEARER_TOKEN}"}
url = f"{API_BASE_URL}/api/problem-info"
# Make problem-info GET request to LiveHint AI
try:
response = requests.get(url, headers=headers, params=params, timeout=API_TIMEOUT)
response.raise_for_status()
except requests.exceptions.Timeout:
print("The request timed out. Please try again later.")
raise APIException(APIExceptionType.TIMEOUT, "Timeout occurred during API request")
except requests.exceptions.HTTPError as e:
print(f"HTTP error ({e.response.status_code}): {e.response.reason}")
raise APIException(APIExceptionType.HTTP_ERROR, f"HTTP error: {e.response.status_code} - {e.response.reason}")
except requests.exceptions.ConnectionError:
print("Connection error. Please check your network connection.")
raise APIException(APIExceptionType.CONNECTION_ERROR, "Connection error during API request")
except requests.exceptions.RequestException as e:
print(f"An error occurred during the request: {str(e)}")
raise APIException(APIExceptionType.REQUEST_ERROR, f"Request error: {str(e)}")
except Exception as e:
print(f"An unexpected error occurred: {str(e)}")
raise APIException(APIExceptionType.UNEXPECTED_ERROR, f"Unexpected error: {str(e)}")
# Extract data from the response
data = response.json()
problem_id = data.get("problem_id")
tutorbot = data.get("tutorbot")
return problem_id, tutorbot
def create_session(app_context, problem_id, tutorbot):
request_data = {
"app_context": app_context,
"problem_id": problem_id,
"tutorbot": tutorbot
}
headers = {"Authorization": f"Bearer {API_BEARER_TOKEN}"}
url = f"{API_BASE_URL}/api/session"
# Make session POST request to LiveHint AI
try:
response = requests.post(url, json=request_data, headers=headers)
response.raise_for_status()
except requests.exceptions.Timeout:
print("The request timed out. Please try again later.")
raise APIException(APIExceptionType.TIMEOUT, "Timeout occurred during API request")
except requests.exceptions.HTTPError as e:
print(f"HTTP error ({e.response.status_code}): {e.response.reason}")
raise APIException(APIExceptionType.HTTP_ERROR, f"HTTP error: {e.response.status_code} - {e.response.reason}")
except requests.exceptions.ConnectionError:
print("Connection error. Please check your network connection.")
raise APIException(APIExceptionType.CONNECTION_ERROR, "Connection error during API request")
except requests.exceptions.RequestException as e:
print(f"An error occurred during the request: {str(e)}")
raise APIException(APIExceptionType.REQUEST_ERROR, f"Request error: {str(e)}")
except Exception as e:
print(f"An unexpected error occurred: {str(e)}")
raise APIException(APIExceptionType.UNEXPECTED_ERROR, f"Unexpected error: {str(e)}")
# Extract data from the response
data = response.json()
session_id = data.get("session_id")
return session_id
def update_session(session_id, model, temperature):
request_data = {
"model": model,
"stream": False,
"temperature": temperature,
"tutorbot_type": API_TUTORBOT_TYPE
}
headers = {"Authorization": f"Bearer {API_BEARER_TOKEN}"}
url = f"{API_BASE_URL}/api/sessions/{session_id}"
# Make sessions/<session_id> PUT request to LiveHint AI
try:
response = requests.put(url, json=request_data, headers=headers)
response.raise_for_status()
except requests.exceptions.Timeout:
print("The request timed out. Please try again later.")
raise APIException(APIExceptionType.TIMEOUT, "Timeout occurred during API request")
except requests.exceptions.HTTPError as e:
print(f"HTTP error ({e.response.status_code}): {e.response.reason}")
raise APIException(APIExceptionType.HTTP_ERROR, f"HTTP error: {e.response.status_code} - {e.response.reason}")
except requests.exceptions.ConnectionError:
print("Connection error. Please check your network connection.")
raise APIException(APIExceptionType.CONNECTION_ERROR, "Connection error during API request")
except requests.exceptions.RequestException as e:
print(f"An error occurred during the request: {str(e)}")
raise APIException(APIExceptionType.REQUEST_ERROR, f"Request error: {str(e)}")
except Exception as e:
print(f"An unexpected error occurred: {str(e)}")
raise APIException(APIExceptionType.UNEXPECTED_ERROR, f"Unexpected error: {str(e)}")
return None
# helper function
def parse_stream_response(stream_content):
result = []
for line in stream_content.splitlines():
if line:
line = line.decode('utf-8')
if line.startswith('data:'):
data = line[5:].strip()
try:
message_data = json.loads(data)
result.append(message_data)
except json.JSONDecodeError:
print(f"Error decoding JSON: {data}")
# normally wouldn't have lines below
elif line.strip() == 'event: done':
break
return result
# called at the beginning; called once for each chat
def init(app_context, course=None, module=None, page=None, question=None, item_id=None, model=API_DEFAULT_MODEL, temperature=API_DEFAULT_TEMPERATURE):
problem_id, tutorbot = get_problem_info(app_context, course, module, page, question, item_id)
session_id = create_session(app_context, problem_id, tutorbot)
update_session(session_id, model, temperature)
return session_id
# called after session is updated; called once for each chat
def start_chat(session_id:str, stream=API_DEFAULT_STREAM) -> list:
stream_str = "true" if stream else "false"
params = {
"system_prompt_num": "9",
"stream": stream_str
}
headers = {"Authorization": f"Bearer {API_BEARER_TOKEN}"}
url = f"{API_BASE_URL}/api/chat-response/{session_id}"
# GET /api/chat-response/<session_id>
try:
response = requests.get(url, params=params, headers=headers)
response.raise_for_status()
except requests.exceptions.Timeout:
print("The request timed out. Please try again later.")
raise APIException(APIExceptionType.TIMEOUT, "Timeout occurred during API request")
except requests.exceptions.HTTPError as e:
print(f"HTTP error ({e.response.status_code}): {e.response.reason}")
raise APIException(APIExceptionType.HTTP_ERROR, f"HTTP error: {e.response.status_code} - {e.response.reason}")
except requests.exceptions.ConnectionError:
print("Connection error. Please check your network connection.")
raise APIException(APIExceptionType.CONNECTION_ERROR, "Connection error during API request")
except requests.exceptions.RequestException as e:
print(f"An error occurred during the request: {str(e)}")
raise APIException(APIExceptionType.REQUEST_ERROR, f"Request error: {str(e)}")
except Exception as e:
print(f"An unexpected error occurred: {str(e)}")
raise APIException(APIExceptionType.UNEXPECTED_ERROR, f"Unexpected error: {str(e)}")
# return list with single response if stream is false, otherwise return list with multiple responses
response_data = [response.json()] if (not stream) else parse_stream_response(response.content)
return response_data
# called whenever the user responds back to the tutorbot's message
def get_chat_response(session_id:str, message:str) -> list:
params = {
"content": message
}
headers = {"Authorization": f"Bearer {API_BEARER_TOKEN}"}
url = f"{API_BASE_URL}/api/chat-response/{session_id}"
# GET /api/chat-response/<session_id>
try:
response = requests.get(url, params=params, headers=headers)
response.raise_for_status() # Raise an exception for 4xx or 5xx status codes
except requests.exceptions.Timeout:
print("The request timed out. Please try again later.")
raise APIException(APIExceptionType.TIMEOUT, "Timeout occurred during API request")
except requests.exceptions.HTTPError as e:
print(f"HTTP error ({e.response.status_code}): {e.response.reason}")
raise APIException(APIExceptionType.HTTP_ERROR, f"HTTP error: {e.response.status_code} - {e.response.reason}")
except requests.exceptions.ConnectionError:
print("Connection error. Please check your network connection.")
raise APIException(APIExceptionType.CONNECTION_ERROR, "Connection error during API request")
except requests.exceptions.RequestException as e:
print(f"An error occurred during the request: {str(e)}")
raise APIException(APIExceptionType.REQUEST_ERROR, f"Request error: {str(e)}")
except Exception as e:
print(f"An unexpected error occurred: {str(e)}")
raise APIException(APIExceptionType.UNEXPECTED_ERROR, f"Unexpected error: {str(e)}")
response_data = [response.json()]
return response_data