sie-sdk-rs 0.1.0

Rust client for the SIE inference server: embeddings, reranking, extraction and text generation
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! Request items: the multimodal unit every inference endpoint consumes.

use std::path::{Path, PathBuf};

use rmpv::Value as MsgValue;
use serde_json::Value;

use crate::error::{Error, Result};
use crate::media::{self, Samples};

/// An image, in whatever form the caller already has it.
#[derive(Debug, Clone, PartialEq)]
enum ImageData {
    /// Already encoded; the format is sniffed from the bytes.
    Encoded(Vec<u8>),
    /// Read from disk at send time, then sniffed.
    Path(PathBuf),
    /// Decoded pixels, re-encoded as JPEG at send time.
    Decoded(Box<image::DynamicImage>),
}

/// One image attached to an [`Item`].
#[derive(Debug, Clone, PartialEq)]
pub struct ImageInput {
    data: ImageData,
    format: Option<String>,
}

impl ImageInput {
    /// An image that is already encoded. The format is detected from its magic bytes.
    pub fn bytes(data: impl Into<Vec<u8>>) -> Self {
        Self {
            data: ImageData::Encoded(data.into()),
            format: None,
        }
    }

    /// An encoded image read from disk when the request is sent.
    pub fn path(path: impl Into<PathBuf>) -> Self {
        Self {
            data: ImageData::Path(path.into()),
            format: None,
        }
    }

    /// Decoded pixels, re-encoded as JPEG when the request is sent.
    pub fn decoded(image: image::DynamicImage) -> Self {
        Self {
            data: ImageData::Decoded(Box::new(image)),
            format: None,
        }
    }

    /// Declare the format explicitly. A declaration that contradicts the bytes is an error.
    pub fn format(mut self, format: impl Into<String>) -> Self {
        self.format = Some(format.into());
        self
    }

    pub(crate) fn resolve(&self) -> Result<(Vec<u8>, String)> {
        let encoded = match &self.data {
            ImageData::Encoded(data) => data.clone(),
            ImageData::Path(path) => read_file(path)?,
            ImageData::Decoded(image) => media::encode_jpeg(image)?,
        };
        let detected = media::detect_image_format(&encoded)?;
        if let Some(declared) = &self.format {
            let declared = media::canonical_format(declared)?;
            if declared != detected {
                return Err(Error::invalid(format!(
                    "Image format mismatch: declared {declared:?}, detected {detected:?}"
                )));
            }
        }
        Ok((encoded, detected.to_string()))
    }
}

#[derive(Debug, Clone, PartialEq)]
enum AudioData {
    Encoded(Vec<u8>),
    Path(PathBuf),
    Waveform {
        samples: Samples,
        channels: u16,
        sample_rate: u32,
    },
}

/// Audio attached to an [`Item`].
#[derive(Debug, Clone, PartialEq)]
pub struct AudioInput {
    data: AudioData,
    format: Option<String>,
}

impl AudioInput {
    /// Audio that is already in a container the server understands.
    pub fn bytes(data: impl Into<Vec<u8>>) -> Self {
        Self {
            data: AudioData::Encoded(data.into()),
            format: None,
        }
    }

    /// Encoded audio read from disk when the request is sent; the format comes from the
    /// filename.
    pub fn path(path: impl Into<PathBuf>) -> Self {
        Self {
            data: AudioData::Path(path.into()),
            format: None,
        }
    }

    /// A raw waveform, wrapped in a 16-bit PCM WAV container when the request is sent.
    pub fn waveform(samples: Samples, channels: u16, sample_rate: u32) -> Self {
        Self {
            data: AudioData::Waveform {
                samples,
                channels,
                sample_rate,
            },
            format: None,
        }
    }

    /// Declare the container format explicitly.
    pub fn format(mut self, format: impl Into<String>) -> Self {
        self.format = Some(format.into());
        self
    }

    pub(crate) fn resolve(&self) -> Result<(Vec<u8>, Option<String>, Option<u32>)> {
        match &self.data {
            AudioData::Encoded(data) => Ok((data.clone(), self.format.clone(), None)),
            AudioData::Path(path) => {
                let inferred = media::infer_audio_format(path).map(str::to_string);
                Ok((read_file(path)?, self.format.clone().or(inferred), None))
            }
            AudioData::Waveform {
                samples,
                channels,
                sample_rate,
            } => Ok((
                media::encode_wav(samples, *channels, *sample_rate)?,
                Some(self.format.clone().unwrap_or_else(|| "wav".to_string())),
                Some(*sample_rate),
            )),
        }
    }
}

/// A document or video attached to an [`Item`]: bytes plus an optional format token.
#[derive(Debug, Clone, PartialEq)]
pub struct BinaryInput {
    data: BinaryData,
    format: Option<String>,
    kind: BinaryKind,
}

#[derive(Debug, Clone, PartialEq)]
enum BinaryData {
    Encoded(Vec<u8>),
    Path(PathBuf),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum BinaryKind {
    Document,
    Video,
}

impl BinaryInput {
    /// A document held in memory.
    pub fn document_bytes(data: impl Into<Vec<u8>>) -> Self {
        Self {
            data: BinaryData::Encoded(data.into()),
            format: None,
            kind: BinaryKind::Document,
        }
    }

    /// A document read from disk; the format comes from the filename.
    pub fn document_path(path: impl Into<PathBuf>) -> Self {
        Self {
            data: BinaryData::Path(path.into()),
            format: None,
            kind: BinaryKind::Document,
        }
    }

    /// A video held in memory.
    pub fn video_bytes(data: impl Into<Vec<u8>>) -> Self {
        Self {
            data: BinaryData::Encoded(data.into()),
            format: None,
            kind: BinaryKind::Video,
        }
    }

    /// A video read from disk; the format comes from the filename.
    pub fn video_path(path: impl Into<PathBuf>) -> Self {
        Self {
            data: BinaryData::Path(path.into()),
            format: None,
            kind: BinaryKind::Video,
        }
    }

    /// Declare the format explicitly rather than inferring it from the filename.
    pub fn format(mut self, format: impl Into<String>) -> Self {
        self.format = Some(format.into());
        self
    }

    pub(crate) fn resolve(&self) -> Result<(Vec<u8>, Option<String>)> {
        match &self.data {
            BinaryData::Encoded(data) => Ok((data.clone(), self.format.clone())),
            BinaryData::Path(path) => {
                let inferred = match self.kind {
                    BinaryKind::Document => media::infer_document_format(path),
                    BinaryKind::Video => media::infer_video_format(path),
                }
                .map(str::to_string);
                Ok((read_file(path)?, self.format.clone().or(inferred)))
            }
        }
    }
}

fn read_file(path: &Path) -> Result<Vec<u8>> {
    std::fs::read(path).map_err(|err| {
        Error::Io(std::io::Error::new(
            err.kind(),
            format!("could not read {}: {err}", path.display()),
        ))
    })
}

/// One unit of input: text, images, audio, video or a document, in any combination the
/// model accepts.
#[derive(Debug, Clone, Default, PartialEq)]
#[allow(missing_docs)]
pub struct Item {
    /// Caller-chosen identifier, echoed back on the matching result.
    pub id: Option<String>,
    pub text: Option<String>,
    pub images: Vec<ImageInput>,
    pub audio: Option<AudioInput>,
    pub video: Option<BinaryInput>,
    pub document: Option<BinaryInput>,
    /// Opaque metadata passed through to the model adapter.
    pub metadata: Option<Value>,
}

impl Item {
    /// An empty item.
    pub fn new() -> Self {
        Self::default()
    }

    /// A text-only item.
    pub fn text(text: impl Into<String>) -> Self {
        Self {
            text: Some(text.into()),
            ..Self::default()
        }
    }

    /// An item holding a single image.
    pub fn image(image: ImageInput) -> Self {
        Self {
            images: vec![image],
            ..Self::default()
        }
    }

    /// Set the identifier echoed back on the result.
    pub fn with_id(mut self, id: impl Into<String>) -> Self {
        self.id = Some(id.into());
        self
    }

    /// Set or replace the text.
    pub fn with_text(mut self, text: impl Into<String>) -> Self {
        self.text = Some(text.into());
        self
    }

    /// Append an image.
    pub fn with_image(mut self, image: ImageInput) -> Self {
        self.images.push(image);
        self
    }

    /// Attach audio.
    pub fn with_audio(mut self, audio: AudioInput) -> Self {
        self.audio = Some(audio);
        self
    }

    /// Attach a video.
    pub fn with_video(mut self, video: BinaryInput) -> Self {
        self.video = Some(video);
        self
    }

    /// Attach a document.
    pub fn with_document(mut self, document: BinaryInput) -> Self {
        self.document = Some(document);
        self
    }

    /// Attach adapter metadata.
    pub fn with_metadata(mut self, metadata: Value) -> Self {
        self.metadata = Some(metadata);
        self
    }

    /// Encode to the msgpack shape, resolving every attachment to bytes.
    ///
    /// Only fields the caller set are emitted: `/v1` is additive, so an absent key and a
    /// null one are not the same thing.
    pub(crate) fn to_msgpack(&self) -> Result<MsgValue> {
        let mut fields: Vec<(MsgValue, MsgValue)> = Vec::new();

        if let Some(id) = &self.id {
            fields.push((MsgValue::from("id"), MsgValue::from(id.as_str())));
        }
        if let Some(text) = &self.text {
            fields.push((MsgValue::from("text"), MsgValue::from(text.as_str())));
        }
        if !self.images.is_empty() {
            let mut images = Vec::with_capacity(self.images.len());
            for image in &self.images {
                let (data, format) = image.resolve()?;
                images.push(MsgValue::Map(vec![
                    (MsgValue::from("data"), MsgValue::Binary(data)),
                    (MsgValue::from("format"), MsgValue::from(format)),
                ]));
            }
            fields.push((MsgValue::from("images"), MsgValue::Array(images)));
        }
        if let Some(audio) = &self.audio {
            let (data, format, sample_rate) = audio.resolve()?;
            fields.push((
                MsgValue::from("audio"),
                MsgValue::Map(vec![
                    (MsgValue::from("data"), MsgValue::Binary(data)),
                    (MsgValue::from("format"), optional_str(format)),
                    (
                        MsgValue::from("sample_rate"),
                        sample_rate.map_or(MsgValue::Nil, |rate| MsgValue::from(u64::from(rate))),
                    ),
                ]),
            ));
        }
        if let Some(video) = &self.video {
            let (data, format) = video.resolve()?;
            fields.push((
                MsgValue::from("video"),
                MsgValue::Map(vec![
                    (MsgValue::from("data"), MsgValue::Binary(data)),
                    (MsgValue::from("format"), optional_str(format)),
                ]),
            ));
        }
        if let Some(document) = &self.document {
            let (data, format) = document.resolve()?;
            fields.push((
                MsgValue::from("document"),
                MsgValue::Map(vec![
                    (MsgValue::from("data"), MsgValue::Binary(data)),
                    (MsgValue::from("format"), optional_str(format)),
                ]),
            ));
        }
        if let Some(metadata) = &self.metadata {
            fields.push((MsgValue::from("metadata"), json_to_msgpack(metadata)));
        }
        Ok(MsgValue::Map(fields))
    }
}

fn optional_str(value: Option<String>) -> MsgValue {
    value.map_or(MsgValue::Nil, MsgValue::from)
}

/// Translate a JSON value into its msgpack equivalent.
pub(crate) fn json_to_msgpack(value: &Value) -> MsgValue {
    match value {
        Value::Null => MsgValue::Nil,
        Value::Bool(flag) => MsgValue::Boolean(*flag),
        Value::Number(number) => number.as_i64().map_or_else(
            || {
                number.as_u64().map_or_else(
                    || MsgValue::from(number.as_f64().unwrap_or(0.0)),
                    MsgValue::from,
                )
            },
            MsgValue::from,
        ),
        Value::String(text) => MsgValue::from(text.as_str()),
        Value::Array(items) => MsgValue::Array(items.iter().map(json_to_msgpack).collect()),
        Value::Object(entries) => MsgValue::Map(
            entries
                .iter()
                .map(|(key, value)| (MsgValue::from(key.as_str()), json_to_msgpack(value)))
                .collect(),
        ),
    }
}

#[cfg(test)]
mod tests {
    // These assertions are about exact values, so exact comparison is the point.
    #![allow(clippy::float_cmp)]

    use super::*;
    use serde_json::json;

    fn field<'a>(value: &'a MsgValue, name: &str) -> Option<&'a MsgValue> {
        match value {
            MsgValue::Map(entries) => entries
                .iter()
                .find(|(key, _)| key.as_str() == Some(name))
                .map(|(_, value)| value),
            _ => None,
        }
    }

    fn png_bytes() -> Vec<u8> {
        let image = image::DynamicImage::ImageRgb8(image::RgbImage::new(2, 2));
        let mut buffer = std::io::Cursor::new(Vec::new());
        image
            .write_to(&mut buffer, image::ImageFormat::Png)
            .unwrap();
        buffer.into_inner()
    }

    #[test]
    fn a_text_item_emits_only_text() {
        let wire = Item::text("hello").to_msgpack().unwrap();
        assert_eq!(field(&wire, "text").unwrap().as_str(), Some("hello"));
        assert!(field(&wire, "images").is_none());
        assert!(field(&wire, "id").is_none());
    }

    #[test]
    fn encoded_images_pass_through_with_a_detected_format() {
        let png = png_bytes();
        let wire = Item::image(ImageInput::bytes(png.clone()))
            .to_msgpack()
            .unwrap();
        let images = field(&wire, "images").unwrap();
        let MsgValue::Array(images) = images else {
            panic!("expected an array")
        };
        assert_eq!(field(&images[0], "format").unwrap().as_str(), Some("png"));
        // The bytes are carried verbatim: an already-encoded image is never re-encoded.
        assert_eq!(
            field(&images[0], "data").unwrap(),
            &MsgValue::Binary(png),
            "image bytes were rewritten"
        );
    }

    #[test]
    fn decoded_images_become_jpeg() {
        let image = image::DynamicImage::ImageRgb8(image::RgbImage::new(2, 2));
        let (data, format) = ImageInput::decoded(image).resolve().unwrap();
        assert_eq!(format, "jpeg");
        assert_eq!(media::detect_image_format(&data).unwrap(), "jpeg");
    }

    #[test]
    fn a_declared_format_that_contradicts_the_bytes_is_rejected() {
        let err = ImageInput::bytes(png_bytes())
            .format("jpeg")
            .resolve()
            .unwrap_err();
        assert!(err.to_string().contains("mismatch"), "{err}");
        // The alias still matches its canonical form.
        assert!(
            ImageInput::bytes(png_bytes())
                .format("PNG")
                .resolve()
                .is_ok()
        );
    }

    #[test]
    fn waveforms_are_wrapped_in_wav() {
        let audio = AudioInput::waveform(Samples::F32(vec![0.0, 0.5, -0.5, 0.0]), 1, 16_000);
        let (data, format, sample_rate) = audio.resolve().unwrap();
        assert_eq!(format.as_deref(), Some("wav"));
        assert_eq!(sample_rate, Some(16_000));
        assert_eq!(&data[..4], b"RIFF");
    }

    #[test]
    fn document_format_is_inferred_from_the_path_but_never_overrides_a_declaration() {
        let dir = std::env::temp_dir().join("sie-sdk-item-tests");
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("report.pdf");
        std::fs::write(&path, b"%PDF-1.4").unwrap();

        let (data, format) = BinaryInput::document_path(&path).resolve().unwrap();
        assert_eq!(data, b"%PDF-1.4");
        assert_eq!(format.as_deref(), Some("pdf"));

        let (_, declared) = BinaryInput::document_path(&path)
            .format("txt")
            .resolve()
            .unwrap();
        assert_eq!(declared.as_deref(), Some("txt"));

        // In-memory documents carry no format unless the caller declares one.
        assert_eq!(
            BinaryInput::document_bytes(b"raw".to_vec())
                .resolve()
                .unwrap()
                .1,
            None
        );
        std::fs::remove_file(&path).unwrap();
    }

    #[test]
    fn a_missing_file_names_itself_in_the_error() {
        let err = ImageInput::path("/nonexistent/sie-sdk/image.png")
            .resolve()
            .unwrap_err();
        assert!(
            err.to_string().contains("/nonexistent/sie-sdk/image.png"),
            "{err}"
        );
    }

    #[test]
    fn metadata_translates_into_msgpack() {
        let wire = Item::text("x")
            .with_id("doc-1")
            .with_metadata(
                json!({"source": "web", "rank": 3, "tags": ["a"], "keep": null, "score": 1.5}),
            )
            .to_msgpack()
            .unwrap();
        assert_eq!(field(&wire, "id").unwrap().as_str(), Some("doc-1"));
        let metadata = field(&wire, "metadata").unwrap();
        assert_eq!(field(metadata, "source").unwrap().as_str(), Some("web"));
        assert_eq!(field(metadata, "rank").unwrap().as_i64(), Some(3));
        assert_eq!(field(metadata, "score").unwrap().as_f64(), Some(1.5));
        assert_eq!(field(metadata, "keep").unwrap(), &MsgValue::Nil);
    }
}