llmg-providers 0.1.7

Provider implementations for LLMG - LLM Gateway
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
#![allow(clippy::redundant_closure)]

use futures::TryStreamExt;
use llmg_core::{
    provider::{ChatCompletionStream, LlmError, Provider},
    streaming::{ChatCompletionChunk, ChoiceDelta, DeltaContent},
    types::{
        ChatCompletionRequest, ChatCompletionResponse, Choice, EmbeddingRequest, EmbeddingResponse,
        Message, Usage,
    },
};
use std::future::Future;
use std::pin::Pin;

/// Ollama API client
#[derive(Debug)]
pub struct OllamaClient {
    http_client: reqwest::Client,
    base_url: String,
}

/// Ollama chat request format
#[derive(Debug, serde::Serialize)]
struct OllamaChatRequest {
    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(Debug, serde::Serialize, Default)]
struct OllamaOptions {
    #[serde(skip_serializing_if = "Option::is_none")]
    temperature: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    num_predict: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    top_p: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    stop: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    repeat_penalty: Option<f32>,
}

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

/// Ollama chat response format
#[derive(Debug, serde::Deserialize)]
struct OllamaChatResponse {
    message: OllamaResponseMessage,
    #[serde(rename = "eval_count")]
    eval_count: Option<u32>,
    #[serde(rename = "prompt_eval_count")]
    prompt_eval_count: Option<u32>,
    done: bool,
}

#[derive(Debug, serde::Deserialize)]
struct OllamaStreamResponse {
    message: Option<OllamaResponseMessage>,
    #[serde(rename = "eval_count")]
    eval_count: Option<u32>,
    #[serde(rename = "prompt_eval_count")]
    prompt_eval_count: Option<u32>,
    done: bool,
}

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

impl OllamaClient {
    /// Create a new Ollama client with default localhost URL
    pub fn new() -> Self {
        Self {
            http_client: reqwest::Client::new(),
            base_url: "http://localhost:11434".to_string(),
        }
    }

    /// Create a new OllamaClient from environment variables.
    ///
    /// Reads `OLLAMA_BASE_URL` (default "http://localhost:11434").
    pub fn from_env() -> Self {
        let mut client = Self::new();
        if let Ok(base_url) = std::env::var("OLLAMA_BASE_URL") {
            client = client.with_base_url(base_url);
        }
        client
    }

    /// Create with custom base URL
    pub fn with_base_url(mut self, url: impl Into<String>) -> Self {
        self.base_url = url.into();
        self
    }

    /// Convert OpenAI format to Ollama format
    fn convert_request(&self, request: ChatCompletionRequest) -> OllamaChatRequest {
        let messages: Vec<OllamaMessage> = request
            .messages
            .into_iter()
            .filter_map(|msg| match msg {
                Message::System { content, .. } => Some(OllamaMessage {
                    role: "system".to_string(),
                    content,
                }),
                Message::User { content, .. } => Some(OllamaMessage {
                    role: "user".to_string(),
                    content,
                }),
                Message::Assistant { content, .. } => content.map(|c| OllamaMessage {
                    role: "assistant".to_string(),
                    content: c,
                }),
                _ => None,
            })
            .collect();

        OllamaChatRequest {
            model: request.model,
            messages,
            stream: request.stream,
            options: Some(OllamaOptions {
                temperature: request.temperature,
                num_predict: request.max_tokens,
                top_p: request.top_p,
                stop: request.stop,
                // Ollama's repeat_penalty is similar to frequency_penalty
                // It penalizes tokens based on how frequently they appear
                repeat_penalty: request.frequency_penalty,
            }),
        }
    }

    /// Convert Ollama response to OpenAI format
    fn convert_response(
        &self,
        response: OllamaChatResponse,
        model: String,
    ) -> ChatCompletionResponse {
        let usage = if let (Some(prompt), Some(completion)) =
            (response.prompt_eval_count, response.eval_count)
        {
            Some(Usage {
                prompt_tokens: prompt,
                completion_tokens: completion,
                total_tokens: prompt + completion,
            })
        } else {
            None
        };

        ChatCompletionResponse {
            id: format!("ollama-{})", uuid::Uuid::new_v4()),
            object: "chat.completion".to_string(),
            created: chrono::Utc::now().timestamp(),
            model,
            choices: vec![Choice {
                index: 0,
                message: Message::Assistant {
                    content: Some(response.message.content),
                    refusal: None,
                    tool_calls: None,
                },
                finish_reason: if response.done {
                    Some("stop".to_string())
                } else {
                    None
                },
            }],
            usage,
        }
    }

    async fn make_request(
        &self,
        request: ChatCompletionRequest,
    ) -> Result<ChatCompletionResponse, LlmError> {
        let model = request.model.clone();
        let ollama_req = self.convert_request(request);
        let url = format!("{}/api/chat", self.base_url);

        let response = self
            .http_client
            .post(&url)
            .json(&ollama_req)
            .send()
            .await
            .map_err(|e| LlmError::HttpError(e.to_string()))?;

        if !response.status().is_success() {
            let status = response.status().as_u16();
            let text = response.text().await.unwrap_or_default();
            return Err(LlmError::ApiError {
                status,
                message: text,
            });
        }

        let ollama_resp: OllamaChatResponse = response
            .json()
            .await
            .map_err(|e| LlmError::HttpError(e.to_string()))?;

        Ok(self.convert_response(ollama_resp, model))
    }

    async fn make_stream_request(
        &self,
        request: ChatCompletionRequest,
    ) -> Result<ChatCompletionStream, LlmError> {
        let model = request.model.clone();
        let mut ollama_req = self.convert_request(request);
        ollama_req.stream = Some(true);

        let url = format!("{}/api/chat", self.base_url);

        let response = self
            .http_client
            .post(&url)
            .json(&ollama_req)
            .send()
            .await
            .map_err(|e| LlmError::HttpError(e.to_string()))?;

        if !response.status().is_success() {
            let status = response.status().as_u16();
            let text = response.text().await.unwrap_or_default();
            return Err(LlmError::ApiError {
                status,
                message: text,
            });
        }

        let chunk_id = ChatCompletionChunk::generate_id();
        let stream_model = model.clone();

        let stream = response
            .bytes_stream()
            .map_err(|e| LlmError::HttpError(e.to_string()))
            .and_then(move |bytes| {
                let chunk_id = chunk_id.clone();
                let stream_model = stream_model.clone();
                async move {
                    let text = String::from_utf8_lossy(&bytes);
                    parse_ollama_stream_line(&text, &chunk_id, &stream_model)
                }
            })
            .try_filter_map(|chunk| async move { Ok(chunk) });

        Ok(Box::pin(stream) as ChatCompletionStream)
    }
}

impl Default for OllamaClient {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait::async_trait]
impl Provider for OllamaClient {
    async fn chat_completion(
        &self,
        request: ChatCompletionRequest,
    ) -> Result<ChatCompletionResponse, LlmError> {
        self.make_request(request).await
    }

    fn chat_completion_stream(
        &self,
        request: ChatCompletionRequest,
    ) -> Pin<Box<dyn Future<Output = Result<ChatCompletionStream, LlmError>> + Send + '_>> {
        Box::pin(self.make_stream_request(request))
    }

    async fn embeddings(&self, request: EmbeddingRequest) -> Result<EmbeddingResponse, LlmError> {
        let url = format!("{}/api/embeddings", self.base_url);

        let body = serde_json::json!({
            "model": request.model,
            "prompt": request.input,
        });

        let response = self
            .http_client
            .post(&url)
            .json(&body)
            .send()
            .await
            .map_err(|e| LlmError::HttpError(e.to_string()))?;

        if !response.status().is_success() {
            let status = response.status().as_u16();
            let text = response.text().await.unwrap_or_default();
            return Err(LlmError::ApiError {
                status,
                message: text,
            });
        }

        let embedding_resp: serde_json::Value = response
            .json()
            .await
            .map_err(|e| LlmError::HttpError(e.to_string()))?;

        let embedding: Vec<f32> = embedding_resp["embedding"]
            .as_array()
            .map(|arr| {
                arr.iter()
                    .filter_map(|v| v.as_f64().map(|f| f as f32))
                    .collect()
            })
            .unwrap_or_default();

        Ok(EmbeddingResponse {
            id: format!("ollama-embed-{})", uuid::Uuid::new_v4()),
            object: "list".to_string(),
            data: vec![llmg_core::types::Embedding {
                index: 0,
                object: "embedding".to_string(),
                embedding,
            }],
            model: request.model,
            usage: llmg_core::types::Usage {
                prompt_tokens: 0,
                completion_tokens: 0,
                total_tokens: 0,
            },
        })
    }
    fn provider_name(&self) -> &'static str {
        "ollama"
    }
}

fn parse_ollama_stream_line(
    line: &str,
    chunk_id: &str,
    model: &str,
) -> Result<Option<ChatCompletionChunk>, LlmError> {
    let line = line.trim();
    if line.is_empty() {
        return Ok(None);
    }

    let parsed: OllamaStreamResponse =
        serde_json::from_str(line).map_err(LlmError::SerializationError)?;

    if let Some(message) = parsed.message {
        if !message.content.is_empty() {
            return Ok(Some(ChatCompletionChunk {
                id: chunk_id.to_string(),
                object: "chat.completion.chunk".to_string(),
                created: chrono::Utc::now().timestamp(),
                model: model.to_string(),
                choices: vec![ChoiceDelta {
                    index: 0,
                    delta: DeltaContent::content(message.content),
                    finish_reason: None,
                }],
            }));
        }
    }

    if parsed.done {
        return Ok(Some(ChatCompletionChunk::final_chunk(
            chunk_id.to_string(),
            model.to_string(),
            "stop",
        )));
    }

    Ok(None)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ollama_client_creation() {
        let client = OllamaClient::new();
        assert_eq!(client.provider_name(), "ollama");
        assert_eq!(client.base_url, "http://localhost:11434");
    }

    #[test]
    fn test_ollama_custom_url() {
        let client = OllamaClient::new().with_base_url("http://custom-server:8080");
        assert_eq!(client.base_url, "http://custom-server:8080");
    }

    #[test]
    fn test_request_conversion() {
        let client = OllamaClient::new();

        let request = ChatCompletionRequest {
            model: "llama2".to_string(),
            messages: vec![Message::User {
                content: "Hello!".to_string(),
                name: None,
            }],
            temperature: Some(0.7),
            max_tokens: Some(100),
            stream: None,
            top_p: None,
            frequency_penalty: None,
            presence_penalty: None,
            stop: None,
            user: None,
            tools: None,
            tool_choice: None,
        };

        let ollama_req = client.convert_request(request);

        assert_eq!(ollama_req.model, "llama2");
        assert_eq!(ollama_req.messages.len(), 1);
        assert_eq!(ollama_req.messages[0].role, "user");
        assert_eq!(ollama_req.messages[0].content, "Hello!");
    }
}