llm-connector 0.5.4

Next-generation Rust library for LLM protocol abstraction with native multi-modal support. Supports 11+ providers (OpenAI, Anthropic, Aliyun, Zhipu, Ollama, Tencent, Volcengine, LongCat, Moonshot, DeepSeek) with clean Protocol/Provider separation, type-safe interface, and universal streaming.
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
//! Ollama服务提供商实现
//!
//! Ollama是一个本地LLM服务,具有特殊的模型管理功能,因此需要自定义Provider实现。

use crate::core::{HttpClient, Provider};
use crate::error::LlmConnectorError;
use crate::types::{ChatRequest, ChatResponse, Choice, Message, Role};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::any::Any;

#[cfg(feature = "streaming")]
use crate::types::ChatStream;

/// Ollama服务提供商
///
/// 由于Ollama具有特殊的模型管理功能,我们使用自定义Provider实现
/// 而不是GenericProvider模式。
#[derive(Clone, Debug)]
pub struct OllamaProvider {
    client: HttpClient,
    base_url: String,
}

impl OllamaProvider {
    /// 创建新的Ollama提供商
    ///
    /// # 参数
    /// - `base_url`: Ollama服务的URL (默认: http://localhost:11434)
    ///
    /// # 示例
    /// ```rust,no_run
    /// use llm_connector::providers::OllamaProvider;
    ///
    /// let provider = OllamaProvider::new("http://localhost:11434").unwrap();
    /// ```
    pub fn new(base_url: &str) -> Result<Self, LlmConnectorError> {
        // Content-Type 由 HttpClient::post() 的 .json() 方法自动设置
        let client = HttpClient::new(base_url)?;

        Ok(Self {
            client,
            base_url: base_url.to_string(),
        })
    }

    /// 创建带有自定义配置的Ollama提供商
    pub fn with_config(
        base_url: &str,
        timeout_secs: Option<u64>,
        proxy: Option<&str>,
    ) -> Result<Self, LlmConnectorError> {
        // Content-Type 由 HttpClient::post() 的 .json() 方法自动设置
        let client = HttpClient::with_config(base_url, timeout_secs, proxy)?;

        Ok(Self {
            client,
            base_url: base_url.to_string(),
        })
    }

    /// 拉取模型
    ///
    /// # 参数
    /// - `model_name`: 要拉取的模型名称 (如 "llama2", "codellama")
    ///
    /// # 示例
    /// ```rust,no_run
    /// # use llm_connector::providers::OllamaProvider;
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let provider = OllamaProvider::new("http://localhost:11434")?;
    /// provider.pull_model("llama2").await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn pull_model(&self, model_name: &str) -> Result<(), LlmConnectorError> {
        let request = OllamaPullRequest {
            name: model_name.to_string(),
            stream: Some(false),
        };

        let url = format!("{}/api/pull", self.base_url);
        let response = self.client.post(&url, &request).await?;

        if !response.status().is_success() {
            let text = response
                .text()
                .await
                .map_err(|e| LlmConnectorError::NetworkError(e.to_string()))?;
            return Err(LlmConnectorError::ApiError(format!(
                "Failed to pull model: {}",
                text
            )));
        }

        Ok(())
    }

    /// 删除模型
    ///
    /// # 参数
    /// - `model_name`: 要删除的模型名称
    ///
    /// # 示例
    /// ```rust,no_run
    /// # use llm_connector::providers::OllamaProvider;
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let provider = OllamaProvider::new("http://localhost:11434")?;
    /// provider.delete_model("llama2").await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn delete_model(&self, model_name: &str) -> Result<(), LlmConnectorError> {
        let request = OllamaDeleteRequest {
            name: model_name.to_string(),
        };

        let url = format!("{}/api/delete", self.base_url);
        let response = self.client.post(&url, &request).await?;

        if !response.status().is_success() {
            let text = response
                .text()
                .await
                .map_err(|e| LlmConnectorError::NetworkError(e.to_string()))?;
            return Err(LlmConnectorError::ApiError(format!(
                "Failed to delete model: {}",
                text
            )));
        }

        Ok(())
    }

    /// 获取模型信息
    ///
    /// # 参数
    /// - `model_name`: 模型名称
    ///
    /// # 返回
    /// 模型的详细信息
    pub async fn show_model(&self, model_name: &str) -> Result<OllamaModelInfo, LlmConnectorError> {
        let request = OllamaShowRequest {
            name: model_name.to_string(),
        };

        let url = format!("{}/api/show", self.base_url);
        let response = self.client.post(&url, &request).await?;
        let status = response.status();
        let text = response
            .text()
            .await
            .map_err(|e| LlmConnectorError::NetworkError(e.to_string()))?;

        if !status.is_success() {
            return Err(LlmConnectorError::ApiError(format!(
                "Failed to show model: {}",
                text
            )));
        }

        serde_json::from_str(&text).map_err(|e| {
            LlmConnectorError::ParseError(format!("Failed to parse model info: {}", e))
        })
    }

    /// 检查模型是否存在
    pub async fn model_exists(&self, model_name: &str) -> Result<bool, LlmConnectorError> {
        match self.show_model(model_name).await {
            Ok(_) => Ok(true),
            Err(LlmConnectorError::ApiError(_)) => Ok(false),
            Err(e) => Err(e),
        }
    }
}

#[async_trait]
impl Provider for OllamaProvider {
    fn name(&self) -> &str {
        "ollama"
    }

    async fn models(&self) -> Result<Vec<String>, LlmConnectorError> {
        let url = format!("{}/api/tags", self.base_url);
        let response = self.client.get(&url).await?;
        let status = response.status();
        let text = response
            .text()
            .await
            .map_err(|e| LlmConnectorError::NetworkError(e.to_string()))?;

        if !status.is_success() {
            return Err(LlmConnectorError::ApiError(format!(
                "Failed to get models: {}",
                text
            )));
        }

        let models_response: OllamaModelsResponse = serde_json::from_str(&text)
            .map_err(|e| LlmConnectorError::ParseError(format!("Failed to parse models: {}", e)))?;

        Ok(models_response.models.into_iter().map(|m| m.name).collect())
    }

    async fn chat(&self, request: &ChatRequest) -> Result<ChatResponse, LlmConnectorError> {
        let ollama_request = OllamaChatRequest {
            model: request.model.clone(),
            messages: request
                .messages
                .iter()
                .map(|msg| OllamaMessage {
                    role: match msg.role {
                        Role::User => "user".to_string(),
                        Role::Assistant => "assistant".to_string(),
                        Role::System => "system".to_string(),
                        Role::Tool => "user".to_string(), // Ollama不支持tool角色
                    },
                    // Ollama 使用纯文本格式
                    content: msg.content_as_text(),
                })
                .collect(),
            stream: Some(false),
            options: Some(OllamaOptions {
                temperature: request.temperature,
                num_predict: request.max_tokens.map(|t| t as i32),
                top_p: request.top_p,
            }),
        };

        let url = format!("{}/api/chat", self.base_url);
        let response = self.client.post(&url, &ollama_request).await?;
        let status = response.status();
        let text = response
            .text()
            .await
            .map_err(|e| LlmConnectorError::NetworkError(e.to_string()))?;

        if !status.is_success() {
            return Err(LlmConnectorError::ApiError(format!(
                "Ollama chat failed: {}",
                text
            )));
        }

        let ollama_response: OllamaChatResponse = serde_json::from_str(&text).map_err(|e| {
            LlmConnectorError::ParseError(format!("Failed to parse Ollama response: {}", e))
        })?;

        let content = ollama_response.message.content.clone();

        let choices = vec![Choice {
            index: 0,
            message: Message {
                role: Role::Assistant,
                content: vec![crate::types::MessageBlock::text(&content)],
                name: None,
                tool_calls: None,
                tool_call_id: None,
                reasoning_content: None,
                reasoning: None,
                thought: None,
                thinking: None,
            },
            finish_reason: Some("stop".to_string()),
            logprobs: None,
        }];

        Ok(ChatResponse {
            id: "ollama-response".to_string(),
            object: "chat.completion".to_string(),
            created: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
            model: ollama_response.model,
            choices,
            content,
            reasoning_content: None,
            usage: None, // Ollama不返回token使用信息
            system_fingerprint: None,
        })
    }

    #[cfg(feature = "streaming")]
    async fn chat_stream(&self, request: &ChatRequest) -> Result<ChatStream, LlmConnectorError> {
        let ollama_request = OllamaChatRequest {
            model: request.model.clone(),
            messages: request
                .messages
                .iter()
                .map(|msg| OllamaMessage {
                    role: match msg.role {
                        Role::User => "user".to_string(),
                        Role::Assistant => "assistant".to_string(),
                        Role::System => "system".to_string(),
                        Role::Tool => "user".to_string(),
                    },
                    content: msg.content_as_text(),
                })
                .collect(),
            stream: Some(true),
            options: Some(OllamaOptions {
                temperature: request.temperature,
                num_predict: request.max_tokens.map(|t| t as i32),
                top_p: request.top_p,
            }),
        };

        let url = format!("{}/api/chat", self.base_url);
        let response = self.client.stream(&url, &ollama_request).await?;
        let status = response.status();

        if !status.is_success() {
            let text = response
                .text()
                .await
                .map_err(|e| LlmConnectorError::NetworkError(e.to_string()))?;
            return Err(LlmConnectorError::ApiError(format!(
                "Ollama stream failed: {}",
                text
            )));
        }

        // Ollama使用JSONL格式而不是SSE
        Ok(crate::sse::sse_to_streaming_response(response))
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

// Ollama请求/响应类型
#[derive(Serialize, Debug)]
struct OllamaChatRequest {
    model: String,
    messages: Vec<OllamaMessage>,
    stream: Option<bool>,
    options: Option<OllamaOptions>,
}

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

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

#[derive(Deserialize, Debug)]
struct OllamaChatResponse {
    model: String,
    message: OllamaResponseMessage,
    #[allow(dead_code)]
    done: bool,
}

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

#[derive(Serialize, Debug)]
struct OllamaPullRequest {
    name: String,
    stream: Option<bool>,
}

#[derive(Serialize, Debug)]
struct OllamaDeleteRequest {
    name: String,
}

#[derive(Serialize, Debug)]
struct OllamaShowRequest {
    name: String,
}

#[derive(Deserialize, Debug)]
pub struct OllamaModelInfo {
    pub modelfile: String,
    pub parameters: String,
    pub template: String,
    pub details: OllamaModelDetails,
}

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

#[derive(Deserialize, Debug)]
struct OllamaModelsResponse {
    models: Vec<OllamaModel>,
}

#[derive(Deserialize, Debug)]
struct OllamaModel {
    name: String,
    #[allow(dead_code)]
    modified_at: String,
    #[allow(dead_code)]
    size: u64,
}

/// 创建Ollama服务提供商 (默认本地地址)
///
/// # 示例
/// ```rust,no_run
/// use llm_connector::providers::ollama;
///
/// let provider = ollama().unwrap();
/// ```
pub fn ollama() -> Result<OllamaProvider, LlmConnectorError> {
    OllamaProvider::new("http://localhost:11434")
}

/// 创建带有自定义URL的Ollama服务提供商
///
/// # 参数
/// - `base_url`: Ollama服务的URL
///
/// # 示例
/// ```rust,no_run
/// use llm_connector::providers::ollama_with_base_url;
///
/// let provider = ollama_with_base_url("http://192.168.1.100:11434").unwrap();
/// ```
pub fn ollama_with_base_url(base_url: &str) -> Result<OllamaProvider, LlmConnectorError> {
    OllamaProvider::new(base_url)
}

/// 创建带有自定义配置的Ollama服务提供商
///
/// # 参数
/// - `base_url`: Ollama服务的URL
/// - `timeout_secs`: 超时时间(秒)
/// - `proxy`: 代理URL (可选)
///
/// # 示例
/// ```rust,no_run
/// use llm_connector::providers::ollama_with_config;
///
/// let provider = ollama_with_config(
///     "http://localhost:11434",
///     Some(120), // 2分钟超时
///     None
/// ).unwrap();
/// ```
pub fn ollama_with_config(
    base_url: &str,
    timeout_secs: Option<u64>,
    proxy: Option<&str>,
) -> Result<OllamaProvider, LlmConnectorError> {
    OllamaProvider::with_config(base_url, timeout_secs, proxy)
}