Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Callback API vs. Stream API

Charles Dong edited this page Jul 21, 2024 · 6 revisions

What are they?

Callback and Stream API are just two different ways for you to interact with the Ollama backend.

Callback API is relatively conventional, while Stream API leverages streams. In fact, Stream API was written later during the initial development.

They have completely different "styles" to get called.

For example,

Callback API:

// -- snip -- //
let final_res = ollama.generate(
    &request,
    Some(|res: &GenerationResponse| {
        if !res.done {
            print!("{}", res.response);
            // Flush stdout for each word to allow realtime output
            std::io::stdout().flush().unwrap();
        }
    })
).await.unwrap();

Stream API:

// -- snip -- //
let mut stream = ollama.generate_streamed(&request).await.unwrap();

while let Some(Ok(res)) = stream.next().await {
    if !res.done {
        print!("{}", res.response);
        // Flush stdout for each word to allow realtime output
        std::io::stdout().flush().unwrap();
    }
}

What should I use?

I recommend Stream API for most cases. Handling streams is neater than handling a function or a closure (which is even inside an option enum).

However, Stream API will refuse to handle non-streamed calls. So, if you have stream field set to false in your request, you should use Callback API with on_stream set to None.

Clone this wiki locally