ParthSadaria commited on
Commit
114ca84
·
verified ·
1 Parent(s): c3d6a12

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +4 -4
main.py CHANGED
@@ -76,15 +76,15 @@ async def get_completion(payload: Payload, request: Request):
76
  # Prepare the payload for streaming
77
  payload_dict = {**payload.dict(), "stream": True}
78
 
79
- # Define an asynchronous generator to stream the response
80
  async def stream_generator():
81
  async with httpx.AsyncClient() as client:
82
  try:
83
  async with client.stream("POST", secret_api_endpoint, json=payload_dict, timeout=10) as response:
84
  response.raise_for_status()
85
- async for chunk in response.aiter_bytes(chunk_size=512): # Smaller chunks for faster response
86
- if chunk:
87
- yield chunk
88
  except httpx.RequestError as e:
89
  raise HTTPException(status_code=500, detail=f"Streaming failed: {e}")
90
 
 
76
  # Prepare the payload for streaming
77
  payload_dict = {**payload.dict(), "stream": True}
78
 
79
+ # Define an asynchronous generator to stream the response line by line
80
  async def stream_generator():
81
  async with httpx.AsyncClient() as client:
82
  try:
83
  async with client.stream("POST", secret_api_endpoint, json=payload_dict, timeout=10) as response:
84
  response.raise_for_status()
85
+ async for line in response.aiter_lines():
86
+ if line:
87
+ yield f"{line}\n" # Add a newline to distinguish each line
88
  except httpx.RequestError as e:
89
  raise HTTPException(status_code=500, detail=f"Streaming failed: {e}")
90