Parsing & Document Processor
Parsing & Document Processor
Le parsing des documents est délégué à un microservice dédié (apps/document-processor/) appelé par le serveur Bun via un client HTTP (apps/server/src/utils/document-processor-client.ts).
Architecture
Admin (web) → processUploadedFile (ORPC, admin)
↓
regular-processing.ts
↓
documentProcessor.processDocument()
↓
POST http://DOCUMENT_PROCESSOR_URL/process
↓
document-processor microservice (FastAPI)
↓
Chunks → INSERT INTO chunks (DB)Microservice document-processor
Localisation: apps/document-processor/
Stack: Python, FastAPI, uvicorn
Démarrage
# Variables d'environnement
DOCUMENT_PROCESSOR_HOST=0.0.0.0
DOCUMENT_PROCESSOR_PORT=8001
MAX_FILE_SIZE_MB=50
ENABLE_OCR=false
TESSERACT_PATH= # optionnel, si tesseract n'est pas dans PATH
LOG_LEVEL=INFOcd apps/document-processor
pip install -e .
uvicorn src.main:app --host 0.0.0.0 --port 8001Endpoints
GET /health
Response (HealthResponse):
{
"status": "healthy",
"timestamp": "2024-01-15T10:30:00.000Z"
}POST /process
Parse et découpe un document en chunks.
Request (ProcessingRequest):
{
file_content: string; // base64, data URL, ou texte brut (pour text/plain, text/markdown, text/csv, application/json)
file_name: string;
file_type: string; // MIME type (voir formats supportés)
chunk_size?: number; // default: 1000 (taille en caractères)
overlap?: number; // default: 200 (chevauchement entre chunks)
extract_qa_pairs?: boolean; // default: false
extract_tables?: boolean; // default: true
extract_images?: boolean; // default: false
}Response (ProcessingResult):
{
success: boolean;
chunks: Chunk[]; // liste des chunks extraits
qa_pairs: QAPair[]; // si extract_qa_pairs: true
tables: Table[]; // tableaux extraits (si extract_tables: true)
images: Image[]; // images extraites (si extract_images: true)
metadata: Record<string, any>;
processing_time_ms: number;
error?: string; // présent si success: false
}
type Chunk = {
rank: number;
content: string;
category: string; // "not categorized" par défaut
metadata?: {
source_type: string; // "text", "table", "picture"
section_path?: string[]; // hiérarchie des sections
page_numbers?: number[]; // pages du chunk
bboxes?: Array<{l, t, r, b}>; // coordonnées
docling_item_refs?: string[]; // références items Docling
token_count?: number; // nombre de tokens
origin?: string; // nom du fichier d'origine
parser_version: string;
chunking_config: { max_tokens: number, merge_peers: boolean };
};
page_number?: number;
section_title?: string;
section_path?: string[];
provenance?: {
page_numbers?: number[];
bboxes?: Array<{l, t, r, b}>;
docling_item_refs?: string[];
};
token_count?: number;
source_type?: string;
has_table?: boolean;
has_image?: boolean;
}Codes d'erreur:
400— contenu fichier invalide (base64 malformé)413— fichier trop grand (> MAX_FILE_SIZE_MB)415— type de fichier non supporté500— erreur de parsing interne
Formats supportés
Le microservice utilise Docling 2.116 pour le parsing, offrant une large compatibilité de formats.
| MIME type | Extension | Handler |
|---|---|---|
application/pdf | .pdf | Docling |
application/vnd.openxmlformats-officedocument.wordprocessingml.document | .docx | Docling |
application/vnd.openxmlformats-officedocument.presentationml.presentation | .pptx | Docling |
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet | .xlsx | Docling |
application/vnd.ms-excel | .xls | Docling |
text/csv | .csv | Docling |
text/markdown | .md, .markdown | Docling |
text/plain | .txt | Docling |
text/html | .html, .htm, .xhtml | Docling |
image/png | .png | Docling |
image/jpeg | .jpg, .jpeg | Docling |
image/tiff | .tiff, .tif | Docling |
image/bmp | .bmp | Docling |
image/webp | .webp | Docling |
application/vnd.oasis.opendocument.text | .odt | Docling |
application/vnd.oasis.opendocument.spreadsheet | .ods | Docling |
application/vnd.oasis.opendocument.presentation | .odp | Docling |
application/epub+zip | .epub | Docling |
application/xml | .xml | Docling |
text/asciidoc | .adoc, .asciidoc | Docling |
application/msword | .doc | Docling (legacy) |
application/vnd.ms-powerpoint | .ppt | Docling (legacy) |
application/x-latex | .tex | Docling |
text/x-latex | .tex | Docling |
text/vtt | .vtt | Docling |
application/vtt | .vtt | Docling |
message/rfc822 | .eml | Docling |
application/xml+jats | .jats.xml | Docling |
application/xml+uspto | .uspto.xml | Docling |
application/xml+xbrl | .xbrl | Docling |
application/json | .json | JSON (business/technical) |
Priorité: le type MIME est prioritaire sur l'extension.
OCR (optionnel)
L'OCR est configurée via DOCLING_ENABLE_OCR et DOCLING_OCR_ENGINE. Plusieurs moteurs sont supportés :
easyocr(défaut) — moteur OCR basé sur PyTorchrapidocr— moteur OCR léger basé onnxruntimetesseract-cli— Tesseract via CLItesserocr— Tesseract via Python wrapper
Exemple de configuration :
DOCLING_ENABLE_OCR=true
DOCLING_OCR_ENGINE=easyocr # ou rapidocr, tesseract-cli, tesserocr
DOCLING_OCR_LANGUAGES=en,frPour tesseract-cli ou tesserocr, l'installation de tesseract-ocr sur le système est requise.
Client côté serveur Bun
apps/server/src/utils/document-processor-client.ts
documentProcessor.processDocument(request: {
file_content: string,
file_name: string,
file_type: string,
chunk_size: number,
overlap: number,
extract_qa_pairs: boolean,
extract_tables: boolean,
extract_images: boolean
}): Promise<ProcessingResult>La variable d'environnement DOCUMENT_PROCESSOR_URL doit pointer vers le microservice (ex: http://localhost:8001 en local, URL interne Kubernetes en production).
Flux processUploadedFile (serveur)
apps/server/src/utils/regular-processing.ts:
- Valide:
fileContentnon vide,chunkSize > overlap. - Insère un enregistrement
files(status:pending) → obtientfileId. - Appelle
documentProcessor.processDocument(...). - Si échec ou 0 chunks: supprime l'entrée
fileset retourne{ success: false }. - Insère chaque chunk dans
chunks(rank,contentsanitisé,category: "not categorized",fileId). - Retourne
{ success: true, fileId, chunkCount }.
Paramètres d'input:
| Paramètre | Type | Default | Description |
|---|---|---|---|
fileContent | string | — | Contenu base64 ou texte brut |
fileName | string | — | Nom du fichier (détermine le parser) |
fileType | string | "text/plain" | MIME type |
uploaderId | string | — | ID de l'utilisateur (admin) |
chunkSize | number | 1000 | Taille des chunks en caractères |
overlap | number | 200 | Chevauchement entre chunks |
Flux processUploadedQA (serveur)
apps/server/src/utils/qa-processing.ts — pour les fichiers CSV Q&A:
- Parse le CSV (colonnes
question,answer). - Insère un enregistrement
files(type:qa). - Insère chaque ligne dans
qa_pair(rank,question,answer,category: "not categorized").
Le microservice document-processor n'est pas utilisé pour les QA (parsing interne direct).
Variables d'environnement serveur liées au parsing
| Variable | Description | Exemple |
|---|---|---|
DOCUMENT_PROCESSOR_URL | URL du microservice document-processor | http://doc-processor:8001 |
INDEX_API_URL | URL de l'API d'index FAISS (chatbot) | http://chatbot-index:8502 |
Configuration Docling (microservice)
| Variable | Description | Défaut |
|---|---|---|
DOCLING_MAX_TOKENS | Nombre max de tokens par chunk | 512 |
DOCLING_TOKENIZER_MODEL | Modèle de tokenizer (Mistral) | mistralai/Mistral-7B-v0.1 |
DOCLING_ENABLE_OCR | Activer l'OCR | true |
DOCLING_OCR_ENGINE | Moteur OCR (easyocr, rapidocr, tesseract-cli, tesserocr) | easyocr |
DOCLING_OCR_LANGUAGES | Langues OCR (séparées par virgule) | en,fr |
DOCLING_TABLE_MODE | Mode table (fast, accurate) | accurate |
DOCLING_LAYOUT_MODEL | Modèle layout (heron, egret_large, egret_xlarge) | heron |
DOCLING_NUM_THREADS | Nombre de threads | 4 |
DOCLING_DEVICE | Device (auto, cpu, cuda) | auto |
DOCLING_ENABLE_PICTURE_DESCRIPTION | Activer description VLM images (coûteux) | false |
DOCLING_PICTURE_DESCRIPTION_PRESET | Preset VLM (granite_vision, smolvlm, pixtral, qwen) | granite_vision |
DOCLING_ENABLE_CODE_ENRICHMENT | Activer enrichissement code | false |
DOCLING_ENABLE_FORMULA_ENRICHMENT | Activer enrichissement formules | false |
DOCLING_ENABLED_FORMATS | Formats activés (séparés par virgule) | pdf,docx,pptx,... |
Kalli