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

document streaming #49

Merged
merged 19 commits into from
Apr 27, 2024
Merged
Changes from 14 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
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,56 @@ LIT_SERVER_API_KEY=supersecretkey python main.py
Clients are expected to auth with the same API key set in the `X-API-Key` HTTP header.

</details>


<details>
<summary>Stream long responses</summary>

&nbsp;

`LitServer` can stream longer responses, such as LLM generated text.
aniketmaurya marked this conversation as resolved.
Show resolved Hide resolved

To enable streaming, you need to implement `LitAPI.predict` and `LitAPI.encode_response` as a generator (a Python
function that yields output) and set `LitServer(..., stream=True)`.

For example, streaming long responses generated over time:

```python
from typing import Generator

import json
from litserve import Request
from litserve import LitServer
from litserve import LitAPI
aniketmaurya marked this conversation as resolved.
Show resolved Hide resolved


class SimpleStreamAPI(LitAPI):
def setup(self, device) -> None:
aniketmaurya marked this conversation as resolved.
Show resolved Hide resolved
self.model = lambda x, y: x*y

def decode_request(self, request: Request) -> float:
return request["input"]

def predict(self, x) -> Generator:
for i in range(10):
yield self.model(x, i)

def encode_response(self, output: Generator) -> Generator:
for out in output:
yield json.dumps({"output": out})


if __name__ == "__main__":
api = SimpleStreamAPI()
server = LitServer(api, stream=True)
server.run(port=8000)
```

&nbsp;

</details>


# Contribute
LitServe is a community project accepting contributions. Let's make the world's most advanced AI inference engine.

Expand All @@ -366,4 +414,3 @@ pytest tests

litserve is released under the [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0) license.
See LICENSE file for details.
Then, run pytest in your terminal as follows:
Loading