<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shaafabdullah on Medium]]></title>
        <description><![CDATA[Stories by Shaafabdullah on Medium]]></description>
        <link>https://medium.com/@shaafabdullah?source=rss-ae7e7893926b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*awf7k6gehHzzx6hbEpqtVw.jpeg</url>
            <title>Stories by Shaafabdullah on Medium</title>
            <link>https://medium.com/@shaafabdullah?source=rss-ae7e7893926b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 21:34:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shaafabdullah/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a Production-Ready Azure Document Ingestion Pipeline]]></title>
            <link>https://medium.com/@shaafabdullah/building-a-production-ready-azure-document-ingestion-pipeline-8272a71fe142?source=rss-ae7e7893926b------2</link>
            <guid isPermaLink="false">https://medium.com/p/8272a71fe142</guid>
            <category><![CDATA[enterprise-ai]]></category>
            <category><![CDATA[generative-ai-use-cases]]></category>
            <category><![CDATA[azure-ai]]></category>
            <category><![CDATA[document-intelligence]]></category>
            <category><![CDATA[azure-cognitive-search]]></category>
            <dc:creator><![CDATA[Shaafabdullah]]></dc:creator>
            <pubDate>Thu, 11 Sep 2025 14:53:44 GMT</pubDate>
            <atom:updated>2025-09-12T12:00:53.222Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/832/1*tbdGrpQYOk3Gw-HDJNLwMA.jpeg" /></figure><p>How we architected an enterprise-grade document processing system using Azure Document Intelligence, Cognitive Search, and OpenAI for scalable multi-modal content extraction</p><h3><strong>The Azure Advantage: Why Cloud-Native Document Processing Matters</strong></h3><p>Imagine uploading a 300-page financial report and instantly being able to ask questions like:</p><ul><li><em>“What were the top five cost drivers last year?”</em></li><li><em>“Summarize all contracts signed with Vendor X.”</em></li></ul><p>That’s the promise of a modern <strong>document ingestion pipeline</strong> — not just extracting text, but turning static documents into an <strong>interactive knowledge base</strong>.</p><p>Traditional on-premises systems often struggle here. They treat documents as flat text, failing to capture the richness of modern formats that include structured tables, images, and layered layouts. They also lack the scalability needed to handle thousands of documents concurrently.</p><p>Azure’s AI ecosystem provides the perfect foundation to address these challenges:</p><ul><li><strong>Azure Document Intelligence</strong>: Advanced OCR and layout understanding for PDFs, Word, and PowerPoint.</li><li><strong>Azure Cognitive Search</strong>: Vector-based semantic search with HNSW indexing.</li><li><strong>Azure OpenAI</strong>: Multi-modal AI for image understanding and content summarization.</li><li><strong>Azure’s Global Infrastructure</strong>: Scalable, reliable processing with enterprise-grade SLA guarantees.</li></ul><p>By combining these services, we built a pipeline that transforms static documents into an <strong>intelligent, searchable knowledge base</strong>.</p><h3>Multi-Modal Document Intelligence: Beyond Text Processing</h3><h4>The Multi-Modal Challenge</h4><p>Traditional document processing often reduces content to plain text, losing critical meaning embedded in tables, images, and layout. But modern business documents are <strong>inherently multi-modal</strong>:</p><ul><li>Financial reports combine narrative text, tables, and charts.</li><li>Technical manuals integrate procedural text with diagrams and screenshots.</li><li>Legal contracts include structured clauses, signature blocks, and embedded exhibits.</li><li>Research papers merge academic text with figures, equations, and visualizations.</li></ul><p>Our Azure-powered pipeline treats documents as multi-modal entities <strong>from extraction through retrieval</strong>, ensuring that no valuable signal is lost.</p><h3>Azure-Powered Architecture: Five-Stage Ingestion Pipeline</h3><p>Our production-ready solution leverages Azure’s AI services through a carefully orchestrated five-stage pipeline that transforms raw documents into searchable, intelligent content:</p><pre><br>                                    📈 DATA FLOW<br><br>Input Documents → Document Intelligence → Text Chunking → Image Processing → <br>Search Indexing → Vector Storage → Query Processing → Context Retrieval → <br>Answer Generation → Structured Response<br><br>                                ⚡ PERFORMANCE FEATURES<br><br>• Concurrent Processing (8 workers)     • Smart Token Management (60K limit)<br>• Batch Operations                      • Error Recovery &amp; Retry Logic<br>• Connection Pooling                    • Memory Optimization<br>• Endpoint Validation                   • Configurable Logging</pre><p>With this architecture, organizations can scale document processing securely in the cloud while enabling <strong>retrieval-augmented generation (RAG)</strong> for real-time, context-aware answers.</p><h3>Stage 1: Azure Document Intelligence Integration</h3><p>The first stage of our pipeline transforms raw documents — <strong>PDF, DOCX, and PPTX</strong> — into <strong>structured, searchable content</strong> using <strong>Azure Document Intelligence</strong>. Unlike traditional OCR systems that simply dump plain text, this service provides a deeper, <strong>layout-aware understanding</strong> of documents.</p><p>With Document Intelligence, we can:</p><ul><li><strong>Extract text with structure</strong> — preserve headings, paragraphs, and contextual flow.</li><li><strong>Capture tables accurately</strong> — maintain row/column relationships instead of flattening them.</li><li><strong>Process embedded images</strong> — extract visuals along with contextual placement.</li><li><strong>Preserve spatial relationships</strong> — retain the layout that connects different content types.</li></ul><p>This <strong>multi-modal extraction</strong> ensures that our downstream pipeline stages — chunking, image summarization, and semantic search — are all built on <strong>rich, structured inputs</strong> rather than flat text. It forms the <strong>foundation for intelligent document search and retrieval</strong>.</p><h4>Document Extractor (document_extractor.py)</h4><p>At the heart of Stage 1 is the <strong>Document Extractor</strong> component. This Python module serves as the orchestrator of the extraction process, leveraging Azure Document Intelligence APIs to:</p><ul><li>Handle multiple file formats (PDF, DOCX, PPTX).</li><li>Execute <strong>OCR + layout analysis</strong>.</li><li>Extract <strong>text, tables, and images</strong> concurrently.</li><li>Package outputs into a <strong>multi-modal data structure</strong> (ready for chunking and indexing).</li></ul><p>By isolating this functionality into a dedicated module, we ensure modularity, reusability, and scalability as the pipeline evolves.</p><pre>```python<br>class AzureDocumentIntelligence:<br>    &quot;&quot;&quot;Ultra-optimized Azure Document Intelligence extractor (single-pass, PDF-optimized).&quot;&quot;&quot;<br>    def __init__(self, config: DIConfig):<br>        self.config = config<br>        self.executor = ThreadPoolExecutor(max_workers=config.max_workers)<br>        if config.validate():<br>            self.di_client = DocumentIntelligenceClient(<br>                endpoint=config.di_endpoint,<br>                credential=AzureKeyCredential(config.di_key)<br>            )<br>            if config.debug_extraction:<br>                logger.info(f&quot;✅ DI client initialized with {config.max_workers} workers&quot;)<br>        else:<br>            self.di_client = None<br>            logger.error(&quot;⚠️ DI client not initialized - missing credentials&quot;)<br><br>    def __enter__(self):<br>        return self<br><br>    def __exit__(self, exc_type, exc_val, exc_tb):<br>        if hasattr(self, &#39;executor&#39;):<br>            self.executor.shutdown(wait=True)<br><br>    @performance_timer<br>    def extract_from_pdf(self, pdf_bytes: bytes) -&gt; Tuple[List[Dict], List[Dict], List[Dict]]:<br>        if not self.di_client:<br>            raise ValueError(&quot;Document Intelligence client not initialized&quot;)<br>        t0 = time.time()<br>        poller = self.di_client.begin_analyze_document(<br>            &quot;prebuilt-layout&quot;,<br>            body=BytesIO(pdf_bytes),<br>            output_content_format=&quot;markdown&quot;,<br>            output=[&quot;figures&quot;],<br>        )<br>        result = poller.result()<br>        operation_id = poller.details.get(&quot;operation_id&quot;)<br>        pages = list(result.pages or [])<br>        tables = list(result.tables or [])<br>        figures = list(getattr(result, &#39;figures&#39;, []) or [])<br>        paragraphs = list(result.paragraphs or [])<br>        if self.config.debug_performance:<br>            logger.info(f&quot;⚡ Service call: {time.time() - t0:.2f}s | pages={len(pages)} tables={len(tables)} figures={len(figures)} paras={len(paragraphs)}&quot;)<br><br>        # Build ordered paragraph lists per page and caption candidates<br>        page_paragraphs_seq: Dict[int, List[str]] = {}<br>        caption_candidates: Dict[int, Dict[str, List[Tuple[int, str]]]] = {}<br>        if paragraphs:<br>            for p in paragraphs:<br>                try:<br>                    content = (p.content or &quot;&quot;).strip()<br>                    if not content or not p.bounding_regions:<br>                        continue<br>                    for br in p.bounding_regions:<br>                        page_num = br.page_number<br>                        plist = page_paragraphs_seq.setdefault(page_num, [])<br>                        idx = len(plist)<br>                        plist.append(content)<br>                        lower = content.lower()<br>                        cap_type = None<br>                        if lower.startswith((&quot;figure&quot;, &quot;fig.&quot;)):<br>                            cap_type = &quot;figure&quot;<br>                        elif lower.startswith((&quot;table&quot;, &quot;tab.&quot;)):<br>                            cap_type = &quot;table&quot;<br>                        if cap_type:<br>                            caption_candidates.setdefault(page_num, {&quot;figure&quot;: [], &quot;table&quot;: []})[cap_type].append((idx, content))<br>                except Exception:<br>                    continue<br><br>        def _context_slices(page_num: int, cap_idx: Optional[int]) -&gt; Tuple[List[str], List[str]]:<br>            plist = page_paragraphs_seq.get(page_num, [])<br>            if not plist:<br>                return [], []<br>            if cap_idx is None:<br>                above = plist[:3]<br>                below = plist[3:6]<br>                return above, below<br>            start_above = max(0, cap_idx - 3)<br>            above = plist[start_above:cap_idx]<br>            below = plist[cap_idx+1:cap_idx+4]<br>            return above, below<br><br>        table_caption_map: Dict[int, Tuple[str, List[str], List[str]]] = {}<br>        figure_caption_map: Dict[int, Tuple[str, List[str], List[str]]] = {}<br>        for tb in tables:<br>            try:<br>                page_num = tb.bounding_regions[0].page_number if tb.bounding_regions else 1<br>                cap_list = caption_candidates.get(page_num, {}).get(&quot;table&quot;, [])<br>                if cap_list:<br>                    cap_idx, caption = cap_list.pop(0)<br>                    above, below = _context_slices(page_num, cap_idx)<br>                else:<br>                    caption = &quot;&quot;<br>                    above, below = _context_slices(page_num, None)<br>                table_caption_map[id(tb)] = (caption, above, below)<br>            except Exception:<br>                table_caption_map[id(tb)] = (&quot;&quot;, [], [])<br>        for fg in figures:<br>            try:<br>                page_num = fg.bounding_regions[0].page_number if fg.bounding_regions else 1<br>                cap_list = caption_candidates.get(page_num, {}).get(&quot;figure&quot;, [])<br>                if cap_list:<br>                    cap_idx, caption = cap_list.pop(0)<br>                    above, below = _context_slices(page_num, cap_idx)<br>                else:<br>                    caption = &quot;&quot;<br>                    above, below = _context_slices(page_num, None)<br>                figure_caption_map[id(fg)] = (caption, above, below)<br>            except Exception:<br>                figure_caption_map[id(fg)] = (&quot;&quot;, [], [])<br><br>        if not tables and not figures and len(pages) &lt;= self.config.pdf_parallel_page_threshold:<br>            text_array = []<br>            if paragraphs:<br>                page_paras: Dict[int, List[str]] = {}<br>                for p in paragraphs:<br>                    if p.bounding_regions:<br>                        for br in p.bounding_regions:<br>                            page_paras.setdefault(br.page_number, []).append(p.content)<br>                for pg in sorted(page_paras.keys()):<br>                    joined = &quot;\n\n&quot;.join(page_paras[pg])<br>                    if joined.strip():<br>                        text_array.append({&quot;page_number&quot;: pg, &quot;markdown_text&quot;: joined})<br>            return text_array, [], []<br><br>        text_array: List[Dict] = []<br>        tables_array: List[Dict] = []<br>        images_array: List[Dict] = []<br><br>        enable_async = self.config.enable_async<br>        futures: List[concurrent.futures.Future] = []<br>        timeout = self.config.timeout_seconds<br>        exec_submit = self.executor.submit<br>        as_comp = as_completed<br><br>        page_paragraphs: Dict[int, List[str]] = {pg: plist for pg, plist in page_paragraphs_seq.items()}<br><br>        if pages:<br>            if enable_async and len(pages) &gt;= self.config.pdf_parallel_page_threshold:<br>                for pg in pages:<br>                    pg_num = pg.page_number<br>                    futures.append(exec_submit(self._assemble_page_text, pg_num, page_paragraphs.get(pg_num)))<br>            else:<br>                for pg in pages:<br>                    pg_num = pg.page_number<br>                    data = self._assemble_page_text(pg_num, page_paragraphs.get(pg_num))<br>                    if data: text_array.append(data)<br><br>        if tables:<br>            if enable_async and len(tables) &gt;= self.config.table_parallel_threshold:<br>                for tb in tables:<br>                    caption, above, below = table_caption_map.get(id(tb), (&quot;&quot;, [], []))<br>                    futures.append(exec_submit(self._extract_table_data, tb, caption, above, below))<br>            else:<br>                for tb in tables:<br>                    caption, above, below = table_caption_map.get(id(tb), (&quot;&quot;, [], []))<br>                    td = self._extract_table_data(tb, caption, above, below)<br>                    if td: tables_array.append(td)<br><br>        if figures:<br>            if enable_async and len(figures) &gt;= self.config.figure_parallel_threshold:<br>                for fg in figures:<br>                    caption, above, below = figure_caption_map.get(id(fg), (&quot;&quot;, [], []))<br>                    futures.append(exec_submit(self._extract_figure_data, fg, operation_id, result, caption, above, below))<br>            else:<br>                for fg in figures:<br>                    fd = self._extract_figure_data(fg, operation_id, result, *figure_caption_map.get(id(fg), (&quot;&quot;, [], [])))<br>                    if fd:<br>                        pg, img, caption, above, below = fd<br>                        images_array.append({&quot;page_number&quot;: pg, &quot;image_base64&quot;: img, &quot;caption&quot;: caption, &quot;context_above&quot;: above, &quot;context_below&quot;: below})<br><br>        if futures:<br>            for fut in as_completed(futures):<br>                try:<br>                    res = fut.result(timeout=timeout)<br>                    if not res:<br>                        continue<br>                    if isinstance(res, dict):<br>                        if &#39;markdown_text&#39; in res:<br>                            text_array.append(res)<br>                        elif &#39;markdown_table&#39; in res:<br>                            tables_array.append(res)<br>                    elif isinstance(res, tuple):<br>                        if len(res) == 5:<br>                            pg, img_b64, caption, above, below = res<br>                            images_array.append({&quot;page_number&quot;: pg, &quot;image_base64&quot;: img_b64, &quot;caption&quot;: caption, &quot;context_above&quot;: above, &quot;context_below&quot;: below})<br>                        elif len(res) == 3:<br>                            pg, img_b64, caption = res<br>                            images_array.append({&quot;page_number&quot;: pg, &quot;image_base64&quot;: img_b64, &quot;caption&quot;: caption, &quot;context_above&quot;: [], &quot;context_below&quot;: []})<br>                except Exception as e:<br>                    logger.error(f&quot;Worker failure: {e}&quot;)<br><br>        if self.config.debug_performance:<br>            logger.info(f&quot;📊 Aggregated PDF: text={len(text_array)} tables={len(tables_array)} images={len(images_array)} total_time={time.time() - t0:.2f}s&quot;)<br>        return text_array, tables_array, images_array<br><br>    def _assemble_page_text(self, page_num: int, para_list: Optional[List[str]]) -&gt; Optional[Dict]:<br>        if not para_list:<br>            return None<br>        joined = &quot;\n\n&quot;.join(para_list)<br>        if not joined.strip():<br>            return None<br>        return {&quot;page_number&quot;: page_num, &quot;markdown_text&quot;: joined}<br><br>    def _extract_page_text(self, result, page) -&gt; Optional[Dict]:<br>        try:<br>            page_num = page.page_number<br>            page_text = &quot;&quot;<br>            if result.paragraphs:<br>                page_paragraphs = [p for p in result.paragraphs if p.bounding_regions and any(br.page_number == page_num for br in p.bounding_regions)]<br>                page_text = &quot;\n\n&quot;.join([p.content for p in page_paragraphs])<br>            if page_text.strip():<br>                return {&quot;page_number&quot;: page_num, &quot;markdown_text&quot;: page_text}<br>            return None<br>        except Exception as e:<br>            logger.error(f&quot;Page text extraction failed: {e}&quot;)<br>            return None<br><br>    def _extract_table_data(self, table, caption: str = &quot;&quot;, context_above: Optional[List[str]] = None, context_below: Optional[List[str]] = None) -&gt; Optional[Dict]:<br>        try:<br>            if table.bounding_regions:<br>                page_num = table.bounding_regions[0].page_number<br>                table_md = self._table_to_markdown(table)<br>                return {&quot;page_number&quot;: page_num, &quot;markdown_table&quot;: table_md, &quot;caption&quot;: caption, &quot;context_above&quot;: context_above or [], &quot;context_below&quot;: context_below or []}<br>            return None<br>        except Exception as e:<br>            logger.error(f&quot;Table extraction failed: {e}&quot;)<br>            return None<br><br>    def _extract_figure_data(self, fig, operation_id, result, caption: str = &quot;&quot;, context_above: Optional[List[str]] = None, context_below: Optional[List[str]] = None) -&gt; Optional[Tuple[int, str, str, List[str], List[str]]]:<br>        try:<br>            pg = fig.bounding_regions[0].page_number if fig.bounding_regions else 1<br>            if fig.id and operation_id:<br>                if self.di_client:<br>                    png_stream = self.di_client.get_analyze_result_figure(<br>                        model_id=result.model_id,<br>                        result_id=operation_id,<br>                        figure_id=fig.id<br>                    )<br>                    if png_stream:<br>                        png_bytes = b&quot;&quot;.join(png_stream)<br>                    else:<br>                        return None<br>                else:<br>                    return None<br>                return int(pg), _b64_png(png_bytes), caption, (context_above or []), (context_below or [])<br>            return None<br>        except Exception as e:<br>            if self.config.debug_extraction:<br>                logger.error(f&quot;Figure extraction failed: {e}&quot;)<br>            return None<br><br>    @performance_timer<br>    def extract_from_docx(self, docx_bytes: bytes) -&gt; Tuple[List[Dict], List[Dict], List[Dict]]:<br>        doc = Document(BytesIO(docx_bytes))<br>        text_array: List[Dict] = []<br>        tables_array: List[Dict] = []<br>        images_array: List[Dict] = []<br>        page_num = 1<br>        paragraphs = [p.text.strip() for p in doc.paragraphs if p.text.strip()]<br>        if paragraphs:<br>            chunks = create_chunks(paragraphs, max(1, self.config.docx_chunk_size // 100))<br>            if self.config.enable_async and len(chunks) &gt; 1:<br>                futures = [self.executor.submit(self._process_docx_chunk, i, c) for i, c in enumerate(chunks, 1)]<br>                for f in as_completed(futures):<br>                    try:<br>                        r = f.result(timeout=self.config.timeout_seconds)<br>                        if r: text_array.append(r)<br>                    except Exception as e:<br>                        logger.error(f&quot;DOCX chunk fail: {e}&quot;)<br>            else:<br>                for i, c in enumerate(chunks, 1):<br>                    text_array.append({&quot;page_number&quot;: i, &quot;markdown_text&quot;: &quot;\n\n&quot;.join(c)})<br>        if doc.tables:<br>            if self.config.enable_async and len(doc.tables) &gt; 1:<br>                futures = [self.executor.submit(self._process_docx_table, page_num, t) for t in doc.tables]<br>                for f in as_completed(futures):<br>                    try:<br>                        r = f.result(timeout=self.config.timeout_seconds)<br>                        if r: tables_array.append(r)<br>                    except Exception as e:<br>                        logger.error(f&quot;DOCX table fail: {e}&quot;)<br>            else:<br>                for t in doc.tables:<br>                    tables_array.append({&quot;page_number&quot;: page_num, &quot;markdown_table&quot;: self._docx_table_to_markdown(t), &quot;caption&quot;: &quot;&quot;, &quot;context_above&quot;: [], &quot;context_below&quot;: []})<br>        image_rels = [rel for rel in doc.part.rels.values() if rel.reltype == RT.IMAGE]<br>        if image_rels:<br>            if self.config.enable_async and len(image_rels) &gt; 1:<br>                futures = [self.executor.submit(self._process_docx_image, page_num, rel) for rel in image_rels]<br>                for f in as_completed(futures):<br>                    try:<br>                        r = f.result(timeout=self.config.timeout_seconds)<br>                        if r: images_array.append(r)<br>                    except Exception as e:<br>                        logger.error(f&quot;DOCX image fail: {e}&quot;)<br>            else:<br>                for rel in image_rels:<br>                    try:<br>                        img_data = rel.target_part.blob<br>                        img_b64 = base64.b64encode(img_data).decode()<br>                        ct = getattr(rel.target_part, &#39;content_type&#39;, &#39;image/png&#39;)<br>                        images_array.append({&quot;page_number&quot;: page_num, &quot;image_base64&quot;: f&quot;data:{ct};base64,{img_b64}&quot;, &quot;caption&quot;: &quot;&quot;, &quot;context_above&quot;: [], &quot;context_below&quot;: []})<br>                    except Exception as e:<br>                        logger.error(f&quot;Image extraction fail: {e}&quot;)<br>        if self.config.debug_performance:<br>            logger.info(f&quot;📝 DOCX: {len(text_array)} text, {len(tables_array)} tables, {len(images_array)} images&quot;)<br>        return text_array, tables_array, images_array<br><br>    def _process_docx_chunk(self, page_num: int, chunk: List[str]) -&gt; Dict:<br>        return {&quot;page_number&quot;: page_num, &quot;markdown_text&quot;: &quot;\n\n&quot;.join(chunk)}<br><br>    def _process_docx_table(self, page_num: int, table) -&gt; Dict:<br>        return {&quot;page_number&quot;: page_num, &quot;markdown_table&quot;: self._docx_table_to_markdown(table), &quot;caption&quot;: &quot;&quot;, &quot;context_above&quot;: [], &quot;context_below&quot;: []}<br><br>    def _process_docx_image(self, page_num: int, rel) -&gt; Optional[Dict]:<br>        try:<br>            img_data = rel.target_part.blob<br>            img_b64 = base64.b64encode(img_data).decode()<br>            ct = getattr(rel.target_part, &#39;content_type&#39;, &#39;image/png&#39;)<br>            return {&quot;page_number&quot;: page_num, &quot;image_base64&quot;: f&quot;data:{ct};base64,{img_b64}&quot;, &quot;caption&quot;: &quot;&quot;, &quot;context_above&quot;: [], &quot;context_below&quot;: []}<br>        except Exception as e:<br>            logger.error(f&quot;DOCX image process fail: {e}&quot;)<br>            return None<br><br>    @performance_timer<br>    def extract_from_pptx(self, pptx_bytes: bytes) -&gt; Tuple[List[Dict], List[Dict], List[Dict]]:<br>        prs = Presentation(BytesIO(pptx_bytes))<br>        text_array: List[Dict] = []<br>        tables_array: List[Dict] = []<br>        images_array: List[Dict] = []<br>        if self.config.enable_async and len(prs.slides) &gt; 1:<br>            futures = [self.executor.submit(self._process_pptx_slide, i, s) for i, s in enumerate(prs.slides, 1)]<br>            for f in as_completed(futures):<br>                try:<br>                    sd = f.result(timeout=self.config.timeout_seconds)<br>                    if sd:<br>                        st, tt, im = sd<br>                        if st: text_array.append(st)<br>                        tables_array.extend(tt)<br>                        images_array.extend(im)<br>                except Exception as e:<br>                    logger.error(f&quot;Slide fail: {e}&quot;)<br>        else:<br>            for i, s in enumerate(prs.slides, 1):<br>                sd = self._process_pptx_slide(i, s)<br>                if sd:<br>                    st, tt, im = sd<br>                    if st: text_array.append(st)<br>                    tables_array.extend(tt)<br>                    images_array.extend(im)<br>        if self.config.debug_performance:<br>            logger.info(f&quot;📊 PPTX: {len(text_array)} text, {len(tables_array)} tables, {len(images_array)} images&quot;)<br>        return text_array, tables_array, images_array<br><br>    def _process_pptx_slide(self, slide_num: int, slide) -&gt; Optional[Tuple]:<br>        try:<br>            slide_text: List[str] = []<br>            slide_tables: List[Dict] = []<br>            slide_images: List[Dict] = []<br>            for shape in slide.shapes:<br>                if hasattr(shape, &quot;text&quot;) and shape.text.strip():<br>                    slide_text.append(shape.text.strip())<br>                if shape.shape_type == MSO_SHAPE_TYPE.TABLE:<br>                    slide_tables.append({&quot;page_number&quot;: slide_num, &quot;markdown_table&quot;: self._pptx_table_to_markdown(shape.table), &quot;caption&quot;: &quot;&quot;, &quot;context_above&quot;: [], &quot;context_below&quot;: []})<br>                elif shape.shape_type == MSO_SHAPE_TYPE.PICTURE:<br>                    try:<br>                        img_data = shape.image.blob<br>                        img_b64 = base64.b64encode(img_data).decode()<br>                        ct = shape.image.content_type<br>                        slide_images.append({&quot;page_number&quot;: slide_num, &quot;image_base64&quot;: f&quot;data:{ct};base64,{img_b64}&quot;, &quot;caption&quot;: &quot;&quot;, &quot;context_above&quot;: [], &quot;context_below&quot;: []})<br>                    except Exception as e:<br>                        logger.error(f&quot;Slide {slide_num} image fail: {e}&quot;)<br>            slide_text_data = None<br>            if slide_text:<br>                slide_text_data = {&quot;page_number&quot;: slide_num, &quot;markdown_text&quot;: &quot;\n\n&quot;.join(slide_text)}<br>            return slide_text_data, slide_tables, slide_images<br>        except Exception as e:<br>            logger.error(f&quot;Slide {slide_num} processing failed: {e}&quot;)<br>            return None<br><br>    def _table_to_markdown(self, table) -&gt; str:<br>        if not table.cells:<br>            return &quot;&quot;<br>        rows: Dict[int, Dict[int, str]] = {}<br>        for cell in table.cells:<br>            rows.setdefault(cell.row_index, {})[cell.column_index] = cell.content or &quot;&quot;<br>        md = []<br>        for r in sorted(rows.keys()):<br>            row = rows[r]<br>            max_col = max(row.keys()) if row else 0<br>            cells_line = [row.get(c, &quot;&quot;) for c in range(max_col + 1)]<br>            md.append(&quot;| &quot; + &quot; | &quot;.join(cells_line) + &quot; |&quot;)<br>            if r == 0:<br>                md.append(&quot;| &quot; + &quot; | &quot;.join([&quot;---&quot;] * len(cells_line)) + &quot; |&quot;)<br>        return &quot;\n&quot;.join(md)<br><br>    def _docx_table_to_markdown(self, table) -&gt; str:<br>        md = []<br>        for i, row in enumerate(table.rows):<br>            cells_line = [cell.text.strip() for cell in row.cells]<br>            md.append(&quot;| &quot; + &quot; | &quot;.join(cells_line) + &quot; |&quot;)<br>            if i == 0:<br>                md.append(&quot;| &quot; + &quot; | &quot;.join([&quot;---&quot;] * len(cells_line)) + &quot; |&quot;)<br>        return &quot;\n&quot;.join(md)<br><br>    def _pptx_table_to_markdown(self, table) -&gt; str:<br>        md = []<br>        for i, row in enumerate(table.rows):<br>            cells_line = [cell.text.strip() for cell in row.cells]<br>            md.append(&quot;| &quot; + &quot; | &quot;.join(cells_line) + &quot; |&quot;)<br>            if i == 0:<br>                md.append(&quot;| &quot; + &quot; | &quot;.join([&quot;---&quot;] * len(cells_line)) + &quot; |&quot;)<br>        return &quot;\n&quot;.join(md)<br><br>    @performance_timer<br>    def extract_document(self, file_bytes: bytes, filename: str) -&gt; Tuple[List[List], List[Dict], List[List]]:<br>        ext = os.path.splitext(filename.lower())[1]<br>        if self.config.debug_extraction:<br>            logger.info(f&quot;🚀 Single-pass extraction start: {filename}&quot;)<br>        if ext == &quot;.pdf&quot;:<br>            text_dicts, tables_dicts, images_dicts = self.extract_from_pdf(file_bytes)<br>        elif ext == &quot;.docx&quot;:<br>            text_dicts, tables_dicts, images_dicts = self.extract_from_docx(file_bytes)<br>        elif ext == &quot;.pptx&quot;:<br>            text_dicts, tables_dicts, images_dicts = self.extract_from_pptx(file_bytes)<br>        else:<br>            raise ValueError(f&quot;Unsupported file format: {ext}&quot;)<br>        text_array = [[d[&quot;page_number&quot;], d[&quot;markdown_text&quot;]] for d in text_dicts]<br>        # tables_array already list[dict]; keep as-is for richer metadata<br>        tables_array = tables_dicts<br>        page_images: Dict[int, List[Dict[str, str]]] = {}<br>        for d in images_dicts:<br>            page_images.setdefault(d[&quot;page_number&quot;], []).append({<br>                &quot;image_base64&quot;: d[&quot;image_base64&quot;],<br>                &quot;caption&quot;: d.get(&quot;caption&quot;, &quot;&quot;),<br>                &quot;context_above&quot;: d.get(&quot;context_above&quot;, []),<br>                &quot;context_below&quot;: d.get(&quot;context_below&quot;, [])<br>            })<br>        images_array = [[pg, imgs] for pg, imgs in page_images.items()]<br>        if self.config.debug_extraction:<br>            logger.info(f&quot;✅ Extraction finished: {len(text_array)} text pages, {len(tables_array)} tables, {len(images_array)} image pages&quot;)<br>        return text_array, tables_array, images_array<br><br>    def get_supported_formats(self) -&gt; List[str]:<br>        return [&quot;.pdf&quot;, &quot;.docx&quot;, &quot;.pptx&quot;]<br>```</pre><h4>🚀 Why This Matters</h4><p>This comprehensive <strong>Azure Document Intelligence integration</strong> provides:</p><ul><li><strong>Multi-format support</strong> — optimized processing for PDF, DOCX, and PPTX.</li><li><strong>Structure-aware extraction</strong> — preserving document hierarchy and reading order.</li><li><strong>High-confidence outputs</strong> — with quality scoring and filtering for reliability.</li><li><strong>Cross-modal relationships</strong> — linking text references to tables and images.</li><li><strong>Contextual understanding</strong> — capturing spatial relationships across content types.</li><li><strong>Scalable performance</strong> — async operations with configurable concurrency.</li></ul><p>Together, these capabilities ensure that documents are not just digitized, but <strong>intelligently understood</strong>, creating a solid foundation for downstream <strong>semantic search and retrieval-augmented generation (RAG)</strong>.</p><h3>Stage 2: Intelligent Text Segmentation</h3><p>Once documents have been <strong>extracted and structured</strong> by Azure Document Intelligence, the next step is to transform this content into <strong>optimally-sized text segments</strong> for <strong>Azure Cognitive Search’s vector indexing and retrieval</strong>.</p><p>This stage is more than just splitting text — it’s about <strong>preserving meaning and context</strong> while ensuring compatibility with <strong>Azure OpenAI’s token limits</strong> and <strong>embedding models</strong>.</p><p>The <strong>Intelligent Text Chunker</strong> applies several strategies:</p><ul><li><strong>Token-aware segmentation</strong> — splits text into manageable sizes without breaking semantic flow.</li><li><strong>Heading &amp; hierarchy preservation</strong> — maintains document structure for better contextual search.</li><li><strong>Overlapping segments</strong> — prevents information loss at chunk boundaries.</li><li><strong>Metadata enrichment</strong> — attaches file name, page number, and content type for precise attribution.</li></ul><p>By balancing <strong>semantic coherence</strong> with <strong>technical constraints</strong>, this stage ensures that each chunk can be <strong>indexed effectively</strong> and retrieved <strong>with context intact</strong>.</p><h4>Text Chunker (text_chunker.py)</h4><p>The <strong>Text Chunker</strong> is a dedicated Python module responsible for:</p><ul><li>Processing extracted document content.</li><li>Segmenting text using token-aware logic.</li><li>Preserving structural elements (headings, sections).</li><li>Enriching each chunk with metadata.</li><li>Preparing data for <strong>vector embeddings and Cognitive Search indexing</strong>.</li></ul><p>This component ensures that downstream stages — especially <strong>semantic retrieval</strong> and <strong>RAG with Azure OpenAI</strong> — operate on <strong>high-quality, contextually meaningful segments</strong>.</p><pre>```python<br>class TextChunker:<br>    &quot;&quot;&quot;Chunk document text into units with ONLY page_number &amp; type in metadata.<br><br>    Input can be:<br>      - List[str]<br>      - List[[page_number, text], ...]<br><br>    Output chunk dict:<br>      { &#39;id&#39;: str, &#39;content&#39;: str, &#39;metadata&#39;: { &#39;page_number&#39;: int, &#39;type&#39;: &#39;text&#39; } }<br>    &quot;&quot;&quot;<br>    DEFAULT_HEADING_REGEX = r&quot;^(?:#{1,6}\s+|[A-Z0-9][A-Z0-9 \-_/]{2,}$|\d+(?:\.\d+)*\s+.+:?$|[A-Z][A-Za-z0-9 &amp;\-/]+:)$&quot;<br>    SENTENCE_SPLIT_REGEX = re.compile(r&quot;(?&lt;=[.!?。؟])\s+(?=[A-Z0-9\[(])&quot;)<br><br>    def __init__(<br>        self,<br>        max_tokens: int = 800,<br>        min_tokens: int = 80,<br>        overlap_tokens: int = 60,<br>        max_sentence_tokens: int = 300,<br>        join_separator: str = &quot; &quot;,<br>        model_name: Optional[str] = None,<br>    ) -&gt; None:<br>        self.max_tokens = max_tokens<br>        self.min_tokens = min_tokens<br>        self.overlap_tokens = overlap_tokens<br>        self.max_sentence_tokens = max_sentence_tokens<br>        self.join_separator = join_separator<br>        self._encoding = None<br>        if _HAS_TIKTOKEN:<br>            try:<br>                if model_name:<br>                    self._encoding = tiktoken.encoding_for_model(model_name)<br>                else:<br>                    self._encoding = tiktoken.get_encoding(&quot;cl100k_base&quot;)<br>            except Exception:<br>                self._encoding = None<br>        self.heading_pattern = re.compile(self.DEFAULT_HEADING_REGEX)<br><br>    # Public API<br>    def chunk(self, text_arr: Union[List[str], List[Sequence[Any]]], file_name: Optional[str] = None) -&gt; List[Dict[str, Any]]:<br>        pages: List[int] = []<br>        raw_segments: List[str] = []<br>        if text_arr and isinstance(text_arr[0], (list, tuple)) and len(text_arr[0]) &gt;= 2 and isinstance(text_arr[0][0], int):<br>            for item in text_arr:  # type: ignore<br>                try:<br>                    p, txt = int(item[0]), str(item[1])<br>                except Exception:<br>                    continue<br>                if txt and txt.strip():<br>                    pages.append(p)<br>                    raw_segments.append(txt.strip())<br>        else:<br>            raw_segments = [str(x).strip() for x in text_arr if str(x).strip()]  # type: ignore<br>            pages = [1] * len(raw_segments)<br><br>        sentences = self._segment(raw_segments, pages)<br>        return self._build_chunks(sentences, file_name)<br><br>    def to_azure_documents(self, chunks: List[Dict[str, Any]], source_name: Optional[str] = None) -&gt; List[Dict[str, Any]]:<br>        docs = []<br>        for c in chunks:<br>            meta = dict(c[&#39;metadata&#39;])<br>            if source_name:<br>                meta.setdefault(&#39;source&#39;, source_name)  # optional extra if desired<br>            # Remove &#39;source&#39; if strictly only page_number/type wanted; comment line above out otherwise.<br>            docs.append({ &#39;id&#39;: c[&#39;id&#39;], &#39;content&#39;: c[&#39;content&#39;], &#39;metadata&#39;: meta })<br>        return docs<br><br>    # Internal helpers<br>    def _estimate_tokens(self, text: str) -&gt; int:<br>        if self._encoding is not None:<br>            try:<br>                return len(self._encoding.encode(text))<br>            except Exception:<br>                pass<br>        return max(1, len(text) // 4)<br><br>    def _split_sentences(self, paragraph: str) -&gt; List[str]:<br>        if len(paragraph) &lt; 120:<br>            return [paragraph]<br>        parts = self.SENTENCE_SPLIT_REGEX.split(paragraph.strip())<br>        merged: List[str] = []<br>        buf: List[str] = []<br>        for p in parts:<br>            if not p:<br>                continue<br>            buf.append(p)<br>            if len(&quot; &quot;.join(buf)) &gt; 60:<br>                merged.append(&quot; &quot;.join(buf))<br>                buf = []<br>        if buf:<br>            merged.append(&quot; &quot;.join(buf))<br>        return merged or [paragraph]<br><br>    def _segment(self, segments: List[str], pages: List[int]) -&gt; List[Dict[str, Any]]:<br>        sentences = []<br>        global_index = 0<br>        for seg_idx, (seg, pg) in enumerate(zip(segments, pages)):<br>            if self.heading_pattern.match(seg.strip()):<br>                sentences.append({<br>                    &#39;text&#39;: seg.strip(),<br>                    &#39;tokens&#39;: self._estimate_tokens(seg),<br>                    &#39;page&#39;: pg,<br>                    &#39;is_heading&#39;: True,<br>                    &#39;global_sentence_index&#39;: global_index,<br>                })<br>                global_index += 1<br>                continue<br>            for s in self._split_sentences(seg):<br>                tok = self._estimate_tokens(s)<br>                if tok &gt; self.max_sentence_tokens:<br>                    step = max(20, len(s) // (tok // self.max_sentence_tokens + 1))<br>                    for i in range(0, len(s), step):<br>                        piece = s[i:i+step]<br>                        sentences.append({<br>                            &#39;text&#39;: piece,<br>                            &#39;tokens&#39;: self._estimate_tokens(piece),<br>                            &#39;page&#39;: pg,<br>                            &#39;is_heading&#39;: False,<br>                            &#39;global_sentence_index&#39;: global_index,<br>                        })<br>                        global_index += 1<br>                else:<br>                    sentences.append({<br>                        &#39;text&#39;: s,<br>                        &#39;tokens&#39;: tok,<br>                        &#39;page&#39;: pg,<br>                        &#39;is_heading&#39;: False,<br>                        &#39;global_sentence_index&#39;: global_index,<br>                    })<br>                    global_index += 1<br>        return sentences<br><br>    def _build_chunks(self, sentences: List[Dict[str, Any]], file_name: Optional[str] = None) -&gt; List[Dict[str, Any]]:<br>        chunks: List[Dict[str, Any]] = []<br>        buf: List[Dict[str, Any]] = []<br>        buf_tokens = 0<br><br>        def flush(add_overlap: bool = True):<br>            nonlocal buf, buf_tokens<br>            if not buf:<br>                return<br>            content = self.join_separator.join([b[&#39;text&#39;] for b in buf]).strip()<br>            pages_spanned = sorted({b[&#39;page&#39;] for b in buf})<br>            representative_page = pages_spanned[0] if pages_spanned else 1<br>            metadata = {<br>                &#39;page_number&#39;: representative_page,<br>                &#39;type&#39;: &#39;text&#39;<br>            }<br>            if file_name:<br>                metadata[&#39;file_name&#39;] = file_name<br>            chunks.append({ &#39;id&#39;: str(uuid.uuid4()), &#39;content&#39;: content, &#39;metadata&#39;: metadata })<br>            if add_overlap and self.overlap_tokens &gt; 0:<br>                overlap_buf = []<br>                overlap_tok = 0<br>                for b in reversed(buf):<br>                    if overlap_tok + b[&#39;tokens&#39;] &gt; self.overlap_tokens:<br>                        break<br>                    overlap_buf.insert(0, b)<br>                    overlap_tok += b[&#39;tokens&#39;]<br>                buf = overlap_buf<br>                buf_tokens = overlap_tok<br>            else:<br>                buf = []<br>                buf_tokens = 0<br><br>        for sent in sentences:<br>            if buf_tokens + sent[&#39;tokens&#39;] &lt;= self.max_tokens:<br>                buf.append(sent)<br>                buf_tokens += sent[&#39;tokens&#39;]<br>            else:<br>                if buf_tokens &lt; self.min_tokens and buf:<br>                    buf.append(sent)<br>                    buf_tokens += sent[&#39;tokens&#39;]<br>                    flush(add_overlap=True)<br>                else:<br>                    flush(add_overlap=True)<br>                    buf.append(sent)<br>                    buf_tokens = sent[&#39;tokens&#39;]<br>        flush(add_overlap=False)<br>        return chunks<br>```</pre><h4>🚀 Why This Matters</h4><p>The <strong>Intelligent Text Segmentation</strong> stage delivers key features optimized for seamless <strong>Azure integration</strong>:</p><ul><li><strong>Token-aware chunking</strong> — uses <em>tiktoken</em> encoding for accurate Azure OpenAI compatibility.</li><li><strong>Semantic boundary preservation</strong> — keeps sentence and section integrity intact.</li><li><strong>Configurable overlap</strong> — ensures no information is lost at chunk boundaries.</li><li><strong>Metadata enrichment</strong> — adds file names and page references for precise search attribution.</li></ul><p>By combining <strong>semantic awareness</strong> with <strong>technical optimization</strong>, this stage ensures that every chunk is both <strong>retrieval-friendly</strong> for Cognitive Search and <strong>LLM-ready</strong> for Azure OpenAI.</p><h3>Stage 3: Azure OpenAI Vision Processing</h3><p>Not all critical information in business documents is textual — many insights live inside <strong>charts, diagrams, screenshots, and visual layouts</strong>. To capture this, the pipeline leverages <strong>Azure OpenAI’s advanced vision models</strong> for <strong>multi-modal image understanding</strong>.</p><p>This stage goes far beyond simple OCR or object recognition. Instead, it provides <strong>detailed summaries</strong> that capture both:</p><ul><li><strong>Explicit visual elements</strong> — such as chart data, labels, and diagram structures.</li><li><strong>Contextual meaning</strong> — how images relate to surrounding text and document sections.</li></ul><p>By incorporating these <strong>AI-powered image descriptions</strong> into the document index, the pipeline ensures that visual content becomes <strong>searchable and retrievable</strong>, just like text.</p><p>The <strong>Image Processor</strong> is optimized for performance and resilience:</p><ul><li><strong>Concurrent processing</strong> with configurable worker limits.</li><li><strong>Rate-limit aware orchestration</strong> tuned for Azure OpenAI APIs.</li><li><strong>Robust error handling</strong> with retry logic and exponential backoff.</li><li><strong>Fallback mechanisms</strong> to handle complex or problematic images gracefully.</li></ul><h4>Image Processor (image_processor.py)</h4><p>This dedicated Python module handles concurrent <strong>image analysis</strong> with <strong>Azure OpenAI GPT-4o vision capabilities</strong>, ensuring multi-modal content is fully captured. Its responsibilities include:</p><ul><li>Extracting and summarizing document images.</li><li>Preserving <strong>contextual relationships</strong> between text and visuals.</li><li>Returning <strong>structured descriptions</strong> for Cognitive Search indexing.</li><li>Maintaining throughput with <strong>async concurrency controls</strong>.</li></ul><p>By treating <strong>images as first-class data sources</strong>, this stage ensures the pipeline achieves <strong>true multi-modal document understanding</strong>.</p><pre>```python<br>class ImageSummarizer:<br>    def __init__(self, config: ImageSummarizerConfig):<br>        self.config = config<br>        self._client = None<br>        self._lock = threading.Lock()<br>        if AzureOpenAI is None:<br>            logger.warning(&quot;AzureOpenAI SDK not installed; summarizer inactive&quot;)<br>        if self.config.debug:<br>            logger.info(f&quot;ImageSummarizer init workers={self.config.max_workers} async={self.config.enable_async} retries={self.config.retries} responses_api={self.config.prefer_responses_api}&quot;)<br><br>    # --- Connection / client helpers ---<br>    def _get_client(self):<br>        if self._client is not None:<br>            return self._client<br>        if not (self.config.azure_openai_endpoint and self.config.azure_openai_api_key):<br>            raise ValueError(&quot;Azure OpenAI credentials missing&quot;)<br>        if AzureOpenAI is None:<br>            raise RuntimeError(&quot;AzureOpenAI SDK not available&quot;)<br>        try:<br>            self._client = AzureOpenAI(<br>                azure_endpoint=self.config.azure_openai_endpoint,<br>                api_key=self.config.azure_openai_api_key,<br>                api_version=self.config.azure_openai_api_version,<br>            )<br>            if self.config.debug:<br>                logger.info(&quot;✅ Azure OpenAI client ready (image summarizer)&quot;)<br>        except Exception as e:<br>            raise RuntimeError(f&quot;Azure OpenAI client init failed: {e}&quot;)<br>        return self._client<br><br>    def test_connection(self) -&gt; dict:<br>        &quot;&quot;&quot;Minimal ping test returning structured diagnostics.&quot;&quot;&quot;<br>        t0 = time.time()<br>        info = {<br>            &quot;model&quot;: self.config.azure_openai_image_model,<br>            &quot;endpoint&quot;: self.config.azure_openai_endpoint,<br>            &quot;api_version&quot;: self.config.azure_openai_api_version,<br>            &quot;success&quot;: False,<br>            &quot;path_used&quot;: None,<br>            &quot;latency_s&quot;: None,<br>        }<br>        try:<br>            client = self._get_client()<br>            prompt = &quot;Return OK&quot;<br>            # Try responses path<br>            if self.config.prefer_responses_api:<br>                try:<br>                    r = client.responses.create(<br>                        model=self.config.azure_openai_image_model,<br>                        input=[{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: [{&quot;type&quot;: &quot;input_text&quot;, &quot;text&quot;: prompt}]}],<br>                        max_output_tokens=5,<br>                    )<br>                    txt = getattr(r, &#39;output_text&#39;, &#39;&#39;).strip() if hasattr(r, &#39;output_text&#39;) else &#39;&#39;<br>                    if not txt and getattr(r, &#39;output&#39;, None):<br>                        for seg in r.output:<br>                            for c in getattr(seg, &#39;content&#39;, []) or []:<br>                                t = getattr(c, &#39;text&#39;, &#39;&#39;)<br>                                if t:<br>                                    txt = t.strip(); break<br>                            if txt: break<br>                    if txt:<br>                        info.update(success=True, path_used=&#39;responses&#39;, latency_s=round(time.time()-t0,3))<br>                        return info<br>                    info[&#39;responses_warn&#39;] = &#39;empty_output&#39;<br>                except Exception as er:<br>                    info[&#39;responses_error&#39;] = str(er)<br>            # Chat fallback<br>            try:<br>                comp = client.chat.completions.create(<br>                    model=self.config.azure_openai_image_model,<br>                    messages=[{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: [{&quot;type&quot;: &quot;text&quot;, &quot;text&quot;: prompt}]}],<br>                )<br>                msg = comp.choices[0].message.content.strip() if comp.choices else &#39;&#39;<br>                if msg:<br>                    info.update(success=True, path_used=&#39;chat&#39;, latency_s=round(time.time()-t0,3))<br>                else:<br>                    info.update(error=&#39;empty_chat_response&#39;, latency_s=round(time.time()-t0,3))<br>            except Exception as ce:<br>                info.update(error=str(ce), latency_s=round(time.time()-t0,3))<br>        except Exception as outer:<br>            info.update(error=str(outer), latency_s=round(time.time()-t0,3))<br>        return info<br><br>    # --- Internal helpers ---<br>    def _build_data_url(https://medium.com/self,%20img_b64:%20str) -&gt; str:<br>        if not img_b64:<br>            return &quot;&quot;<br>        if &quot;,&quot; in img_b64:<br>            prefix, rest = img_b64.split(&quot;,&quot;, 1)<br>            if prefix.startswith(&quot;data:&quot;): return img_b64<br>            return f&quot;data:image/png;base64,{rest}&quot;<br>        return f&quot;data:image/png;base64,{img_b64}&quot;<br><br>    def _extract_raw_b64(self, data_url: str) -&gt; str:<br>        if not data_url: return &quot;&quot;<br>        if &quot;,&quot; in data_url: return data_url.split(&quot;,&quot;, 1)[1]<br>        if re.fullmatch(r&#39;[A-Za-z0-9+/=]+&#39;, data_url): return data_url<br>        return &quot;&quot;<br><br>    def _chat_api_call(self, client, model: str, messages):<br>        return client.chat.completions.create(model=model, messages=messages)<br><br>    def _responses_api_call(self, client, model: str, prompt: str, raw_b64: str, max_tokens: int):<br>        return client.responses.create(<br>            model=model,<br>            input=[{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: [<br>                {&quot;type&quot;: &quot;input_text&quot;, &quot;text&quot;: prompt},<br>                {&quot;type&quot;: &quot;input_image&quot;, &quot;image_base64&quot;: raw_b64},<br>            ]}],<br>            max_output_tokens=max_tokens,<br>        )<br><br>    # --- Core single-image summarize ---<br>    def _summ_one(self, client, model: str, max_tokens: int, page_num: int, idx_on_page: int, payload: dict) -&gt; dict:<br>        caption = payload.get(&quot;caption&quot;, &quot;&quot;) or &quot;&quot;<br>        img_b64 = payload.get(&quot;image_base64&quot;, &quot;&quot;)<br>        if not img_b64:<br>            return {&quot;page_number&quot;: page_num, &quot;image_index&quot;: idx_on_page, &quot;image_caption&quot;: caption, &quot;image_summary&quot;: &quot;(no image data)&quot;, &quot;error&quot;: &quot;no_image_data&quot;, &quot;image_base64&quot;: &quot;&quot;}<br>        data_url = self._build_data_url(https://medium.com/img_b64)<br>        raw_b64 = self._extract_raw_b64(data_url)<br>        prompt_lines = [<br>            &quot;Provide a concise factual summary of the image.&quot;,<br>            &quot;List principal visual elements, any legible text, and quantitative cues.&quot;,<br>            &quot;No speculation; neutral technical tone.&quot;,<br>        ]<br>        if caption:<br>            prompt_lines.append(f&quot;Caption: {caption}&quot;)<br>        prompt = &quot;\n&quot;.join(prompt_lines)<br>        messages_chat = [{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: [<br>            {&quot;type&quot;: &quot;text&quot;, &quot;text&quot;: prompt},<br>            {&quot;type&quot;: &quot;image_url&quot;, &quot;image_url&quot;: {&quot;url&quot;: data_url}},<br>        ]}]<br>        last_error = None<br>        summary = &quot;&quot;<br>        method = None<br>        t_start = time.time()<br>        for attempt in range(self.config.retries + 1):<br>            try:<br>                if self.config.prefer_responses_api and raw_b64:<br>                    try:<br>                        resp = self._responses_api_call(client, model, prompt, raw_b64, min(max_tokens, 400))<br>                        txt = getattr(resp, &#39;output_text&#39;, &#39;&#39;).strip() if hasattr(resp, &#39;output_text&#39;) else &#39;&#39;<br>                        if not txt and getattr(resp, &#39;output&#39;, None):<br>                            for seg in resp.output:<br>                                for c in getattr(seg, &#39;content&#39;, []) or []:<br>                                    t = getattr(c, &#39;text&#39;, &#39;&#39;)<br>                                    if t:<br>                                        txt = t.strip(); break<br>                                if txt: break<br>                        if txt:<br>                            summary = txt; method = &#39;responses&#39;; break<br>                        last_error = &#39;empty_responses_output&#39;<br>                    except Exception as er:<br>                        last_error = f&quot;responses_api: {er}&quot;<br>                        if any(k in str(er).lower() for k in [&quot;unsupported&quot;, &quot;not found&quot;, &quot;404&quot;]):<br>                            self.config.prefer_responses_api = False<br>                if not summary:  # chat path<br>                    comp = self._chat_api_call(client, model, messages_chat)<br>                    summary = comp.choices[0].message.content.strip() if comp.choices else &#39;&#39;<br>                    if summary:<br>                        method = &#39;chat&#39;; break<br>                    last_error = &#39;empty_chat_response&#39;<br>            except Exception as e:<br>                last_error = str(e)<br>                if attempt &lt; self.config.retries:<br>                    time.sleep(self.config.retry_backoff ** attempt)<br>                else:<br>                    break<br>        if not summary:<br>            fb_prompt = prompt + &quot;\n(Image failed; produce generic conservative description.)&quot;<br>            try:<br>                fb = self._chat_api_call(client, model, [{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: [{&quot;type&quot;: &quot;text&quot;, &quot;text&quot;: fb_prompt}]}])<br>                summary = fb.choices[0].message.content.strip() if fb.choices else &#39;&#39;<br>                method = (method or &#39;chat&#39;) + &#39;+fallback&#39;<br>            except Exception as e2:<br>                summary = f&quot;(summary unavailable; fallback failed: {e2})&quot;<br>                last_error = f&quot;{last_error} | fallback: {e2}&quot; if last_error else f&quot;fallback: {e2}&quot;<br>        if not summary:<br>            summary = &quot;(summary unavailable)&quot;<br>        latency = round(time.time() - t_start, 3)<br>        result = {&quot;page_number&quot;: page_num, &quot;image_index&quot;: idx_on_page, &quot;image_caption&quot;: caption, &quot;image_summary&quot;: summary, &quot;method&quot;: method or &#39;unknown&#39;, &quot;latency_s&quot;: latency, &quot;image_base64&quot;: img_b64}<br>        if last_error and (&#39;unavailable&#39; in summary or &#39;error&#39; in summary.lower()):<br>            result[&#39;error&#39;] = last_error<br>        return result<br><br>    # --- Batch summarize ---<br>    def summarize(self, images_array: list) -&gt; list:<br>        client = self._get_client()<br>        model = self.config.azure_openai_image_model<br>        max_tokens = self.config.max_tokens<br>        tasks = []<br>        seen_hashes = set()<br>        for entry in images_array:<br>            try:<br>                page_num, imgs = entry<br>                for idx, img_payload in enumerate(imgs):<br>                    if not isinstance(img_payload, dict):<br>                        continue<br>                    if self.config.deduplicate_images:<br>                        b64v = img_payload.get(&#39;image_base64&#39;, &#39;&#39;)<br>                        if b64v:<br>                            hv = hash(b64v[:1500])<br>                            if hv in seen_hashes: continue<br>                            seen_hashes.add(hv)<br>                    tasks.append((page_num, idx, img_payload))<br>            except Exception as e:<br>                logger.error(f&quot;Traversal build error: {e}&quot;)<br>        if not tasks:<br>            return []<br>        effective_workers = min(self.config.max_workers, max(1, len(tasks)))<br>        results = []<br>        if self.config.enable_async and effective_workers &gt; 1:<br>            if self.config.debug:<br>                logger.info(f&quot;Submitting {len(tasks)} image tasks with {effective_workers} workers (dedup={self.config.deduplicate_images})&quot;)<br>            with ThreadPoolExecutor(max_workers=effective_workers) as ex:<br>                fmap = {ex.submit(self._summ_one, client, model, max_tokens, pg, idx, payload): (pg, idx, payload) for pg, idx, payload in tasks}<br>                for fut in as_completed(fmap):<br>                    try:<br>                        results.append(fut.result())<br>                    except Exception as e:<br>                        pg, idx, payload = fmap[fut]<br>                        results.append({&quot;page_number&quot;: pg, &quot;image_index&quot;: idx, &quot;image_caption&quot;: &quot;&quot;, &quot;image_summary&quot;: f&quot;(summary unavailable; executor error: {e})&quot;, &quot;error&quot;: str(e), &quot;image_base64&quot;: payload.get(&#39;image_base64&#39;, &#39;&#39;)})<br>        else:<br>            if self.config.debug:<br>                logger.info(f&quot;Sequential processing {len(tasks)} images&quot;)<br>            for pg, idx, payload in tasks:<br>                results.append(self._summ_one(client, model, max_tokens, pg, idx, payload))<br>        results.sort(key=lambda d: (d.get(&#39;page_number&#39;,0), d.get(&#39;image_index&#39;,0)))<br>        for r in results:<br>            if not r.get(&#39;image_summary&#39;):<br>                r[&#39;image_summary&#39;] = &#39;(summary unavailable)&#39;<br>            # ensure field present (safety for any legacy paths)<br>            r.setdefault(&#39;image_base64&#39;, &#39;&#39;)<br>        if self.config.debug:<br>            errs = sum(1 for r in results if &#39;error&#39; in r)<br>            avg_lat = sum(r.get(&#39;latency_s&#39;,0) for r in results)/max(1,len(results))<br>            logger.info(f&quot;Done {len(results)} images, errors={errs}, avg_latency={avg_lat:.3f}s&quot;)<br>        return results<br>```</pre><h4>🚀 Why This Matters</h4><p>The <strong>Azure OpenAI Vision Processing</strong> stage introduces powerful integration features that make image understanding both scalable and reliable:</p><ul><li><strong>Concurrent processing</strong> — configurable worker limits ensure optimal throughput.</li><li><strong>Smart endpoint handling</strong> — with fallback mechanisms across Azure deployments.</li><li><strong>Comprehensive error handling</strong> — retry logic with exponential backoff for resilience.</li><li><strong>Image context preservation</strong> — linking visual content back to the document structure.</li></ul><p>With these capabilities, the pipeline transforms <strong>visual information</strong> into <strong>searchable, context-aware knowledge</strong>, ensuring that charts, diagrams, and images contribute to a complete multi-modal understanding of enterprise documents.</p><h3>Stage 4: Azure Cognitive Search Indexing</h3><p>The fourth stage of our pipeline transforms processed document content into a <strong>searchable knowledge base</strong> using Azure Cognitive Search’s powerful <strong>vector and semantic search</strong> capabilities. Unlike traditional keyword-based search engines, this stage enables the system to understand <strong>meaning and context</strong>, making search results more accurate and relevant.</p><p>With Cognitive Search, we can:<br><strong>semantic similarity search</strong> — vector embeddings from Azure OpenAI capture context beyond keywords.<br><strong>Scale efficiently</strong> — HNSW (Hierarchical Navigable Small World) indexing ensures fast, high-dimensional search at scale.<br><strong>Optimize ingestion</strong> — concurrent batch uploads with configurable limits for performance tuning.<br><strong>Support multi-modal content</strong> — index text chunks, image summaries, and tables with enriched metadata.<br><strong>Preserve attribution</strong> — every indexed item links back to its original document source.</p><p>This stage builds a <strong>rich, cross-modal knowledge base</strong> that enables enterprise-grade document retrieval — whether the relevant information is hidden in <strong>paragraphs, charts, or tables</strong>.</p><h4><strong>Search Indexer (search_indexer.py)</strong></h4><p>At the core of Stage 4 is the <strong>Search Indexer module</strong>, which is responsible for:</p><ul><li>Generating embeddings with Azure OpenAI.</li><li>Uploading content into Azure Cognitive Search with batch optimization.</li><li>Supporting multi-modal indexing for text, tables, and images.</li><li>Enabling semantic, context-aware retrieval at scale.</li></ul><p>By isolating search indexing into this dedicated module, we ensure the pipeline is <strong>scalable, accurate, and optimized</strong> for enterprise knowledge retrieval.</p><pre>```python<br>class AzureIndexer:<br>    &quot;&quot;&quot;Manages document ingestion into Azure Cognitive Search.&quot;&quot;&quot;<br><br>    def __init__(self, config: AzureSearchConfig):<br>        self.config = config<br>        self.embeddings = AzureOpenAIEmbeddings(<br>            azure_deployment=config.azure_deployment_embeddings,<br>            openai_api_version=config.azure_api_version,<br>            azure_endpoint=config.azure_endpoint,<br>            api_key=config.azure_api_key,<br>        )<br>        self.embedding_function = self.embeddings.embed_query<br><br>        # ✅ Index fields<br>        self.fields = [<br>            SimpleField(name=&quot;id&quot;, type=SearchFieldDataType.String, key=True, filterable=True),<br>            SearchableField(name=&quot;content&quot;, type=SearchFieldDataType.String, searchable=True),<br>            SearchField(<br>                name=&quot;content_vector&quot;,<br>                type=SearchFieldDataType.Collection(SearchFieldDataType.Single),<br>                searchable=True,<br>                vector_search_dimensions=len(self.embedding_function(&quot;Text&quot;)),<br>                vector_search_profile_name=&quot;myHnswProfile&quot;,<br>            ),<br>            SearchableField(name=&quot;metadata&quot;, type=SearchFieldDataType.String, searchable=True),<br>            SearchableField(name=&quot;type&quot;, type=SearchFieldDataType.String, searchable=True),<br>            SimpleField(name=&quot;page_no&quot;, type=SearchFieldDataType.String, filterable=True),<br>            SearchableField(name=&quot;file_name&quot;, type=SearchFieldDataType.String, searchable=True, filterable=True),<br><br>            # Optional multimodal fields<br>            SimpleField(name=&quot;image_index&quot;, type=SearchFieldDataType.Int32, filterable=True, sortable=True, facetable=True),<br>            SearchableField(name=&quot;table_caption&quot;, type=SearchFieldDataType.String, searchable=True),<br>            SearchableField(name=&quot;image_caption&quot;, type=SearchFieldDataType.String, searchable=True),<br>            SearchableField(name=&quot;image_base64&quot;, type=SearchFieldDataType.String, searchable=False, filterable=False),<br>        ]<br><br>        # ✅ Index client for lifecycle management<br>        self.index_client = SearchIndexClient(<br>            endpoint=config.search_endpoint,<br>            credential=AzureKeyCredential(config.search_api_key),<br>        )<br><br>        # ✅ Vector store<br>        self.vector_store = AzureSearch(<br>            azure_search_endpoint=config.search_endpoint,<br>            azure_search_key=config.search_api_key,<br>            index_name=config.index_name,<br>            embedding_function=self.embedding_function,<br>            fields=self.fields,<br>        )<br><br>    async def delete_index_if_exists(self):<br>        &quot;&quot;&quot;Delete the index if it exists.&quot;&quot;&quot;<br>        try:<br>            await asyncio.to_thread(self.index_client.get_index, self.config.index_name)<br>            logger.info(f&quot;Index &#39;{self.config.index_name}&#39; exists → deleting...&quot;)<br>            await asyncio.to_thread(self.index_client.delete_index, self.config.index_name)<br>            logger.info(&quot;✅ Index deleted successfully.&quot;)<br>        except Exception:<br>            logger.info(f&quot;Index &#39;{self.config.index_name}&#39; does not exist. Skipping delete.&quot;)<br><br>    async def create_index(self, force: bool = False):<br>        &quot;&quot;&quot;Create index, optionally deleting if it already exists.&quot;&quot;&quot;<br>        if force:<br>            await self.delete_index_if_exists()<br><br>        try:<br>            await asyncio.to_thread(self.index_client.get_index, self.config.index_name)<br>            logger.info(f&quot;Index &#39;{self.config.index_name}&#39; already exists. Skipping creation.&quot;)<br>        except Exception:<br>            logger.info(f&quot;Creating index &#39;{self.config.index_name}&#39;...&quot;)<br>            index = SearchIndex(<br>                name=self.config.index_name,<br>                fields=self.fields,<br>                vector_search=VectorSearch(<br>                    algorithms=[HnswAlgorithmConfiguration(name=&quot;myHnsw&quot;)],<br>                    profiles=[VectorSearchProfile(name=&quot;myHnswProfile&quot;, algorithm_configuration_name=&quot;myHnsw&quot;)],<br>                ),<br>            )<br>            await asyncio.to_thread(self.index_client.create_index, index)<br>            logger.info(&quot;✅ Index created successfully.&quot;)<br><br>    # =====================<br>    # 🚀 Concurrent Upload<br>    # =====================<br>    async def _add_doc(self, doc: Document, semaphore: asyncio.Semaphore):<br>        &quot;&quot;&quot;Async helper to upload a single doc with concurrency control.&quot;&quot;&quot;<br>        async with semaphore:<br>            await asyncio.to_thread(self.vector_store.add_documents, [doc])<br>            logger.info(f&quot;✅ Uploaded doc (type={doc.metadata.get(&#39;type&#39;)}, page={doc.metadata.get(&#39;page_no&#39;)})&quot;)<br><br>    async def add_documents_concurrent(self, docs: List[Document], max_concurrency: int = 20):<br>        &quot;&quot;&quot;Upload all documents concurrently (one task per doc).&quot;&quot;&quot;<br>        semaphore = asyncio.Semaphore(max_concurrency)<br>        tasks = [self._add_doc(doc, semaphore) for doc in docs]<br>        await asyncio.gather(*tasks)<br><br>    # =====================<br>    # 🚀 High-level methods<br>    # =====================<br>    async def add_texts(self, text_chunks: List[dict], file_name: str = &quot;&quot;):<br>        docs = [<br>            Document(<br>                page_content=chunk[&quot;content&quot;],<br>                metadata={<br>                    &quot;id&quot;: chunk.get(&quot;id&quot;),<br>                    &quot;page_no&quot;: str(chunk[&quot;metadata&quot;][&quot;page_number&quot;]),<br>                    &quot;type&quot;: chunk[&quot;metadata&quot;][&quot;type&quot;],<br>                    &quot;file_name&quot;: chunk[&quot;metadata&quot;].get(&quot;file_name&quot;, file_name),<br>                    &quot;image_index&quot;: None,<br>                    &quot;table_caption&quot;: None,<br>                    &quot;image_caption&quot;: None,<br>                    &quot;image_base64&quot;: None,<br>                },<br>            )<br>            for chunk in text_chunks<br>        ]<br>        max_concurrency = getattr(self.config, &#39;azure_search_max_concurrent_uploads&#39;, 20)<br>        await self.add_documents_concurrent(docs, max_concurrency=max_concurrency)<br><br>    async def add_images(self, image_summaries: List[dict], file_name: str = &quot;&quot;):<br>        docs = [<br>            Document(<br>                page_content=img[&quot;image_summary&quot;],<br>                metadata={<br>                    &quot;page_no&quot;: str(img[&quot;page_number&quot;]),<br>                    &quot;type&quot;: &quot;image&quot;,<br>                    &quot;file_name&quot;: img.get(&quot;file_name&quot;, file_name),<br>                    &quot;image_index&quot;: img[&quot;image_index&quot;],<br>                    &quot;image_caption&quot;: img.get(&quot;image_caption&quot;),<br>                    &quot;image_base64&quot;: img.get(&quot;image_base64&quot;),<br>                    &quot;table_caption&quot;: None,<br>                },<br>            )<br>            for img in image_summaries<br>        ]<br>        max_concurrency = getattr(self.config, &#39;azure_search_max_concurrent_uploads&#39;, 20)<br>        await self.add_documents_concurrent(docs, max_concurrency=max_concurrency)<br><br>    async def add_tables(self, tables_arr: List[dict], file_name: str = &quot;&quot;):<br>        docs = [<br>            Document(<br>                page_content=table[&quot;markdown_table&quot;],<br>                metadata={<br>                    &quot;page_no&quot;: str(table[&quot;page_number&quot;]),<br>                    &quot;type&quot;: &quot;table&quot;,<br>                    &quot;file_name&quot;: table.get(&quot;file_name&quot;, file_name),<br>                    &quot;table_caption&quot;: table.get(&quot;caption&quot;),<br>                    &quot;image_index&quot;: None,<br>                    &quot;image_caption&quot;: None,<br>                    &quot;image_base64&quot;: None,<br>                },<br>            )<br>            for table in tables_arr<br>        ]<br>        max_concurrency = getattr(self.config, &#39;azure_search_max_concurrent_uploads&#39;, 20)<br>        await self.add_documents_concurrent(docs, max_concurrency=max_concurrency)<br>```</pre><h4>Why This Matters</h4><p>The <strong>Azure Cognitive Search Indexing</strong> stage introduces powerful optimization features that make enterprise-scale search both <strong>fast</strong> and <strong>context-aware</strong>:</p><ul><li><strong>HNSW vector indexing</strong> for high-performance semantic search.</li><li><strong>Batch upload optimization</strong> with configurable concurrency limits.</li><li><strong>Multi-modal content support</strong> indexing text, tables, and image summaries.</li><li><strong>File attribution</strong> ensuring every piece of content traces back to its source document.</li></ul><p>With these capabilities, the pipeline builds a <strong>rich, cross-modal knowledge base</strong>, enabling retrieval that understands both <strong>meaning</strong> and <strong>context</strong> across diverse document formats.</p><h3>Stage 5: Orchestrated Pipeline Execution</h3><p>The fifth stage of our pipeline acts as the <strong>command center</strong>, coordinating the full document processing workflow across Azure Document Intelligence, intelligent text segmentation, OpenAI vision processing, and Cognitive Search indexing. Unlike isolated services that run independently, the orchestrator ensures <strong>synchronization, reliability, and scalability</strong> across all components.</p><p>With the orchestrator, we can:<br> <strong>Maximize throughput</strong> — async/await patterns with semaphore-controlled concurrency.<br> <strong>Ensure resilience</strong> — retry logic, error recovery, and environment validation.<br> <strong>Track performance</strong> — progress monitoring, metrics collection, and logging.<br> <strong>Manage dependencies</strong> — coordinate execution across extraction, chunking, vision, and indexing.<br> <strong>Scale dynamically</strong> — adjust concurrency for both single documents and large batch workloads.</p><p>This orchestration layer ensures that the pipeline runs <strong>smoothly at enterprise scale</strong>, handling everything from a single uploaded contract to thousands of concurrent research papers without bottlenecks or failures.</p><h4><strong>Main Orchestrator (main_ingestion.py)</strong></h4><p>At the core of Stage 5 is the <strong>Main Orchestrator module</strong>, responsible for</p><ul><li>Coordinating all pipeline stages asynchronously.</li><li>Managing concurrency with configurable worker limits.</li><li>Validating connections and environment setup.</li><li>Providing detailed execution logs and metrics.</li></ul><p>By consolidating orchestration into this dedicated module, we ensure that the system remains <strong>modular, fault-tolerant, and production-ready</strong> as new components and services are added.</p><pre>```python<br>class DocumentIngestionPipeline:<br>    &quot;&quot;&quot;Main pipeline orchestrator for document ingestion.<br>    <br>    This class coordinates the complete document processing workflow including<br>    extraction, chunking, image processing, and search indexing with full<br>    async/concurrent support.<br>    &quot;&quot;&quot;<br>    <br>    def __init__(self, config: PipelineConfig) -&gt; None:<br>        &quot;&quot;&quot;Initialize the pipeline with configuration.<br>        <br>        Args:<br>            config: Pipeline configuration settings<br>            <br>        Raises:<br>            ValueError: If component initialization fails<br>        &quot;&quot;&quot;<br>        self.config = config<br>        self.logger = logging.getLogger(self.__class__.__name__)<br>        <br>        self._init_components()<br>    <br>    def _init_components(self) -&gt; None:<br>        &quot;&quot;&quot;Initialize all pipeline components.<br>        <br>        Raises:<br>            ValueError: If any component configuration is invalid<br>            Exception: If component initialization fails<br>        &quot;&quot;&quot;<br>        try:<br>            self.di_config = DIConfig.from_environment()<br>            if not self.di_config.validate():<br>                raise ValueError(&quot;Document Intelligence configuration validation failed&quot;)<br>            <br>            self.image_config = ImageSummarizerConfig.from_environment()<br>            <br>            self.search_config = AzureSearchConfig.from_env()<br>            <br>            self.document_extractor = AzureDocumentIntelligence(self.di_config)<br>            self.text_chunker = TextChunker(<br>                max_tokens=self.config.chunk_size,<br>                overlap_tokens=self.config.chunk_overlap<br>            )<br>            self.image_processor = ImageSummarizer(self.image_config)<br>            self.search_indexer = AzureIndexer(self.search_config)<br>            if hasattr(self.search_indexer, &#39;config&#39;):<br>                self.search_indexer.config.azure_search_max_concurrent_uploads = self.config.azure_search_max_concurrent_uploads<br>            <br>            self.logger.info(&quot;✅ All pipeline components initialized successfully&quot;)<br>            <br>        except Exception as e:<br>            self.logger.error(f&quot;❌ Failed to initialize pipeline components: {e}&quot;)<br>            raise<br>    <br>    def validate_environment(self) -&gt; bool:<br>        &quot;&quot;&quot;Validate that all required environment variables are set.<br>        <br>        Returns:<br>            True if all required environment variables are present, False otherwise<br>        &quot;&quot;&quot;<br>        required_vars = [<br>            &quot;DOCUMENTINTELLIGENCE_ENDPOINT&quot;,<br>            &quot;DOCUMENTINTELLIGENCE_API_KEY&quot;,<br>            &quot;AZURE_OPENAI_ENDPOINT&quot;,<br>            &quot;AZURE_OPENAI_API_KEY&quot;,<br>            &quot;AZURE_SEARCH_ENDPOINT&quot;,<br>            &quot;AZURE_SEARCH_API_KEY&quot;<br>        ]<br>        <br>        missing_vars = []<br>        for var in required_vars:<br>            if not os.getenv(var):<br>                missing_vars.append(var)<br>        <br>        if missing_vars:<br>            self.logger.error(f&quot;❌ Missing required environment variables: {missing_vars}&quot;)<br>            return False<br>        <br>        self.logger.info(&quot;✅ Environment validation passed&quot;)<br>        return True<br>    <br>    async def test_connections(self) -&gt; bool:<br>        &quot;&quot;&quot;Test connections to all services.&quot;&quot;&quot;<br>        self.logger.info(&quot;🔍 Testing service connections...&quot;)<br>        <br>        try:<br>            image_test = await asyncio.to_thread(self.image_processor.test_connection)<br>            if not image_test.get(&#39;success&#39;):<br>                self.logger.warning(f&quot;⚠️ Image processor connection test failed: {image_test}&quot;)<br>                return False<br>            <br>            self.logger.info(&quot;✅ All service connections successful&quot;)<br>            return True<br>            <br>        except Exception as e:<br>            self.logger.error(f&quot;❌ Connection test failed: {e}&quot;)<br>            return False<br>    <br>    async def process_document(self, file_path: str) -&gt; Dict[str, Any]:<br>        &quot;&quot;&quot;Process a single document through the complete pipeline.&quot;&quot;&quot;<br>        start_time = time.time()<br>        file_path_obj = Path(file_path)<br>        <br>        if not file_path_obj.exists():<br>            raise FileNotFoundError(f&quot;Document not found: {file_path}&quot;)<br>        <br>        self.logger.info(f&quot;📄 Processing document: {file_path_obj.name}&quot;)<br>        <br>        try:<br>            self.logger.info(&quot;🔍 Extracting document content...&quot;)<br>            with open(file_path, &quot;rb&quot;) as f:<br>                file_bytes = f.read()<br>            <br>            text_array, tables_array, images_array = await asyncio.to_thread(<br>                self.document_extractor.extract_document, file_bytes, file_path_obj.name<br>            )<br>            <br>            self.logger.info(f&quot;📊 Extracted: {len(text_array)} text segments, {len(tables_array)} tables, {len(images_array)} images&quot;)<br>            <br>            self.logger.info(&quot;✂️ Chunking text content...&quot;)<br>            text_chunks = await asyncio.to_thread(<br>                self.text_chunker.chunk, text_array, file_path_obj.name<br>            )<br>            self.logger.info(f&quot;📝 Created {len(text_chunks)} text chunks&quot;)<br>            <br>            image_summaries = []<br>            if images_array:<br>                self.logger.info(&quot;🖼️ Processing images...&quot;)<br>                images_for_processing = []<br>                for page_num, imgs in images_array:<br>                    for img in imgs:<br>                        if isinstance(img, dict):<br>                            img[&#39;file_name&#39;] = file_path_obj.name<br>                        images_for_processing.append((page_num, [img]))<br>                <br>                image_summaries = await asyncio.to_thread(<br>                    self.image_processor.summarize, images_for_processing<br>                )<br>                <br>                for summary in image_summaries:<br>                    summary[&#39;file_name&#39;] = file_path_obj.name<br>                <br>                self.logger.info(f&quot;🎨 Generated {len(image_summaries)} image summaries&quot;)<br>            <br>            for table in tables_array:<br>                table[&#39;file_name&#39;] = file_path_obj.name<br>            <br>            self.logger.info(&quot;🔍 Indexing content to Azure Search...&quot;)<br>            <br>            await self.search_indexer.create_index(force=self.config.force_recreate_index)<br>            <br>            index_tasks = []<br>            if text_chunks:<br>                index_tasks.append(self.search_indexer.add_texts(text_chunks, file_path_obj.name))<br>            if tables_array:<br>                index_tasks.append(self.search_indexer.add_tables(tables_array, file_path_obj.name))<br>            if image_summaries:<br>                index_tasks.append(self.search_indexer.add_images(image_summaries, file_path_obj.name))<br>            <br>            if index_tasks:<br>                await asyncio.gather(*index_tasks)<br>            <br>            processing_time = time.time() - start_time<br>            <br>            result = {<br>                &#39;file_path&#39;: str(file_path_obj),<br>                &#39;file_name&#39;: file_path_obj.name,<br>                &#39;text_chunks&#39;: len(text_chunks),<br>                &#39;tables&#39;: len(tables_array),<br>                &#39;images&#39;: len(image_summaries),<br>                &#39;processing_time_seconds&#39;: round(processing_time, 2),<br>                &#39;status&#39;: &#39;success&#39;<br>            }<br>            <br>            self.logger.info(f&quot;✅ Document processed successfully in {processing_time:.2f}s&quot;)<br>            return result<br>            <br>        except Exception as e:<br>            self.logger.error(f&quot;❌ Failed to process document {file_path_obj.name}: {e}&quot;)<br>            return {<br>                &#39;file_path&#39;: str(file_path_obj),<br>                &#39;file_name&#39;: file_path_obj.name,<br>                &#39;status&#39;: &#39;error&#39;,<br>                &#39;error&#39;: str(e),<br>                &#39;processing_time_seconds&#39;: time.time() - start_time<br>            }<br>    <br>    async def process_batch(self, directory_path: str, file_patterns: Optional[List[str]] = None) -&gt; List[Dict[str, Any]]:<br>        &quot;&quot;&quot;Process multiple documents in a directory concurrently.&quot;&quot;&quot;<br>        directory = Path(directory_path)<br>        <br>        if not directory.exists():<br>            raise FileNotFoundError(f&quot;Directory not found: {directory}&quot;)<br>        <br>        if file_patterns is None:<br>            file_patterns = [&#39;*.pdf&#39;, &#39;*.docx&#39;, &#39;*.pptx&#39;]<br>        <br>        files = []<br>        for pattern in file_patterns:<br>            files.extend(directory.glob(pattern))<br>        <br>        if not files:<br>            self.logger.warning(f&quot;⚠️ No matching files found in {directory}&quot;)<br>            return []<br>        <br>        self.logger.info(f&quot;📁 Found {len(files)} files to process&quot;)<br>        <br>        semaphore = asyncio.Semaphore(self.config.max_workers)<br>        <br>        async def process_with_semaphore(file_path: Path, index: int) -&gt; Dict[str, Any]:<br>            async with semaphore:<br>                self.logger.info(f&quot;📄 Processing file {index}/{len(files)}: {file_path.name}&quot;)<br>                return await self.process_document(str(file_path))<br>        <br>        tasks = [<br>            process_with_semaphore(file_path, i + 1) <br>            for i, file_path in enumerate(files)<br>        ]<br>        <br>        results = await asyncio.gather(*tasks, return_exceptions=True)<br>        <br>        processed_results = []<br>        for i, result in enumerate(results):<br>            if isinstance(result, Exception):<br>                self.logger.error(f&quot;❌ Failed to process {files[i].name}: {result}&quot;)<br>                processed_results.append({<br>                    &#39;file_path&#39;: str(files[i]),<br>                    &#39;file_name&#39;: files[i].name,<br>                    &#39;status&#39;: &#39;error&#39;,<br>                    &#39;error&#39;: str(result),<br>                    &#39;processing_time_seconds&#39;: 0<br>                })<br>            else:<br>                processed_results.append(result)<br>        <br>        successful = sum(1 for r in processed_results if r[&#39;status&#39;] == &#39;success&#39;)<br>        failed = len(processed_results) - successful<br>        total_time = sum(r.get(&#39;processing_time_seconds&#39;, 0) for r in processed_results)<br>        <br>        self.logger.info(f&quot;📊 Batch processing complete: {successful} successful, {failed} failed, {total_time:.2f}s total&quot;)<br>        <br>        return processed_results<br>```</pre><h4>Orchestration Highlights</h4><p>The orchestrated pipeline execution introduces enterprise-grade control and reliability with:</p><ul><li><strong>Async/await concurrency</strong> with semaphore limits for balanced throughput.</li><li><strong>Error recovery and retry logic</strong> to ensure resilience during large-scale operations.</li><li><strong>Dynamic scaling</strong> for both single-document and batch processing workloads.</li><li><strong>Environment validation</strong> with connection testing before execution.</li><li><strong>Detailed monitoring</strong> through logs, metrics, and progress tracking.</li></ul><p>With these capabilities, the orchestrator ensures the pipeline runs <strong>smoothly, fault-tolerantly, and at scale</strong>, making it production-ready for real enterprise deployments.</p><h3>Stage 6: Retrieval-Augmented Generation (RAG)</h3><p>The final stage brings the pipeline full circle — turning processed, indexed knowledge into <strong>intelligent answers</strong>. When a user submits a query, the system doesn’t just run keyword search. Instead, it uses <strong>vector-based semantic retrieval</strong> to surface the most relevant text chunks, table data, and image summaries from Azure Cognitive Search.</p><p>These multi-modal results are then assembled into a <strong>context-rich prompt</strong> for Azure OpenAI’s LLMs, enabling the model to reason over structured and unstructured content alike. Importantly, the responses aren’t “black box” answers — every output is tied back to its <strong>source document</strong>, ensuring enterprise-grade trust and transparency.</p><p>With this approach, the system delivers:<br><strong>Semantic relevance</strong> — retrieving answers based on meaning, not just keywords.<br><strong>Multi-modal context assembly</strong> — blending text, tables, and visuals for richer responses.<br><strong>Source attribution</strong> — grounding every answer in its original document.<br><strong>Enterprise-ready precision</strong> — reliable, explainable insights from complex collections.</p><h4>RAG Engine (document_retrieve.py)</h4><p>This component powers the query-to-answer workflow by:</p><ul><li>Generating embeddings for incoming queries.</li><li>Running top-K vector similarity search on Azure Cognitive Search.</li><li>Assembling context windows with token-aware management.</li><li>Calling Azure OpenAI LLMs for contextual, source-cited answers.</li></ul><p>By combining <strong>retrieval precision</strong> with <strong>generative intelligence</strong>, the RAG stage transforms a static knowledge base into a <strong>living assistant</strong> — capable of answering questions, explaining insights, and guiding decision-making with confidence.</p><pre>class DocumentRetriever:<br>    &quot;&quot;&quot;Azure Document Retrieval Pipeline for RAG-based question answering.<br>    <br>    Provides comprehensive document retrieval capabilities using Azure Cognitive Search<br>    and Azure OpenAI for embedding generation and chat completion. Supports multi-modal<br>    content including text, tables, and images with structured context formatting.<br>    &quot;&quot;&quot;<br>    <br>    def __init__(self, config: DocumentRetrieverConfig):<br>        &quot;&quot;&quot;Initialize the document retriever with Azure services.<br>        <br>        Args:<br>            config: Configuration containing Azure service credentials and settings<br>        &quot;&quot;&quot;<br>        self.config = config<br>        self._embeddings: Optional[AzureOpenAIEmbeddings] = None<br>        self._vector_store: Optional[AzureSearch] = None<br>        self._llm: Optional[AzureChatOpenAI] = None<br>        <br>        if config.debug:<br>            logger.info(&quot;Initializing DocumentRetriever with configuration&quot;)<br>    <br>    @property<br>    def embeddings(self) -&gt; AzureOpenAIEmbeddings:<br>        &quot;&quot;&quot;Lazy-loaded Azure OpenAI embeddings client.&quot;&quot;&quot;<br>        if self._embeddings is None:<br>            self._embeddings = AzureOpenAIEmbeddings(<br>                azure_deployment=self.config.azure_deployment_embeddings,<br>                openai_api_version=self.config.azure_api_version,<br>                azure_endpoint=self.config.azure_endpoint,<br>                api_key=self.config.azure_api_key,<br>            )<br>            if self.config.debug:<br>                logger.info(&quot;Initialized Azure OpenAI embeddings client&quot;)<br>        return self._embeddings<br>    <br>    @property<br>    def vector_store(self) -&gt; AzureSearch:<br>        &quot;&quot;&quot;Lazy-loaded Azure Cognitive Search vector store.&quot;&quot;&quot;<br>        if self._vector_store is None:<br>            self._vector_store = AzureSearch(<br>                azure_search_endpoint=self.config.search_endpoint,<br>                azure_search_key=self.config.search_api_key,<br>                index_name=self.config.index_name,<br>                embedding_function=self.embeddings.embed_query,<br>            )<br>            if self.config.debug:<br>                logger.info(f&quot;Initialized Azure Search vector store for index: {self.config.index_name}&quot;)<br>        return self._vector_store<br>    <br>    @property<br>    def llm(self) -&gt; AzureChatOpenAI:<br>        &quot;&quot;&quot;Lazy-loaded Azure OpenAI chat completion client.&quot;&quot;&quot;<br>        if self._llm is None:<br>            self._llm = AzureChatOpenAI(<br>                azure_deployment=self.config.deployment_name,<br>                api_key=self.config.azure_api_key,<br>                azure_endpoint=self.config.azure_endpoint,<br>                api_version=self.config.azure_api_version,<br>                temperature=self.config.temperature,<br>                max_tokens=self.config.max_tokens,<br>            )<br>            if self.config.debug:<br>                logger.info(f&quot;Initialized Azure OpenAI chat client: {self.config.deployment_name}&quot;)<br>        return self._llm<br><br>    def retrieve_documents(self, query: str, k: Optional[int] = None) -&gt; List[Document]:<br>        &quot;&quot;&quot;Retrieve top-k documents from Azure Cognitive Search using vector similarity.<br>        <br>        Args:<br>            query: Search query string<br>            k: Number of documents to retrieve (defaults to config.k)<br>            <br>        Returns:<br>            List of retrieved documents with metadata<br>            <br>        Raises:<br>            ValueError: If query is empty or k is invalid<br>        &quot;&quot;&quot;<br>        if not query.strip():<br>            raise ValueError(&quot;Query cannot be empty&quot;)<br>        <br>        k = k or self.config.k<br>        if k &lt;= 0 or k &gt; 50:<br>            raise ValueError(f&quot;Invalid k value: {k}. Must be between 1 and 50&quot;)<br>        <br>        if self.config.debug:<br>            logger.info(f&quot;Retrieving top {k} documents for query: {query[:100]}...&quot;)<br>        <br>        try:<br>            docs = self.vector_store.similarity_search(query, k=k)<br>            logger.info(f&quot;Successfully retrieved {len(docs)} documents&quot;)<br>            return docs<br>        except Exception as e:<br>            logger.error(f&quot;Failed to retrieve documents: {str(e)}&quot;)<br>            raise<br><br>    def estimate_tokens(self, text: str) -&gt; int:<br>        &quot;&quot;&quot;Estimate token count for text (rough approximation: 1 token ≈ 4 characters).<br>        <br>        Args:<br>            text: Text to estimate tokens for<br>            <br>        Returns:<br>            Estimated token count<br>        &quot;&quot;&quot;<br>        return len(text) // 4<br><br>    def format_context(self, docs: List[Document]) -&gt; str:<br>        &quot;&quot;&quot;Format retrieved documents into structured context for LLM processing with token management.<br>        <br>        Args:<br>            docs: List of retrieved documents with metadata<br>            <br>        Returns:<br>            Formatted context string with document type-specific formatting, truncated if needed<br>        &quot;&quot;&quot;<br>        if not docs:<br>            return &quot;No relevant documents found.&quot;<br>        <br>        context_parts = []<br>        total_tokens = 0<br>        max_context_tokens = 60000  # Conservative limit to leave room for response<br>        <br>        for i, doc in enumerate(docs, 1):<br>            meta = doc.metadata<br>            doc_type = meta.get(&quot;type&quot;, &quot;text&quot;)<br>            page_no = meta.get(&quot;page_no&quot;, &quot;Unknown&quot;)<br>            file_name = meta.get(&quot;file_name&quot;, &quot;Unknown&quot;)<br><br>            if doc_type == &quot;text&quot;:<br>                doc_context = (<br>                    f&quot;Document {i} [File: {file_name}, Page: {page_no}]\n&quot;<br>                    f&quot;Content: {doc.page_content}&quot;<br>                )<br><br>            elif doc_type == &quot;table&quot;:<br>                caption = meta.get(&quot;table_caption&quot;, &quot;No caption available&quot;)<br>                doc_context = (<br>                    f&quot;Document {i} [File: {file_name}, Page: {page_no}] - TABLE\n&quot;<br>                    f&quot;Caption: {caption}\n&quot;<br>                    f&quot;Content:\n{doc.page_content}&quot;<br>                )<br><br>            elif doc_type == &quot;image&quot;:<br>                img_index = meta.get(&quot;image_index&quot;, &quot;Unknown&quot;)<br>                caption = meta.get(&quot;image_caption&quot;, &quot;No caption available&quot;)<br>                summary = doc.page_content<br>                image_base64 = meta.get(&quot;image_base64&quot;, &quot;&quot;)<br>                <br>                doc_context = (<br>                    f&quot;Document {i} [File: {file_name}, Page: {page_no}] - IMAGE {img_index}\n&quot;<br>                    f&quot;Caption: {caption}\n&quot;<br>                    f&quot;Summary: {summary}&quot;<br>                )<br>                <br>                if self.config.include_image_data and image_base64:<br>                    image_tokens = self.estimate_tokens(image_base64)<br>                    <br>                    if total_tokens + image_tokens &lt; max_context_tokens - 5000:<br>                        display_b64 = image_base64[:100] + &quot;...&quot; if len(image_base64) &gt; 100 else image_base64<br>                        doc_context += f&quot;\nImage Data: {display_b64}&quot;<br>                        doc_context += f&quot;\n[VISION_MODEL_IMAGE_DATA]{image_base64}[/VISION_MODEL_IMAGE_DATA]&quot;<br>                        if self.config.debug:<br>                            logger.info(f&quot;Including image data for document {i} (~{image_tokens} tokens)&quot;)<br>                    else:<br>                        if self.config.debug:<br>                            logger.warning(f&quot;Skipping image data for document {i} to stay within token limits (~{image_tokens} tokens)&quot;)<br>            else:<br>                doc_context = f&quot;Document {i} [File: {file_name}, Page: {page_no}] - UNKNOWN TYPE\nContent: {doc.page_content}&quot;<br>            <br>            doc_tokens = self.estimate_tokens(doc_context)<br>            if total_tokens + doc_tokens &gt; max_context_tokens:<br>                if self.config.debug:<br>                    logger.warning(f&quot;Truncating context at document {i} to stay within token limits&quot;)<br>                break<br>            <br>            context_parts.append(doc_context)<br>            total_tokens += doc_tokens<br>            <br>            if self.config.debug:<br>                logger.info(f&quot;Document {i}: ~{doc_tokens} tokens (total: ~{total_tokens})&quot;)<br><br>        final_context = &quot;\n\n&quot;.join(context_parts)<br>        <br>        if self.config.debug:<br>            logger.info(f&quot;Final context: ~{self.estimate_tokens(final_context)} tokens&quot;)<br>        <br>        return final_context<br><br>    def answer_question(self, question: str, k: Optional[int] = None, <br>                       custom_prompt: Optional[str] = None) -&gt; Dict[str, Any]:<br>        &quot;&quot;&quot;Generate an answer to a question using retrieval-augmented generation with token management.<br>        <br>        Args:<br>            question: Question to answer<br>            k: Number of documents to retrieve (defaults to config.k)<br>            custom_prompt: Optional custom prompt template<br>            <br>        Returns:<br>            Dictionary containing answer, retrieved documents, and metadata<br>            <br>        Raises:<br>            ValueError: If question is empty<br>        &quot;&quot;&quot;<br>        if not question.strip():<br>            raise ValueError(&quot;Question cannot be empty&quot;)<br>        <br>        if self.config.debug:<br>            logger.info(f&quot;Processing question: {question[:100]}...&quot;)<br>        <br>        docs = self.retrieve_documents(question, k)<br>        context = self.format_context(docs)<br>        <br>        if custom_prompt:<br>            prompt = custom_prompt.format(context=context, question=question)<br>        else:<br>            prompt = f&quot;&quot;&quot;You are a helpful AI assistant. Use the following context to answer the question accurately and comprehensively.<br><br>Context:<br>{context}<br><br>Question: {question}<br><br>Instructions:<br>- Base your answer primarily on the provided context<br>- If the context doesn&#39;t contain enough information, clearly state this<br>- Cite specific documents when referencing information<br>- Provide a clear, well-structured response<br><br>Answer:&quot;&quot;&quot;<br><br>        estimated_tokens = self.estimate_tokens(prompt)<br>        <br>        if self.config.debug:<br>            logger.info(f&quot;Estimated prompt tokens: {estimated_tokens}&quot;)<br>        <br>        if estimated_tokens &gt; 100000:  # Conservative limit<br>            logger.warning(f&quot;Prompt is very large ({estimated_tokens} estimated tokens). Consider reducing top-k or disabling image data.&quot;)<br><br>        try:<br>            if self.config.debug:<br>                logger.info(&quot;Sending query to Azure OpenAI...&quot;)<br>            <br>            response = self.llm.invoke(prompt)<br>            answer = response.content.strip()<br>            <br>            return {<br>                &quot;answer&quot;: answer,<br>                &quot;question&quot;: question,<br>                &quot;retrieved_documents&quot;: len(docs),<br>                &quot;context_length&quot;: len(context),<br>                &quot;estimated_tokens&quot;: estimated_tokens,<br>                &quot;documents&quot;: [<br>                    {<br>                        &quot;content&quot;: doc.page_content[:200] + &quot;...&quot; if len(doc.page_content) &gt; 200 else doc.page_content,<br>                        &quot;metadata&quot;: doc.metadata<br>                    }<br>                    for doc in docs<br>                ]<br>            }<br>            <br>        except Exception as e:<br>            logger.error(f&quot;Failed to generate answer: {str(e)}&quot;)<br>            raise</pre><h3>Lessons Learned: Azure Production Deployment</h3><h4>1. Azure Service Quotas and Rate Limits</h4><p>Understanding <strong>service limitations</strong> is crucial for building a production-grade pipeline. Azure Document Intelligence enforces request quotas that vary by tier, so processing large or complex documents can quickly consume available capacity. Azure OpenAI imposes rate limits depending on the model and region, with embedding models generally supporting higher throughput than large generative models like GPT-4o. Azure Cognitive Search also sets limits on query rates, batch indexing, and storage. Awareness of these constraints allows for <strong>optimized concurrency, batch processing, and overall pipeline performance</strong>.</p><h4>2. Azure Region Selection Strategy</h4><p>Choosing the right <strong>Azure region</strong> impacts latency, service availability, and reliability. Regions differ in their support for services like Document Intelligence, OpenAI, and Cognitive Search. Selecting a primary region with full service coverage, along with secondary regions for redundancy or specific workloads, helps ensure <strong>consistent performance and minimal downtime</strong>.</p><h4>3. Cost Optimization Strategies</h4><p>Running multi-modal document pipelines in Azure can be costly if not managed carefully. Key strategies include:</p><ul><li><strong>Document Intelligence:</strong> Use batch processing and caching to avoid redundant work; select service tiers that match document volume.</li><li><strong>Azure OpenAI:</strong> Monitor token usage, select cost-effective models for simple tasks, and optimize prompts to reduce unnecessary tokens.</li><li><strong>Cognitive Search:</strong> Right-size service tiers, configure replicas and partitions appropriately, and optimize queries to minimize overhead.</li></ul><p>These measures help maintain <strong>cost efficiency without compromising performance</strong>.</p><h4>4. Security and Compliance</h4><p>Enterprise deployments require <strong>robust security and compliance practices</strong>. Key steps include:</p><ul><li><strong>Authentication &amp; Authorization:</strong> Use Azure AD or managed identities rather than embedding API keys.</li><li><strong>Data Privacy:</strong> Enable Azure Private Link, enforce data residency requirements, and use Azure Key Vault for secrets management.</li><li><strong>Audit &amp; Monitoring:</strong> Maintain detailed logs and compliance records to meet regulatory standards.</li></ul><p>By following these practices, pipelines remain <strong>secure, auditable, and compliant</strong>.</p><h4>5. Monitoring and Alerting</h4><p>Proactive monitoring ensures <strong>pipeline health and reliability</strong>. Integrating Azure Monitor or Application Insights allows you to track:</p><ul><li>Processing duration per document or batch</li><li>Success rates for each stage</li><li>Errors, retries, and system performance</li></ul><p>Custom metrics help <strong>detect bottlenecks, enforce SLAs, and improve operational efficiency</strong>.</p><h4>6. Real-World Production Insights</h4><p>From our deployments, we observed:</p><ul><li>Large PDFs (50+ pages) take 2–3 minutes end-to-end; standard documents complete in under a minute.</li><li>Batch processing throughput scales to 50–100 documents per hour with optimal configuration.</li><li>Image analysis can process 200+ images per minute concurrently using Azure OpenAI.</li><li>Semantic search across millions of chunks is near-instantaneous, with sub-second retrieval times.</li></ul><p>These insights highlight the importance of <strong>careful design, concurrency management, and resource monitoring</strong> for production-grade multi-modal pipelines.</p><h3>Conclusion: Multi-Modal Document Intelligence with Azure AI</h3><p>Our Azure-powered document pipeline demonstrates the power of <strong>multi-modal AI for enterprise document processing</strong>. By combining <strong>Azure Document Intelligence, Cognitive Search, and OpenAI</strong>, the system doesn’t just extract information — it understands documents holistically, connecting text, tables, and images to preserve context and enable cross-modal search.</p><p>Built natively on Azure, the pipeline scales automatically, integrates with enterprise security models, and delivers reliable performance across large document collections. From financial reports and contracts to technical manuals and research papers, it enables organizations to gain <strong>deep, actionable insights</strong> from complex content.</p><p>Ready to build your own Azure document intelligence pipeline? Explore the full implementation on GitHub, with detailed guidance for deployment and configuration:</p><p><a href="https://github.com/Shaaf-Mobiz/Azure-Document-Ingestion-Retrieval-Pipeline.git">https://github.com/Shaaf-Mobiz/Azure-Document-Ingestion-Retrieval-Pipeline.git</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8272a71fe142" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>