Skip to content

Commit 47daa8b

Browse files
committed
feat(server): add LLM processing and error handling strategies to chunkr
1 parent e82f47a commit 47daa8b

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

pdf2md/server/src/operators/chunkr.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,39 @@ pub enum OcrStrategy {
324324
Auto,
325325
}
326326

327+
#[derive(Debug, Serialize, Deserialize, Clone, ToSchema, Default)]
328+
/// Specifies the fallback strategy for LLM processing
329+
///
330+
/// This can be:
331+
/// 1. None - No fallback will be used
332+
/// 2. Default - The system default fallback model will be used
333+
/// 3. Model - A specific model ID will be used as fallback (check the documentation for the models.)
334+
pub enum FallbackStrategy {
335+
/// No fallback will be used
336+
None,
337+
/// Use the system default fallback model
338+
#[default]
339+
Default,
340+
/// Use a specific model as fallback
341+
Model(String),
342+
}
343+
344+
#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
345+
/// Controls the LLM used for the task.
346+
pub struct LlmProcessing {
347+
/// The ID of the model to use for the task. If not provided, the default model will be used.
348+
/// Please check the documentation for the model you want to use.
349+
pub model_id: Option<String>,
350+
/// The fallback strategy to use for the LLMs in the task.
351+
#[serde(default)]
352+
pub fallback_strategy: FallbackStrategy,
353+
/// The maximum number of tokens to generate.
354+
pub max_completion_tokens: Option<u32>,
355+
/// The temperature to use for the LLM.
356+
#[serde(default)]
357+
pub temperature: f32,
358+
}
359+
327360
#[derive(Serialize, Deserialize, Debug, Clone, Display, Eq, PartialEq, ToSchema, Default)]
328361
/// Controls the segmentation strategy:
329362
/// - `LayoutAnalysis`: Analyzes pages for layout elements (e.g., `Table`, `Picture`, `Formula`, etc.) using bounding boxes. Provides fine-grained segmentation and better chunking. (Latency penalty: ~TBD seconds per page).
@@ -334,6 +367,16 @@ pub enum SegmentationStrategy {
334367
Page,
335368
}
336369

370+
#[derive(Serialize, Deserialize, Debug, Clone, Display, Eq, PartialEq, ToSchema, Default)]
371+
/// Controls how errors are handled during processing:
372+
/// - `Fail`: Stops processing and fails the task when any error occurs
373+
/// - `Continue`: Attempts to continue processing despite non-critical errors (eg. LLM refusals etc.)
374+
pub enum ErrorHandlingStrategy {
375+
#[default]
376+
Fail,
377+
Continue,
378+
}
379+
337380
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, ToSchema, Display, Default)]
338381
pub enum PipelineType {
339382
Azure,
@@ -376,6 +419,8 @@ pub struct CreateFormWithoutFile {
376419
pub segment_processing: Option<SegmentProcessing>,
377420
#[schema(default = "LayoutAnalysis")]
378421
pub segmentation_strategy: Option<SegmentationStrategy>,
422+
pub error_handling: Option<ErrorHandlingStrategy>,
423+
pub llm_processing: Option<LlmProcessing>,
379424
}
380425

381426
#[derive(Debug, Serialize, Clone, Deserialize, ToSchema, IntoParams)]
@@ -400,6 +445,8 @@ pub struct CreateForm {
400445
pub segment_processing: Option<SegmentProcessing>,
401446
#[schema(default = "LayoutAnalysis")]
402447
pub segmentation_strategy: Option<SegmentationStrategy>,
448+
pub error_handling: Option<ErrorHandlingStrategy>,
449+
pub llm_processing: Option<LlmProcessing>,
403450
}
404451

405452
fn get_chunkr_credentials(api_key: Option<&str>) -> Result<(String, String), ServiceError> {
@@ -433,6 +480,13 @@ pub async fn create_chunkr_task(
433480
pipeline: Some(PipelineType::Chunkr),
434481
segment_processing: None,
435482
segmentation_strategy: Some(SegmentationStrategy::LayoutAnalysis),
483+
error_handling: Some(ErrorHandlingStrategy::default()),
484+
llm_processing: Some(LlmProcessing {
485+
model_id: None,
486+
fallback_strategy: FallbackStrategy::default(),
487+
max_completion_tokens: None,
488+
temperature: 0.0,
489+
}),
436490
},
437491
|payload| CreateForm {
438492
file: file_base64.to_string(),
@@ -444,6 +498,8 @@ pub async fn create_chunkr_task(
444498
pipeline: payload.pipeline,
445499
segment_processing: payload.segment_processing,
446500
segmentation_strategy: payload.segmentation_strategy,
501+
error_handling: payload.error_handling,
502+
llm_processing: payload.llm_processing,
447503
},
448504
);
449505

0 commit comments

Comments
 (0)