Skip to content

Commit

Permalink
[Inference] fix openai sdk steaming response (#117)
Browse files Browse the repository at this point in the history
* fix openai sdk steaming response

* print content
  • Loading branch information
KepingYan authored Feb 26, 2024
1 parent 57ade22 commit 55a24b9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ python examples/inference/api_server_openai/query_http_requests.py

# using OpenAI SDK
# please install openai in current env by running: pip install openai>=1.0
export OPENAI_API_BASE=http://localhost:8000/v1
export OPENAI_BASE_URL=http://localhost:8000/v1
export OPENAI_API_KEY="not_a_real_key"
python examples/inference/api_server_openai/query_openai_sdk.py
```
Expand Down
2 changes: 1 addition & 1 deletion docs/serve.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ python examples/inference/api_server_openai/query_http_requests.py

# using OpenAI SDK
# please install openai in current env by running: pip install openai>=1.0
export OPENAI_API_BASE=http://localhost:8000/v1
export OPENAI_BASE_URL=http://localhost:8000/v1
export OPENAI_API_KEY="not_a_real_key"
python examples/inference/api_server_openai/query_openai_sdk.py
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
response = s.post(url, json=body, proxies=proxies, stream=args.streaming_response) # type: ignore
for chunk in response.iter_lines(decode_unicode=True):
try:
if chunk is not None:
if chunk is not None and chunk != "":
if args.streaming_response:
# Get data from reponse chunk
chunk_data = chunk.split("data: ")[1]
Expand Down
9 changes: 8 additions & 1 deletion examples/inference/api_server_openai/query_openai_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,11 @@
temperature=args.temperature,
top_p=args.top_p,
)
print(chat_completion)
if args.streaming_response:
for chunk in chat_completion:
content = chunk.choices[0].delta.content
if content is not None:
print(content, end="")
print("")
else:
print(chat_completion)
18 changes: 9 additions & 9 deletions inference/api_openai_backend/router_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async def _completions_wrapper(
logger.error(f"{subresult_dict['error']}")
all_results.pop()
had_error = True
yield "data: " + ModelResponse(**subresult_dict).json() + "\n"
yield "data: " + ModelResponse(**subresult_dict).json() + "\n\n"
# Return early in case of an error
break
choices = [
Expand All @@ -125,7 +125,7 @@ async def _completions_wrapper(
model=body.model,
choices=choices,
usage=usage,
).json() + "\n"
).json() + "\n\n"
if had_error:
# Return early in case of an error
break
Expand All @@ -141,8 +141,8 @@ async def _completions_wrapper(
model=body.model,
choices=choices,
usage=usage,
).json() + "\n"
yield "data: [DONE]\n"
).json() + "\n\n"
yield "data: [DONE]\n\n"


async def _chat_completions_wrapper(
Expand All @@ -167,7 +167,7 @@ async def _chat_completions_wrapper(
model=body.model,
choices=choices,
usage=None,
).json() + "\n"
).json() + "\n\n"

all_results = []
async for results in generator:
Expand All @@ -182,7 +182,7 @@ async def _chat_completions_wrapper(
subresult_dict["finish_reason"] = None
all_results.pop()
had_error = True
yield "data: " + ModelResponse(**subresult_dict).json() + "\n"
yield "data: " + ModelResponse(**subresult_dict).json() + "\n\n"
# Return early in case of an error
break
else:
Expand All @@ -200,7 +200,7 @@ async def _chat_completions_wrapper(
model=body.model,
choices=choices,
usage=None,
).json() + "\n"
).json() + "\n\n"
if had_error:
# Return early in case of an error
break
Expand All @@ -223,8 +223,8 @@ async def _chat_completions_wrapper(
model=body.model,
choices=choices,
usage=usage,
).json() + "\n"
yield "data: [DONE]\n"
).json() + "\n\n"
yield "data: [DONE]\n\n"


class Router:
Expand Down

0 comments on commit 55a24b9

Please sign in to comment.