Microsoft Foundry Tools(原 Azure AI Services)封装了 Azure AI 服务 API,用于代理工具使用。这些工具位于 langchain-azure-ai 包中,从 langchain_azure_ai.tools导出,可以单独实例化或与 AzureAIServicesToolkit.
当您希望 LangChain 代理使用 Azure 托管服务分析文档、图像或医疗文本时,请使用这些工具。
概述
| 工具 | 描述 |
|---|---|
AzureAIContentUnderstandingTool | 从文档、图像、音频和视频中提取结构化内容。 |
AzureAIDocumentIntelligenceTool | 将文档解析为 OCR 文本、表格和键值对。 |
AzureAIImageAnalysisTool | 执行 OCR、标题、标记、目标检测及相关图像分析。 |
AzureAISpeechToTextTool | 使用语言支持将音频文件转录为文本。 |
AzureAITextToSpeechTool | 使用多语言支持将文本转换为合成语音音频。 |
AzureAITextAnalyticsHealthTool | 从医疗文本中提取医学实体。 |
功能特性
- - 所有服务工具共享身份验证和端点处理。
- - 支持 Azure AI Foundry 项目端点和直接服务端点。
- - 提供多模态提取、文档解析、图像分析、语音转文本、文本转语音和医疗文本分析等独立工具。
- -
AzureAIServicesToolkit用于一次性加载所有服务工具。 - - 自动检测音频源(本地文件和远程 URL)以便转录。
- - 使用 BCP-47 语言代码进行多语言语音识别和合成。
- - 为合成语音输出生成 WAV 文件。
设置
安装集成包,配置 Azure AI Foundry 项目端点或直接 Azure AI Services 端点,并提供凭证。
安装
使用 tools extra:
pip install -U "langchain-azure-ai[tools]"
uv add "langchain-azure-ai[tools]"
安装此额外依赖项会安装这些工具使用的服务特定依赖项,包括 azure-ai-documentintelligence, azure-ai-vision-imageanalysis, azure-cognitiveservices-speech和 azure-ai-textanalytics。基础包包含 azure-ai-contentunderstanding.
凭证
传递 DefaultAzureCredential() 或 API 密钥字符串通过 credential 参数。如果您使用 Foundry 项目端点,请使用 Microsoft Entra ID 凭证,例如 DefaultAzureCredential().
from azure.identity import DefaultAzureCredential
credential = DefaultAzureCredential()
配置端点
服务工具支持两种端点样式:
- - 通过
project_endpointorAZURE_AI_PROJECT_ENDPOINT - - 通过
endpointorAZURE_AI_INFERENCE_ENDPOINT
如果两者都可用,请优先使用 project_endpoint 因为它会为基于 Foundry 的工作流自动解析后备服务端点。
实例化工具
If AZURE_AI_PROJECT_ENDPOINT 已设置时,通常可以省略 project_endpoint 实例化期间。
from azure.identity import DefaultAzureCredential
from langchain_azure_ai.tools import AzureAIContentUnderstandingTool
tool = AzureAIContentUnderstandingTool(
credential=DefaultAzureCredential(),
)
result = tool.invoke(
{"source": "https://example.com/invoice.pdf", "source_type": "url"}
)
print(result)
与代理配合使用
将一个或多个工具传递给 create_agent.
from azure.identity import DefaultAzureCredential
from langchain.agents import create_agent
from langchain.chat_models import init_chat_model
from langchain_azure_ai.tools import (
AzureAIDocumentIntelligenceTool,
AzureAIImageAnalysisTool,
)
credential = DefaultAzureCredential()
tools = [
AzureAIDocumentIntelligenceTool(credential=credential),
AzureAIImageAnalysisTool(credential=credential),
]
agent = create_agent(
model=init_chat_model("azure_ai:gpt-4.1", credential=credential),
tools=tools,
system_prompt=(
"You are a document and image analysis assistant. Use tools when the "
"user asks you to inspect files or images."
),
)
使用工具包
使用 AzureAIServicesToolkit 获取所有使用共享凭证和终结点配置的服务工具。
from azure.identity import DefaultAzureCredential
from langchain_azure_ai.tools import AzureAIServicesToolkit
toolkit = AzureAIServicesToolkit(credential=DefaultAzureCredential())
tools = toolkit.get_tools()
工具
AzureAIContentUnderstandingTool
AzureAIContentUnderstandingTool 从文档、图片、音频和视频中提取结构化内容。它返回类似 markdown 的提取内容,还可以从选定的分析器中呈现结构化字段。
该工具默认使用 analyzer_id="prebuilt-documentSearch"。您可以切换分析器以处理其他模态,例如 prebuilt-audioSearch or prebuilt-videoSearch,您还可以在分析器依赖自定义模型部署名称时提供 model_deployments 当分析器依赖自定义模型部署名称时适用。
from azure.identity import DefaultAzureCredential
from langchain_azure_ai.tools import AzureAIContentUnderstandingTool
tool = AzureAIContentUnderstandingTool(
credential=DefaultAzureCredential(),
analyzer_id="prebuilt-documentSearch",
)
result = tool.invoke(
{"source": "https://example.com/contract.pdf", "source_type": "url"}
)
print(result)
Configuration options
要分析的内容。传递公共 URL、本地文件路径或 base64 编码的有效载荷。
控制工具如何解释 source.
要运行的内容理解分析器。
当自定义分析器需要模型名称到部署名称的可选映射。
AzureAIDocumentIntelligenceTool
AzureAIDocumentIntelligenceTool 从文档中提取 OCR 文本、表格和键值对。它非常适合发票、表单、收据、合同以及其他需要结构化输出而非仅原始文本的文档密集型工作流程。
该工具默认使用 model_id="prebuilt-layout"。其公共输入架构接受 url, path和 base64 sources.
from azure.identity import DefaultAzureCredential
from langchain_azure_ai.tools import AzureAIDocumentIntelligenceTool
tool = AzureAIDocumentIntelligenceTool(
credential=DefaultAzureCredential(),
model_id="prebuilt-layout",
)
result = tool.invoke(
{"source": "https://example.com/invoice.pdf", "source_type": "url"}
)
print(result)
Configuration options
文档输入。传递公共 URL、本地文件路径或 base64 编码的有效载荷。
控制工具如何解释 source.
要运行的文档智能模型。
AzureAIImageAnalysisTool
AzureAIImageAnalysisTool 分析图像并在启用这些功能时返回包含标题、OCR 文本、标签、对象、人物和智能裁剪的 JSON 格式摘要。
默认情况下,该工具启用一组广泛的视觉功能,包括 TAGS, OBJECTS, CAPTION, DENSE_CAPTIONS, READ, SMART_CROPS和 PEOPLE.
from azure.identity import DefaultAzureCredential
from azure.ai.vision.imageanalysis.models import VisualFeatures
from langchain_azure_ai.tools import AzureAIImageAnalysisTool
tool = AzureAIImageAnalysisTool(
credential=DefaultAzureCredential(),
visual_features=[
VisualFeatures.CAPTION,
VisualFeatures.READ,
VisualFeatures.TAGS,
],
)
result = tool.invoke(
{"source": "https://example.com/whiteboard.png", "source_type": "url"}
)
print(result)
Configuration options
图像输入。传递公共 URL、本地文件路径或 base64 编码的有效载荷。
控制工具如何解释 source.
要请求的图像分析功能可选列表。如果省略,工具将使用其默认功能集。
AzureAITextAnalyticsHealthTool
AzureAITextAnalyticsHealthTool 从医疗文本中提取医疗实体。它适用于临床笔记、患者摘要、 intake 表单以及需要医疗实体而非自由形式摘要的研究工作流程。
该工具接受纯文本 query 并可以配置可选的 language 和 country_hint defaults.
from azure.identity import DefaultAzureCredential
from langchain_azure_ai.tools import AzureAITextAnalyticsHealthTool
tool = AzureAITextAnalyticsHealthTool(
credential=DefaultAzureCredential(),
language="en",
country_hint="us",
)
result = tool.invoke(
"The patient reports chest pain and was prescribed aspirin after the visit."
)
print(result)
Configuration options
要分析的医疗文本。
输入文本的可选默认语言。
底层文本分析客户端使用的可选国家/地区提示。
AzureAISpeechToTextTool
AzureAISpeechToTextTool 使用 Azure AI 语音服务将音频文件转录为文本。它支持多种音频格式,可以处理本地文件和远程音频 URL。
该工具自动检测输入是本地文件路径还是远程 URL,并根据需要处理下载远程文件。它适用于需要将口语音频转换为书面文本的工作流程。
from azure.identity import DefaultAzureCredential
from langchain_azure_ai.tools import AzureAISpeechToTextTool
tool = AzureAISpeechToTextTool(
credential=DefaultAzureCredential(),
endpoint="https://eastus.api.cognitive.microsoft.com/",
speech_language="en-US",
)
result = tool.invoke("path/to/audio.wav")
print(result)
Configuration options
本地音频文件的路径或指向音频文件的 URL。支持 WAV、MP3、OGG、FLAC 和其他常见音频格式。
语音的 BCP-47 格式语言代码(例如, "en-US", "es-ES", "fr-FR")。默认为 "en-US".
Azure AI 语音服务终结点。例如, https://eastus.api.cognitive.microsoft.com/。可通过 AZURE_AI_INFERENCE_ENDPOINT 环境变量或从以下内容解析 AZURE_AI_PROJECT_ENDPOINT.
要使用的凭据。可以是订阅密钥字符串或任何 TokenCredential 例如 DefaultAzureCredential.
AzureAITextToSpeechTool
AzureAITextToSpeechTool 使用 Azure AI 语音服务将文本转换为语音。它会合成提供的文本并返回包含合成语音的本地 WAV 音频文件路径。
该工具支持使用 BCP-47 语言代码进行多语言合成,适用于需要从文本生成语音解说、配音内容或音频通知的工作流。
from azure.identity import DefaultAzureCredential
from langchain_azure_ai.tools import AzureAITextToSpeechTool
tool = AzureAITextToSpeechTool(
credential=DefaultAzureCredential(),
endpoint="https://eastus.api.cognitive.microsoft.com/",
speech_language="en-US",
)
result = tool.invoke("Hello, this is a test of text to speech synthesis.")
print(result) # Returns path to generated WAV file
Configuration options
要转换为语音的文本。
合成语音的语言,采用 BCP-47 格式(例如, "en-US", "es-ES", "fr-FR")。默认为 "en-US".
Azure AI 语音服务终结点。例如, https://eastus.api.cognitive.microsoft.com/。可通过 AZURE_AI_INFERENCE_ENDPOINT 环境变量或从以下内容解析 AZURE_AI_PROJECT_ENDPOINT.
要使用的凭据。可以是订阅密钥字符串或任何 TokenCredential 例如 DefaultAzureCredential.
API 参考
from langchain_azure_ai.tools import (
AzureAIServicesToolkit,
AzureAIContentUnderstandingTool,
AzureAIDocumentIntelligenceTool,
AzureAIImageAnalysisTool,
AzureAISpeechToTextTool,
AzureAITextToSpeechTool,
AzureAITextAnalyticsHealthTool,
)