Skip to content

adm_ai_ocr_fallback_api

OCR for files that the built-in text extraction cannot read, with no RAG collection needed.

The built-in filter reads the text layer of a file. A scanned PDF has none, and the filter stores an empty text for it. An image never reaches the filter at all. In both cases the file is not found by the full-text search, and no collection can answer from it. This package reads such files with OCR (uc_ai.ocr) and stores the result in adm_document_versions.index_content with index_content_source = 'OCR'.

The fallback is off until the setting AI_OCR_FALLBACK_CONFIG holds a configuration. The value is a JSON object with the keys of the ocr_text_extraction section of a collection (provider, model, config, options and apply_to) and one more key, min_text_chars.

{
"provider": "mistral",
"config": { "g_apex_web_credential": "MISTRAL_API_KEY" },
"options": { "tables": true },
"apply_to": {
"mime_types": ["application/pdf", "image/png", "image/jpeg"],
"annotation_key": "scanned"
},
"min_text_chars": 20
}

A file is read when all of these are true:

  • It is the latest version of a document that is not in the trash or archived.
  • Its MIME type is in apply_to.mime_types (default: application/pdf) and, if apply_to.annotation_key is set, the document carries that annotation.
  • It has less than min_text_chars characters of text that are not blank (default 20) and the text is at most 4000 characters long, or it is an image with no text.
  • No collection has chunked its text. The chunks point into the text by offset, and a new text under them would corrupt them.
  • No collection waits for its own extraction job on it.
  • It has failed less than AI_JOB_MAX_ATTEMPTS times.

The job ADM_AI_RAG_QUEUE_JOB runs this stage before it chunks, so a scanned file is read before a collection sees its empty text. The setting AI_TEXT_EXTRACTION must be Y, as for all other extraction that sends files to an external service.

The form extraction (adm_ai_form_extract_api) reads the stored OCR text too. Its automatic processing waits for a document whose latest version waits here, unless the ocr_text_extraction of its profile reads the document itself. That OCR stores its text with store_ocr_text, under the same restrictions as this stage.

Returns true when the fallback has a configuration and AI_TEXT_EXTRACTION is Y.

Signature:

function is_enabled return boolean;

Returns: boolean - true when process_ocr_fallback does work


Reads waiting file versions with OCR and stores their text. Does nothing when the fallback is off. A failure costs the version one attempt and does not stop the run. Does not commit.

Only one session runs this at a time. A session that finds another one running returns at once and does nothing. The lock is released when the caller commits, so a caller that calls this in batches must commit between the batches. The grant execute on dbms_lock that the installation already requires is enough.

Signature:

procedure process_ocr_fallback (
p_limit in number default null
);

Parameters:

Name Direction Type Description
p_limit in number default null Most versions to read in this call (default: AI_JOB_BATCH_SIZE)