OpenAI API: stop streaming on client disconnect (closes #4521)
This commit is contained in:
parent
effb3aef42
commit
d86f1fd2c3
1 changed files with 8 additions and 0 deletions
|
@ -91,6 +91,10 @@ async def openai_completions(request: Request, request_data: CompletionRequest):
|
||||||
async with streaming_semaphore:
|
async with streaming_semaphore:
|
||||||
response = OAIcompletions.stream_completions(to_dict(request_data), is_legacy=is_legacy)
|
response = OAIcompletions.stream_completions(to_dict(request_data), is_legacy=is_legacy)
|
||||||
for resp in response:
|
for resp in response:
|
||||||
|
disconnected = await request.is_disconnected()
|
||||||
|
if disconnected:
|
||||||
|
break
|
||||||
|
|
||||||
yield {"data": json.dumps(resp)}
|
yield {"data": json.dumps(resp)}
|
||||||
|
|
||||||
return EventSourceResponse(generator()) # SSE streaming
|
return EventSourceResponse(generator()) # SSE streaming
|
||||||
|
@ -110,6 +114,10 @@ async def openai_chat_completions(request: Request, request_data: ChatCompletion
|
||||||
async with streaming_semaphore:
|
async with streaming_semaphore:
|
||||||
response = OAIcompletions.stream_chat_completions(to_dict(request_data), is_legacy=is_legacy)
|
response = OAIcompletions.stream_chat_completions(to_dict(request_data), is_legacy=is_legacy)
|
||||||
for resp in response:
|
for resp in response:
|
||||||
|
disconnected = await request.is_disconnected()
|
||||||
|
if disconnected:
|
||||||
|
break
|
||||||
|
|
||||||
yield {"data": json.dumps(resp)}
|
yield {"data": json.dumps(resp)}
|
||||||
|
|
||||||
return EventSourceResponse(generator()) # SSE streaming
|
return EventSourceResponse(generator()) # SSE streaming
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue