Skip to main content

oai_sdk/
web.rs

1// Copyright 2026 Cloudflavor GmbH
2
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6
7// http://www.apache.org/licenses/LICENSE-2.0
8
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::client::ModelClient;
16use crate::error::{OllamaError, Result};
17use serde::{Deserialize, Serialize};
18
19/// Request for web search.
20#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct WebSearchRequest {
22    pub query: String,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub max_results: Option<u8>,
25}
26
27/// Request for web fetch.
28#[derive(Debug, Clone, Serialize, Deserialize)]
29pub struct WebFetchRequest {
30    pub url: String,
31}
32
33/// Response for web search.
34#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct WebSearchResponse {
36    pub results: Vec<WebSearchResult>,
37}
38
39/// A single result from a web search.
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct WebSearchResult {
42    pub title: String,
43    pub url: String,
44    pub content: String,
45}
46
47/// Response for web fetch.
48#[derive(Debug, Clone, Serialize, Deserialize)]
49pub struct WebFetchResponse {
50    pub title: String,
51    pub content: String,
52    pub links: Vec<String>,
53}
54
55impl ModelClient {
56    /// Perform a web search via the Ollama cloud API.
57    ///
58    /// Requires an authenticated client (API key set via `auth_token`).
59    pub async fn web_search(&self, request: WebSearchRequest) -> Result<WebSearchResponse> {
60        let url = self
61            .cloud_url
62            .join("api/web_search")
63            .map_err(OllamaError::UrlError)?;
64        let response = self
65            .client
66            .post(url)
67            .json(&request)
68            .send()
69            .await
70            .map_err(OllamaError::RequestError)?;
71
72        self.handle_response(response, None).await
73    }
74
75    /// Fetch content from a web URL via the Ollama cloud API.
76    ///
77    /// Requires an authenticated client (API key set via `auth_token`).
78    pub async fn web_fetch(&self, request: WebFetchRequest) -> Result<WebFetchResponse> {
79        let url = self
80            .cloud_url
81            .join("api/web_fetch")
82            .map_err(OllamaError::UrlError)?;
83        let response = self
84            .client
85            .post(url)
86            .json(&request)
87            .send()
88            .await
89            .map_err(OllamaError::RequestError)?;
90
91        self.handle_response(response, None).await
92    }
93}