ollama-api-rs 0.4.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.

//! Thinking mode example
//!
//! This example demonstrates how to enable reasoning traces on models
//! that support thinking (e.g., qwen3, deepseek-r1).

use oai_sdk::{ChatRequest, Message, ModelClient};

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

    println!("Thinking Mode Example");
    println!("Enable reasoning traces and view the model's thought process.\n");

    // Example with a boolean think value
    let request = ChatRequest {
        model: "qwen3".to_string(),
        messages: vec![Message::user(
            "How many letter r are in strawberry?"
        )],
        think: Some(true.into()),
        ..Default::default()
    };

    println!("Sending request with think=true...");
    match client.chat(request).await {
        Ok(response) => {
            if let Some(thinking) = &response.message.thinking {
                println!("\nThinking:");
                println!("{}", thinking);
                println!("\nAnswer:");
            }
            println!("{}", response.message.content);
        }
        Err(e) => println!("Error: {}", e),
    }

    // Example with a string think level (GPT-OSS models)
    println!("\n\n--- String Level Example (GPT-OSS) ---");
    let request = ChatRequest {
        model: "gpt-oss".to_string(),
        messages: vec![Message::user("Tell me about Canada.")],
        think: Some("medium".into()),
        ..Default::default()
    };

    println!("Sending request with think='medium'...");
    match client.chat(request).await {
        Ok(response) => {
            if let Some(thinking) = &response.message.thinking {
                println!("\nThinking:");
                println!("{}", thinking);
                println!("\nAnswer:");
            }
            println!("{}", response.message.content);
        }
        Err(e) => println!("Error: {}", e),
    }

    Ok(())
}