llm-connector 0.3.3

Minimal Rust library for LLM protocol abstraction. Supports 4 protocols (OpenAI, Anthropic, Aliyun, Ollama) with unified interface, and dynamic model discovery.
Documentation
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//! Ollama Protocol Implementation
//!
//! This module implements the Ollama API protocol for local LLM servers.
//!
//! # Supported Providers
//!
//! - **Ollama (Local)** - `ollama()` - Llama3.2, Llama3.1, Mistral, Qwen, etc.
//!
//! # Protocol Differences
//!
//! Ollama uses a custom protocol that differs from OpenAI and Anthropic:
//!
//! ## 1. Endpoint
//! - Ollama: `POST /api/chat`
//! - OpenAI: `POST /v1/chat/completions`
//!
//! ## 2. Request Structure
//! - **No API key**: Local authentication only
//! - **Model field": Required, model must be pulled first
//! - **Options**: Separate options object for generation parameters
//!
//! ## 3. Response Structure
//! - **Done flag**: Indicates if generation is complete
//! - **Timing info**: Includes duration and evaluation metrics
//! - **Usage**: Different token counting approach
//!
//! # Request Format
//!
//! ```json
//! {
//!   "model": "llama3.2",
//!   "messages": [
//!     {"role": "user", "content": "Hello"}
//!   ],
//!   "stream": false,
//!   "options": {
//!     "temperature": 0.7,
//!     "num_predict": 1000
//!   }
//! }
//! ```
//!
//! # Response Format
//!
//! ```json
//! {
//!   "model": "llama3.2",
//!   "created_at": "2024-01-01T00:00:00Z",
//!   "message": {
//!     "role": "assistant",
//!     "content": "Hello! How can I help you?"
//!   },
//!   "done": true,
//!   "eval_count": 20,
//!   "eval_duration": 1000000
//! }
//! ```

use crate::error::LlmConnectorError;
use crate::protocols::core::{ErrorMapper, ProviderAdapter};
use crate::types::{ChatRequest, ChatResponse, Choice, Message, Role, Usage};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::sync::Arc;

/// Parse a role string into a Role enum
fn parse_role(role: &str) -> Role {
    match role {
        "system" => Role::System,
        "user" => Role::User,
        "assistant" => Role::Assistant,
        "tool" => Role::Tool,
        _ => Role::User, // Default to user for unknown roles
    }
}

// ============================================================================
// Ollama-specific Request/Response Structures
// ============================================================================

/// Ollama chat request
#[derive(Serialize, Debug)]
pub struct OllamaRequest {
    model: String,
    messages: Vec<OllamaMessage>,
    #[serde(skip_serializing_if = "Option::is_none")]
    stream: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    options: Option<OllamaOptions>,
}

#[derive(Serialize, Debug)]
pub struct OllamaMessage {
    role: String,
    content: String,
}

#[derive(Serialize, Debug)]
pub struct OllamaOptions {
    #[serde(skip_serializing_if = "Option::is_none")]
    temperature: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    top_p: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    num_predict: Option<u32>,
}

/// Ollama chat response
#[derive(Deserialize, Debug)]
pub struct OllamaResponse {
    model: String,
    #[serde(default)]
    _created_at: String,
    message: OllamaResponseMessage,
    done: bool,
    #[serde(default)]
    eval_count: Option<u32>,
}

/// Ollama model information
#[derive(Deserialize, Debug, Clone)]
pub struct OllamaModel {
    pub name: String,
    pub model: String,
    pub modified_at: String,
    pub size: Option<u64>,
    pub digest: Option<String>,
    pub details: Option<OllamaModelDetails>,
    pub expires_at: Option<String>,
}

/// Ollama model details
#[derive(Deserialize, Debug, Clone)]
pub struct OllamaModelDetails {
    pub format: Option<String>,
    pub family: Option<String>,
    pub families: Option<Vec<String>>,
    pub parameter_size: Option<String>,
    pub quantization_level: Option<String>,
}

/// Ollama models list response
#[derive(Deserialize, Debug)]
pub struct OllamaModelsResponse {
    pub models: Vec<OllamaModel>,
}

/// Ollama model pull/push request
#[derive(Serialize, Debug)]
pub struct OllamaModelRequest {
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub insecure: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream: Option<bool>,
}

/// Ollama model delete request
#[derive(Serialize, Debug)]
pub struct OllamaModelDeleteRequest {
    pub name: String,
}

/// Ollama model operation response (for pull/push progress)
#[derive(Deserialize, Debug)]
pub struct OllamaModelProgressResponse {
    pub status: String,
    #[serde(default)]
    pub digest: Option<String>,
    #[serde(default)]
    pub total: Option<u64>,
    #[serde(default)]
    pub completed: Option<u64>,
}

#[derive(Deserialize, Debug)]
pub struct OllamaResponseMessage {
    role: String,
    content: String,
}

// ============================================================================
// Ollama Error Mapper
// ============================================================================

pub struct OllamaErrorMapper;

impl ErrorMapper for OllamaErrorMapper {
    fn map_http_error(status: u16, body: Value) -> LlmConnectorError {
        let error_message = body["error"]
            .as_str()
            .or_else(|| body["message"].as_str())
            .unwrap_or("Unknown Ollama error");

        match status {
            400 => LlmConnectorError::InvalidRequest(format!(
                "Ollama: {}",
                error_message
            )),
            404 => LlmConnectorError::InvalidRequest(format!(
                "Ollama: Model not found. Make sure to pull the model first with 'ollama pull <model>'"
            )),
            500 => LlmConnectorError::ServerError(format!(
                "Ollama: Server error. Is Ollama running on localhost:11434?"
            )),
            429 => LlmConnectorError::RateLimitError(format!(
                "Ollama: {}",
                error_message
            )),
            _ => LlmConnectorError::ProviderError(format!(
                "Ollama HTTP {}: {}",
                status, error_message
            )),
        }
    }

    fn map_network_error(error: reqwest::Error) -> LlmConnectorError {
        if error.is_timeout() {
            LlmConnectorError::TimeoutError(format!("Ollama: {}", error))
        } else if error.is_connect() {
            LlmConnectorError::ConnectionError(format!(
                "Ollama: Cannot connect to Ollama server. Is it running on localhost:11434?"
            ))
        } else {
            LlmConnectorError::NetworkError(format!("Ollama: {}", error))
        }
    }

    fn is_retriable_error(error: &LlmConnectorError) -> bool {
        matches!(
            error,
            LlmConnectorError::RateLimitError(_)
                | LlmConnectorError::ServerError(_)
                | LlmConnectorError::TimeoutError(_)
                | LlmConnectorError::ConnectionError(_)
        )
    }
}

// ============================================================================
// Ollama Protocol Adapter
// ============================================================================

/// Ollama Protocol implementation for local LLM servers
#[derive(Debug, Clone)]
pub struct OllamaProtocol {
    base_url: Arc<str>,
}

impl OllamaProtocol {
    /// Create new Ollama protocol with default localhost:11434
    pub fn new() -> Self {
        Self {
            base_url: Arc::from("http://localhost:11434"),
        }
    }

    /// Create new Ollama protocol with custom URL
    pub fn with_url(base_url: &str) -> Self {
        Self {
            base_url: Arc::from(base_url),
        }
    }

    /// List all available models
    pub async fn list_models(&self, client: &reqwest::Client) -> Result<Vec<String>, LlmConnectorError> {
        let base = &self.base_url;
        let url = format!("{}/api/tags", base);

        let response = client
            .get(&url)
            .send()
            .await
            .map_err(|e| OllamaErrorMapper::map_network_error(e))?;

        if response.status().is_success() {
            let models_response: OllamaModelsResponse = response.json().await
                .map_err(|e| LlmConnectorError::ParseError(e.to_string()))?;

            Ok(models_response.models.into_iter().map(|m| m.name).collect())
        } else {
            let status = response.status().as_u16();
            let body = response.json().await.unwrap_or_default();
            Err(OllamaErrorMapper::map_http_error(status, body))
        }
    }

    /// Pull a model from Ollama registry
    pub async fn pull_model(&self, client: &reqwest::Client, model_name: &str) -> Result<(), LlmConnectorError> {
        let base = &self.base_url;
        let url = format!("{}/api/pull", base);

        let request = OllamaModelRequest {
            name: model_name.to_string(),
            insecure: None,
            stream: Some(false),
        };

        let response = client
            .post(&url)
            .json(&request)
            .send()
            .await
            .map_err(|e| OllamaErrorMapper::map_network_error(e))?;

        if response.status().is_success() {
            Ok(())
        } else {
            let status = response.status().as_u16();
            let body = response.json().await.unwrap_or_default();
            Err(OllamaErrorMapper::map_http_error(status, body))
        }
    }

    /// Push a model to Ollama registry
    pub async fn push_model(&self, client: &reqwest::Client, model_name: &str) -> Result<(), LlmConnectorError> {
        let base = &self.base_url;
        let url = format!("{}/api/push", base);

        let request = OllamaModelRequest {
            name: model_name.to_string(),
            insecure: None,
            stream: Some(false),
        };

        let response = client
            .post(&url)
            .json(&request)
            .send()
            .await
            .map_err(|e| OllamaErrorMapper::map_network_error(e))?;

        if response.status().is_success() {
            Ok(())
        } else {
            let status = response.status().as_u16();
            let body = response.json().await.unwrap_or_default();
            Err(OllamaErrorMapper::map_http_error(status, body))
        }
    }

    /// Delete a model
    pub async fn delete_model(&self, client: &reqwest::Client, model_name: &str) -> Result<(), LlmConnectorError> {
        let base = &self.base_url;
        let url = format!("{}/api/delete", base);

        let request = OllamaModelDeleteRequest {
            name: model_name.to_string(),
        };

        let response = client
            .delete(&url)
            .json(&request)
            .send()
            .await
            .map_err(|e| OllamaErrorMapper::map_network_error(e))?;

        if response.status().is_success() {
            Ok(())
        } else {
            let status = response.status().as_u16();
            let body = response.json().await.unwrap_or_default();
            Err(OllamaErrorMapper::map_http_error(status, body))
        }
    }

    /// Get model details
    pub async fn show_model(&self, client: &reqwest::Client, model_name: &str) -> Result<OllamaModel, LlmConnectorError> {
        let base = &self.base_url;
        let url = format!("{}/api/show", base);

        let request = serde_json::json!({
            "name": model_name
        });

        let response = client
            .post(&url)
            .json(&request)
            .send()
            .await
            .map_err(|e| OllamaErrorMapper::map_network_error(e))?;

        if response.status().is_success() {
            let model_info: OllamaModel = response.json().await
                .map_err(|e| LlmConnectorError::ParseError(e.to_string()))?;
            Ok(model_info)
        } else {
            let status = response.status().as_u16();
            let body = response.json().await.unwrap_or_default();
            Err(OllamaErrorMapper::map_http_error(status, body))
        }
    }
}

#[async_trait]
impl ProviderAdapter for OllamaProtocol {
    type RequestType = OllamaRequest;
    type ResponseType = OllamaResponse;
    #[cfg(feature = "streaming")]
    type StreamResponseType = serde_json::Value; // Ollama streaming is not implemented in this version
    type ErrorMapperType = OllamaErrorMapper;

    fn name(&self) -> &str {
        "ollama"
    }

    fn endpoint_url(&self, base_url: &Option<String>) -> String {
        let base = base_url.as_deref().unwrap_or(&self.base_url);
        format!("{}/api/chat", base)
    }

    fn models_endpoint_url(&self, base_url: &Option<String>) -> Option<String> {
        let base = base_url.as_deref().unwrap_or(&self.base_url);
        Some(format!("{}/api/tags", base))
    }

    fn build_request_data(&self, request: &ChatRequest, _stream: bool) -> Self::RequestType {
        let messages = request
            .messages
            .iter()
            .map(|msg| OllamaMessage {
                role: match msg.role {
                    Role::System => "system".to_string(),
                    Role::User => "user".to_string(),
                    Role::Assistant => "assistant".to_string(),
                    Role::Tool => "tool".to_string(),
                },
                content: msg.content.clone(),
            })
            .collect();

        OllamaRequest {
            model: request.model.clone(),
            messages,
            stream: None, // Not implemented in this minimal version
            options: Some(OllamaOptions {
                temperature: request.temperature,
                top_p: request.top_p,
                num_predict: request.max_tokens,
            }),
        }
    }

    fn parse_response_data(&self, response: Self::ResponseType) -> ChatResponse {
        // Convenience content before moving the message content
        let first_content = response.message.content.clone();
        ChatResponse {
            id: format!("ollama-{}", response.model),
            object: "chat.completion".to_string(),
            created: chrono::Utc::now().timestamp() as u64,
            model: response.model,
            choices: vec![Choice {
                index: 0,
                message: Message {
                    role: parse_role(&response.message.role),
                    content: response.message.content,
                    name: None,
                    tool_calls: None,
                    tool_call_id: None,
                    ..Default::default()
                },
                finish_reason: if response.done {
                    Some("stop".to_string())
                } else {
                    Some("length".to_string())
                },
                logprobs: None,
            }],
            content: first_content,
            usage: Some(Usage {
                prompt_tokens: 0, // Ollama doesn't provide prompt tokens
                completion_tokens: response.eval_count.unwrap_or(0),
                total_tokens: response.eval_count.unwrap_or(0),
                prompt_cache_hit_tokens: None,
                prompt_cache_miss_tokens: None,
                prompt_tokens_details: None,
                completion_tokens_details: None,
            }),
            system_fingerprint: None,
        }
    }

    #[cfg(feature = "streaming")]
    fn parse_stream_response_data(&self, _response: Self::StreamResponseType) -> crate::types::StreamingResponse {
        // Ollama streaming is not implemented in this version
        // Return a placeholder response
        crate::types::StreamingResponse {
            id: "ollama-stream".to_string(),
            object: "chat.completion.chunk".to_string(),
            created: chrono::Utc::now().timestamp() as u64,
            model: "ollama".to_string(),
            choices: vec![crate::types::StreamingChoice {
                index: 0,
                delta: crate::types::Delta {
                    role: None,
                    content: None,
                    tool_calls: None,
                    reasoning_content: None,
                    ..Default::default()
                },
                finish_reason: None,
                logprobs: None,
            }],
            content: String::new(),
            reasoning_content: None,
            usage: None,
            system_fingerprint: None,
        }
    }
}

// ============================================================================
// Convenience Functions
// ============================================================================

/// Create an Ollama protocol adapter
pub fn ollama() -> OllamaProtocol {
    OllamaProtocol::new()
}

/// Ollama provider type alias
pub type OllamaProvider = crate::protocols::core::GenericProvider<OllamaProtocol>;