1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// 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.
//! # Library Name Note
//!
//! This library is published as `ollama-api-rs` on crates.io.
//! Users should write `use oai_sdk::{ModelClient, ChatRequest, Message};`
//!
//! # Features
//!
//! - **Async/await support** - Built on top of Tokio for efficient async operations
//! - **Easy configuration** - Simple client setup with `ModelClient::builder()`
//! - **Streaming responses** - Real-time streaming for both chat and generation
//! - **Full Ollama API compatibility** - Complete coverage of all Ollama API endpoints
//! - **Modular design** - Separate modules for chat, generate, embed, and model operations
//! - **Comprehensive error handling** - Custom error types with detailed context
//! - **Tool calling** - Support for function/tool calling in chat completions
//! - **Structured outputs** - JSON schema validation support for responses
//! - **Model lifecycle management** - Load/unload models programmatically
//! - **Blob management** - Push and check model blobs
//! - **Batch embeddings** - Efficient batch processing for embeddings
//!
//! # Examples
//!
//! ## Basic Chat Completion
//!
//! ```no_run
//! use oai_sdk::{ModelClient, ChatRequest, Message};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let client = ModelClient::builder()
//! .base_url("http://localhost:11434".to_string())
//! .build()?;
//!
//! let request = ChatRequest {
//! model: "llama3".to_string(),
//! messages: vec![
//! Message {
//! role: "user".to_string(),
//! content: "Why is the sky blue?".to_string(),
//! images: None,
//! tool_calls: None,
//! tool_name: None,
//! thinking: None,
//! }
//! ],
//! stream: false,
//! format: None,
//! options: None,
//! keep_alive: None,
//! tools: None,
//! think: None,
//! };
//!
//! let response = client.chat(request).await?;
//! println!("{}", response.message.content);
//!
//! Ok(())
//! }
//! ```
//!
//! ## Streaming Chat
//!
//! ```no_run
//! use oai_sdk::{ModelClient, ChatRequest, Message};
//! use tokio_stream::StreamExt;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let client = ModelClient::builder()
//! .base_url("http://localhost:11434".to_string())
//! .build()?;
//!
//! let request = ChatRequest {
//! model: "llama3".to_string(),
//! messages: vec![
//! Message {
//! role: "user".to_string(),
//! content: "Write a story about Rust".to_string(),
//! images: None,
//! tool_calls: None,
//! tool_name: None,
//! thinking: None,
//! }
//! ],
//! stream: true,
//! format: None,
//! options: None,
//! keep_alive: None,
//! tools: None,
//! think: None,
//! };
//!
//! let mut stream = client.chat_stream(request).await?;
//! while let Some(result) = stream.next().await {
//! match result {
//! Ok(response) => print!("{}", response.message.content),
//! Err(e) => eprintln!("Error: {}", e),
//! }
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! ## Text Generation
//!
//! ```no_run
//! use oai_sdk::{ModelClient, GenerateRequest};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let client = ModelClient::builder()
//! .base_url("http://localhost:11434".to_string())
//! .build()?;
//!
//! let request = GenerateRequest {
//! model: "llama3".to_string(),
//! prompt: "Why is the sky blue?".to_string(),
//! stream: false,
//! ..Default::default()
//! };
//!
//! let response = client.generate(request).await?;
//! println!("{}", response.response);
//!
//! Ok(())
//! }
//! ```
//!
//! ## Embeddings
//!
//! ```no_run
//! use oai_sdk::{ModelClient, EmbedRequest, EmbedInput};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let client = ModelClient::builder()
//! .base_url("http://localhost:11434".to_string())
//! .build()?;
//!
//! let request = EmbedRequest {
//! model: "llama3:8b".to_string(),
//! input: EmbedInput::Single("Hello, world!".to_string()),
//! truncate: Some(true),
//! options: None,
//! keep_alive: None,
//! dimensions: None,
//! };
//!
//! let response = client.embed(request).await?;
//! println!("Embeddings: {:?}", response.embeddings);
//!
//! Ok(())
//! }
//! ```
//!
//! ## Tool Calling
//!
//! ```no_run
//! use oai_sdk::{ModelClient, ChatRequest, Message, Tool, ToolFunction};
//! use serde_json::json;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let client = ModelClient::builder()
//! .base_url("http://localhost:11434".to_string())
//! .build()?;
//!
//! let tools = vec![
//! Tool {
//! tool_type: "function".to_string(),
//! function: ToolFunction {
//! name: "get_current_weather".to_string(),
//! description: "Get the current weather for a location".to_string(),
//! parameters: json!({
//! "type": "object",
//! "properties": {
//! "location": {
//! "type": "string",
//! "description": "The location to get the weather for"
//! },
//! "format": {
//! "type": "string",
//! "enum": ["celsius", "fahrenheit"]
//! }
//! },
//! "required": ["location", "format"]
//! }),
//! }
//! }
//! ];
//!
//! let request = ChatRequest {
//! model: "llama3".to_string(),
//! messages: vec![
//! Message {
//! role: "user".to_string(),
//! content: "What is the weather in Tokyo?".to_string(),
//! images: None,
//! tool_calls: None,
//! tool_name: None,
//! thinking: None,
//! }
//! ],
//! stream: false,
//! format: None,
//! options: None,
//! keep_alive: None,
//! tools: Some(tools),
//! think: None,
//! };
//!
//! let response = client.chat(request).await?;
//! if let Some(tool_calls) = response.message.tool_calls {
//! for tool_call in tool_calls {
//! println!("Tool call: {}", tool_call.function.name);
//! }
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! ## Model Management
//!
//! ```no_run
//! use oai_sdk::{ModelClient, ShowModelRequest, CopyModelRequest, DeleteModelRequest};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let client = ModelClient::builder()
//! .base_url("http://localhost:11434".to_string())
//! .build()?;
//!
//! // List models
//! let models = client.list_models().await?;
//! for model in models {
//! println!("Model: {}", model.name);
//! }
//!
//! // Show model information
//! let request = ShowModelRequest {
//! model: "llama3".to_string(),
//! verbose: Some(true),
//! };
//! let info = client.show_model(request).await?;
//! println!("Model info: {:?}", info);
//!
//! // Copy model
//! let copy_req = CopyModelRequest {
//! source: "llama3".to_string(),
//! destination: "llama3-backup".to_string(),
//! };
//! client.copy_model(copy_req).await?;
//!
//! // Delete model
//! let delete_req = DeleteModelRequest {
//! model: "llama3-backup".to_string(),
//! };
//! client.delete_model(delete_req).await?;
//!
//! Ok(())
//! }
//! ```
//!
//! ## OpenAI-Compatible Endpoints
//!
//! Use OpenAI client libraries with Ollama by specifying the base URL:
//!
//! ```no_run
//! use oai_sdk::{ModelClient, ChatCompletionsRequest, ChatMessage};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let client = ModelClient::builder()
//! .base_url("http://localhost:11434".to_string())
//! .build()?;
//!
//! let request = ChatCompletionsRequest {
//! model: "llama3".to_string(),
//! messages: vec![
//! ChatMessage {
//! role: "user".to_string(),
//! content: serde_json::json!("Why is the sky blue?"),
//! }
//! ],
//! stream: Some(false),
//! ..Default::default()
//! };
//!
//! let response = client.chat_completions(request).await?;
//! println!("{}", response.choices[0].message.content);
//!
//! Ok(())
//! }
//! ```
//!
//! ## Model Lifecycle (requires `local` feature)
//!
//! ```no_run
//! # #[cfg(feature = "local")]
//! # {
//! use oai_sdk::ModelClient;
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let client = ModelClient::builder()
//! .base_url("http://localhost:11434".to_string())
//! .build()?;
//!
//! // Load model into memory
//! client.load_model("llama3").await?;
//! println!("Model loaded");
//!
//! // Unload model from memory
//! client.unload_model("llama3").await?;
//! println!("Model unloaded");
//!
//! Ok(())
//! # }
//! # }
//! ```
//!
//! ## API Modules
//!
//! - [`chat`](crate::chat) - Chat completion with streaming and tool support
//! - [`generate`](crate::generate) - Text generation with streaming support
//! - [`embed`](crate::embed) - Single and batch embeddings
//! - [`model`](crate::model) - Model management (CRUD, pull, push, running models)
//! - [`openai`](crate::openai) - OpenAI-compatible endpoints (chat, embeddings, responses)
//! - [`client`](crate::client) - Core client, blob management, model lifecycle
//! - [`error`](crate::error) - Error types and handling
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;