Skip to content

Commit

Permalink
Support streaming APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
yangwenz08 committed Jul 5, 2024
1 parent a1df35c commit ec03428
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/streaming/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

url = "http://localhost:8080/v1/models/streaming:generate"
with requests.post(url, stream=True, json={"repeat": 5}) as r:
for chunk in r.iter_content(16):
for chunk in r.iter_lines():
print(chunk)
2 changes: 1 addition & 1 deletion kservehelper/kserve/rest/v1_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async def generate(self, model_name: str, request: Request) -> StreamingResponse
headers = dict(request.headers.items())
results_generator, response_headers = \
await self.dataplane.generate(model_name=model_name, body=body, headers=headers)
return StreamingResponse(results_generator())
return StreamingResponse(results_generator(), media_type="application/x-ndjson")

async def explain(self, model_name: str, request: Request) -> Union[Response, Dict]:
"""Explain handler.
Expand Down
2 changes: 1 addition & 1 deletion kservehelper/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def generate_filepath(filename: str) -> str:
def wrap_generator(g):
def _g():
for i, data in enumerate(g()):
yield json.dumps({"id": i, "data": data})
yield json.dumps({"id": i, "data": data}) + "\n"

return _g

Expand Down

0 comments on commit ec03428

Please sign in to comment.