### Install Azure Document Intelligence SDK Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Python(v4.0)/Read_model/README.md Installs the required Python client library using pip. ```bash pip install azure-ai-documentintelligence ``` -------------------------------- ### Install required Python packages Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Python(v4.0)/Retrieval_Augmented_Generation_(RAG)_samples/sample_rag_langchain.ipynb Install the necessary libraries for LangChain, Azure AI services, and OpenAI integration. ```python ! pip install python-dotenv langchain langchain-community langchain-openai langchainhub openai tiktoken azure-ai-documentintelligence azure-identity azure-search-documents==11.6.0b3 ``` -------------------------------- ### Install Required Python Packages Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Python(v4.0)/Pre_or_post_processing_samples/sample_identify_cross_page_tables.ipynb Install the necessary Azure AI Document Intelligence and environment management libraries. ```python ! pip install azure-ai-documentintelligence python-dotenv azure-identity ``` -------------------------------- ### Install Document Intelligence Package Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/JavaScript(v4.0)/README.md Use npm to install the required REST client library for Azure Document Intelligence. ```bash npm install @azure-rest/ai-document-intelligence ``` -------------------------------- ### Execute Layout Analysis Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Python(v4.0)/Retrieval_Augmented_Generation_(RAG)_samples/sample_figure_understanding.ipynb Example of calling the layout analysis function and printing the resulting markdown content. ```python updated_md_with_figure_understanding = analyze_layout("data/layout-sample.pdf", "data/cropped") print("-------------------------------------------------------------------------------------------") print(f"Updated markdown content with figure understanding:\n\n {updated_md_with_figure_understanding}") ``` -------------------------------- ### Run Python Application Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Python(v4.0)/Read_model/README.md Executes the sample application from the command line. ```bash python sample_analyze_read.py ``` -------------------------------- ### Initialize Azure Service Clients and Environment Variables Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Python(v4.0)/Retrieval_Augmented_Generation_(RAG)_samples/sample_figure_understanding.ipynb Loads configuration from a .env file and initializes the Document Intelligence and Azure OpenAI clients. ```python """ This code loads environment variables using the `dotenv` library and sets the necessary environment variables for Azure services. The environment variables are loaded from the `.env` file in the same directory as this notebook. """ import os from dotenv import load_dotenv from azure.core.credentials import AzureKeyCredential from azure.ai.documentintelligence import DocumentIntelligenceClient from azure.ai.documentintelligence.models import ContentFormat from openai import AzureOpenAI load_dotenv() doc_intelligence_endpoint = os.getenv("AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT") doc_intelligence_key = os.getenv("AZURE_DOCUMENT_INTELLIGENCE_KEY") aoai_api_base = os.getenv("AZURE_OPENAI_ENDPOINT") aoai_api_key= os.getenv("AZURE_OPENAI_API_KEY") aoai_deployment_name = 'gpt-4v' # your model deployment name for GPT-4V aoai_api_version = '2024-02-15-preview' # this might change in the future ``` -------------------------------- ### Initialize Document Intelligence Client Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Python(v4.0)/Pre_or_post_processing_samples/sample_identify_cross_page_tables.ipynb Load environment variables from a .env file and initialize the DocumentIntelligenceClient. ```python """ This code loads environment variables using the `dotenv` library and sets the necessary environment variables for Azure services. The environment variables are loaded from the `.env` file in the same directory as this notebook. """ import os from dotenv import load_dotenv from azure.core.credentials import AzureKeyCredential from azure.ai.documentintelligence import DocumentIntelligenceClient load_dotenv() endpoint = os.getenv("AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT") key = os.getenv("AZURE_DOCUMENT_INTELLIGENCE_KEY") ``` -------------------------------- ### Get Service Information Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/JavaScript(v4.0)/README.md Retrieves metadata and limits for the Document Intelligence service. ```typescript const response = await client.path("/info").get(); if (isUnexpected(response)) { throw response.body.error; } console.log(response.body.customDocumentModels.limit); ``` -------------------------------- ### Initialize Document Intelligence Client Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Python(v4.0)/Retrieval_Augmented_Generation_(RAG)_samples/sample_identify_and_merge_cross_page_tables.ipynb Sets up the Document Intelligence client for processing documents. ```python def identify_and_merge_cross_page_tables(): """ Identifies and merges tables that span across multiple pages in a document. Returns: None """ document_intelligence_client = DocumentIntelligenceClient( endpoint=endpoint, credential=AzureKeyCredential(key), headers={"x-ms-useragent":"sample-code-merge-cross-tables/1.0.0"}, ) file_path = "" ``` -------------------------------- ### Create Document Intelligence clients with key credential Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Java(v4.0)/README.md Initialize synchronous clients using an Azure Key Credential and service endpoint. ```java DocumentIntelligenceClient documentIntelligenceClient = new DocumentIntelligenceClientBuilder() .credential(new AzureKeyCredential("{key}")) .endpoint("{endpoint}") .buildClient(); ``` ```java DocumentIntelligenceAdministrationClient client = new DocumentIntelligenceAdministrationClientBuilder() .credential(new AzureKeyCredential("{key}")) .endpoint("{endpoint}") .buildClient(); ``` -------------------------------- ### Configure environment variables Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Python(v4.0)/Retrieval_Augmented_Generation_(RAG)_samples/sample_rag_langchain.ipynb Load configuration from a .env file and set environment variables for Azure OpenAI and Document Intelligence. ```python """ This code loads environment variables using the `dotenv` library and sets the necessary environment variables for Azure services. The environment variables are loaded from the `.env` file in the same directory as this notebook. """ import os from dotenv import load_dotenv load_dotenv() os.environ["AZURE_OPENAI_ENDPOINT"] = os.getenv("AZURE_OPENAI_ENDPOINT") os.environ["AZURE_OPENAI_API_KEY"] = os.getenv("AZURE_OPENAI_API_KEY") doc_intelligence_endpoint = os.getenv("AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT") doc_intelligence_key = os.getenv("AZURE_DOCUMENT_INTELLIGENCE_KEY") ``` -------------------------------- ### Run Python Application Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Python(v4.0)/Custom_model/README.md Execute the custom document analysis script from the command line. ```bash python sample_analyze_custom_documents.py ``` -------------------------------- ### Authenticate with API Key Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/JavaScript(v4.0)/README.md Initialize the client using an API key retrieved from environment variables. ```ts import DocumentIntelligence from "@azure-rest/ai-document-intelligence"; const client = DocumentIntelligence(process.env["DOCUMENT_INTELLIGENCE_ENDPOINT"], { key: process.env["DOCUMENT_INTELLIGENCE_API_KEY"], }); ``` -------------------------------- ### Enable Logging Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/JavaScript(v4.0)/README.md Configures the SDK logger to output HTTP request and response details to the console. ```javascript const { setLogLevel } = require("@azure/logger"); setLogLevel("info"); ``` -------------------------------- ### Create Document Intelligence client with AAD credential Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Java(v4.0)/README.md Initialize an asynchronous client using DefaultAzureCredential for Active Directory authentication. ```java DocumentIntelligenceAsyncClient documentIntelligenceAsyncClient = new DocumentIntelligenceClientBuilder() .credential(new DefaultAzureCredentialBuilder().build()) .endpoint("{endpoint}") .buildAsyncClient(); ``` -------------------------------- ### Analyze receipts using prebuilt-receipt model Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Java(v4.0)/README.md Uses the beginAnalyzeDocument method to process a local receipt file and iterate through the extracted fields. ```java File sourceFile = new File("../documentintelligence/azure-ai-documentintelligence/src/samples/resources/" + "sample-forms/receipts/contoso-allinone.jpg"); SyncPoller analyzeReceiptPoller = documentIntelligenceClient.beginAnalyzeDocument("prebuilt-receipt", null, null, null, null, null, null, new AnalyzeDocumentRequest().setBase64Source(Files.readAllBytes(sourceFile.toPath()))); AnalyzeResult receiptResults = analyzeReceiptPoller.getFinalResult().getAnalyzeResult(); for (int i = 0; i < receiptResults.getDocuments().size(); i++) { Document analyzedReceipt = receiptResults.getDocuments().get(i); Map receiptFields = analyzedReceipt.getFields(); System.out.printf("----------- Analyzing receipt info %d -----------%n", i); DocumentField merchantNameField = receiptFields.get("MerchantName"); if (merchantNameField != null) { if (DocumentFieldType.STRING == merchantNameField.getType()) { String merchantName = merchantNameField.getValueString(); System.out.printf("Merchant Name: %s, confidence: %.2f%n", merchantName, merchantNameField.getConfidence()); } } DocumentField merchantPhoneNumberField = receiptFields.get("MerchantPhoneNumber"); if (merchantPhoneNumberField != null) { if (DocumentFieldType.PHONE_NUMBER == merchantPhoneNumberField.getType()) { String merchantAddress = merchantPhoneNumberField.getValuePhoneNumber(); System.out.printf("Merchant Phone number: %s, confidence: %.2f%n", merchantAddress, merchantPhoneNumberField.getConfidence()); } } DocumentField merchantAddressField = receiptFields.get("MerchantAddress"); if (merchantAddressField != null) { if (DocumentFieldType.STRING == merchantAddressField.getType()) { String merchantAddress = merchantAddressField.getValueString(); System.out.printf("Merchant Address: %s, confidence: %.2f%n", merchantAddress, merchantAddressField.getConfidence()); } } DocumentField transactionDateField = receiptFields.get("TransactionDate"); if (transactionDateField != null) { if (DocumentFieldType.DATE == transactionDateField.getType()) { LocalDate transactionDate = transactionDateField.getValueDate(); System.out.printf("Transaction Date: %s, confidence: %.2f%n", transactionDate, transactionDateField.getConfidence()); } } } ``` -------------------------------- ### Document Q&A with source references Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Python(v4.0)/Retrieval_Augmented_Generation_(RAG)_samples/sample_rag_langchain.ipynb Constructs a RAG chain that returns both the generated answer and the metadata of the source documents. ```python # Return the retrieved documents or certain source metadata from the documents from operator import itemgetter from langchain.schema.runnable import RunnableMap rag_chain_from_docs = ( { "context": lambda input: format_docs(input["documents"]), "question": itemgetter("question"), } | prompt | llm | StrOutputParser() ) rag_chain_with_source = RunnableMap( {"documents": retriever, "question": RunnablePassthrough()} ) | { "documents": lambda input: [doc.metadata for doc in input["documents"]], "answer": rag_chain_from_docs, } rag_chain_with_source.invoke("") ``` -------------------------------- ### Analyze document from URL Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/JavaScript(v4.0)/README.md Initiates an analysis request for a prebuilt layout model using a document URL. ```typescript const initialResponse = await client .path("/documentModels/{modelId}:analyze", "prebuilt-layout") .post({ contentType: "application/json", body: { urlSource: "https://raw.githubusercontent.com/Azure/azure-sdk-for-js/6704eff082aaaf2d97c1371a28461f512f8d748a/sdk/formrecognizer/ai-form-recognizer/assets/forms/Invoice_1.pdf", }, queryParameters: { locale: "en-IN" }, }); ``` -------------------------------- ### Set Environment Variables on Windows Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Python(v4.0)/Read_model/README.md Configures API key and endpoint variables for the current user session on Windows. ```bash setx DOCUMENTINTELLIGENCE_API_KEY ``` ```bash setx DOCUMENTINTELLIGENCE_ENDPOINT ``` -------------------------------- ### Build a custom document model Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Java(v4.0)/README.md Trains a custom machine-learned model using documents stored in an Azure Blob Storage container. Requires a valid SAS URL for the container. ```java // Build custom document analysis model String blobContainerUrl = "{SAS_URL_of_your_container_in_blob_storage}"; // The shared access signature (SAS) Url of your Azure Blob Storage container with your forms. SyncPoller buildOperationPoller = administrationClient.beginBuildDocumentModel(new BuildDocumentModelRequest("modelID", DocumentBuildMode.TEMPLATE) .setAzureBlobSource(new AzureBlobContentSource(blobContainerUrl))); DocumentModelDetails documentModelDetails = buildOperationPoller.getFinalResult(); // Model Info System.out.printf("Model ID: %s%n", documentModelDetails.getModelId()); System.out.printf("Model Description: %s%n", documentModelDetails.getDescription()); System.out.printf("Model created on: %s%n%n", documentModelDetails.getCreatedDateTime()); System.out.println("Document Fields:"); documentModelDetails.getDocTypes().forEach((key, documentTypeDetails) -> { documentTypeDetails.getFieldSchema().forEach((field, documentFieldSchema) -> { System.out.printf("Field: %s", field); System.out.printf("Field type: %s", documentFieldSchema.getType()); System.out.printf("Field confidence: %.2f", documentTypeDetails.getFieldConfidence().get(field)); }); }); ``` -------------------------------- ### Retrieve chunks and initialize RAG chain Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Python(v4.0)/Retrieval_Augmented_Generation_(RAG)_samples/sample_rag_langchain.ipynb Configures a vector store retriever and defines a RAG chain using a prompt from the LangChain hub and AzureChatOpenAI. ```python # Retrieve relevant chunks based on the question retriever = vector_store.as_retriever(search_type="similarity", search_kwargs={"k": 3}) retrieved_docs = retriever.get_relevant_documents( "" ) print(retrieved_docs[0].page_content) # Use a prompt for RAG that is checked into the LangChain prompt hub (https://smith.langchain.com/hub/rlm/rag-prompt?organizationId=989ad331-949f-4bac-9694-660074a208a7) prompt = hub.pull("rlm/rag-prompt") llm = AzureChatOpenAI( openai_api_version="", # e.g., "2023-12-01-preview" azure_deployment="", temperature=0, ) def format_docs(docs): return "\n\n".join(doc.page_content for doc in docs) rag_chain = ( {"context": retriever | format_docs, "question": RunnablePassthrough()} | prompt | llm | StrOutputParser() ) ``` -------------------------------- ### Analyze document from base64 Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/JavaScript(v4.0)/README.md Initiates an analysis request for a prebuilt layout model using a base64 encoded file. ```typescript import fs from "fs"; import path from "path"; const filePath = path.join(ASSET_PATH, "forms", "Invoice_1.pdf"); const base64Source = fs.readFileSync(filePath, { encoding: "base64" }); const initialResponse = await client .path("/documentModels/{modelId}:analyze", "prebuilt-layout") .post({ contentType: "application/json", body: { base64Source, }, queryParameters: { locale: "en-IN" }, }); ``` -------------------------------- ### Load and split documents with LangChain Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Python(v4.0)/Retrieval_Augmented_Generation_(RAG)_samples/sample_rag_langchain.ipynb Uses AzureAIDocumentIntelligenceLoader to ingest files and MarkdownHeaderTextSplitter to segment content by headers. ```python # Initiate Azure AI Document Intelligence to load the document. You can either specify file_path or url_path to load the document. loader = AzureAIDocumentIntelligenceLoader(file_path="", api_key = doc_intelligence_key, api_endpoint = doc_intelligence_endpoint, api_model="prebuilt-layout") docs = loader.load() # Split the document into chunks base on markdown headers. headers_to_split_on = [ ("#", "Header 1"), ("##", "Header 2"), ("###", "Header 3"), ] text_splitter = MarkdownHeaderTextSplitter(headers_to_split_on=headers_to_split_on) docs_string = docs[0].page_content splits = text_splitter.split_text(docs_string) print("Length of splits: " + str(len(splits))) ``` -------------------------------- ### List Document Models Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/JavaScript(v4.0)/README.md Uses the pagination helper to iterate through all document models available in the account. ```typescript import { paginate } from "@azure-rest/ai-document-intelligence"; const response = await client.path("/documentModels").get(); if (isUnexpected(response)) { throw response.body.error; } const modelsInAccount: string[] = []; for await (const model of paginate(client, response)) { console.log(model.modelId); } ``` -------------------------------- ### Analyze image with GPT-4V Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Python(v4.0)/Retrieval_Augmented_Generation_(RAG)_samples/sample_figure_understanding.ipynb Sends an image and an optional caption to an Azure OpenAI GPT-4V model to generate a descriptive analysis. ```python MAX_TOKENS = 2000 def understand_image_with_gptv(api_base, api_key, deployment_name, api_version, image_path, caption): """ Generates a description for an image using the GPT-4V model. Parameters: - api_base (str): The base URL of the API. - api_key (str): The API key for authentication. - deployment_name (str): The name of the deployment. - api_version (str): The version of the API. - image_path (str): The path to the image file. - caption (str): The caption for the image. Returns: - img_description (str): The generated description for the image. """ client = AzureOpenAI( api_key=api_key, api_version=api_version, base_url=f"{api_base}/openai/deployments/{deployment_name}" ) data_url = local_image_to_data_url(image_path) # We send both image caption and the image body to GPTv for better understanding if caption != "": response = client.chat.completions.create( model=deployment_name, messages=[ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": [ { "type": "text", "text": f"Describe this image (note: it has image caption: {caption}):" }, { "type": "image_url", "image_url": { "url": data_url } } ] } ], max_tokens=MAX_TOKENS ) else: response = client.chat.completions.create( model=deployment_name, messages=[ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": [ { "type": "text", "text": "Describe this image:" }, { "type": "image_url", "image_url": { "url": data_url } } ] } ], max_tokens=MAX_TOKENS ) img_description = response.choices[0].message.content return img_description ``` -------------------------------- ### Analyze Document Layout with Java Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Java(v4.0)/README.md Extracts text, table structures, and selection marks from a document using the prebuilt-layout model. Requires a local file path and a configured documentIntelligenceClient. ```java File layoutDocument = new File("local/file_path/filename.png"); Path filePath = layoutDocument.toPath(); BinaryData layoutDocumentData = BinaryData.fromFile(filePath, (int) layoutDocument.length()); SyncPoller analyzeLayoutResultPoller = documentIntelligenceClient.beginAnalyzeDocument("prebuilt-layout", null, null, null, null, null, null, new AnalyzeDocumentRequest().setBase64Source(Files.readAllBytes(layoutDocument.toPath()))); AnalyzeResult analyzeLayoutResult = analyzeLayoutResultPoller.getFinalResult().getAnalyzeResult(); // pages analyzeLayoutResult.getPages().forEach(documentPage -> { System.out.printf("Page has width: %.2f and height: %.2f, measured with unit: %s%n", documentPage.getWidth(), documentPage.getHeight(), documentPage.getUnit()); // lines documentPage.getLines().forEach(documentLine -> System.out.printf("Line '%s' is within a bounding box %s.%n", documentLine.getContent(), documentLine.getPolygon().toString())); // selection marks documentPage.getSelectionMarks().forEach(documentSelectionMark -> System.out.printf("Selection mark is '%s' and is within a bounding box %s with confidence %.2f.%n", documentSelectionMark.getState().toString(), documentSelectionMark.getPolygon().toString(), documentSelectionMark.getConfidence())); }); // tables List tables = analyzeLayoutResult.getTables(); for (int i = 0; i < tables.size(); i++) { DocumentTable documentTable = tables.get(i); System.out.printf("Table %d has %d rows and %d columns.%n", i, documentTable.getRowCount(), documentTable.getColumnCount()); documentTable.getCells().forEach(documentTableCell -> { System.out.printf("Cell '%s', has row index %d and column index %d.%n", documentTableCell.getContent(), documentTableCell.getRowIndex(), documentTableCell.getColumnIndex()); }); System.out.println(); } ``` -------------------------------- ### prebuilt-tax.us.1040ScheduleC Model Fields Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/schema/2024-11-30-ga/us-tax/1040/1040-schedule-c.md The following fields are supported for extraction by the prebuilt-tax.us.1040ScheduleC model. ```APIDOC ## Model: prebuilt-tax.us.1040ScheduleC ### Supported Fields - **TaxYear** (string) - Tax Year extracted from Form 1040-ScheduleC. - **Taxpayer.SSN** (string) - Taxpayer tax social security number. - **Taxpayer.Name** (string) - Taxpayer name as written on the form. - **BoxA** (string) - Box A extracted from Form 1040-ScheduleC. - **BoxB** (string) - Box B extracted from Form 1040-ScheduleC. - **BoxC** (string) - Box C extracted from Form 1040-ScheduleC. - **BoxD** (string) - Box D extracted from Form 1040-ScheduleC. - **BoxE** (address) - Box E extracted from Form 1040-ScheduleC. - **BoxF** (selectionGroup) - List containing 'cash', 'accrual', or 'other'. - **BoxFExtraInfo** (string) - Box F Extra Info extracted from Form 1040-ScheduleC. - **BoxG** (selectionGroup) - List containing 'yes' or 'no'. - **BoxH** (boolean) - Box H extracted from Form 1040-ScheduleC. - **BoxI** (selectionGroup) - List containing 'yes' or 'no'. - **BoxJ** (selectionGroup) - List containing 'yes' or 'no'. - **Box1Checkbox** (number) - Box1 Checkbox extracted from Form 1040-ScheduleC. - **Box1** through **Box31** (number) - Financial line items extracted from Form 1040-ScheduleC. - **Box32a** (boolean) - Box 32a extracted from Form 1040-ScheduleC. - **Box32b** (boolean) - Box 32b extracted from Form 1040-ScheduleC. - **Box33** (selectionGroup) - List containing 'cost', 'lowerCostOrMarket', or 'other'. - **Box34** (selectionGroup) - List containing 'yes' or 'no'. - **Box35** (number) - Box 35 extracted from Form 1040-ScheduleC. ``` -------------------------------- ### Authenticate with Token Credential Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/JavaScript(v4.0)/README.md Initialize the client using DefaultAzureCredential from the @azure/identity library. ```ts import DocumentIntelligence from "@azure-rest/ai-document-intelligence"; const client = DocumentIntelligence( process.env["DOCUMENT_INTELLIGENCE_ENDPOINT"], new DefaultAzureCredential() ); ``` -------------------------------- ### Embed and index documents in Azure AI Search Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Python(v4.0)/Retrieval_Augmented_Generation_(RAG)_samples/sample_rag_langchain.ipynb Configures AzureOpenAIEmbeddings and uploads document splits to an Azure Search vector store index. ```python # Embed the splitted documents and insert into Azure Search vector store aoai_embeddings = AzureOpenAIEmbeddings( azure_deployment="", openai_api_version="", # e.g., "2023-12-01-preview" ) vector_store_address: str = os.getenv("AZURE_SEARCH_ENDPOINT") vector_store_password: str = os.getenv("AZURE_SEARCH_ADMIN_KEY") index_name: str = "" vector_store: AzureSearch = AzureSearch( azure_search_endpoint=vector_store_address, azure_search_key=vector_store_password, index_name=index_name, embedding_function=aoai_embeddings.embed_query, ) vector_store.add_documents(documents=splits) ``` -------------------------------- ### Execute Disambiguation Workflow Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Python(v4.0)/Pre_or_post_processing_samples/sample_disambiguate_similar_characters.ipynb Demonstrates the end-to-end process of generating variations and filtering them using the validation function. ```python # Take input from the user input_string = "126.99" # Generate confusing strings confusing_strings = generate_confusing_strings(input_string) print(confusing_strings) # Verify if any of the generated strings are ICD-10 codes icd10_codes = [code for code in confusing_strings if verify_icd10_code(code)] if icd10_codes: print("Generated ICD-10 codes:") for code in icd10_codes: print(code) # if there are more than 1 possibility or no possible ICD-10 code, call Human-in-the-loop else: print("No valid ICD-10 codes generated.") ``` -------------------------------- ### Import LangChain modules Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Python(v4.0)/Retrieval_Augmented_Generation_(RAG)_samples/sample_rag_langchain.ipynb Import the necessary classes and functions from LangChain and its community packages. ```python from langchain import hub from langchain_openai import AzureChatOpenAI from langchain_community.document_loaders import AzureAIDocumentIntelligenceLoader from langchain_openai import AzureOpenAIEmbeddings from langchain.schema import StrOutputParser from langchain.schema.runnable import RunnablePassthrough from langchain.text_splitter import MarkdownHeaderTextSplitter from langchain.vectorstores.azuresearch import AzureSearch ``` -------------------------------- ### Find Merge Table Candidates Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Python(v4.0)/Pre_or_post_processing_samples/sample_identify_cross_page_tables.ipynb Identify tables that are candidates for merging by checking if they appear on consecutive pages. ```python def find_merge_table_candidates(tables): """ Finds the merge table candidates based on the given list of tables. Parameters: tables (list): A list of tables. Returns: list: A list of merge table candidates, where each candidate is a dictionary with keys: - pre_table_idx: The index of the first candidate table to be merged (the other table to be merged is the next one). - start: The start offset of the 2nd candidate table. - end: The end offset of the 1st candidate table. """ merge_tables_candidates = [] pre_table_idx = -1 pre_table_page = -1 pre_max_offset = 0 for table_idx, table in enumerate(tables): min_offset, max_offset = get_table_span_offsets(table) table_page = min(get_table_page_numbers(table)) # If there is a table on the next page, it is a candidate for merging with the previous table. if table_page == pre_table_page + 1: pre_table = {"pre_table_idx": pre_table_idx, "start": pre_max_offset, "end": min_offset} merge_tables_candidates.append(pre_table) print(f"Table {table_idx} has offset range: {min_offset} - {max_offset} on page {table_page}") pre_table_idx = table_idx pre_table_page = table_page pre_max_offset = max_offset return merge_tables_candidates ``` -------------------------------- ### Add Azure Identity dependency Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Java(v4.0)/README.md Include the Azure Identity library to support Active Directory authentication. ```xml com.azure azure-identity 1.11.3 ``` -------------------------------- ### Crop figures from images and PDFs Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/Python(v4.0)/Retrieval_Augmented_Generation_(RAG)_samples/sample_figure_understanding.ipynb Utility functions to handle cropping from image files (including TIFF) and PDF pages. Requires PIL and PyMuPDF libraries. ```python from PIL import Image import fitz # PyMuPDF import mimetypes def crop_image_from_image(image_path, page_number, bounding_box): """ Crops an image based on a bounding box. :param image_path: Path to the image file. :param page_number: The page number of the image to crop (for TIFF format). :param bounding_box: A tuple of (left, upper, right, lower) coordinates for the bounding box. :return: A cropped image. :rtype: PIL.Image.Image """ with Image.open(image_path) as img: if img.format == "TIFF": # Open the TIFF image img.seek(page_number) img = img.copy() # The bounding box is expected to be in the format (left, upper, right, lower). cropped_image = img.crop(bounding_box) return cropped_image def crop_image_from_pdf_page(pdf_path, page_number, bounding_box): """ Crops a region from a given page in a PDF and returns it as an image. :param pdf_path: Path to the PDF file. :param page_number: The page number to crop from (0-indexed). :param bounding_box: A tuple of (x0, y0, x1, y1) coordinates for the bounding box. :return: A PIL Image of the cropped area. """ doc = fitz.open(pdf_path) page = doc.load_page(page_number) # Cropping the page. The rect requires the coordinates in the format (x0, y0, x1, y1). bbx = [x * 72 for x in bounding_box] rect = fitz.Rect(bbx) pix = page.get_pixmap(matrix=fitz.Matrix(300/72, 300/72), clip=rect) img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples) doc.close() return img def crop_image_from_file(file_path, page_number, bounding_box): """ Crop an image from a file. Args: file_path (str): The path to the file. page_number (int): The page number (for PDF and TIFF files, 0-indexed). bounding_box (tuple): The bounding box coordinates in the format (x0, y0, x1, y1). Returns: A PIL Image of the cropped area. """ mime_type = mimetypes.guess_type(file_path)[0] if mime_type == "application/pdf": return crop_image_from_pdf_page(file_path, page_number, bounding_box) else: return crop_image_from_image(file_path, page_number, bounding_box) ``` -------------------------------- ### Request Markdown output format Source: https://github.com/azure-samples/document-intelligence-code-samples/blob/main/JavaScript(v4.0)/README.md Configures the analysis request to return content in Markdown format. ```typescript import DocumentIntelligence from "@azure-rest/ai-document-intelligence"; const client = DocumentIntelligence(process.env["DOCUMENT_INTELLIGENCE_ENDPOINT"], { key: process.env["DOCUMENT_INTELLIGENCE_API_KEY"], }); const initialResponse = await client .path("/documentModels/{modelId}:analyze", "prebuilt-layout") .post({ contentType: "application/json", body: { urlSource: "https://raw.githubusercontent.com/Azure/azure-sdk-for-js/6704eff082aaaf2d97c1371a28461f512f8d748a/sdk/formrecognizer/ai-form-recognizer/assets/forms/Invoice_1.pdf", }, queryParameters: { outputContentFormat: "markdown" }, // <-- new query parameter }); ```