mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
refactor(ocr): declare formats and share transformation plumbing
This commit is contained in:
parent
afc4080e8f
commit
c6033b32c1
15 changed files with 588 additions and 537 deletions
|
|
@ -62,4 +62,5 @@ pub(crate) const MISTRAL_OCR_API_BASE: &str = "https://api.mistral.ai/v1";
|
|||
pub(crate) const REDUCTO_OCR_API_BASE: &str = "https://platform.reducto.ai";
|
||||
pub(crate) const REDUCTO_ID_PREFIX: &str = "reducto://";
|
||||
pub(crate) const VERTEX_OCR_DEFAULT_LOCATION: &str = "us-central1";
|
||||
pub(crate) const DEEPSEEK_OCR_MODEL_NAMESPACE: &str = "deepseek-ai";
|
||||
pub(crate) const VERTEX_DEEPSEEK_API_BASE: &str = "https://aiplatform.googleapis.com";
|
||||
|
|
|
|||
|
|
@ -1,112 +1,16 @@
|
|||
mod transformation;
|
||||
pub mod types;
|
||||
|
||||
use self::types::*;
|
||||
use crate::ocr::error::OcrRequestError;
|
||||
use crate::ocr::error::OcrResponseError;
|
||||
use crate::ocr::formats::OcrFormat;
|
||||
use crate::ocr::types::{OcrDocument, OcrPage, OcrResponseData};
|
||||
use serde::de::IntoDeserializer;
|
||||
use serde_json::Value;
|
||||
use types::*;
|
||||
|
||||
pub struct DeepSeekOcrFormat;
|
||||
use crate::ocr::formats::ocr_format;
|
||||
use crate::ocr::types::OcrDocument;
|
||||
|
||||
fn decode_content(text: &str) -> Result<Option<DeepSeekOcrResult>, OcrResponseError> {
|
||||
match serde_json::from_str::<Value>(text) {
|
||||
Err(_) => Ok(None),
|
||||
Ok(value) => serde_path_to_error::deserialize(value.into_deserializer())
|
||||
.map(Some)
|
||||
.map_err(|error| OcrResponseError::ResponseField {
|
||||
path: format!("choices[0].message.content.{}", error.path()),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
impl OcrFormat for DeepSeekOcrFormat {
|
||||
type InputParams = DeepSeekOcrParams;
|
||||
type MappedParams = DeepSeekOcrParams;
|
||||
type PreparedDocument = OcrDocument;
|
||||
type RequestBody = DeepSeekOcrRequest;
|
||||
type ResponseBody = DeepSeekOcrResponse;
|
||||
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
|
||||
fn map_params(&self, params: Self::InputParams) -> Result<Self::MappedParams, OcrRequestError> {
|
||||
Ok(params)
|
||||
}
|
||||
|
||||
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
|
||||
fn transform_request(
|
||||
&self,
|
||||
model: &str,
|
||||
ocr_format! {
|
||||
pub DeepSeekOcrFormat {
|
||||
params: DeepSeekOcrParams,
|
||||
document: OcrDocument,
|
||||
params: &Self::MappedParams,
|
||||
) -> Result<Self::RequestBody, OcrRequestError> {
|
||||
if document.source().is_empty() {
|
||||
return Err(OcrRequestError::MissingField("document URL"));
|
||||
}
|
||||
let model = if model.contains('/') {
|
||||
model.to_string()
|
||||
} else {
|
||||
format!("deepseek-ai/{model}")
|
||||
};
|
||||
Ok(DeepSeekOcrRequest {
|
||||
model,
|
||||
messages: vec![DeepSeekOcrMessage {
|
||||
role: UserRole::User,
|
||||
content: vec![document],
|
||||
}],
|
||||
params: params.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
|
||||
fn transform_response(
|
||||
&self,
|
||||
model: &str,
|
||||
response: Self::ResponseBody,
|
||||
_params: &Self::MappedParams,
|
||||
) -> Result<OcrResponseData, OcrResponseError> {
|
||||
let content = response
|
||||
.choices
|
||||
.into_iter()
|
||||
.next()
|
||||
.and_then(|choice| choice.message.content)
|
||||
.ok_or(OcrResponseError::EmptyContent)?;
|
||||
let (result, fallback) = match content {
|
||||
DeepSeekContent::Text(text) if text.is_empty() => {
|
||||
return Err(OcrResponseError::EmptyContent);
|
||||
}
|
||||
DeepSeekContent::Text(text) => {
|
||||
let result = if text.trim_start().starts_with('{') {
|
||||
decode_content(&text)?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
(result, text)
|
||||
}
|
||||
DeepSeekContent::Object(object) => {
|
||||
let fallback = serde_json::to_string(&object).map_err(|_| {
|
||||
OcrResponseError::ResponseField {
|
||||
path: "choices[0].message.content".into(),
|
||||
}
|
||||
})?;
|
||||
(Some(object), fallback)
|
||||
}
|
||||
};
|
||||
let result = result.unwrap_or_default();
|
||||
let pages = match result.pages {
|
||||
Some(pages) if !pages.is_empty() => pages
|
||||
.into_iter()
|
||||
.map(|page| OcrPage {
|
||||
images: page.images,
|
||||
dimensions: page.dimensions,
|
||||
..OcrPage::text(page.index, page.markdown)
|
||||
})
|
||||
.collect(),
|
||||
_ => vec![OcrPage::text(0, fallback)],
|
||||
};
|
||||
Ok(OcrResponseData {
|
||||
document_annotation: result.document_annotation,
|
||||
usage_info: result.usage_info.or(response.usage),
|
||||
..OcrResponseData::new(result.model.unwrap_or_else(|| model.to_string()), pages)
|
||||
})
|
||||
request: DeepSeekOcrRequest => transformation::request,
|
||||
response: DeepSeekOcrResponse => transformation::response,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,106 @@
|
|||
use serde::de::IntoDeserializer;
|
||||
use serde_json::Value;
|
||||
|
||||
use super::types::*;
|
||||
use crate::constants::DEEPSEEK_OCR_MODEL_NAMESPACE;
|
||||
use crate::ocr::error::{OcrRequestError, OcrResponseError};
|
||||
use crate::ocr::types::{OcrDocument, OcrPage, OcrResponseData};
|
||||
|
||||
fn decode_json_content(text: &str) -> Result<Option<DeepSeekOcrResult>, OcrResponseError> {
|
||||
match serde_json::from_str::<Value>(text) {
|
||||
Err(_) => Ok(None),
|
||||
Ok(value) => serde_path_to_error::deserialize(value.into_deserializer())
|
||||
.map(Some)
|
||||
.map_err(|error| OcrResponseError::ResponseField {
|
||||
path: format!("choices[0].message.content.{}", error.path()),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn request(
|
||||
model: &str,
|
||||
document: OcrDocument,
|
||||
params: &DeepSeekOcrParams,
|
||||
) -> Result<DeepSeekOcrRequest, OcrRequestError> {
|
||||
if document.source().is_empty() {
|
||||
return Err(OcrRequestError::MissingField("document URL"));
|
||||
}
|
||||
let model = if model.contains('/') {
|
||||
model.to_string()
|
||||
} else {
|
||||
format!("{DEEPSEEK_OCR_MODEL_NAMESPACE}/{model}")
|
||||
};
|
||||
Ok(DeepSeekOcrRequest {
|
||||
model,
|
||||
messages: vec![DeepSeekOcrMessage {
|
||||
role: UserRole::User,
|
||||
content: vec![document],
|
||||
}],
|
||||
params: params.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn response(
|
||||
model: &str,
|
||||
response: DeepSeekOcrResponse,
|
||||
_params: &DeepSeekOcrParams,
|
||||
) -> Result<OcrResponseData, OcrResponseError> {
|
||||
let content = response
|
||||
.choices
|
||||
.into_iter()
|
||||
.next()
|
||||
.and_then(|choice| choice.message.content)
|
||||
.ok_or(OcrResponseError::EmptyContent)?;
|
||||
let DecodedContent {
|
||||
result,
|
||||
fallback_markdown,
|
||||
} = decode_content(content)?;
|
||||
let pages = match result.pages {
|
||||
Some(pages) if !pages.is_empty() => pages
|
||||
.into_iter()
|
||||
.map(|page| OcrPage {
|
||||
images: page.images,
|
||||
dimensions: page.dimensions,
|
||||
..OcrPage::text(page.index, page.markdown)
|
||||
})
|
||||
.collect(),
|
||||
_ => vec![OcrPage::text(0, fallback_markdown)],
|
||||
};
|
||||
Ok(OcrResponseData {
|
||||
document_annotation: result.document_annotation,
|
||||
usage_info: result.usage_info.or(response.usage),
|
||||
..OcrResponseData::new(result.model.unwrap_or_else(|| model.to_string()), pages)
|
||||
})
|
||||
}
|
||||
|
||||
struct DecodedContent {
|
||||
result: DeepSeekOcrResult,
|
||||
fallback_markdown: String,
|
||||
}
|
||||
|
||||
fn decode_content(content: DeepSeekContent) -> Result<DecodedContent, OcrResponseError> {
|
||||
let (result, fallback) = match content {
|
||||
DeepSeekContent::Text(text) if text.is_empty() => {
|
||||
return Err(OcrResponseError::EmptyContent);
|
||||
}
|
||||
DeepSeekContent::Text(text) => {
|
||||
let result = if text.trim_start().starts_with('{') {
|
||||
decode_json_content(&text)?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
(result, text)
|
||||
}
|
||||
DeepSeekContent::Object(object) => {
|
||||
let fallback =
|
||||
serde_json::to_string(&object).map_err(|_| OcrResponseError::ResponseField {
|
||||
path: "choices[0].message.content".into(),
|
||||
})?;
|
||||
(Some(object), fallback)
|
||||
}
|
||||
};
|
||||
Ok(DecodedContent {
|
||||
result: result.unwrap_or_default(),
|
||||
fallback_markdown: fallback,
|
||||
})
|
||||
}
|
||||
|
|
@ -1,244 +1,19 @@
|
|||
use crate::ocr::document::InlineDocument;
|
||||
use crate::ocr::error::OcrRequestError;
|
||||
use crate::ocr::error::OcrResponseError;
|
||||
use crate::ocr::error::PagesError;
|
||||
use base64::{Engine, engine::general_purpose::STANDARD};
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
mod params;
|
||||
mod transformation;
|
||||
pub mod types;
|
||||
|
||||
use self::types::*;
|
||||
use crate::constants::{AZURE_DI_DEFAULT_DPI, AZURE_DI_DEFAULT_HEIGHT, AZURE_DI_DEFAULT_WIDTH};
|
||||
use crate::ocr::formats::OcrFormat;
|
||||
use crate::ocr::types::{OcrDocument, OcrPage, OcrPageDimensions, OcrResponseData, OcrUsageInfo};
|
||||
use serde_json::{Map, Value};
|
||||
use types::*;
|
||||
|
||||
fn validate_input_params(params: &Map<String, Value>) -> Result<(), OcrRequestError> {
|
||||
let Some(Value::Array(pages)) = params.get("pages") else {
|
||||
return Ok(());
|
||||
};
|
||||
if pages.iter().any(Value::is_boolean) {
|
||||
return Err(PagesError::BooleanIndex.into());
|
||||
}
|
||||
if pages
|
||||
.iter()
|
||||
.any(|page| page.is_number() && page.as_i64().is_none())
|
||||
{
|
||||
return Err(PagesError::IndexOutOfRange.into());
|
||||
}
|
||||
if !pages.iter().all(Value::is_i64) && !pages.iter().all(Value::is_string) {
|
||||
return Err(PagesError::MixedElementTypes.into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
use crate::ocr::formats::ocr_format;
|
||||
use crate::ocr::types::OcrDocument;
|
||||
|
||||
fn pages_token_is_valid(token: &str) -> bool {
|
||||
let mut parts = token.split('-');
|
||||
let start = parts.next().unwrap_or_default();
|
||||
if start.is_empty() || !start.chars().all(|ch| ch.is_ascii_digit()) {
|
||||
return false;
|
||||
}
|
||||
match parts.next() {
|
||||
None => true,
|
||||
Some(end) => {
|
||||
!end.is_empty() && end.chars().all(|ch| ch.is_ascii_digit()) && parts.next().is_none()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize_pages_param(pages: PagesInput) -> Result<Option<NormalizedPages>, PagesError> {
|
||||
let normalized = match pages {
|
||||
PagesInput::ZeroBasedIndices(indices) => {
|
||||
if indices.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
indices
|
||||
.into_iter()
|
||||
.map(|page| {
|
||||
if page < 0 {
|
||||
return Err(PagesError::NegativeIndex);
|
||||
}
|
||||
page.checked_add(1).ok_or(PagesError::IndexOutOfRange)
|
||||
})
|
||||
.collect::<Result<BTreeSet<_>, _>>()?
|
||||
.into_iter()
|
||||
.map(|page| page.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(",")
|
||||
}
|
||||
PagesInput::NativeTokens(tokens) => {
|
||||
if tokens.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
tokens
|
||||
.iter()
|
||||
.map(|token| token.trim())
|
||||
.collect::<Vec<_>>()
|
||||
.join(",")
|
||||
}
|
||||
PagesInput::NativeRange(range) => range
|
||||
.split(',')
|
||||
.map(str::trim)
|
||||
.collect::<Vec<_>>()
|
||||
.join(","),
|
||||
};
|
||||
if !normalized.split(',').all(pages_token_is_valid) {
|
||||
return Err(PagesError::InvalidNativeRange);
|
||||
}
|
||||
Ok(Some(NormalizedPages(normalized)))
|
||||
}
|
||||
|
||||
fn normalize_features_param(
|
||||
features: FeaturesInput,
|
||||
) -> Result<Option<NormalizedFeatures>, OcrRequestError> {
|
||||
let tokens = match features {
|
||||
FeaturesInput::Names(names) => names,
|
||||
FeaturesInput::CommaSeparated(names) => names.split(',').map(str::to_string).collect(),
|
||||
};
|
||||
if tokens.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let normalized = tokens.iter().map(|token| token.trim()).collect::<Vec<_>>();
|
||||
if !normalized.iter().all(|token| {
|
||||
let Some((first, rest)) = token.as_bytes().split_first() else {
|
||||
return false;
|
||||
};
|
||||
first.is_ascii_alphabetic() && rest.iter().all(u8::is_ascii_alphanumeric)
|
||||
}) {
|
||||
return Err(OcrRequestError::Features);
|
||||
}
|
||||
Ok(Some(NormalizedFeatures(normalized.join(","))))
|
||||
}
|
||||
|
||||
fn pixel_dimension(value: f64, scale: f64, field: &'static str) -> Result<i64, OcrResponseError> {
|
||||
let pixels = (value * scale).trunc();
|
||||
if !pixels.is_finite() || pixels < i64::MIN as f64 || pixels >= -(i64::MIN as f64) {
|
||||
return Err(OcrResponseError::NumericRange(field));
|
||||
}
|
||||
Ok(pixels as i64)
|
||||
}
|
||||
|
||||
fn convert_dimensions(
|
||||
width: f64,
|
||||
height: f64,
|
||||
unit: &str,
|
||||
) -> Result<OcrPageDimensions, OcrResponseError> {
|
||||
let scale = if unit == "inch" {
|
||||
AZURE_DI_DEFAULT_DPI as f64
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
Ok(OcrPageDimensions {
|
||||
dpi: Some(AZURE_DI_DEFAULT_DPI),
|
||||
width: Some(pixel_dimension(width, scale, "page.width")?),
|
||||
height: Some(pixel_dimension(height, scale, "page.height")?),
|
||||
})
|
||||
}
|
||||
|
||||
fn page_markdown(lines: &[AzureDocumentIntelligenceLine]) -> String {
|
||||
lines
|
||||
.iter()
|
||||
.map(|line| line.content.as_deref().unwrap_or_default())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
fn transform_azure_page(page: AzureDocumentIntelligencePage) -> Result<OcrPage, OcrResponseError> {
|
||||
let index = page
|
||||
.page_number
|
||||
.unwrap_or(1)
|
||||
.checked_sub(1)
|
||||
.ok_or(OcrResponseError::NumericRange("page.pageNumber"))?;
|
||||
Ok(OcrPage {
|
||||
dimensions: Some(convert_dimensions(
|
||||
page.width.unwrap_or(AZURE_DI_DEFAULT_WIDTH),
|
||||
page.height.unwrap_or(AZURE_DI_DEFAULT_HEIGHT),
|
||||
page.unit.as_deref().unwrap_or("inch"),
|
||||
)?),
|
||||
..OcrPage::text(index, page_markdown(&page.lines))
|
||||
})
|
||||
}
|
||||
|
||||
pub struct AzureDocumentIntelligenceOcrFormat;
|
||||
|
||||
impl OcrFormat for AzureDocumentIntelligenceOcrFormat {
|
||||
type InputParams = DocumentIntelligenceInputParams;
|
||||
type MappedParams = DocumentIntelligenceParams;
|
||||
type PreparedDocument = OcrDocument;
|
||||
type RequestBody = DocumentIntelligenceRequest;
|
||||
type ResponseBody = AzureDocumentIntelligenceOperation;
|
||||
|
||||
fn validate_input_params(params: &Map<String, Value>) -> Result<(), OcrRequestError> {
|
||||
validate_input_params(params)
|
||||
}
|
||||
|
||||
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
|
||||
fn map_params(&self, params: Self::InputParams) -> Result<Self::MappedParams, OcrRequestError> {
|
||||
Ok(DocumentIntelligenceParams {
|
||||
pages: params
|
||||
.pages
|
||||
.map(normalize_pages_param)
|
||||
.transpose()?
|
||||
.flatten(),
|
||||
features: params
|
||||
.features
|
||||
.map(normalize_features_param)
|
||||
.transpose()?
|
||||
.flatten(),
|
||||
request_format: params.req_format.unwrap_or_default(),
|
||||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
|
||||
fn transform_request(
|
||||
&self,
|
||||
_model: &str,
|
||||
ocr_format! {
|
||||
pub AzureDocumentIntelligenceOcrFormat {
|
||||
params: DocumentIntelligenceInputParams => DocumentIntelligenceParams,
|
||||
validate: params::validate,
|
||||
map: params::normalize,
|
||||
document: OcrDocument,
|
||||
_params: &Self::MappedParams,
|
||||
) -> Result<DocumentIntelligenceRequest, OcrRequestError> {
|
||||
let source = document.source();
|
||||
if source.is_empty() {
|
||||
return Err(OcrRequestError::MissingField("document URL"));
|
||||
}
|
||||
Ok(if let Some(document) = InlineDocument::parse(source)? {
|
||||
DocumentIntelligenceRequest::Base64Source(
|
||||
STANDARD.encode(document.decode(crate::constants::OCR_INLINE_MAX_BYTES)?),
|
||||
)
|
||||
} else {
|
||||
DocumentIntelligenceRequest::UrlSource(source.to_string())
|
||||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
|
||||
fn transform_response(
|
||||
&self,
|
||||
model: &str,
|
||||
response: Self::ResponseBody,
|
||||
_params: &Self::MappedParams,
|
||||
) -> Result<OcrResponseData, OcrResponseError> {
|
||||
if response.status != Some(OperationStatus::Succeeded) {
|
||||
return Err(OcrResponseError::OperationStatus(
|
||||
response
|
||||
.status
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or_else(|| "None".into()),
|
||||
));
|
||||
}
|
||||
let result = response.analyze_result.unwrap_or_default();
|
||||
let pages = result
|
||||
.pages
|
||||
.into_iter()
|
||||
.map(transform_azure_page)
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
Ok(OcrResponseData {
|
||||
usage_info: Some(OcrUsageInfo {
|
||||
pages_processed: Some(pages.len() as i64),
|
||||
..Default::default()
|
||||
}),
|
||||
content: result.content,
|
||||
tables: result.tables,
|
||||
key_value_pairs: result.key_value_pairs,
|
||||
..OcrResponseData::new(model.to_string(), pages)
|
||||
})
|
||||
request: DocumentIntelligenceRequest => transformation::request,
|
||||
response: AzureDocumentIntelligenceOperation => transformation::response,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,121 @@
|
|||
use std::collections::BTreeSet;
|
||||
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
use super::types::*;
|
||||
use crate::ocr::error::{OcrRequestError, PagesError};
|
||||
|
||||
pub(super) fn validate(params: &Map<String, Value>) -> Result<(), OcrRequestError> {
|
||||
let Some(Value::Array(pages)) = params.get("pages") else {
|
||||
return Ok(());
|
||||
};
|
||||
if pages.iter().any(Value::is_boolean) {
|
||||
return Err(PagesError::BooleanIndex.into());
|
||||
}
|
||||
if pages
|
||||
.iter()
|
||||
.any(|page| page.is_number() && page.as_i64().is_none())
|
||||
{
|
||||
return Err(PagesError::IndexOutOfRange.into());
|
||||
}
|
||||
if !pages.iter().all(Value::is_i64) && !pages.iter().all(Value::is_string) {
|
||||
return Err(PagesError::MixedElementTypes.into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn pages_token_is_valid(token: &str) -> bool {
|
||||
let mut parts = token.split('-');
|
||||
let start = parts.next().unwrap_or_default();
|
||||
if start.is_empty() || !start.chars().all(|ch| ch.is_ascii_digit()) {
|
||||
return false;
|
||||
}
|
||||
match parts.next() {
|
||||
None => true,
|
||||
Some(end) => {
|
||||
!end.is_empty() && end.chars().all(|ch| ch.is_ascii_digit()) && parts.next().is_none()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize_pages_param(pages: PagesInput) -> Result<Option<NormalizedPages>, PagesError> {
|
||||
let normalized = match pages {
|
||||
PagesInput::ZeroBasedIndices(indices) => {
|
||||
if indices.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
indices
|
||||
.into_iter()
|
||||
.map(|page| {
|
||||
if page < 0 {
|
||||
return Err(PagesError::NegativeIndex);
|
||||
}
|
||||
page.checked_add(1).ok_or(PagesError::IndexOutOfRange)
|
||||
})
|
||||
.collect::<Result<BTreeSet<_>, _>>()?
|
||||
.into_iter()
|
||||
.map(|page| page.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(",")
|
||||
}
|
||||
PagesInput::NativeTokens(tokens) => {
|
||||
if tokens.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
tokens
|
||||
.iter()
|
||||
.map(|token| token.trim())
|
||||
.collect::<Vec<_>>()
|
||||
.join(",")
|
||||
}
|
||||
PagesInput::NativeRange(range) => range
|
||||
.split(',')
|
||||
.map(str::trim)
|
||||
.collect::<Vec<_>>()
|
||||
.join(","),
|
||||
};
|
||||
if !normalized.split(',').all(pages_token_is_valid) {
|
||||
return Err(PagesError::InvalidNativeRange);
|
||||
}
|
||||
Ok(Some(NormalizedPages(normalized)))
|
||||
}
|
||||
|
||||
fn normalize_features_param(
|
||||
features: FeaturesInput,
|
||||
) -> Result<Option<NormalizedFeatures>, OcrRequestError> {
|
||||
let tokens = match features {
|
||||
FeaturesInput::Names(names) => names,
|
||||
FeaturesInput::CommaSeparated(names) => names.split(',').map(str::to_string).collect(),
|
||||
};
|
||||
if tokens.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let normalized = tokens.iter().map(|token| token.trim()).collect::<Vec<_>>();
|
||||
if !normalized.iter().all(|token| {
|
||||
let Some((first, rest)) = token.as_bytes().split_first() else {
|
||||
return false;
|
||||
};
|
||||
first.is_ascii_alphabetic() && rest.iter().all(u8::is_ascii_alphanumeric)
|
||||
}) {
|
||||
return Err(OcrRequestError::Features);
|
||||
}
|
||||
Ok(Some(NormalizedFeatures(normalized.join(","))))
|
||||
}
|
||||
|
||||
pub(super) fn normalize(
|
||||
params: DocumentIntelligenceInputParams,
|
||||
) -> Result<DocumentIntelligenceParams, OcrRequestError> {
|
||||
Ok(DocumentIntelligenceParams {
|
||||
pages: params
|
||||
.pages
|
||||
.map(normalize_pages_param)
|
||||
.transpose()?
|
||||
.flatten(),
|
||||
features: params
|
||||
.features
|
||||
.map(normalize_features_param)
|
||||
.transpose()?
|
||||
.flatten(),
|
||||
request_format: params.req_format.unwrap_or_default(),
|
||||
})
|
||||
}
|
||||
|
|
@ -0,0 +1,102 @@
|
|||
use base64::{Engine, engine::general_purpose::STANDARD};
|
||||
|
||||
use super::types::*;
|
||||
use crate::constants::{AZURE_DI_DEFAULT_DPI, AZURE_DI_DEFAULT_HEIGHT, AZURE_DI_DEFAULT_WIDTH};
|
||||
use crate::ocr::document::InlineDocument;
|
||||
use crate::ocr::error::{OcrRequestError, OcrResponseError};
|
||||
use crate::ocr::types::{OcrDocument, OcrPage, OcrPageDimensions, OcrResponseData, OcrUsageInfo};
|
||||
|
||||
fn pixel_dimension(value: f64, scale: f64, field: &'static str) -> Result<i64, OcrResponseError> {
|
||||
crate::ocr::wire::checked_truncated_i64(value * scale)
|
||||
.ok_or(OcrResponseError::NumericRange(field))
|
||||
}
|
||||
|
||||
fn convert_dimensions(
|
||||
width: f64,
|
||||
height: f64,
|
||||
unit: &str,
|
||||
) -> Result<OcrPageDimensions, OcrResponseError> {
|
||||
let scale = if unit == "inch" {
|
||||
AZURE_DI_DEFAULT_DPI as f64
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
Ok(OcrPageDimensions {
|
||||
dpi: Some(AZURE_DI_DEFAULT_DPI),
|
||||
width: Some(pixel_dimension(width, scale, "page.width")?),
|
||||
height: Some(pixel_dimension(height, scale, "page.height")?),
|
||||
})
|
||||
}
|
||||
|
||||
fn page_markdown(lines: &[AzureDocumentIntelligenceLine]) -> String {
|
||||
lines
|
||||
.iter()
|
||||
.map(|line| line.content.as_deref().unwrap_or_default())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
fn transform_azure_page(page: AzureDocumentIntelligencePage) -> Result<OcrPage, OcrResponseError> {
|
||||
let index = page
|
||||
.page_number
|
||||
.unwrap_or(1)
|
||||
.checked_sub(1)
|
||||
.ok_or(OcrResponseError::NumericRange("page.pageNumber"))?;
|
||||
Ok(OcrPage {
|
||||
dimensions: Some(convert_dimensions(
|
||||
page.width.unwrap_or(AZURE_DI_DEFAULT_WIDTH),
|
||||
page.height.unwrap_or(AZURE_DI_DEFAULT_HEIGHT),
|
||||
page.unit.as_deref().unwrap_or("inch"),
|
||||
)?),
|
||||
..OcrPage::text(index, page_markdown(&page.lines))
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn request(
|
||||
_model: &str,
|
||||
document: OcrDocument,
|
||||
_params: &DocumentIntelligenceParams,
|
||||
) -> Result<DocumentIntelligenceRequest, OcrRequestError> {
|
||||
let source = document.source();
|
||||
if source.is_empty() {
|
||||
return Err(OcrRequestError::MissingField("document URL"));
|
||||
}
|
||||
Ok(if let Some(document) = InlineDocument::parse(source)? {
|
||||
DocumentIntelligenceRequest::Base64Source(
|
||||
STANDARD.encode(document.decode(crate::constants::OCR_INLINE_MAX_BYTES)?),
|
||||
)
|
||||
} else {
|
||||
DocumentIntelligenceRequest::UrlSource(source.to_string())
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn response(
|
||||
model: &str,
|
||||
response: AzureDocumentIntelligenceOperation,
|
||||
_params: &DocumentIntelligenceParams,
|
||||
) -> Result<OcrResponseData, OcrResponseError> {
|
||||
if response.status != Some(OperationStatus::Succeeded) {
|
||||
return Err(OcrResponseError::OperationStatus(
|
||||
response
|
||||
.status
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or_else(|| "None".into()),
|
||||
));
|
||||
}
|
||||
let result = response.analyze_result.unwrap_or_default();
|
||||
let pages = result
|
||||
.pages
|
||||
.into_iter()
|
||||
.map(transform_azure_page)
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
Ok(OcrResponseData {
|
||||
usage_info: Some(OcrUsageInfo {
|
||||
pages_processed: Some(pages.len() as i64),
|
||||
..Default::default()
|
||||
}),
|
||||
content: result.content,
|
||||
tables: result.tables,
|
||||
key_value_pairs: result.key_value_pairs,
|
||||
..OcrResponseData::new(model.to_string(), pages)
|
||||
})
|
||||
}
|
||||
68
litellm-rust/crates/core/src/ocr/formats/macros.rs
Normal file
68
litellm-rust/crates/core/src/ocr/formats/macros.rs
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
macro_rules! ocr_format {
|
||||
($vis:vis $name:ident {
|
||||
params: $params:ty,
|
||||
$($rest:tt)*
|
||||
}) => {
|
||||
$crate::ocr::formats::ocr_format! {
|
||||
$vis $name {
|
||||
params: $params => $params,
|
||||
map: ::std::result::Result::Ok,
|
||||
$($rest)*
|
||||
}
|
||||
}
|
||||
};
|
||||
($vis:vis $name:ident {
|
||||
params: $input:ty => $mapped:ty,
|
||||
$(validate: $validate:path,)?
|
||||
map: $map:path,
|
||||
document: $document:ty,
|
||||
request: $request:ty => $build:path,
|
||||
response: $response:ty => $normalize:path $(,)?
|
||||
}) => {
|
||||
$vis struct $name;
|
||||
|
||||
impl $crate::ocr::formats::OcrFormat for $name {
|
||||
type InputParams = $input;
|
||||
type MappedParams = $mapped;
|
||||
type PreparedDocument = $document;
|
||||
type RequestBody = $request;
|
||||
type ResponseBody = $response;
|
||||
|
||||
$(fn validate_input_params(
|
||||
params: &::serde_json::Map<String, ::serde_json::Value>,
|
||||
) -> Result<(), $crate::ocr::error::OcrRequestError> {
|
||||
$validate(params)
|
||||
})?
|
||||
|
||||
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
|
||||
fn map_params(
|
||||
&self,
|
||||
params: Self::InputParams,
|
||||
) -> Result<Self::MappedParams, $crate::ocr::error::OcrRequestError> {
|
||||
$map(params)
|
||||
}
|
||||
|
||||
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
|
||||
fn transform_request(
|
||||
&self,
|
||||
model: &str,
|
||||
document: Self::PreparedDocument,
|
||||
params: &Self::MappedParams,
|
||||
) -> Result<Self::RequestBody, $crate::ocr::error::OcrRequestError> {
|
||||
$build(model, document, params)
|
||||
}
|
||||
|
||||
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
|
||||
fn transform_response(
|
||||
&self,
|
||||
model: &str,
|
||||
response: Self::ResponseBody,
|
||||
params: &Self::MappedParams,
|
||||
) -> Result<$crate::ocr::types::OcrResponseData, $crate::ocr::error::OcrResponseError> {
|
||||
$normalize(model, response, params)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) use ocr_format;
|
||||
|
|
@ -1,53 +1,16 @@
|
|||
mod transformation;
|
||||
pub mod types;
|
||||
|
||||
use self::types::{MistralOcrParams, MistralOcrRequest, MistralOcrResponse};
|
||||
use crate::ocr::error::OcrRequestError;
|
||||
use crate::ocr::error::OcrResponseError;
|
||||
use crate::ocr::formats::OcrFormat;
|
||||
use crate::ocr::types::{OcrDocument, OcrResponseData};
|
||||
use types::*;
|
||||
|
||||
pub struct MistralOcrFormat;
|
||||
use crate::ocr::formats::ocr_format;
|
||||
use crate::ocr::types::OcrDocument;
|
||||
|
||||
impl OcrFormat for MistralOcrFormat {
|
||||
type InputParams = MistralOcrParams;
|
||||
type MappedParams = MistralOcrParams;
|
||||
type PreparedDocument = OcrDocument;
|
||||
type RequestBody = MistralOcrRequest;
|
||||
type ResponseBody = MistralOcrResponse;
|
||||
|
||||
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
|
||||
fn map_params(&self, params: MistralOcrParams) -> Result<MistralOcrParams, OcrRequestError> {
|
||||
Ok(params)
|
||||
}
|
||||
|
||||
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
|
||||
fn transform_request(
|
||||
&self,
|
||||
model: &str,
|
||||
ocr_format! {
|
||||
pub MistralOcrFormat {
|
||||
params: MistralOcrParams,
|
||||
document: OcrDocument,
|
||||
params: &MistralOcrParams,
|
||||
) -> Result<MistralOcrRequest, OcrRequestError> {
|
||||
Ok(MistralOcrRequest {
|
||||
model: model.to_string(),
|
||||
document,
|
||||
params: params.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
|
||||
fn transform_response(
|
||||
&self,
|
||||
model: &str,
|
||||
response: MistralOcrResponse,
|
||||
_params: &MistralOcrParams,
|
||||
) -> Result<OcrResponseData, OcrResponseError> {
|
||||
Ok(OcrResponseData {
|
||||
document_annotation: response.document_annotation,
|
||||
usage_info: response.usage_info,
|
||||
..OcrResponseData::new(
|
||||
response.model.unwrap_or_else(|| model.to_string()),
|
||||
response.pages,
|
||||
)
|
||||
})
|
||||
request: MistralOcrRequest => transformation::request,
|
||||
response: MistralOcrResponse => transformation::response,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
use super::types::{MistralOcrParams, MistralOcrRequest, MistralOcrResponse};
|
||||
use crate::ocr::error::{OcrRequestError, OcrResponseError};
|
||||
use crate::ocr::types::{OcrDocument, OcrResponseData};
|
||||
|
||||
pub(super) fn request(
|
||||
model: &str,
|
||||
document: OcrDocument,
|
||||
params: &MistralOcrParams,
|
||||
) -> Result<MistralOcrRequest, OcrRequestError> {
|
||||
Ok(MistralOcrRequest {
|
||||
model: model.to_string(),
|
||||
document,
|
||||
params: params.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn response(
|
||||
model: &str,
|
||||
response: MistralOcrResponse,
|
||||
_params: &MistralOcrParams,
|
||||
) -> Result<OcrResponseData, OcrResponseError> {
|
||||
Ok(OcrResponseData {
|
||||
document_annotation: response.document_annotation,
|
||||
usage_info: response.usage_info,
|
||||
..OcrResponseData::new(
|
||||
response.model.unwrap_or_else(|| model.to_string()),
|
||||
response.pages,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
|
@ -6,6 +6,10 @@ use crate::Error;
|
|||
use serde::{Serialize, de::DeserializeOwned};
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
mod macros;
|
||||
|
||||
pub(crate) use macros::ocr_format;
|
||||
|
||||
pub mod deepseek;
|
||||
pub mod document_intelligence;
|
||||
pub mod mistral;
|
||||
|
|
|
|||
|
|
@ -1,151 +1,24 @@
|
|||
use crate::ocr::error::OcrRequestError;
|
||||
use crate::ocr::error::OcrResponseError;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
mod transformation;
|
||||
pub mod types;
|
||||
|
||||
use self::types::*;
|
||||
use crate::ocr::formats::OcrFormat;
|
||||
use crate::ocr::types::{OcrPage, OcrResponseData, OcrUsageInfo};
|
||||
use types::*;
|
||||
|
||||
fn build_pages(chunks: Vec<ReductoChunk>) -> Vec<OcrPage> {
|
||||
let blocks_by_page = chunks
|
||||
.iter()
|
||||
.flat_map(|chunk| chunk.blocks.iter().flatten())
|
||||
.filter_map(|block| block.bbox.as_ref()?.page.map(|page| (page, block)))
|
||||
.fold(
|
||||
BTreeMap::<i64, Vec<&ReductoBlock>>::new(),
|
||||
|mut pages, (page, block)| {
|
||||
pages.entry(page).or_default().push(block);
|
||||
pages
|
||||
},
|
||||
);
|
||||
if blocks_by_page.is_empty() {
|
||||
let markdown = chunks
|
||||
.iter()
|
||||
.filter_map(|chunk| chunk.content.as_deref())
|
||||
.filter(|s| !s.is_empty())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n\n");
|
||||
return if markdown.is_empty() {
|
||||
Vec::new()
|
||||
} else {
|
||||
vec![OcrPage::text(0, markdown)]
|
||||
};
|
||||
}
|
||||
blocks_by_page
|
||||
.into_iter()
|
||||
.map(|(page, blocks)| {
|
||||
let markdown = blocks
|
||||
.iter()
|
||||
.filter_map(|block| block.content.as_deref())
|
||||
.filter(|s| !s.is_empty())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n\n");
|
||||
OcrPage {
|
||||
extra_fields: serde_json::Map::from_iter([(
|
||||
"blocks".into(),
|
||||
serde_json::json!(blocks),
|
||||
)]),
|
||||
..OcrPage::text(page.saturating_sub(1).max(0), markdown)
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
use crate::ocr::formats::ocr_format;
|
||||
|
||||
fn transform_reducto_response(model: &str, response: ReductoResponse) -> OcrResponseData {
|
||||
let result = match response.result {
|
||||
Some(result) => result.unwrap_or_default(),
|
||||
None => ReductoResult {
|
||||
chunks: response.chunks,
|
||||
},
|
||||
};
|
||||
let usage = response.usage.unwrap_or_default();
|
||||
OcrResponseData {
|
||||
usage_info: Some(OcrUsageInfo {
|
||||
pages_processed: usage.num_pages,
|
||||
credits: usage.credits,
|
||||
..Default::default()
|
||||
}),
|
||||
..OcrResponseData::new(
|
||||
model.to_string(),
|
||||
build_pages(result.chunks.unwrap_or_default()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ReductoParseV3Format;
|
||||
|
||||
impl OcrFormat for ReductoParseV3Format {
|
||||
type InputParams = ReductoV3Params;
|
||||
type MappedParams = ReductoV3Params;
|
||||
type PreparedDocument = ReductoFileId;
|
||||
type RequestBody = ReductoV3Request;
|
||||
type ResponseBody = ReductoResponse;
|
||||
|
||||
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
|
||||
fn map_params(&self, params: Self::InputParams) -> Result<Self::MappedParams, OcrRequestError> {
|
||||
Ok(params)
|
||||
}
|
||||
|
||||
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
|
||||
fn transform_request(
|
||||
&self,
|
||||
_model: &str,
|
||||
ocr_format! {
|
||||
pub ReductoParseV3Format {
|
||||
params: ReductoV3Params,
|
||||
document: ReductoFileId,
|
||||
params: &Self::MappedParams,
|
||||
) -> Result<Self::RequestBody, OcrRequestError> {
|
||||
Ok(ReductoV3Request {
|
||||
input: document.0,
|
||||
params: params.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
|
||||
fn transform_response(
|
||||
&self,
|
||||
model: &str,
|
||||
response: Self::ResponseBody,
|
||||
_params: &Self::MappedParams,
|
||||
) -> Result<OcrResponseData, OcrResponseError> {
|
||||
Ok(transform_reducto_response(model, response))
|
||||
request: ReductoV3Request => transformation::v3_request,
|
||||
response: ReductoResponse => transformation::response,
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ReductoParseLegacyFormat;
|
||||
|
||||
impl OcrFormat for ReductoParseLegacyFormat {
|
||||
type InputParams = ReductoLegacyParams;
|
||||
type MappedParams = ReductoLegacyParams;
|
||||
type PreparedDocument = ReductoFileId;
|
||||
type RequestBody = ReductoLegacyRequest;
|
||||
type ResponseBody = ReductoResponse;
|
||||
|
||||
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
|
||||
fn map_params(&self, params: Self::InputParams) -> Result<Self::MappedParams, OcrRequestError> {
|
||||
Ok(params)
|
||||
}
|
||||
|
||||
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
|
||||
fn transform_request(
|
||||
&self,
|
||||
_model: &str,
|
||||
ocr_format! {
|
||||
pub ReductoParseLegacyFormat {
|
||||
params: ReductoLegacyParams,
|
||||
document: ReductoFileId,
|
||||
params: &Self::MappedParams,
|
||||
) -> Result<Self::RequestBody, OcrRequestError> {
|
||||
Ok(ReductoLegacyRequest {
|
||||
document_url: document.0,
|
||||
options: params.enhance.as_ref().map(|_| params.clone()),
|
||||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
|
||||
fn transform_response(
|
||||
&self,
|
||||
model: &str,
|
||||
response: Self::ResponseBody,
|
||||
_params: &Self::MappedParams,
|
||||
) -> Result<OcrResponseData, OcrResponseError> {
|
||||
Ok(transform_reducto_response(model, response))
|
||||
request: ReductoLegacyRequest => transformation::legacy_request,
|
||||
response: ReductoResponse => transformation::response,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,95 @@
|
|||
use std::collections::BTreeMap;
|
||||
|
||||
use super::types::*;
|
||||
use crate::ocr::error::{OcrRequestError, OcrResponseError};
|
||||
use crate::ocr::types::{OcrPage, OcrResponseData, OcrUsageInfo};
|
||||
|
||||
fn join_content<'a>(content: impl Iterator<Item = Option<&'a str>>) -> String {
|
||||
content
|
||||
.flatten()
|
||||
.filter(|text| !text.is_empty())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n\n")
|
||||
}
|
||||
|
||||
fn build_pages(chunks: Vec<ReductoChunk>) -> Vec<OcrPage> {
|
||||
let blocks_by_page = chunks
|
||||
.iter()
|
||||
.flat_map(|chunk| chunk.blocks.iter().flatten())
|
||||
.filter_map(|block| block.bbox.as_ref()?.page.map(|page| (page, block)))
|
||||
.fold(
|
||||
BTreeMap::<i64, Vec<&ReductoBlock>>::new(),
|
||||
|mut pages, (page, block)| {
|
||||
pages.entry(page).or_default().push(block);
|
||||
pages
|
||||
},
|
||||
);
|
||||
if blocks_by_page.is_empty() {
|
||||
let markdown = join_content(chunks.iter().map(|chunk| chunk.content.as_deref()));
|
||||
return if markdown.is_empty() {
|
||||
Vec::new()
|
||||
} else {
|
||||
vec![OcrPage::text(0, markdown)]
|
||||
};
|
||||
}
|
||||
blocks_by_page
|
||||
.into_iter()
|
||||
.map(|(page, blocks)| {
|
||||
let markdown = join_content(blocks.iter().map(|block| block.content.as_deref()));
|
||||
OcrPage {
|
||||
extra_fields: serde_json::Map::from_iter([(
|
||||
"blocks".into(),
|
||||
serde_json::json!(blocks),
|
||||
)]),
|
||||
..OcrPage::text(page.saturating_sub(1).max(0), markdown)
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub(super) fn response<P>(
|
||||
model: &str,
|
||||
response: ReductoResponse,
|
||||
_params: &P,
|
||||
) -> Result<OcrResponseData, OcrResponseError> {
|
||||
let result = match response.result {
|
||||
Some(result) => result.unwrap_or_default(),
|
||||
None => ReductoResult {
|
||||
chunks: response.chunks,
|
||||
},
|
||||
};
|
||||
let usage = response.usage.unwrap_or_default();
|
||||
Ok(OcrResponseData {
|
||||
usage_info: Some(OcrUsageInfo {
|
||||
pages_processed: usage.num_pages,
|
||||
credits: usage.credits,
|
||||
..Default::default()
|
||||
}),
|
||||
..OcrResponseData::new(
|
||||
model.to_string(),
|
||||
build_pages(result.chunks.unwrap_or_default()),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn v3_request(
|
||||
_model: &str,
|
||||
document: ReductoFileId,
|
||||
params: &ReductoV3Params,
|
||||
) -> Result<ReductoV3Request, OcrRequestError> {
|
||||
Ok(ReductoV3Request {
|
||||
input: document.0,
|
||||
params: params.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn legacy_request(
|
||||
_model: &str,
|
||||
document: ReductoFileId,
|
||||
params: &ReductoLegacyParams,
|
||||
) -> Result<ReductoLegacyRequest, OcrRequestError> {
|
||||
Ok(ReductoLegacyRequest {
|
||||
document_url: document.0,
|
||||
options: params.enhance.as_ref().map(|_| params.clone()),
|
||||
})
|
||||
}
|
||||
|
|
@ -101,13 +101,9 @@ fn reducto_page<'de, D: serde::Deserializer<'de>>(
|
|||
let value = Value::deserialize(deserializer)?;
|
||||
Ok(match value {
|
||||
Value::Number(number) => number.as_i64().or_else(|| {
|
||||
number.as_f64().and_then(|value| {
|
||||
let truncated = value.trunc();
|
||||
(truncated.is_finite()
|
||||
&& truncated >= i64::MIN as f64
|
||||
&& truncated < -(i64::MIN as f64))
|
||||
.then_some(truncated as i64)
|
||||
})
|
||||
number
|
||||
.as_f64()
|
||||
.and_then(crate::ocr::wire::checked_truncated_i64)
|
||||
}),
|
||||
Value::String(value) => value.trim().parse().ok(),
|
||||
Value::Bool(value) => Some(i64::from(value)),
|
||||
|
|
|
|||
|
|
@ -178,9 +178,17 @@ impl PythonNumber {
|
|||
}
|
||||
}
|
||||
fn exact_integer(n: f64) -> Option<i64> {
|
||||
(n.is_finite() && n.fract() == 0.0 && n >= i64::MIN as f64 && n < -(i64::MIN as f64))
|
||||
.then_some(n as i64)
|
||||
if n.fract() != 0.0 {
|
||||
return None;
|
||||
}
|
||||
checked_truncated_i64(n)
|
||||
}
|
||||
pub(crate) fn checked_truncated_i64(value: f64) -> Option<i64> {
|
||||
let truncated = value.trunc();
|
||||
(truncated.is_finite() && truncated >= i64::MIN as f64 && truncated < -(i64::MIN as f64))
|
||||
.then_some(truncated as i64)
|
||||
}
|
||||
|
||||
pub fn optional_i64<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Option<i64>, D::Error> {
|
||||
Option::<PythonNumber>::deserialize(deserializer)?
|
||||
.map(|n| {
|
||||
|
|
|
|||
|
|
@ -29,6 +29,10 @@ fn deepseek_request_uses_single_provider_namespace(#[case] model: &str) {
|
|||
#[rstest]
|
||||
#[case(json!("# hello"),"# hello")]
|
||||
#[case(json!("{broken"),"{broken")]
|
||||
#[case(json!(" {\"pages\":[]} "), " {\"pages\":[]} ")]
|
||||
#[case(json!({"pages":[]}), "{\"pages\":[]}")]
|
||||
#[case(json!({}), "{}")]
|
||||
#[case(json!("[]"), "[]")]
|
||||
#[case(json!("{\"pages\":[{\"markdown\":\"json text\"}]}"),"json text")]
|
||||
#[case(json!({"pages":[{"markdown":"object"}]}),"object")]
|
||||
fn vertex_deepseek_response_wraps_markdown_content(
|
||||
|
|
@ -51,6 +55,7 @@ fn vertex_deepseek_response_wraps_markdown_content(
|
|||
fn deepseek_rejects_missing_content_and_invalid_structured_pages() {
|
||||
for response in [
|
||||
json!({"choices":[]}),
|
||||
json!({"choices":[{"message":{"content":"{\"pages\":[{\"markdown\":42}]}"}}]}),
|
||||
json!({"choices":[{"message":{"content":""}}]}),
|
||||
json!({"choices":[{"message":{"content":{"pages":[{"markdown":42}]}}}]}),
|
||||
] {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue