Skip to main content

ollama_vision/
lib.rs

1//! # ollama-vision
2//!
3//! Robust Ollama vision model toolkit for image tagging and captioning.
4//!
5//! ## Features
6//!
7//! - **Image tagging** with a 7-strategy response parser that handles every
8//!   common LLM output format (JSON arrays, code blocks, `<think>` tags,
9//!   JSON objects, numbered lists, comma-separated text)
10//! - **Image captioning** with automatic `<think>` block stripping
11//! - **Works with any Ollama vision model** (llava, minicpm-v, llama3.2-vision, etc.)
12//! - **Base64 API** for in-memory images (no file I/O required)
13//!
14//! ## Quick Start
15//!
16//! ```rust,no_run
17//! use ollama_vision::{OllamaVisionConfig, TagOptions, CaptionOptions};
18//! use std::path::Path;
19//!
20//! #[tokio::main]
21//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
22//!     let config = OllamaVisionConfig::with_model("llava");
23//!     let client = reqwest::Client::new();
24//!
25//!     // Tag an image
26//!     let tags = ollama_vision::tag_image(
27//!         &client, &config,
28//!         Path::new("photo.jpg"),
29//!         &TagOptions::default(),
30//!     ).await?;
31//!     println!("Tags: {:?}", tags);
32//!
33//!     // Caption an image
34//!     let caption = ollama_vision::caption_image(
35//!         &client, &config,
36//!         Path::new("photo.jpg"),
37//!         &CaptionOptions::default(),
38//!     ).await?;
39//!     println!("Caption: {}", caption);
40//!
41//!     Ok(())
42//! }
43//! ```
44//!
45//! ## Parsing Robustness
46//!
47//! The tag parser handles all common LLM response formats:
48//!
49//! ```rust
50//! use ollama_vision::parse_tags;
51//!
52//! // Pure JSON array
53//! assert!(parse_tags(r#"["portrait", "fantasy"]"#).is_ok());
54//!
55//! // With <think> blocks from reasoning models
56//! assert!(parse_tags(r#"<think>analyzing...</think>["portrait"]"#).is_ok());
57//!
58//! // Markdown code blocks
59//! assert!(parse_tags("```json\n[\"portrait\"]\n```").is_ok());
60//!
61//! // JSON objects with "tags" key
62//! assert!(parse_tags(r#"{"tags": ["portrait"]}"#).is_ok());
63//!
64//! // Comma-separated fallback
65//! assert!(parse_tags("portrait, fantasy, dark").is_ok());
66//! ```
67
68pub mod captioner;
69pub mod parser;
70pub mod tagger;
71pub mod types;
72
73// Re-export main types at crate root
74pub use captioner::{caption_image, caption_image_base64, CaptionError};
75pub use parser::{parse_tags, strip_think_tags, ParseError};
76pub use tagger::{tag_image, tag_image_base64, TagError};
77pub use types::{CaptionOptions, GenerateOptions, OllamaVisionConfig, TagOptions};