ollama-api-rs 0.3.1

An async Rust SDK for the Ollama API with OpenAI compatibility
Documentation
// Copyright 2026 Cloudflavor GmbH

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Streaming chat example
//!
//! This example demonstrates how to use the ollama-api-rs with streaming
//! responses for a more interactive chat experience.

use oai_sdk::{ChatRequest, Message, ModelClient};
use std::io::Write;
use tokio_stream::StreamExt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create the client
    let client = ModelClient::builder()
        .base_url("http://localhost:11434")
        .build()?;

    println!("Ollama Client Streaming Chat Example");
    println!("This example shows streaming responses from the model.\n");

    // Have a conversation with streaming
    let messages = vec![Message::user("Explain quantum computing in simple terms")];

    let request = ChatRequest {
        model: "llama3.1:8b".to_string(),
        messages,
        stream: true, // Enable streaming
        format: None,
        options: None,
        keep_alive: None,
        tools: None,
        think: None,
    };

    println!("User: Explain quantum computing in simple terms");
    print!("Assistant: ");

    // Stream the response
    let mut stream = client.chat_stream(request).await?;

    while let Some(result) = stream.next().await {
        match result {
            Ok(response) => {
                print!("{}", response.message.content);
                std::io::stdout().flush()?;

                if response.done {
                    break;
                }
            }
            Err(e) => {
                println!("\nError: {}", e);
                break;
            }
        }
    }

    println!("\n");

    Ok(())
}