Skip to main content

ollama_sdk/
lib.rs

1//! A Rust SDK for interacting with the Ollama API.
2//!
3//! This crate provides a convenient and type-safe way to communicate with an Ollama server,
4//! allowing you to interact with various language models, perform chat completions,
5//! generate text, and manage models.
6//!
7//! ## Features
8//!
9//! - **Idiomatic Rust API:** Designed with Rust's best practices in mind.
10//! - **Streaming support**: Handle streaming responses for chat and generate operations efficiently.
11//! - **Configurable Transport:** Uses `reqwest` by default, with an extensible [`Transport`](crate::transport::Transport) trait for custom implementations.
12//! - **Robust Error Handling:** Comprehensive error types for predictable error management.
13//! - **Observability:** Optional `tracing` for detailed logging and `metrics` for performance monitoring.
14//! - **Tooling Integration**: Support for tool definitions and registry.
15//!
16//! ## Getting Started
17//!
18//! To use this SDK, add `ollama-sdk` to your `Cargo.toml`.
19//!
20//! Check `examples` directory in GitHub for example usages.
21
22use thiserror::Error;
23
24mod builder;
25mod client;
26pub mod parser;
27pub mod tools;
28pub mod transport;
29pub mod types;
30
31pub use crate::{builder::OllamaClientBuilder, client::OllamaClient};
32
33/// An alias for [`std::result::Result<T, E>`] where E is [`enum@Error`].
34pub type Result<T> = std::result::Result<T, Error>;
35
36/// Represents errors that can occur when interacting with the Ollama API.
37#[derive(Error, Debug)]
38pub enum Error {
39    /// A client-side error, typically due to invalid input or configuration.
40    #[error("Client error: {0}")]
41    Client(String),
42
43    /// An error originating from the underlying HTTP transport layer (e.g., network issues).
44    #[error("Transport error: {0}")]
45    Transport(#[from] reqwest::Error),
46
47    /// An error returned by the Ollama server.
48    #[error("Server error: {0}")]
49    Server(String),
50
51    /// An error during JSON serialization or deserialization.
52    #[error("JSON error: {0}")]
53    JsonParse(#[from] serde_json::Error),
54
55    /// An error related to the Ollama API protocol (e.g., unexpected response format).
56    #[error("Protocol error: {0}")]
57    Protocol(String),
58
59    /// An error related to tool execution or definition.
60    #[error("Tool error: {0}")]
61    Tool(String),
62
63    /// The streaming operation was cancelled.
64    #[error("Stream cancelled")]
65    Cancelled,
66}