-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.py
More file actions
317 lines (258 loc) · 9.85 KB
/
service.py
File metadata and controls
317 lines (258 loc) · 9.85 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
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
import asyncio
import time
import uuid
from typing import List, Optional, Dict, Any, AsyncGenerator
import random
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
import tiktoken
# Pydantic models for request/response
class ImageUrl(BaseModel):
url: str
detail: Optional[str] = "auto"
class ContentItem(BaseModel):
type: str
text: Optional[str] = None
image_url: Optional[ImageUrl] = None
class Message(BaseModel):
role: str
content: Optional[str | List[ContentItem]] = None
class ChatCompletionRequest(BaseModel):
model: str
messages: List[Message]
stream: Optional[bool] = False
max_tokens: Optional[int] = 100
temperature: Optional[float] = 1.0
class Choice(BaseModel):
index: int
message: Message
finish_reason: Optional[str] = None
class Usage(BaseModel):
prompt_tokens: int
completion_tokens: int
total_tokens: int
class ChatCompletionResponse(BaseModel):
id: str
object: str = "chat.completion"
created: int
model: str
choices: List[Choice]
usage: Usage
class DeltaChoice(BaseModel):
index: int
delta: Dict[str, Any]
finish_reason: Optional[str] = None
class ChatCompletionStreamResponse(BaseModel):
id: str
object: str = "chat.completion.chunk"
created: int
model: str
choices: List[DeltaChoice]
class ModelInfo(BaseModel):
id: str
object: str = "model"
created: int
owned_by: str
class ModelsResponse(BaseModel):
object: str = "list"
data: List[ModelInfo]
AVAILABLE_MODELS = [
"gpt-3.5-turbo",
"gpt-3.5-turbo-0301",
"gpt-3.5-turbo-0613",
"gpt-3.5-turbo-16k",
"gpt-4",
"gpt-4-0314",
"gpt-4-0613",
"gpt-4-32k",
"text-davinci-003",
"text-davinci-002",
]
SAMPLE_RESPONSES = [
"Hello! How can I assist you today?",
"I'm here to help you with any questions you might have.",
"That's an interesting question. Let me think about it.",
"I understand what you're asking. Here's my response:",
"Thank you for your question. I'd be happy to help.",
"Based on the information provided, I can offer the following insights:",
"Let me provide you with a comprehensive answer to your query.",
"I appreciate you reaching out. Here's what I can tell you:",
]
FILLER_PHRASES = [
"Additionally, I want to mention that",
"Furthermore, it's important to note that",
"Moreover, we should consider that",
"In fact, this reminds me that",
"It's worth noting that",
"Please also consider that",
"Also, I should add that",
"On a related note,",
"To elaborate further,",
"In this context,",
]
EXTENSION_TEMPLATES = [
"this is a very interesting topic that deserves careful consideration",
"there are many aspects to explore in this particular area of discussion",
"we can approach this from multiple different perspectives and viewpoints",
"the implications of this are quite significant and far-reaching in nature",
"this subject matter has various nuances that are worth examining closely",
"there are several factors that contribute to the overall understanding here",
"the complexity of this issue requires thorough analysis and careful thought",
]
try:
encoding = tiktoken.get_encoding("cl100k_base")
except Exception:
encoding = None
def _get_timing_params(request: Request) -> tuple[float, float, int, int]:
ttft_ms = float(request.headers.get("X-TTFT-MS", 100))
itl_ms = float(request.headers.get("X-ITL-MS", 50))
output_length = int(request.headers.get("X-OUTPUT-LENGTH", 20))
sse_batch_size = int(request.headers.get("X-SSE-BATCH-SIZE", 4))
return ttft_ms / 1000.0, itl_ms / 1000.0, output_length, sse_batch_size
def _count_tokens(text: str) -> int:
if encoding is None:
return len(text) // 4
try:
return len(encoding.encode(text))
except Exception:
return len(text) // 4
def _generate_response_content(target_tokens: int) -> str:
base_response = random.choice(SAMPLE_RESPONSES)
current_content = base_response
current_tokens = _count_tokens(current_content)
if current_tokens >= target_tokens:
if encoding is not None:
try:
encoded = encoding.encode(current_content)
return encoding.decode(encoded[:target_tokens])
except Exception:
pass
words = current_content.split()
return " ".join(words[: max(1, int(target_tokens // 1.3))])
while current_tokens < target_tokens:
filler = random.choice(FILLER_PHRASES)
extension = random.choice(EXTENSION_TEMPLATES)
addition = f" {filler} {extension}."
addition_tokens = _count_tokens(addition)
if current_tokens + addition_tokens <= target_tokens:
current_content += addition
current_tokens += addition_tokens
else:
remaining = target_tokens - current_tokens
if remaining > 0 and encoding is not None:
try:
encoded_addition = encoding.encode(addition)
partial = encoding.decode(encoded_addition[:remaining])
current_content += partial
except Exception:
current_content += " more"
else:
current_content += " more"
break
return current_content.strip()
async def _stream_response(
request_data: ChatCompletionRequest,
ttft: float,
itl: float,
output_length: int,
sse_batch_size: int,
) -> AsyncGenerator[str, None]:
request_id = f"chatcmpl-{uuid.uuid4().hex[:8]}"
created = int(time.time())
await asyncio.sleep(ttft)
content = _generate_response_content(output_length)
first_chunk = ChatCompletionStreamResponse(
id=request_id,
created=created,
model=request_data.model,
choices=[DeltaChoice(index=0, delta={"role": "assistant", "content": ""}, finish_reason=None)],
)
yield f"data: {first_chunk.model_dump_json()}\n\n"
if encoding is not None:
try:
tokens = encoding.encode(content)
for i in range(0, len(tokens), sse_batch_size):
if i > 0:
await asyncio.sleep(itl)
batch_text = encoding.decode(tokens[i : i + sse_batch_size])
chunk = ChatCompletionStreamResponse(
id=request_id,
created=created,
model=request_data.model,
choices=[DeltaChoice(index=0, delta={"content": batch_text}, finish_reason=None)],
)
yield f"data: {chunk.model_dump_json()}\n\n"
except Exception:
words = content.split()
for i in range(0, len(words), sse_batch_size):
if i > 0:
await asyncio.sleep(itl)
batch_text = " ".join(words[i : i + sse_batch_size]) + " "
chunk = ChatCompletionStreamResponse(
id=request_id,
created=created,
model=request_data.model,
choices=[DeltaChoice(index=0, delta={"content": batch_text}, finish_reason=None)],
)
yield f"data: {chunk.model_dump_json()}\n\n"
else:
words = content.split()
for i in range(0, len(words), sse_batch_size):
if i > 0:
await asyncio.sleep(itl)
batch_text = " ".join(words[i : i + sse_batch_size]) + " "
chunk = ChatCompletionStreamResponse(
id=request_id,
created=created,
model=request_data.model,
choices=[DeltaChoice(index=0, delta={"content": batch_text}, finish_reason=None)],
)
yield f"data: {chunk.model_dump_json()}\n\n"
final_chunk = ChatCompletionStreamResponse(
id=request_id,
created=created,
model=request_data.model,
choices=[DeltaChoice(index=0, delta={}, finish_reason="stop")],
)
yield f"data: {final_chunk.model_dump_json()}\n\n"
yield "data: [DONE]\n\n"
app = FastAPI(title="OpenAI Emulator")
@app.post("/v1/chat/completions")
async def chat_completions(request: Request):
try:
body = await request.json()
request_data = ChatCompletionRequest(**body)
ttft, itl, output_length, sse_batch_size = _get_timing_params(request)
if request_data.stream:
return StreamingResponse(
_stream_response(request_data, ttft, itl, output_length, sse_batch_size),
media_type="text/plain",
headers={"Cache-Control": "no-cache", "Connection": "keep-alive"},
)
await asyncio.sleep(ttft)
content = _generate_response_content(output_length)
await asyncio.sleep(itl * output_length)
prompt_tokens = len(str(request_data.messages)) // 4
completion_tokens = _count_tokens(content)
response = ChatCompletionResponse(
id=f"chatcmpl-{uuid.uuid4().hex[:8]}",
created=int(time.time()),
model=request_data.model,
choices=[Choice(index=0, message=Message(role="assistant", content=content), finish_reason="stop")],
usage=Usage(
prompt_tokens=prompt_tokens,
completion_tokens=completion_tokens,
total_tokens=prompt_tokens + completion_tokens,
),
)
return response.model_dump()
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.get("/v1/models")
async def models():
data = [ModelInfo(id=m, created=int(time.time()), owned_by="openai") for m in AVAILABLE_MODELS]
return ModelsResponse(data=data).model_dump()
@app.get("/health")
async def health_check():
return {"status": "healthy", "timestamp": int(time.time())}