-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
236 lines (193 loc) · 7.82 KB
/
Copy pathmodel.py
File metadata and controls
236 lines (193 loc) · 7.82 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
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
import requests
import json
import websockets
# import asyncio
from utils import *
class BaseModel:
def predict(self, utterance, history):
pass
class ChatGLM6B_API(BaseModel):
def __init__(self, url) -> None:
self.url = url
def predict(self, utterance, history):
payload = {"question": utterance, "history": history}
# 发送 POST 请求
# response = requests.post(self.url, json=payload, timeout=30)
response = requests.post(self.url, json=payload, timeout=300)
# 获取响应结果
if response.status_code == 200:
data = response.json()
if "response" in data.keys():
return data["response"], data["history"], 0
else:
print(json.dumps(response, ensure_ascii=False, indent=2))
raise "请求失败"
else:
print("请求失败:%d" % (response.status_code))
raise "请求失败"
def close(self):
pass
def getInitHistory(self, init_prompt):
return [[init_prompt, "好的"]]
class ChatGLM6B_SOCK(BaseModel):
def __init__(self, websocket_url) -> None:
self.websocket_url = websocket_url
self.websocket = None
async def connect(self):
self.websocket = await websockets.connect(
self.websocket_url, ping_interval=None
)
async def send_request(self, req):
await self.websocket.send(json.dumps(req))
async def receive_response(self):
resp = await self.websocket.recv()
return json.loads(resp)
async def close(self):
await self.websocket.close()
async def predict(self, utterance, history):
if not self.websocket:
await self.connect()
payload = {"question": utterance, "history": history}
try:
await self.send_request(payload)
except Exception as e:
print(e)
try:
resp = await self.receive_response()
except Exception as e:
print(e)
print(resp)
return resp["response"], resp["history"], 0
def getInitHistory(self, init_prompt):
return [[init_prompt, "好的"]]
class TextDavinci003_API(BaseModel):
def __init__(self, url) -> None:
self.url = url
def predict(self, utterance, history):
dialogue_str = "".join(
[f"User:{item[0]}\nAssistant:{item[1]}\n" for item in history]
) + "\nUser:{}\nAssistant:".format(utterance)
# dialogue_str = ''.join([f'用户:{item[0]}\n助手:{item[1]}\n' for item in history]) + '\n用户:{}\n助手:'.format(utterance)
print("-----------------------------------------")
print("dialogue_str:\n", dialogue_str)
print("-----------------------------------------")
response = completions_with_backoff(
engine="text-davinci-003",
# engine='text-davinci-002',
prompt=dialogue_str,
n=1,
max_tokens=1000, # 设置生成的代码长度
temperature=0.7, # 控制输出的多样性,较高的值生成更随机的结果,较低的值生成更确定的结果
)
response_str = response.choices[0].text.strip()
print("finish_reason:\t", response.choices[0].finish_reason)
print("response:\n", response_str)
history.append([utterance, response_str])
return response_str, history, response["usage"]["total_tokens"]
# response:
# {
# "choices": [
# {
# "finish_reason": "stop",
# "index": 0,
# "logprobs": null,
# "text": "\n\ndef double_list(lst):\n return [x * 2 for x in lst]"
# }
# ],
# "created": 1685419452,
# "id": "cmpl-7LkseBxlTSMPloeXvWvWHw2eJvVwp",
# "model": "text-davinci-003",
# "object": "text_completion",
# "usage": {
# "completion_tokens": 23,
# "prompt_tokens": 51,
# "total_tokens": 74
# }
# }
def close(self):
pass
def getInitHistory(self, init_prompt):
return [[init_prompt, "好的"]]
class GPT35Turbo_API(BaseModel):
def __init__(self, url) -> None:
self.url = url
def getInitHistory(self, init_prompt):
return [{"role": "system", "content": init_prompt}]
def predict(self, utterance, history):
# messages is history
# messages=[
# {"role": "system", "content": "You are a helpful assistant."},
# {"role": "user", "content": "Who won the world series in 2020?"},
# {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
# {"role": "user", "content": "Where was it played?"}
# ]
print("-----------------------------------------")
print("history:\n", history)
print("-----------------------------------------")
history.append({"role": "user", "content": utterance})
response = chat_completions_with_backoff(
model="gpt-3.5-turbo",
messages=history,
n=1,
max_tokens=1000, # 设置生成的代码长度
temperature=0.7, # 控制输出的多样性,较高的值生成更随机的结果,较低的值生成更确定的结果
)
# response = openai.ChatCompletion.create(
# model="gpt-3.5-turbo",
# messages=[
# {"role": "system", "content": "You are a helpful assistant."},
# {"role": "user", "content": "Who won the world series in 2020?"},
# {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
# {"role": "user", "content": "Where was it played?"}
# ],
# n=1,
# max_tokens=1000, # 设置生成的代码长度
# temperature=0.7, # 控制输出的多样性,较高的值生成更随机的结果,较低的值生成更确定的结果
# )
response_str = response.choices[0].message.content.strip()
print("finish_reason:\t", response.choices[0].finish_reason)
print("response:\n", response_str)
history.append({"role": "assistant", "content": response_str})
return response_str, history, response.usage.total_tokens
# response:
# {
# "choices": [
# {
# "finish_reason": "stop",
# "index": 0,
# "message": {
# "content": "The 2020 World Series was played at Globe Life Field in Arlington, Texas due to the COVID-19 pandemic.",
# "role": "assistant"
# }
# }
# ],
# "created": 1685449137,
# "id": "chatcmpl-7LsbRMkFNY5YA7qCapR4Ia5maw7Th",
# "model": "gpt-3.5-turbo-0301",
# "object": "chat.completion",
# "usage": {
# "completion_tokens": 24,
# "prompt_tokens": 57,
# "total_tokens": 81
# }
# }
def close(self):
pass
class ERNIE_Bot_API(BaseModel):
def __init__(self, url) -> None:
self.url = url
def getInitHistory(self, init_prompt):
return [{"role": "user", "content": init_prompt},{"role": "assistant", "content": "好的"}]
def predict(self, utterance, history):
print("-----------------------------------------")
print("history:\n", history)
print("-----------------------------------------")
history.append({"role": "user", "content": utterance})
response = chat_with_ernie(self.url, history=history)
response_str = response.get('result')
# print("finish_reason:\t", response.choices[0].finish_reason)
print("response:\n", response_str)
history.append({"role": "assistant", "content": response_str})
return response_str, history, response.get('usage').get('total_tokens')
def close(self):
pass