Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ from fastapi.responses import StreamingResponse
|
|
3 |
from pydantic import BaseModel
|
4 |
import requests
|
5 |
import json
|
6 |
-
import
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
@@ -35,40 +35,39 @@ async def chat(request: ChatRequest):
|
|
35 |
"max_tokens": request.max_tokens
|
36 |
}
|
37 |
|
38 |
-
#
|
39 |
-
|
40 |
-
|
41 |
-
with requests.post(url, headers=headers, data=json.dumps(payload), stream=True) as response:
|
42 |
-
# Check if the request was successful
|
43 |
-
if response.status_code == 200:
|
44 |
-
# Stream the response
|
45 |
-
for line in response.iter_lines():
|
46 |
-
if line:
|
47 |
-
# Decode the line
|
48 |
-
decoded_line = line.decode('utf-8')
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
content = parsed_data['choices'][0]['delta'].get('content', '')
|
61 |
-
if content:
|
62 |
-
yield content
|
63 |
-
except json.JSONDecodeError:
|
64 |
-
continue
|
65 |
-
else:
|
66 |
-
raise HTTPException(status_code=response.status_code, detail=response.text)
|
67 |
-
except Exception as e:
|
68 |
-
raise HTTPException(status_code=500, detail=str(e))
|
69 |
-
|
70 |
-
return StreamingResponse(event_stream(), media_type="text/event-stream")
|
71 |
|
72 |
if __name__ == "__main__":
|
73 |
import uvicorn
|
74 |
-
uvicorn.run(app, host="0.0.0.0", port=8083)
|
|
|
3 |
from pydantic import BaseModel
|
4 |
import requests
|
5 |
import json
|
6 |
+
from typing import AsyncIterator
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
|
|
35 |
"max_tokens": request.max_tokens
|
36 |
}
|
37 |
|
38 |
+
# Make the POST request with streaming
|
39 |
+
try:
|
40 |
+
response = requests.post(url, headers=headers, data=json.dumps(payload), stream=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
# Check if the request was successful
|
43 |
+
if response.status_code == 200:
|
44 |
+
async def event_stream() -> AsyncIterator[str]:
|
45 |
+
# Stream the response line by line
|
46 |
+
for line in response.iter_lines():
|
47 |
+
if line:
|
48 |
+
# Decode the line
|
49 |
+
decoded_line = line.decode('utf-8')
|
50 |
+
# Check if the line starts with "data: "
|
51 |
+
if decoded_line.startswith("data: "):
|
52 |
+
# Extract the JSON part
|
53 |
+
json_data = decoded_line[6:] # Remove "data: " prefix
|
54 |
+
# Parse the JSON
|
55 |
+
try:
|
56 |
+
parsed_data = json.loads(json_data)
|
57 |
+
# Check if 'choices' and 'delta' exist and yield the content
|
58 |
+
if 'choices' in parsed_data and len(parsed_data['choices']) > 0:
|
59 |
+
content = parsed_data['choices'][0]['delta'].get('content', '')
|
60 |
+
if content:
|
61 |
+
yield content
|
62 |
+
except json.JSONDecodeError:
|
63 |
+
continue
|
64 |
|
65 |
+
return StreamingResponse(event_stream(), media_type="text/event-stream")
|
66 |
+
else:
|
67 |
+
raise HTTPException(status_code=response.status_code, detail=response.text)
|
68 |
+
except Exception as e:
|
69 |
+
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
if __name__ == "__main__":
|
72 |
import uvicorn
|
73 |
+
uvicorn.run(app, host="0.0.0.0", port=8083)
|