Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add "[DONE]" to the end of stream generation for better openai SDK compatibility #1062

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions xinference/api/restful_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,7 @@ async def stream_results():
self.handle_request_limit_error(re)
async for item in iterator:
yield item
yield "[DONE]"
except Exception as ex:
logger.exception("Chat completion stream got an error: %s", ex)
await self._report_error_event(model_uid, str(ex))
Expand Down
2 changes: 2 additions & 0 deletions xinference/client/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def streaming_response_iterator(
line = line.strip()
if line.startswith(b"data:"):
json_str = line[len(b"data:") :].strip()
if json_str == b"[DONE]":
continue
data = json.loads(json_str.decode("utf-8"))
error = data.get("error")
if error is not None:
Expand Down
4 changes: 3 additions & 1 deletion xinference/client/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ def _check_stream():
generate_config={"stream": True, "max_tokens": 5},
)
for chunk in streaming_response:
assert "content" or "role" in chunk["choices"][0]["delta"]
assert ("content" in chunk["choices"][0]["delta"]) or (
"role" in chunk["choices"][0]["delta"]
)

_check_stream()

Expand Down
Loading