Niansuh commited on
Commit
2f2df1f
·
verified ·
1 Parent(s): 10cdc26

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +20 -47
main.py CHANGED
@@ -7,10 +7,6 @@ from aiohttp import ClientSession
7
  from fastapi import FastAPI, HTTPException
8
  from pydantic import BaseModel
9
  from typing import List, Dict, Any, Optional
10
- import time
11
- import uuid
12
- import json
13
- from fastapi.responses import StreamingResponse
14
 
15
  # Mock implementations for ImageResponse and to_data_uri
16
  class ImageResponse:
@@ -186,7 +182,6 @@ class ChatRequest(BaseModel):
186
  model: str
187
  messages: List[Message]
188
 
189
-
190
  @app.post("/v1/chat/completions")
191
  async def chat_completions(request: ChatRequest):
192
  messages = [{"role": msg.role, "content": msg.content} for msg in request.messages]
@@ -197,45 +192,23 @@ async def chat_completions(request: ChatRequest):
197
  messages=messages
198
  )
199
 
200
- response_id = f"chatcmpl-{uuid.uuid4()}"
201
- created_timestamp = int(time.time())
202
-
203
- async def generate_response():
204
- # Stream the response content
205
- async for chunk in async_generator:
206
- if isinstance(chunk, str):
207
- yield json.dumps({
208
- "id": response_id,
209
- "object": "chat.completion.chunk",
210
- "created": created_timestamp,
211
- "model": request.model,
212
- "choices": [
213
- {
214
- "message": {
215
- "role": "assistant",
216
- "content": chunk
217
- },
218
- "finish_reason": None,
219
- "index": 0
220
- }
221
- ]
222
- }).encode('utf-8')
223
- else:
224
- yield json.dumps({
225
- "id": response_id,
226
- "object": "chat.completion.chunk",
227
- "created": created_timestamp,
228
- "model": request.model,
229
- "choices": [
230
- {
231
- "message": {
232
- "role": "assistant",
233
- "content": chunk.content
234
- },
235
- "finish_reason": None,
236
- "index": 0
237
- }
238
- ]
239
- }).encode('utf-8')
240
-
241
- return StreamingResponse(generate_response(), media_type="application/json")
 
7
  from fastapi import FastAPI, HTTPException
8
  from pydantic import BaseModel
9
  from typing import List, Dict, Any, Optional
 
 
 
 
10
 
11
  # Mock implementations for ImageResponse and to_data_uri
12
  class ImageResponse:
 
182
  model: str
183
  messages: List[Message]
184
 
 
185
  @app.post("/v1/chat/completions")
186
  async def chat_completions(request: ChatRequest):
187
  messages = [{"role": msg.role, "content": msg.content} for msg in request.messages]
 
192
  messages=messages
193
  )
194
 
195
+ response_content = ""
196
+ async for chunk in async_generator:
197
+ response_content += chunk if isinstance(chunk, str) else chunk.content # Concatenate response
198
+
199
+ return {
200
+ "id": f"chatcmpl-{uuid.uuid4()}", # Generate a unique ID
201
+ "object": "chat.completion",
202
+ "created": int(datetime.now().timestamp()), # Current timestamp
203
+ "model": request.model,
204
+ "choices": [
205
+ {
206
+ "message": {
207
+ "role": "assistant",
208
+ "content": response_content
209
+ },
210
+ "finish_reason": "stop",
211
+ "index": 0
212
+ }
213
+ ]
214
+ }