use super::vlm_ocr::vlm_ocr;
use crate::core::config::LlmConfig;
use crate::types::{LlmUsage, RegionKind};
pub async fn extract_region_with_vlm(
image_bytes: &[u8],
image_mime: &str,
region_kind: RegionKind,
llm_config: &LlmConfig,
custom_prompt: Option<&str>,
) -> crate::Result<String> {
let (text, _usage) =
extract_region_with_vlm_usage(image_bytes, image_mime, region_kind, llm_config, custom_prompt).await?;
Ok(text)
}
#[cfg_attr(alef, alef(skip))]
pub async fn extract_region_with_vlm_usage(
image_bytes: &[u8],
image_mime: &str,
region_kind: RegionKind,
llm_config: &LlmConfig,
custom_prompt: Option<&str>,
) -> crate::Result<(String, Option<LlmUsage>)> {
let prompt = custom_prompt.unwrap_or_else(|| region_kind.default_prompt());
vlm_ocr(image_bytes, image_mime, "eng", llm_config, Some(prompt)).await
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_region_kind_default_prompt_figure() {
let prompt = RegionKind::Figure.default_prompt();
assert!(
prompt.contains("diagram") || prompt.contains("figure"),
"figure prompt must mention figures/diagrams; got: {prompt}"
);
}
#[test]
fn test_region_kind_default_prompt_dense_table() {
let prompt = RegionKind::DenseTable.default_prompt();
assert!(
prompt.contains("Markdown") || prompt.contains("table"),
"dense table prompt must mention Markdown/table; got: {prompt}"
);
}
#[test]
fn test_region_kind_default_prompt_complex_layout() {
let prompt = RegionKind::ComplexLayout.default_prompt();
assert!(
prompt.contains("Markdown") || prompt.contains("reading order"),
"complex layout prompt must mention Markdown; got: {prompt}"
);
}
#[test]
fn test_region_kind_prompts_are_non_empty() {
for kind in [
RegionKind::Figure,
RegionKind::DenseTable,
RegionKind::ComplexLayout,
RegionKind::Caption,
] {
assert!(
!kind.default_prompt().is_empty(),
"{kind:?} default prompt must not be empty"
);
}
}
#[test]
fn test_region_kind_default_prompt_caption() {
let prompt = RegionKind::Caption.default_prompt();
assert!(
prompt.contains("caption") || prompt.contains("alt text"),
"caption prompt must mention captions/alt text; got: {prompt}"
);
}
#[test]
fn test_region_kind_equality() {
assert_eq!(RegionKind::Figure, RegionKind::Figure);
assert_ne!(RegionKind::Figure, RegionKind::DenseTable);
assert_ne!(RegionKind::DenseTable, RegionKind::ComplexLayout);
}
}