-
Notifications
You must be signed in to change notification settings - Fork 35
/
openai_api.py
346 lines (288 loc) · 10.7 KB
/
openai_api.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import sys, signal, time, os
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
import itertools
this_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
PATH_BINDS = os.path.join(this_dir, '..', 'bindings')
sys.path.append(PATH_BINDS)
from chatllm import LibChatLLM, ChatLLM, ChatLLMStreamer
class HttpResponder:
def __init__(self, req: BaseHTTPRequestHandler, id: str, timestamp: int, model: str) -> None:
self.req = req
self.timestamp = timestamp
self.model = model
self.id = id
def recv_chunk(self, chunk: str) -> bool:
return True
def done(self) -> None:
pass
def send_str(self, s: str) -> bool:
self.req.wfile.write(s.encode('utf-8'))
return True
class ChatCompletionNonStreamResponder(HttpResponder):
def __init__(self, req: BaseHTTPRequestHandler, id: str, timestamp: int, model: str) -> None:
super().__init__(req, id, timestamp, model)
self.acc = ''
def recv_chunk(self, content: str) -> bool:
self.acc = self.acc + content
return True
def done(self) -> None:
rsp = {
"id": self.id,
"object": "chat.completion",
"created": self.timestamp,
"model": self.model,
"system_fingerprint": "fp_xxx",
"choices": [{
"index": 0,
"delta": {
"role": "assistant",
"content": self.acc,
},
"logprobs": None,
"finish_reason" : "stop"
}],
'usage': {
"prompt_tokens": 1,
"completion_tokens": 1,
"total_tokens": 1
}
}
self.send_str(json.dumps(rsp) + '\n\n')
class ChatCompletionStreamResponder(HttpResponder):
def __init__(self, req: BaseHTTPRequestHandler, id: str, timestamp: int, model: str) -> None:
super().__init__(req, id, timestamp, model)
def recv_chunk(self, content: str) -> bool:
rsp = {
"id": self.id,
"object": "chat.completion.chunk",
"created": self.timestamp,
"model": self.model,
"system_fingerprint": "fp_xxx",
"choices": [{
"index": 0,
"delta": {
"role": "assistant",
"content": content,
},
"logprobs": None,
"finish_reason" : None
}]
}
self.send_str(json.dumps(rsp) + '\n\n')
return True
def done(self) -> None:
rsp = {
"id": self.id,
"object": "chat.completion",
"created": self.timestamp,
"model": self.model,
"system_fingerprint": "fp_xxx",
"choices": [{
"index": 0,
"delta": {
"role": "assistant",
"content": '',
},
"logprobs": None,
"finish_reason" : "stop"
}],
'usage': {
"prompt_tokens": 1,
"completion_tokens": 1,
"total_tokens": 1
}
}
self.send_str(json.dumps(rsp) + '\n\n')
class LegacyCompletionNonStreamResponder(HttpResponder):
def __init__(self, req: BaseHTTPRequestHandler, id: str, timestamp: int, model: str) -> None:
super().__init__(req, id, timestamp, model)
self.acc = ''
def recv_chunk(self, content: str) -> bool:
self.acc = self.acc + content
return True
def done(self) -> None:
rsp = {
"id": self.id,
"object": "text_completion",
"created": self.timestamp,
"model": self.model,
"system_fingerprint": "fp_xxx",
"choices": [
{
"text": self.acc,
"index": 0,
"logprobs": None,
"finish_reason": "length"
}
],
"usage": {
"prompt_tokens": 1,
"completion_tokens": 1,
"total_tokens": 1
}
}
self.send_str(json.dumps(rsp) + '\n')
class LegacyCompletionStreamResponder(HttpResponder):
def __init__(self, req: BaseHTTPRequestHandler, id: str, timestamp: int, model: str) -> None:
super().__init__(req, id, timestamp, model)
self.first_chunk = True
def recv_chunk(self, content: str) -> bool:
rsp = {
"id": self.id,
"object": "chat.completion.chunk",
"created": self.timestamp,
"choices": [
{
"text": content,
"index": 0,
"logprobs": None,
#"finish_reason": None
}
],
"model": self.model,
"system_fingerprint": "fp_xxx"
}
self.send_str('data: ' + json.dumps(rsp) + '\n\n')
return True
def done(self) -> None:
self.send_str('data: [DONE]\n')
class SessionManager:
def __init__(self) -> None:
self._id = 1
def make_id(self) -> str:
self._id += 1
return '_chatllm_' + str(self._id)
session_man: SessionManager = SessionManager()
chat_streamer: ChatLLMStreamer = None
fim_streamer: ChatLLMStreamer = None
emb_model_obj: ChatLLM = None
http_server: HTTPServer = None
def get_streamer(model: str) -> ChatLLMStreamer | None:
if model.endswith('fim') or model.startswith('fim'):
return fim_streamer
else:
return chat_streamer
def handler(signal_received, frame):
http_server.shutdown()
sys.exit(0)
class HttpHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(404, 'POST')
def handl_EMBEDDING(self, obj: dict):
if emb_model_obj is None:
self.send_response(404, 'NOT SUPPORTED')
return
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
def mk_emb(i: int, emb) -> dict:
return { "object": "embedding", "embedding": emb, "index": i }
input = obj['input']
if isinstance(input, str):
input = [input]
r = []
for i, s in enumerate(input):
r.append(mk_emb(i, emb_model_obj.text_embedding(s)))
rsp = {
"object": "list", "data": r,
"model": obj['model'],
"usage": { "prompt_tokens": 8, "total_tokens": 8 }
}
self.wfile.write(json.dumps(rsp).encode('utf-8'))
def do_POST(self):
print(self.path)
args = self.rfile.read(int(self.headers['content-length'])).decode('utf-8')
try:
obj = json.loads(args)
print(obj)
except:
self.send_error(404, 'BAD REQ')
return
model = obj['model'] if 'model' in obj else 'chat'
max_tokens = obj['max_tokens'] if 'max_tokens' in obj else -1
if self.path.endswith('/completions'):
pass
elif self.path.endswith('/generate'):
model = 'fim'
elif self.path.endswith('/embeddings'):
self.handl_EMBEDDING(obj)
return
else:
self.send_error(404, 'NOT FOUND')
return
self.send_response(200)
id = session_man.make_id()
timestamp = int(time.time())
prompt = ''
stream = False
restart = False
if 'stream' in obj:
stream = obj['stream']
if 'messages' in obj:
counter = 0
flag = True
# aggregate all user messages
for i in range(len(obj['messages']) - 1, -1, -1):
x = obj['messages'][i]
if x['role'] == 'user':
counter = counter + 1
else:
flag = False
if flag:
prompt = x['content'] + '\n' + prompt
restart = counter < 2
responder_cls = ChatCompletionStreamResponder if stream else ChatCompletionNonStreamResponder
self.send_header('Content-type', 'application/json')
else:
prompt = obj['prompt']
responder_cls = LegacyCompletionStreamResponder if stream else LegacyCompletionNonStreamResponder
if stream:
self.send_header('Content-type', 'text/event-stream')
else:
self.send_header('Content-type', 'application/json')
self.end_headers()
responder = responder_cls(self, id, timestamp, model)
streamer = get_streamer(model)
if streamer is not None:
streamer.set_max_gen_tokens(max_tokens)
try:
if restart: streamer.restart()
for x in streamer.chat(prompt):
responder.recv_chunk(x)
except:
streamer.abort()
else:
responder.recv_chunk('FIM model not loaded!')
responder.done()
def split_list(lst, val):
return [list(group) for k,
group in
itertools.groupby(lst, lambda x: x==val) if not k]
if __name__ == '__main__':
signal.signal(signal.SIGINT, handler)
ARG_SEP = '---'
args = sys.argv[1:]
if len(args) < 1:
print(f"usage: python openai_api.py path/to/chat/model path/to/fim/model path/to/emb/model [more args for chat model {ARG_SEP} more args for fim model {ARG_SEP} more args for embedding model]")
print('Use * to skip loading a model')
exit(-1)
if len(args) < 3:
args = args + ['*' for i in range(3 - len(args))]
chat_model = args[0]
fim_model = args[1]
emb_model = args[2]
all_model_args = split_list(args[3:], ARG_SEP)
chat_args = all_model_args[0] if len(all_model_args) >= 1 else []
fim_args = all_model_args[1] if len(all_model_args) >= 2 else []
emb_args = all_model_args[2] if len(all_model_args) >= 3 else []
basic_args = ['-m']
if chat_model != '*':
chat_streamer = ChatLLMStreamer(ChatLLM(LibChatLLM(PATH_BINDS), basic_args + [chat_model] + chat_args, False))
if fim_model != '*':
fim_streamer = ChatLLMStreamer(ChatLLM(LibChatLLM(PATH_BINDS), basic_args + [fim_model, '--format', 'completion'] + fim_args, False))
fim_streamer.auto_restart = True
if emb_model != '*':
emb_model_obj = ChatLLM(LibChatLLM(PATH_BINDS), basic_args + [emb_model] + emb_args)
http_server = HTTPServer(('0.0.0.0', 3000), HttpHandler)
http_server.serve_forever()