Update main.py
Browse files
main.py
CHANGED
@@ -187,28 +187,74 @@ class ChatRequest(BaseModel):
|
|
187 |
from fastapi.responses import Response
|
188 |
|
189 |
@app.post("/v1/chat/completions")
|
190 |
-
async def chat_completions(
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
if isinstance(chunk, str):
|
201 |
-
response_content += chunk
|
202 |
-
else:
|
203 |
-
response_content += chunk.content
|
204 |
|
205 |
-
#
|
206 |
-
|
207 |
|
208 |
-
|
209 |
-
"
|
210 |
-
"
|
211 |
-
"
|
212 |
-
|
213 |
-
|
|
|
|
|
|
|
214 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
from fastapi.responses import Response
|
188 |
|
189 |
@app.post("/v1/chat/completions")
|
190 |
+
async def chat_completions(
|
191 |
+
request: ChatRequest, app_secret: str = Depends(verify_app_secret)
|
192 |
+
):
|
193 |
+
logger.info(f"Received chat completion request for model: {request.model}")
|
194 |
+
|
195 |
+
if request.model not in [model['id'] for model in ALLOWED_MODELS]:
|
196 |
+
raise HTTPException(
|
197 |
+
status_code=400,
|
198 |
+
detail=f"Model {request.model} is not allowed. Allowed models are: {', '.join(model['id'] for model in ALLOWED_MODELS)}",
|
199 |
+
)
|
|
|
|
|
|
|
|
|
200 |
|
201 |
+
# Generate a UUID for the conversation
|
202 |
+
conversation_id = str(uuid.uuid4()).replace("-", "")
|
203 |
|
204 |
+
json_data = {
|
205 |
+
"attachments": [],
|
206 |
+
"conversationId": conversation_id,
|
207 |
+
"prompt": "\n".join(
|
208 |
+
[
|
209 |
+
f"{'User' if msg.role == 'user' else 'Assistant'}: {msg.content}"
|
210 |
+
for msg in request.messages
|
211 |
+
]
|
212 |
+
),
|
213 |
}
|
214 |
+
|
215 |
+
headers["uniqueid"] = conversation_id
|
216 |
+
|
217 |
+
async def generate():
|
218 |
+
async with httpx.AsyncClient() as client:
|
219 |
+
try:
|
220 |
+
async with client.stream('POST', f'{BASE_URL}/api/chat/gpt4o/chat', headers=headers, json=json_data, timeout=120.0) as response:
|
221 |
+
response.raise_for_status()
|
222 |
+
async for line in response.aiter_lines():
|
223 |
+
if line and line != "[DONE]":
|
224 |
+
content = json.loads(line)["data"]
|
225 |
+
yield f"data: {json.dumps(create_chat_completion_data(content['message'], request.model))}\n\n"
|
226 |
+
yield f"data: {json.dumps(create_chat_completion_data('', request.model, 'stop'))}\n\n"
|
227 |
+
yield "data: [DONE]\n\n"
|
228 |
+
except httpx.HTTPStatusError as e:
|
229 |
+
logger.error(f"HTTP error occurred: {e}")
|
230 |
+
raise HTTPException(status_code=e.response.status_code, detail=str(e))
|
231 |
+
except httpx.RequestError as e:
|
232 |
+
logger.error(f"An error occurred while requesting: {e}")
|
233 |
+
raise HTTPException(status_code=500, detail=str(e))
|
234 |
+
|
235 |
+
if request.stream:
|
236 |
+
logger.info("Streaming response")
|
237 |
+
return StreamingResponse(generate(), media_type="text/event-stream")
|
238 |
+
else:
|
239 |
+
logger.info("Non-streaming response")
|
240 |
+
full_response = ""
|
241 |
+
async for chunk in generate():
|
242 |
+
if chunk.startswith("data: ") and not chunk[6:].startswith("[DONE]"):
|
243 |
+
data = json.loads(chunk[6:])
|
244 |
+
if data["choices"][0]["delta"].get("content"):
|
245 |
+
full_response += data["choices"][0]["delta"]["content"]
|
246 |
+
|
247 |
+
return {
|
248 |
+
"id": f"chatcmpl-{uuid.uuid4()}",
|
249 |
+
"object": "chat.completion",
|
250 |
+
"created": int(datetime.now().timestamp()),
|
251 |
+
"model": request.model,
|
252 |
+
"choices": [
|
253 |
+
{
|
254 |
+
"index": 0,
|
255 |
+
"message": {"role": "assistant", "content": full_response},
|
256 |
+
"finish_reason": "stop",
|
257 |
+
}
|
258 |
+
],
|
259 |
+
"usage": None,
|
260 |
+
}
|