_doc_intel / _cu — Azure 雲端轉換器

兩條雲端路徑:Document Intelligence 版面分析、Content Understanding 多模態
檔案:_doc_intel_converter.py · _cu_converter.py(Cloud 整合源碼)

大方向

兩個 converter 都是「有端點才註冊」(enable_builtins 只在提供 docintel_endpoint/cu_endpoint 時加入,且插到堆疊頂端)。 最複雜的是 Content Understanding 的 analyzer 路由:依檔案型別自動選 analyzer、 處理模態相容性、輸出 YAML front matter。


1. DocumentIntelligenceConverter

DocumentIntelligenceFileType / accepts / _analysis_features / convert DocIntel雲端版面分析;依檔型別選 analysis features。

DocumentIntelligenceFileType 是字串列舉(PDF/JPEG/PNG/DOCX…); _get_mime_type_prefixes/_get_file_extensions 從列舉產生 accepts 規則。 _analysis_features 依檔型別挑 OCR/table 等 features;convert 呼叫 azure-ai-documentintelligence client 分析並組 Markdown。

2. ContentUnderstandingConverter(analyzer 路由核心)

ContentUnderstandingFileType / _detect_file_type / _detect_file_type_from_mime CU依副檔名或 MIME 偵測 CU 支援的檔案型別。

ContentUnderstandingFileType 列舉(PDF/JPEG/PNG/MP3/MP4/WAV…)與模態對應; _clean_mime_type/_canonical_mime_type 正規化 MIME, _content_type_for 決定送給 CU API 的 content-type。

_get_modality / _resolve_analyzer_modality / _is_analyzer_compatible CU分析器模態快取、解析與相容性檢查。

零設定時要「自動選 analyzer」:_get_modality 把檔案型別歸成 document/image/audio/video; _resolve_analyzer_modality 用快取或呼叫 get_analyzer() 查 analyzer 的模態; _is_analyzer_compatible 判斷「文件 analyzer 能否處理音訊」。不相容就自動退回預建 analyzer。

convert / to_llm_input CU呼叫 CU API 並把結構化欄位序列化成 YAML front matter。

convert 用 analyzer 結果組 Markdown:欄位(vendor、日期…)放在 --- YAML front matter,正文逐頁 <!-- page N -->to_llm_inputazure-ai-contentunderstanding>=1.2.0b1 提供的 helper, 把結果轉成可直接餵 LLM 的格式(所以 extra 才要求 >=1.2.0b1)。