-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatgpt.py
130 lines (121 loc) · 4.34 KB
/
chatgpt.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
import requests
import json
from loguru import logger
from config import chatgpt_config
chat_endpoint = chatgpt_config.chat_endpoint
draw_endpoint = chatgpt_config.draw_endpoint
token = chatgpt_config.token
chat_model = chatgpt_config.chat_model
draw_model = chatgpt_config.draw_model
max_tokens = chatgpt_config.max_tokens
system_prompt = chatgpt_config.system_prompt
timeout = chatgpt_config.timeout
def chat(message, history):
messages = history
if not messages:
messages.append({"role": "system", "content": system_prompt})
messages.append({"role": "user", "content": message})
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {token}",
}
data = {
"model": chat_model,
"messages": messages
}
if "o1" in chat_model:
data["max_completion_tokens"] = max_tokens
else:
data["max_tokens"] = max_tokens
status = -1 # -1: undefined, 0: ok, 1: response json error, 2: HTTP status error, 3: timeout
try:
# logger.debug(data)
response = requests.post(chat_endpoint, headers=headers, data=json.dumps(data), timeout=(30, int(timeout)))
if response.status_code == 200:
result = response.json()
try:
answer = result["choices"][0]["message"]["content"]
history = messages
history.append({"role": "assistant", "content": answer})
status = 0
except KeyError:
answer = f"Error: {response.text}"
status = 1
else:
answer = f"Error: {response.text}"
status = 2
except requests.exceptions.Timeout:
answer = "Error: Timeout"
status = 3
return answer, history, status
def chat_with_image(message, images, history):
messages = history
if not messages:
messages.append({"role": "system", "content": system_prompt})
content = [{"type": "text", "text": message}]
for image in images:
content.append({"type": "image_url", "image_url": {"url": image}})
messages.append({"role": "user", "content": content})
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {token}",
}
data = {
"model": chat_model,
"messages": messages
}
if "o1" in chat_model:
data["max_completion_tokens"] = max_tokens
else:
data["max_tokens"] = max_tokens
status = -1 # -1: undefined, 0: ok, 1: response json error, 2: HTTP status error, 3: timeout
try:
response = requests.post(chat_endpoint, headers=headers, data=json.dumps(data), timeout=(30, int(timeout)))
if response.status_code == 200:
result = response.json()
try:
answer = result["choices"][0]["message"]["content"]
history = messages
history.append({"role": "assistant", "content": answer})
status = 0
except KeyError:
answer = f"Error: {response.text}"
status = 1
else:
answer = f"Error: {response.text}"
status = 2
except requests.exceptions.Timeout:
answer = "Error: Timeout"
status = 3
return answer, history, status
def draw(prompt):
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {token}",
}
data = {
"model": draw_model,
"prompt": prompt
}
status = -1 # -1: undefined, 0: ok, 1: response json error, 2: HTTP status error, 3: timeout
try:
response = requests.post(draw_endpoint, headers=headers, data=json.dumps(data), timeout=(20, int(timeout)))
if response.status_code == 200:
result = response.json()
try:
answer = result["data"][0]["url"]
status = 0
except KeyError:
answer = f"Error: {response.text}"
status = 1
else:
answer = f"Error: {response.text}"
status = 2
except requests.exceptions.Timeout:
answer = "Error: Timeout"
status = 3
return answer, status
if __name__ == "__main__":
_private_chat_history = [{"role": "system", "content": "You are a helpful assistant."}]
_answer, _private_chat_history, _status = chat("Hello", _private_chat_history)
logger.info(_answer)