Skip to content

Add stream and async_stream methods to client #204

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

Merged
merged 5 commits into from
Nov 27, 2023
Merged

Add stream and async_stream methods to client #204

merged 5 commits into from
Nov 27, 2023

Conversation

mattt
Copy link
Contributor

@mattt mattt commented Nov 22, 2023

Replicate models like meta/llama-2-70b-chat support streaming output via Server-sent Events (SSE).

This PR adds top-level stream and async_stream methods that let you iterate over tokens as they come in.

import replicate

tokens = []
for event in replicate.stream(
    "meta/llama-2-70b-chat:02e509c789964a7ea8736978a43525956ef40397be9033abf9fd2badfe68c9e3",
    input={
        "prompt": "Please write a haiku about llamas.",
    },
):
    print(event)
    tokens.append(str(event))

print("".join(tokens))

@mattt mattt requested a review from a team November 22, 2023 13:51
@mattt mattt merged commit 252c3be into main Nov 27, 2023
@mattt mattt deleted the mattt/stream branch November 27, 2023 12:11
@mattt mattt mentioned this pull request Dec 4, 2023
mattt added a commit that referenced this pull request Dec 4, 2023
Resolves #213 

This PR fixes a few bugs in the streaming implementation added with
#204.

---------

Signed-off-by: Mattt Zmuda <[email protected]>
mattt added a commit that referenced this pull request Dec 8, 2023
Follow-up to #204

This PR adds a `stream` method on `Prediction`. In most cases, you'll
want to call `replicate.stream` to get the stream directly. But if you
have an existing prediction object and want to stream its output, you
can use this method instead. [^1]

[^1]: This rhymes with `run` being a shorthand for creating a prediction
and calling `wait`.

Signed-off-by: Mattt Zmuda <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant