-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
147 lines (132 loc) · 4.99 KB
/
main.py
File metadata and controls
147 lines (132 loc) · 4.99 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
import asyncio
import json
import random
import time
import requests
from fastapi import Body, FastAPI, HTTPException, Request, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, StreamingResponse
from merl import ask, init
session = requests.Session()
data = init(session)
etag = data.get("etag", None)
if __name__ == "__main__":
while True:
question = input(": ")
response, etag, usage = ask(session, data, etag, question)
print(response)
else:
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=[
"POST",
"GET",
"OPTIONS",
],
allow_headers=["*"],
)
@app.post("/v1/chat/completions", response_class=JSONResponse)
async def completions(request: Request, body=Body(...)):
global etag, session, data
try:
question = body["messages"][-1]["content"]
except KeyError:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Could not find messages array",
)
response, etag, usage = ask(session, data, etag, question)
if response is None:
data = init(session)
etag = data.get("etag", None)
response, etag, usage = ask(session, data, etag, question)
if response is None:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Session re-initialization failed. Please try again later.",
)
elif response == 413:
raise HTTPException(
status_code=status.HTTP_413_CONTENT_TOO_LARGE,
detail="Request body too large",
)
if body.get("stream", False):
async def generator(text):
arr = text.split(" ")
i = 0
yield (
'data: {"id":"chatcmpl-'
+ request.headers.get("Cf-Ray", "")
+ '","object":"chat.completion.chunk","created":'
+ str(int(time.time()))
+ ',"model":"merl", "choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}]}\n\n'
)
while True:
if await request.is_disconnected() or i == len(arr):
stoppacket = (
'data: {"id":"chatcmpl-'
+ request.headers.get("Cf-Ray", "")
+ '","object":"chat.completion.chunk","created":'
+ str(int(time.time()))
+ ',"model":"merl", "choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}]}\n\n'
)
yield stoppacket
break
token = (
arr[i]
.replace("\\", "\\\\")
.replace('"', '\\"')
.replace("\n", "\\n")
.replace("\r", "\\r")
.replace("<br></br>", "\\n")
)
packet = (
'data: {"id":"chatcmpl-'
+ request.headers.get("Cf-Ray", "")
+ '","object":"chat.completion.chunk","created":'
+ str(int(time.time()))
+ ',"model":"merl", "choices":[{"index":0,"delta":{"content":"'
+ token
+ " "
+ '"},"logprobs":null,"finish_reason":null}]}\n\n'
)
yield packet
i += 1
await asyncio.sleep(random.random() / 25)
return StreamingResponse(
generator(response), media_type="text/event-stream"
)
else:
return {
"id": f"chatcmpl-{request.headers.get('Cf-Ray', '')}",
"created": int(time.time()),
"model": "merl",
"object": "chat.completion",
"choices": [
{
"text": response,
"finish_reason": "stop",
}
],
"usage": {
"prompt_tokens": usage.get("promptTokens", ""),
"completion_tokens": usage.get("completionTokens", ""),
"total_tokens": usage.get("totalTokens", ""),
},
}
@app.get("/v1/models")
def models():
return {
"object": "list",
"data": [
{
"id": "merl",
"object": "model",
"created": 67,
"owned_by": "microsoft",
}
],
}