Update main.py
Browse files
main.py
CHANGED
|
@@ -6,9 +6,8 @@ from datetime import datetime
|
|
| 6 |
from typing import Any, Dict, List, Optional
|
| 7 |
|
| 8 |
import httpx
|
| 9 |
-
import uvicorn
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
-
from fastapi import FastAPI, HTTPException, Depends
|
| 12 |
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 13 |
from pydantic import BaseModel
|
| 14 |
from starlette.middleware.cors import CORSMiddleware
|
|
@@ -176,13 +175,18 @@ async def chat_completions(
|
|
| 176 |
response.raise_for_status()
|
| 177 |
async for line in response.aiter_lines():
|
| 178 |
if line and ("[DONE]" not in line):
|
| 179 |
-
# Assuming the response starts with some prefix, adjust accordingly
|
| 180 |
try:
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
except json.JSONDecodeError as e:
|
| 185 |
-
logger.error(f"JSON decode error: {e}")
|
|
|
|
|
|
|
| 186 |
# Indicate the end of the stream
|
| 187 |
yield f"data: {json.dumps(create_chat_completion_data('', request.model, 'stop'))}\n\n"
|
| 188 |
yield "data: [DONE]\n\n"
|
|
@@ -190,7 +194,7 @@ async def chat_completions(
|
|
| 190 |
logger.error(f"HTTP error occurred: {e}")
|
| 191 |
raise HTTPException(status_code=e.response.status_code, detail=str(e))
|
| 192 |
except httpx.RequestError as e:
|
| 193 |
-
logger.error(f"
|
| 194 |
raise HTTPException(status_code=500, detail=str(e))
|
| 195 |
|
| 196 |
if request.stream:
|
|
@@ -201,9 +205,14 @@ async def chat_completions(
|
|
| 201 |
full_response = ""
|
| 202 |
async for chunk in generate():
|
| 203 |
if chunk.startswith("data: ") and not chunk[6:].startswith("[DONE]"):
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
return {
|
| 209 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
|
@@ -219,7 +228,3 @@ async def chat_completions(
|
|
| 219 |
],
|
| 220 |
"usage": None,
|
| 221 |
}
|
| 222 |
-
|
| 223 |
-
# Remove the Uvicorn run block for production deployment
|
| 224 |
-
# if __name__ == "__main__":
|
| 225 |
-
# uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 6 |
from typing import Any, Dict, List, Optional
|
| 7 |
|
| 8 |
import httpx
|
|
|
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
+
from fastapi import FastAPI, HTTPException, Depends
|
| 11 |
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 12 |
from pydantic import BaseModel
|
| 13 |
from starlette.middleware.cors import CORSMiddleware
|
|
|
|
| 175 |
response.raise_for_status()
|
| 176 |
async for line in response.aiter_lines():
|
| 177 |
if line and ("[DONE]" not in line):
|
|
|
|
| 178 |
try:
|
| 179 |
+
# Adjust the slicing based on the actual response format
|
| 180 |
+
data = json.loads(line[5:]) if len(line) > 5 else None
|
| 181 |
+
if data and "data" in data and "text" in data["data"]:
|
| 182 |
+
content = data["data"].get("text", "")
|
| 183 |
+
yield f"data: {json.dumps(create_chat_completion_data(content, request.model))}\n\n"
|
| 184 |
+
else:
|
| 185 |
+
logger.warning(f"Unexpected data format: {line}")
|
| 186 |
except json.JSONDecodeError as e:
|
| 187 |
+
logger.error(f"JSON decode error for line: {line} - {e}")
|
| 188 |
+
except Exception as e:
|
| 189 |
+
logger.error(f"Error processing line: {line} - {e}")
|
| 190 |
# Indicate the end of the stream
|
| 191 |
yield f"data: {json.dumps(create_chat_completion_data('', request.model, 'stop'))}\n\n"
|
| 192 |
yield "data: [DONE]\n\n"
|
|
|
|
| 194 |
logger.error(f"HTTP error occurred: {e}")
|
| 195 |
raise HTTPException(status_code=e.response.status_code, detail=str(e))
|
| 196 |
except httpx.RequestError as e:
|
| 197 |
+
logger.error(f"An error occurred while requesting: {e}")
|
| 198 |
raise HTTPException(status_code=500, detail=str(e))
|
| 199 |
|
| 200 |
if request.stream:
|
|
|
|
| 205 |
full_response = ""
|
| 206 |
async for chunk in generate():
|
| 207 |
if chunk.startswith("data: ") and not chunk[6:].startswith("[DONE]"):
|
| 208 |
+
try:
|
| 209 |
+
data = json.loads(chunk[6:])
|
| 210 |
+
if data["choices"][0]["delta"].get("content"):
|
| 211 |
+
full_response += data["choices"][0]["delta"]["content"]
|
| 212 |
+
except json.JSONDecodeError as e:
|
| 213 |
+
logger.error(f"JSON decode error in non-streaming response: {chunk} - {e}")
|
| 214 |
+
except Exception as e:
|
| 215 |
+
logger.error(f"Error processing chunk in non-streaming response: {chunk} - {e}")
|
| 216 |
|
| 217 |
return {
|
| 218 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
|
|
|
| 228 |
],
|
| 229 |
"usage": None,
|
| 230 |
}
|
|
|
|
|
|
|
|
|
|
|
|