model-gateway-rs 0.1.7

A Rust library for model gateway services, providing traits and SDKs for various AI models.
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
use serde::{Deserialize, Serialize};

use crate::model::role::Role;

// ============ Request Structures ============

/// Content types for multimodal messages
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
#[serde(rename_all = "snake_case")]
pub enum MessageContent {
    Text {
        text: String,
    },
    ImageUrl {
        image_url: ImageUrl,
    },
    #[serde(rename = "video_url")]
    VideoUrl {
        video_url: VideoUrl,
    },
}

/// Image URL structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImageUrl {
    pub url: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub detail: Option<String>, // "low", "high", "auto"
}

/// Video URL structure for video input support
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VideoUrl {
    pub url: String,
}

/// Message structure for Doubao Vision
#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum DoubaoVisionMessage {
    Text {
        role: Role,
        content: String,
    },
    Multimodal {
        role: Role,
        content: Vec<MessageContent>,
    },
}

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

/// Thinking mode configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
#[serde(rename_all = "lowercase")]
pub enum ThinkingConfig {
    Enabled,
    Disabled,
}

/// Request body for Doubao Vision chat completion
#[derive(Debug, Clone, Serialize)]
pub struct DoubaoVisionRequest {
    pub model: String, // e.g., "doubao-1-5-thinking-vision-pro-250428"
    pub messages: Vec<DoubaoVisionMessage>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub thinking: Option<ThinkingConfig>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_p: Option<f32>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_tokens: Option<u32>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop: Option<Vec<String>>,
}

// ============ Response Structures ============

/// Choice structure for response
#[derive(Debug, Deserialize)]
pub struct DoubaoChoice {
    pub index: u32,
    pub message: DoubaoResponseMessage,
    pub finish_reason: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub logprobs: Option<serde_json::Value>,
}

/// Response message structure  
#[derive(Debug, Clone, Deserialize)]
pub struct DoubaoResponseMessage {
    pub role: Role,
    pub content: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning_content: Option<String>, // For thinking mode output
}

/// Usage details for token consumption
#[derive(Debug, Deserialize)]
pub struct DoubaoUsage {
    pub prompt_tokens: u32,
    pub completion_tokens: u32,
    pub total_tokens: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning_tokens: Option<u32>, // Tokens used in thinking process
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prompt_tokens_details: Option<PromptTokensDetails>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub completion_tokens_details: Option<CompletionTokensDetails>,
}

/// Detailed prompt tokens breakdown
#[derive(Debug, Deserialize)]
pub struct PromptTokensDetails {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cached_tokens: Option<u32>,
}

/// Detailed completion tokens breakdown
#[derive(Debug, Deserialize)]
pub struct CompletionTokensDetails {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning_tokens: Option<u32>,
}

/// Main response structure for Doubao Vision
#[derive(Debug, Deserialize)]
pub struct DoubaoVisionResponse {
    pub id: String,
    pub object: String,
    pub created: u64,
    pub model: String,
    pub choices: Vec<DoubaoChoice>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub usage: Option<DoubaoUsage>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub service_tier: Option<String>,
}

// ============ Implementation ============

impl DoubaoVisionRequest {
    /// Create a new request with basic parameters
    pub fn new(model: impl Into<String>, messages: Vec<DoubaoVisionMessage>) -> Self {
        Self {
            model: model.into(),
            messages,
            thinking: None,
            stream: None,
            temperature: None,
            top_p: None,
            max_tokens: None,
            stop: None,
        }
    }

    /// Enable thinking mode
    pub fn with_thinking(mut self, enabled: bool) -> Self {
        self.thinking = Some(if enabled {
            ThinkingConfig::Enabled
        } else {
            ThinkingConfig::Disabled
        });
        self
    }

    /// Enable thinking mode (alternative method)
    pub fn enable_thinking(mut self) -> Self {
        self.thinking = Some(ThinkingConfig::Enabled);
        self
    }

    /// Disable thinking mode (alternative method)
    pub fn disable_thinking(mut self) -> Self {
        self.thinking = Some(ThinkingConfig::Disabled);
        self
    }

    /// Set temperature
    pub fn with_temperature(mut self, temperature: f32) -> Self {
        self.temperature = Some(temperature);
        self
    }

    /// Set max tokens
    pub fn with_max_tokens(mut self, max_tokens: u32) -> Self {
        self.max_tokens = Some(max_tokens);
        self
    }

    /// Set streaming
    pub fn with_stream(mut self, stream: bool) -> Self {
        self.stream = Some(stream);
        self
    }

    /// Set top_p
    pub fn with_top_p(mut self, top_p: f32) -> Self {
        self.top_p = Some(top_p);
        self
    }

    /// Set stop sequences
    pub fn with_stop(mut self, stop: Vec<String>) -> Self {
        self.stop = Some(stop);
        self
    }
}

impl DoubaoVisionMessage {
    /// Create a user text message
    pub fn user(content: impl Into<String>) -> Self {
        Self::Text {
            role: Role::User,
            content: content.into(),
        }
    }

    /// Create an assistant message
    pub fn assistant(content: impl Into<String>) -> Self {
        Self::Text {
            role: Role::Assistant,
            content: content.into(),
        }
    }

    /// Create a system message
    pub fn system(content: impl Into<String>) -> Self {
        Self::Text {
            role: Role::System,
            content: content.into(),
        }
    }

    /// Create a user message with image
    pub fn with_image(text: impl Into<String>, image_url: impl Into<String>) -> Self {
        Self::Multimodal {
            role: Role::User,
            content: vec![
                MessageContent::ImageUrl {
                    image_url: ImageUrl {
                        url: image_url.into(),
                        detail: None,
                    },
                },
                MessageContent::Text { text: text.into() },
            ],
        }
    }

    /// Create a user message with image and custom detail level
    pub fn with_image_detail(
        text: impl Into<String>,
        image_url: impl Into<String>,
        detail: impl Into<String>,
    ) -> Self {
        Self::Multimodal {
            role: Role::User,
            content: vec![
                MessageContent::ImageUrl {
                    image_url: ImageUrl {
                        url: image_url.into(),
                        detail: Some(detail.into()),
                    },
                },
                MessageContent::Text { text: text.into() },
            ],
        }
    }

    /// Create a user message with video
    pub fn with_video(text: impl Into<String>, video_url: impl Into<String>) -> Self {
        Self::Multimodal {
            role: Role::User,
            content: vec![
                MessageContent::VideoUrl {
                    video_url: VideoUrl {
                        url: video_url.into(),
                    },
                },
                MessageContent::Text { text: text.into() },
            ],
        }
    }

    /// Create a user message with multiple images
    pub fn with_images(text: impl Into<String>, image_urls: Vec<String>) -> Self {
        let mut contents: Vec<MessageContent> = image_urls
            .into_iter()
            .map(|url| MessageContent::ImageUrl {
                image_url: ImageUrl { url, detail: None },
            })
            .collect();

        contents.push(MessageContent::Text { text: text.into() });

        Self::Multimodal {
            role: Role::User,
            content: contents,
        }
    }

    /// Create a user message with text first, then image
    pub fn with_text_then_image(text: impl Into<String>, image_url: impl Into<String>) -> Self {
        Self::Multimodal {
            role: Role::User,
            content: vec![
                MessageContent::Text { text: text.into() },
                MessageContent::ImageUrl {
                    image_url: ImageUrl {
                        url: image_url.into(),
                        detail: None,
                    },
                },
            ],
        }
    }
}

impl DoubaoVisionResponse {
    /// Get the first choice's message
    pub fn first_message(&self) -> Option<&DoubaoResponseMessage> {
        self.choices.first().map(|choice| &choice.message)
    }

    /// Get the first choice's content as string
    pub fn first_content(&self) -> Option<&str> {
        self.first_message().map(|msg| msg.content.as_str())
    }

    /// Get the reasoning content if available
    pub fn reasoning_content(&self) -> Option<&str> {
        self.first_message()
            .and_then(|msg| msg.reasoning_content.as_deref())
    }

    /// Get total tokens used
    pub fn total_tokens(&self) -> Option<u32> {
        self.usage.as_ref().map(|u| u.total_tokens)
    }

    /// Get reasoning tokens if available
    pub fn reasoning_tokens(&self) -> Option<u32> {
        self.usage.as_ref().and_then(|u| u.reasoning_tokens)
    }

    /// Check if the response is from assistant
    pub fn is_assistant_message(&self) -> bool {
        self.first_message()
            .map(|msg| matches!(msg.role, Role::Assistant))
            .unwrap_or(false)
    }
}

// ============ Helper Functions ============

/// Create a simple OCR request for document recognition
pub fn create_ocr_request(
    image_url: impl Into<String>,
    prompt: impl Into<String>,
) -> DoubaoVisionRequest {
    let message = DoubaoVisionMessage::with_image(prompt, image_url);

    DoubaoVisionRequest::new("doubao-1-5-thinking-vision-pro-250428", vec![message])
        .disable_thinking() // Disable thinking for pure OCR tasks
}

/// Create a vision analysis request with thinking
pub fn create_analysis_request(
    image_url: impl Into<String>,
    prompt: impl Into<String>,
) -> DoubaoVisionRequest {
    let message = DoubaoVisionMessage::with_image(prompt, image_url);

    DoubaoVisionRequest::new("doubao-1-5-thinking-vision-pro-250428", vec![message])
        .enable_thinking() // Enable thinking for analysis
        .with_temperature(0.1) // Lower temperature for more consistent analysis
}

/// Create a multi-turn conversation request
pub fn create_conversation_request(
    system_prompt: impl Into<String>,
    user_prompt: impl Into<String>,
    image_url: impl Into<String>,
) -> DoubaoVisionRequest {
    let messages = vec![
        DoubaoVisionMessage::system(system_prompt),
        DoubaoVisionMessage::with_image(user_prompt, image_url),
    ];

    DoubaoVisionRequest::new("doubao-1-5-thinking-vision-pro-250428", messages)
}

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

    #[test]
    fn test_create_user_message() {
        let msg = DoubaoVisionMessage::user("Hello");
        if let DoubaoVisionMessage::Text { role, content } = msg {
            assert!(matches!(role, Role::User));
            assert_eq!(content, "Hello");
        } else {
            panic!("Expected text message");
        }
    }

    #[test]
    fn test_create_image_message() {
        let msg = DoubaoVisionMessage::with_image(
            "What's in this image?",
            "https://example.com/image.jpg",
        );
        if let DoubaoVisionMessage::Multimodal { role, content } = msg {
            assert!(matches!(role, Role::User));
            assert_eq!(content.len(), 2);
        } else {
            panic!("Expected multimodal message");
        }
    }

    #[test]
    fn test_create_multiple_images_message() {
        let msg = DoubaoVisionMessage::with_images(
            "Compare these images",
            vec![
                "https://example.com/image1.jpg".to_string(),
                "https://example.com/image2.jpg".to_string(),
            ],
        );
        if let DoubaoVisionMessage::Multimodal { role, content } = msg {
            assert!(matches!(role, Role::User));
            assert_eq!(content.len(), 3); // 2 images + 1 text
        } else {
            panic!("Expected multimodal message");
        }
    }

    #[test]
    fn test_request_builder() {
        let messages = vec![
            DoubaoVisionMessage::system("You are a helpful assistant"),
            DoubaoVisionMessage::user("Hello"),
        ];
        let request = DoubaoVisionRequest::new("doubao-1-5-thinking-vision-pro-250428", messages)
            .enable_thinking()
            .with_temperature(0.7)
            .with_max_tokens(2000);

        assert!(request.thinking.is_some());
        assert_eq!(request.temperature, Some(0.7));
        assert_eq!(request.max_tokens, Some(2000));
    }

    #[test]
    fn test_role_serialization() {
        let msg = DoubaoVisionMessage::assistant("I can help");
        if let DoubaoVisionMessage::Text { role, content: _ } = msg {
            assert!(matches!(role, Role::Assistant));
        } else {
            panic!("Expected text message");
        }
    }
}