Skip to content

Commit e1f1e04

Browse files
committed
Add some doctests
Signed-off-by: Dennis Keck <[email protected]>
1 parent eee15c3 commit e1f1e04

File tree

1 file changed

+93
-2
lines changed

1 file changed

+93
-2
lines changed

llama-cpp-2/src/mtmd.rs

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ use crate::model::LlamaModel;
1414
use crate::token::LlamaToken;
1515

1616
/// 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+
/// ```
1730
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1831
pub enum MtmdInputChunkType {
1932
/// Text input chunk
@@ -36,6 +49,20 @@ impl From<llama_cpp_sys_2::mtmd_input_chunk_type> for MtmdInputChunkType {
3649
}
3750

3851
/// 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+
/// ```
3966
#[derive(Debug, Clone)]
4067
pub struct MtmdContextParams {
4168
/// Whether to use GPU acceleration
@@ -73,6 +100,18 @@ impl From<&MtmdContextParams> for llama_cpp_sys_2::mtmd_context_params {
73100
}
74101

75102
/// 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+
/// ```
76115
#[derive(Debug, Clone)]
77116
pub struct MtmdInputText {
78117
/// The input text string
@@ -301,6 +340,19 @@ impl MtmdBitmap {
301340
///
302341
/// * `InvalidDataSize` - Data length doesn't match `nx * ny * 3`
303342
/// * `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+
/// ```
304356
pub fn from_image_data(nx: u32, ny: u32, data: &[u8]) -> Result<Self, MtmdBitmapError> {
305357
if data.len() != (nx * ny * 3) as usize {
306358
return Err(MtmdBitmapError::InvalidDataSize);
@@ -325,6 +377,20 @@ impl MtmdBitmap {
325377
/// # Errors
326378
///
327379
/// * `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+
/// ```
328394
pub fn from_audio_data(data: &[f32]) -> Result<Self, MtmdBitmapError> {
329395
let bitmap =
330396
unsafe { llama_cpp_sys_2::mtmd_bitmap_init_from_audio(data.len(), data.as_ptr()) };
@@ -457,6 +523,17 @@ impl MtmdBitmap {
457523
/// # Errors
458524
///
459525
/// 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+
/// ```
460537
pub fn set_id(&self, id: &str) -> Result<(), std::ffi::NulError> {
461538
let id_cstr = CString::new(id)?;
462539
unsafe {
@@ -493,6 +570,16 @@ impl MtmdInputChunks {
493570
/// # Panics
494571
/// This function will panic if the underlying llama.cpp function returns null,
495572
/// 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+
/// ```
496583
#[must_use] pub fn new() -> Self {
497584
let chunks = unsafe { llama_cpp_sys_2::mtmd_input_chunks_init() };
498585
let chunks = NonNull::new(chunks).unwrap();
@@ -721,12 +808,16 @@ impl Drop for MtmdInputChunk {
721808
///
722809
/// Returns the default media marker as a string slice.
723810
///
724-
/// # Example
811+
/// # Examples
725812
///
726813
/// ```
727-
/// # use llama_cpp_2::mtmd::mtmd_default_marker;
814+
/// use llama_cpp_2::mtmd::mtmd_default_marker;
815+
///
728816
/// let marker = mtmd_default_marker();
817+
/// assert!(!marker.is_empty());
818+
///
729819
/// let text = format!("Describe this image: {}", marker);
820+
/// assert!(text.contains(marker));
730821
/// ```
731822
#[must_use] pub fn mtmd_default_marker() -> &'static str {
732823
unsafe {

0 commit comments

Comments
 (0)