Skip to content

Commit

Permalink
OpenAI API: stop streaming on client disconnect (closes #4521)
Browse files Browse the repository at this point in the history
  • Loading branch information
oobabooga committed Nov 9, 2023
1 parent effb3ae commit d86f1fd
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions extensions/openai/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ async def generator():
async with streaming_semaphore:
response = OAIcompletions.stream_completions(to_dict(request_data), is_legacy=is_legacy)
for resp in response:
disconnected = await request.is_disconnected()
if disconnected:
break

yield {"data": json.dumps(resp)}

return EventSourceResponse(generator()) # SSE streaming
Expand All @@ -110,6 +114,10 @@ async def generator():
async with streaming_semaphore:
response = OAIcompletions.stream_chat_completions(to_dict(request_data), is_legacy=is_legacy)
for resp in response:
disconnected = await request.is_disconnected()
if disconnected:
break

yield {"data": json.dumps(resp)}

return EventSourceResponse(generator()) # SSE streaming
Expand Down

1 comment on commit d86f1fd

@bodybreaker
Copy link

@bodybreaker bodybreaker commented on d86f1fd Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it never called when I disconnect client-server

disconnected is always False

Please sign in to comment.