@@ -14,6 +14,19 @@ use crate::model::LlamaModel;
14
14
use crate :: token:: LlamaToken ;
15
15
16
16
/// Input chunk types for multimodal data
17
+ ///
18
+ /// # Examples
19
+ ///
20
+ /// ```
21
+ /// use llama_cpp_2::mtmd::MtmdInputChunkType;
22
+ ///
23
+ /// let text_chunk = MtmdInputChunkType::Text;
24
+ /// let image_chunk = MtmdInputChunkType::Image;
25
+ /// let audio_chunk = MtmdInputChunkType::Audio;
26
+ ///
27
+ /// assert_eq!(text_chunk, MtmdInputChunkType::Text);
28
+ /// assert_ne!(text_chunk, image_chunk);
29
+ /// ```
17
30
#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
18
31
pub enum MtmdInputChunkType {
19
32
/// Text input chunk
@@ -36,6 +49,20 @@ impl From<llama_cpp_sys_2::mtmd_input_chunk_type> for MtmdInputChunkType {
36
49
}
37
50
38
51
/// Configuration parameters for MTMD context
52
+ ///
53
+ /// # Examples
54
+ ///
55
+ /// ```
56
+ /// use llama_cpp_2::mtmd::{MtmdContextParams, mtmd_default_marker};
57
+ /// use std::ffi::CString;
58
+ ///
59
+ /// let params = MtmdContextParams {
60
+ /// use_gpu: false,
61
+ /// print_timings: true,
62
+ /// n_threads: 4,
63
+ /// media_marker: CString::new(mtmd_default_marker()).unwrap(),
64
+ /// };
65
+ /// ```
39
66
#[ derive( Debug , Clone ) ]
40
67
pub struct MtmdContextParams {
41
68
/// Whether to use GPU acceleration
@@ -73,6 +100,18 @@ impl From<&MtmdContextParams> for llama_cpp_sys_2::mtmd_context_params {
73
100
}
74
101
75
102
/// Text input configuration
103
+ ///
104
+ /// # Examples
105
+ ///
106
+ /// ```
107
+ /// use llama_cpp_2::mtmd::MtmdInputText;
108
+ ///
109
+ /// let input = MtmdInputText {
110
+ /// text: "Describe this image.".to_string(),
111
+ /// add_special: true,
112
+ /// parse_special: true,
113
+ /// };
114
+ /// ```
76
115
#[ derive( Debug , Clone ) ]
77
116
pub struct MtmdInputText {
78
117
/// The input text string
@@ -301,6 +340,19 @@ impl MtmdBitmap {
301
340
///
302
341
/// * `InvalidDataSize` - Data length doesn't match `nx * ny * 3`
303
342
/// * `NullResult` - Underlying C function returned null
343
+ ///
344
+ /// # Examples
345
+ ///
346
+ /// ```
347
+ /// use llama_cpp_2::mtmd::MtmdBitmap;
348
+ ///
349
+ /// // Create a 2x2 red image
350
+ /// let red_pixel = [255, 0, 0]; // RGB values for red
351
+ /// let image_data = red_pixel.repeat(4); // 2x2 = 4 pixels
352
+ ///
353
+ /// let bitmap = MtmdBitmap::from_image_data(2, 2, &image_data);
354
+ /// assert!(bitmap.is_ok());
355
+ /// ```
304
356
pub fn from_image_data ( nx : u32 , ny : u32 , data : & [ u8 ] ) -> Result < Self , MtmdBitmapError > {
305
357
if data. len ( ) != ( nx * ny * 3 ) as usize {
306
358
return Err ( MtmdBitmapError :: InvalidDataSize ) ;
@@ -325,6 +377,20 @@ impl MtmdBitmap {
325
377
/// # Errors
326
378
///
327
379
/// * `NullResult` - Underlying C function returned null
380
+ ///
381
+ /// # Examples
382
+ ///
383
+ /// ```
384
+ /// use llama_cpp_2::mtmd::MtmdBitmap;
385
+ ///
386
+ /// // Create a simple sine wave audio sample
387
+ /// let audio_data: Vec<f32> = (0..100)
388
+ /// .map(|i| (i as f32 * 0.1).sin())
389
+ /// .collect();
390
+ ///
391
+ /// let bitmap = MtmdBitmap::from_audio_data(&audio_data);
392
+ /// // Note: This will likely fail without proper MTMD context setup
393
+ /// ```
328
394
pub fn from_audio_data ( data : & [ f32 ] ) -> Result < Self , MtmdBitmapError > {
329
395
let bitmap =
330
396
unsafe { llama_cpp_sys_2:: mtmd_bitmap_init_from_audio ( data. len ( ) , data. as_ptr ( ) ) } ;
@@ -457,6 +523,17 @@ impl MtmdBitmap {
457
523
/// # Errors
458
524
///
459
525
/// Returns an error if the ID string contains null bytes.
526
+ ///
527
+ /// # Examples
528
+ ///
529
+ /// ```no_run
530
+ /// # use llama_cpp_2::mtmd::MtmdBitmap;
531
+ /// # fn example(bitmap: &MtmdBitmap) -> Result<(), Box<dyn std::error::Error>> {
532
+ /// bitmap.set_id("image_001")?;
533
+ /// assert_eq!(bitmap.id(), Some("image_001".to_string()));
534
+ /// # Ok(())
535
+ /// # }
536
+ /// ```
460
537
pub fn set_id ( & self , id : & str ) -> Result < ( ) , std:: ffi:: NulError > {
461
538
let id_cstr = CString :: new ( id) ?;
462
539
unsafe {
@@ -493,6 +570,16 @@ impl MtmdInputChunks {
493
570
/// # Panics
494
571
/// This function will panic if the underlying llama.cpp function returns null,
495
572
/// which should not happen.
573
+ ///
574
+ /// # Examples
575
+ ///
576
+ /// ```
577
+ /// use llama_cpp_2::mtmd::MtmdInputChunks;
578
+ ///
579
+ /// let chunks = MtmdInputChunks::new();
580
+ /// assert_eq!(chunks.len(), 0);
581
+ /// assert!(chunks.is_empty());
582
+ /// ```
496
583
#[ must_use] pub fn new ( ) -> Self {
497
584
let chunks = unsafe { llama_cpp_sys_2:: mtmd_input_chunks_init ( ) } ;
498
585
let chunks = NonNull :: new ( chunks) . unwrap ( ) ;
@@ -721,12 +808,16 @@ impl Drop for MtmdInputChunk {
721
808
///
722
809
/// Returns the default media marker as a string slice.
723
810
///
724
- /// # Example
811
+ /// # Examples
725
812
///
726
813
/// ```
727
- /// # use llama_cpp_2::mtmd::mtmd_default_marker;
814
+ /// use llama_cpp_2::mtmd::mtmd_default_marker;
815
+ ///
728
816
/// let marker = mtmd_default_marker();
817
+ /// assert!(!marker.is_empty());
818
+ ///
729
819
/// let text = format!("Describe this image: {}", marker);
820
+ /// assert!(text.contains(marker));
730
821
/// ```
731
822
#[ must_use] pub fn mtmd_default_marker ( ) -> & ' static str {
732
823
unsafe {
0 commit comments