-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathth_gpt.py
More file actions
77 lines (62 loc) · 2.45 KB
/
th_gpt.py
File metadata and controls
77 lines (62 loc) · 2.45 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
import ast
import json
from typing import Tuple
import requests
with open("cookies.json", "r") as file:
cookies = json.loads(file.read())
with open("headers.json", "r") as file:
headers = json.loads(file.read())
data = {
"model": "gpt-4-turbo-preview",
# "model": "gpt-3.5-turbo", # this doesn't seem to work. it must be hardcoded server side
"stream": True,
"messages": [],
}
class TH_GPT:
def __init__(self, system_prompt: str = "You are a helpful assistant who works at the University of Applied Sciences in Cologne."):
self.system_prompt = system_prompt
self.messages = []
self.messages.append({"role": "system", "content": system_prompt})
def send_message(self, prompt: str):
self.messages.append({"role": "user", "content": prompt})
answer = generate_answer(self.messages)
self.messages.append({"role": "assistant", "content": answer})
return answer
def send_message_without_context(self, prompt: str):
messages = []
messages.append({"role": "system", "content": self.system_prompt})
messages.append({"role": "user", "content": prompt})
answer = generate_answer(messages)
return answer
def generate_answer(messages: list[dict[str, str]]):
data["messages"] = messages
answer = []
with requests.post(
"https://ki.th-koeln.de/stream-api.php",
cookies=cookies,
headers=headers,
data=json.dumps(data),
stream=True,
) as response:
try:
response.raise_for_status() # Raises stored HTTPError, if one occurred.
# Process the stream.
for chunk in response.iter_lines():
chunk = chunk.decode("utf-8")
if chunk == "":
pass
else:
chunk = str(chunk[6:]).strip()
chunk = chunk.replace("null", "None") # convert js to python)
chunk = ast.literal_eval(chunk) # evaluate as dict
# print(chunk)
if len(chunk["choices"][0]["delta"]) > 0:
content = chunk["choices"][0]["delta"]["content"]
answer.append(content)
else:
break
except requests.exceptions.HTTPError as e:
# Handle any errors that occur during the request.
print(e)
answer = "".join(answer)
return answer