ollama-api-rs 0.4.0

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.

use crate::client::ModelClient;
use crate::error::{OllamaError, Result};
use serde::{Deserialize, Serialize};

/// Request for web search.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebSearchRequest {
    pub query: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_results: Option<u8>,
}

/// Request for web fetch.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebFetchRequest {
    pub url: String,
}

/// Response for web search.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebSearchResponse {
    pub results: Vec<WebSearchResult>,
}

/// A single result from a web search.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebSearchResult {
    pub title: String,
    pub url: String,
    pub content: String,
}

/// Response for web fetch.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebFetchResponse {
    pub title: String,
    pub content: String,
    pub links: Vec<String>,
}

impl ModelClient {
    /// Perform a web search via the Ollama cloud API.
    ///
    /// Requires an authenticated client (API key set via `auth_token`).
    pub async fn web_search(&self, request: WebSearchRequest) -> Result<WebSearchResponse> {
        let url = self
            .cloud_url
            .join("api/web_search")
            .map_err(OllamaError::UrlError)?;
        let response = self
            .client
            .post(url)
            .json(&request)
            .send()
            .await
            .map_err(OllamaError::RequestError)?;

        self.handle_response(response, None).await
    }

    /// Fetch content from a web URL via the Ollama cloud API.
    ///
    /// Requires an authenticated client (API key set via `auth_token`).
    pub async fn web_fetch(&self, request: WebFetchRequest) -> Result<WebFetchResponse> {
        let url = self
            .cloud_url
            .join("api/web_fetch")
            .map_err(OllamaError::UrlError)?;
        let response = self
            .client
            .post(url)
            .json(&request)
            .send()
            .await
            .map_err(OllamaError::RequestError)?;

        self.handle_response(response, None).await
    }
}