本页介绍来自 langchain_azure_ai.tools的 Microsoft Foundry 项目工具。另请参阅 Microsoft Foundry 工具(原 Azure AI 服务).
当您希望代理调用 Microsoft Foundry 项目提供的工具功能时,请使用这些工具。
概述
| 工具 | 描述 |
|---|---|
AzureOpenAIModelImageGenTool | 通过 OpenAI 兼容的 /images/generations 端点生成图像。 |
AzureOpenAITranscriptionsTool | 通过 OpenAI 兼容的 /audio/transcriptions 端点将音频文件转录为文本。 |
CodeInterpreterTool | 在沙箱容器中运行服务器端 Python 代码。 |
WebSearchTool | 在互联网上搜索当前信息和来源。 |
FileSearchTool | 在向量存储中搜索相关文档内容。 |
ImageGenerationTool | 使用 GPT 图像模型生成或编辑图像。 |
McpTool | 访问外部模型上下文协议 (MCP) 服务器。 |
AzureAIProjectToolbox | 从 Azure AI Foundry 工具箱加载工具,并通过模型上下文协议 (MCP) 使用它们。 |
设置
安装依赖项,创建工具使用的资源,并提供凭据。
安装
安装集成包:
pip install -U "langchain-azure-ai[tools]"
uv add "langchain-azure-ai[tools]"
凭据
传递 DefaultAzureCredential() 或 API 密钥字符串通过 credential 参数(除了 AzureAIProjectToolbox 不支持密钥。)
from azure.identity import DefaultAzureCredential
credential = DefaultAzureCredential()
使用 Microsoft Entra ID 需要角色 Azure AI User 用于部署模型的资源。
配置端点
这些工具支持两种端点样式:
- - 通过
project_endpointorAZURE_AI_PROJECT_ENDPOINT(orFOUNDRY_PROJECT_ENDPOINT). - - 通过
endpointorOPENAI_BASE_URL的 Azure AI Foundry 项目端点https://<resource>.services.ai.azure.com/openai/v1.
的直接 OpenAI 兼容端点 project_endpoint 如果两者都可用,优先使用
from azure.identity import DefaultAzureCredential
from langchain_azure_ai.tools import AzureOpenAIModelImageGenTool
tool = AzureOpenAIModelImageGenTool(
endpoint="https://<resource>.services.ai.azure.com/openai/v1",
credential=DefaultAzureCredential(),
model="my-gpt-image-1-deployment",
)
result = tool.invoke(
{
"prompt": "A futuristic cityscape at sunset with flying cars",
"n": 1,
"size": "1024x1024",
}
)
print(result)
因为它会自动为 Foundry 工作流解析后备服务端点。
工具
AzureOpenAIModelImageGenTool AzureOpenAIModelImageGenTool langchain_azure_ai.tools(来自 gpt-image-1.5 or MAI-Image-2.
)使用 Microsoft Foundry Models 或 Azure OpenAI 暴露的 OpenAI 兼容图像生成端点生成图像。您可以使用包括 images.generate 当您想在代理流程中进行明确的图像生成工具调用时,请使用此工具。该工具调用 OpenAI 客户端 output_directory API,并返回 base64 PNG 输出或配置了
时的保存文件路径。 model.
Configuration options
您必须首先部署图像生成模型,然后在
中传递部署名称。使用此参数需要使用 Microsoft Entra ID。 /images/generations 部署了图像模型的 Foundry 项目。
要使用的凭据,可以是密钥或令牌凭据。
描述要生成的图像的文本提示词。
要生成的图像数量。
输出图像大小(例如 1024x1024, 1024x1792, or 1792x1024,取决于模型)。
传递给图像生成模型的可选质量参数(例如 hd,取决于模型)。
可选的样式参数,例如 vivid or natural (取决于模型)。
要使用的图像生成模型的必需部署名称。使用此工具之前,请在 Microsoft Foundry 中创建此部署。可以使用任何 OpenAI 兼容的模型(例如 MAI-Image-2)。
如果设置,生成的图像将保存为 PNG 文件,工具返回保存的文件路径。如果省略,工具返回 base64 PNG 数据。
AzureOpenAITranscriptionsTool
AzureOpenAITranscriptionsTool (来自 langchain_azure_ai.tools)使用 Microsoft Foundry Models 或 Azure OpenAI 公开的 OpenAI 兼容语音转文本端点转录音频为文本(如 Whisper)。
当您想在智能体流程中将音频文件或远程音频 URL 转换为文本转录时使用此工具。该工具自动处理本地文件和远程 URL,支持多种音频格式(MP3、MP4、MPEG、MPGA、M4A、OGG、FLAC、WAV)。
您必须先部署一个语音转文本模型,然后在 model.
from azure.identity import DefaultAzureCredential
from langchain_azure_ai.tools import AzureOpenAITranscriptionsTool
tool = AzureOpenAITranscriptionsTool(
endpoint="https://<resource>.services.ai.azure.com/openai/v1",
credential=DefaultAzureCredential(),
model="my-whisper-deployment",
)
result = tool.invoke(
{
"audio_path": "/path/to/audio.wav",
"language": "en",
}
)
print(result)
Configuration options
中传递部署名称。使用此参数需要使用 Microsoft Entra ID。
路由所在的 OpenAI 兼容端点 /audio/transcriptions 。
要使用的凭据,可以是密钥或令牌凭据。
本地音频文件的路径或指向音频文件的 URL。
可选的 ISO-639-1 格式语言代码(例如 "en", "es", "fr")。如果未指定,语言将由模型自动检测。
要使用的语音转文本模型的必需部署名称。使用此工具之前,请在 Microsoft Foundry 中创建此部署。
CodeInterpreterTool
CodeInterpreterTool 允许模型在沙箱容器中编写和执行 Python 代码,并将结果包含在其响应中。这对于数据分析、数学计算、可视化和通用问题解决非常有用。
from langchain_azure_ai.tools.builtin import CodeInterpreterTool
tool = CodeInterpreterTool(
memory_limit="4g",
)
model_with_code = model.bind_tools([tool])
response = model_with_code.invoke("Plot a sine wave using Python and explain it")
print(response)
Configuration options
可选的上传文件 ID 列表,供代码在容器内处理。
容器的内存限制。可接受值为 "1g", "4g", "16g"和 "64g".
容器的可选网络访问策略。
WebSearchTool
WebSearchTool 允许模型搜索互联网以获取与其查询相关的当前信息和来源。这对于提供最新信息、研究、事实核查和访问实时数据非常有用。
from langchain_azure_ai.tools.builtin import WebSearchTool
tool = WebSearchTool(
search_context_size="high",
)
model_with_search = model.bind_tools([tool])
response = model_with_search.invoke(
"What are the latest developments in quantum computing?"
)
print(response)
Configuration options
搜索结果要使用的上下文窗口空间的高级指导。默认为 "medium".
用户的大致位置。可包含可选键: city, country (ISO-3166 双字母代码), region, timezone (IANA),以及 type="approximate".
搜索过滤器。可包含可选的 allowed_domains 列表以将结果限制在特定域。
文件搜索工具
FileSearchTool 从上传的向量存储中搜索相关内容。这对于从大型文档集合、知识库和已索引到向量存储中的自定义数据源检索信息非常有用。
from langchain_azure_ai.tools.builtin import FileSearchTool
tool = FileSearchTool(
vector_store_ids=["vs_abc123", "vs_def456"],
max_num_results=5,
)
model_with_search = model.bind_tools([tool])
response = model_with_search.invoke(
"Find information about company policies on remote work"
)
print(response)
Configuration options
要搜索的向量存储的 ID。必须提供至少一个 ID。
返回的最大结果数 (1-50)。默认为合理数量。
使用比较或复合过滤器缩小结果范围的可选元数据过滤器。
排名选项。可包含可选键 ranker 和 score_threshold 用于控制结果排名。
图像生成工具
ImageGenerationTool 允许模型使用 GPT 图像模型生成或编辑图像。这对于创建视觉效果、编辑图像以及根据文本描述生成艺术作品非常有用。此工具必须与部署在 Microsoft Foundry 项目中的 OpenAI 模型一起使用。如果您使用其他模型,请使用 AzureOpenAIModelImageGenTool instead.
from langchain_azure_ai.tools.builtin import ImageGenerationTool
tool = ImageGenerationTool(
quality="high",
size="1024x1024",
model_deployment="my-gpt-image-1-deployment",
)
model_with_images = model.bind_tools([tool])
response = model_with_images.invoke(
"Generate an image of a futuristic city with flying cars"
)
print(response)
Configuration options
Azure AI Foundry 中图像生成模型的部署名称。设置后,该工具自动注入 x-ms-oai-image-generation-deployment HTTP 请求头。
要使用的图像生成模型。
是生成新图像还是编辑现有图像。默认为 "auto".
图像质量。默认为 "auto".
图像大小。默认为 "auto".
输出格式。默认为 "png".
图像生成的背景类型。
输出与输入图像风格和面部特征的匹配程度。选项之一 "high" or "low".
图像修复操作的蒙版。
审核级别。默认为 "auto".
压缩级别 (0-100,默认 100)。
要流式传输的部分图像数量 (0-3)。
MCP 工具
McpTool 使模型能够访问外部模型上下文协议 (MCP) 服务器。这允许模型在单个对话轮次中调用远程 MCP 服务器公开的工具,从而实现与自定义服务和外部系统的集成。
from langchain_azure_ai.tools.builtin import McpTool
tool = McpTool(
server_label="my_mcp_server",
server_url="https://my-mcp-server.example.com",
allowed_tools=["tool_1", "tool_2"],
)
model_with_mcp = model.bind_tools([tool])
response = model_with_mcp.invoke(
"Use the MCP server to retrieve user profile information"
)
print(response)
Configuration options
此 MCP 服务器的标签,用于在工具调用中识别它。
MCP 服务器的 URL。必须提供 server_url or connector_id 或
内置服务连接器的标识符(例如, "connector_gmail")。必须提供 server_url or connector_id 必须提供。
模型允许在此服务器上调用的工具名称列表或工具过滤器字典。
随每个 MCP 服务器请求发送的可选 HTTP 头(例如用于身份验证)。
工具调用是否需要人工批准后才能执行。
MCP 服务器的可选描述,供模型使用。
MCP 服务器的 OAuth 访问令牌。
工具箱
设置
安装所需的依赖项:
pip install -U "langchain-azure-ai[tools]" langchain-mcp-adapters httpx
uv add "langchain-azure-ai[tools]" langchain-mcp-adapters httpx
AzureAIProjectToolbox
AzureAIProjectToolbox (来自 langchain_azure_ai.tools)从 Azure AI Foundry Toolbox 加载工具,并通过模型上下文协议 (MCP) 提供这些工具。
Azure AI Foundry Toolbox 是一个托管的多 MCP 服务器,通过单个 MCP 端点聚合多个已配置的工具。当您想在代理中动态加载和使用 Azure AI Foundry 项目中的工具集合时,请使用此功能。
工具箱自动处理:
- - Azure Identity Bearer 令牌身份验证
- - 优雅的 OAuth 同意错误处理:返回带有同意 URL 的备用工具,而不是引发异常
- - 对发出不完整 JSON 架构的 MCP 服务器进行自动工具架构清理
在 Microsoft Foundry 项目中创建工具箱。如需文档,请参阅 Foundry 中的工具箱.
Configuration parameters
Azure AI Foundry 项目端点,例如 https://<resource>.services.ai.azure.com/api/projects/<project>。回退到 AZURE_AI_PROJECT_ENDPOINT or FOUNDRY_PROJECT_ENDPOINT 环境变量。
在 Azure AI Foundry 中配置的工具箱名称。此参数为必需。
追加到 MCP URL 的工具箱 API 版本。
用于 Bearer 令牌身份验证的 Azure 凭据。接受字符串(静态 Bearer 令牌)或任意 TokenCredential 如 DefaultAzureCredential。默认为 DefaultAzureCredential().
要包含在 MCP 请求中的其他 HTTP 头。 Foundry-Features 标头会自动添加默认值(除非已存在)。
基本用法
from azure.identity import DefaultAzureCredential
from langchain_azure_ai.tools import AzureAIProjectToolbox
from langchain.agents import create_agent
from langchain.chat_models import init_chat_model
from langchain.messages import HumanMessage
async def main():
toolbox = AzureAIProjectToolbox(
project_endpoint="https://<resource>.services.ai.azure.com/api/projects/<project>",
toolbox_name="my-toolbox",
)
tools = await toolbox.get_tools()
model = init_chat_model("azure_ai:gpt-5-mini", credential=DefaultAzureCredential())
agent = create_agent(
model=model,
tools=tools,
)
result = await agent.ainvoke({
"messages": [HumanMessage("What can you do with the available tools?")]
})
return result
或改用环境变量:
然后不带参数实例化(或仅 toolbox_name):
from langchain_azure_ai.tools import AzureAIProjectToolbox
toolbox = AzureAIProjectToolbox(toolbox_name="my-toolbox")
tools = await toolbox.get_tools()
使用异步上下文管理器
async with 支持以获得更好的人体工程学兼容性:
async with AzureAIProjectToolbox(toolbox_name="my-toolbox") as toolbox:
tools = await toolbox.get_tools()
与代理的集成
get_tools() 方法返回 BaseTool 实例,准备好与任何 LangChain 代理模式配合使用:
from azure.identity import DefaultAzureCredential
from langchain_azure_ai.tools import AzureAIProjectToolbox
from langchain.agents import create_agent
from langchain.chat_models import init_chat_model
async def main():
credential = DefaultAzureCredential()
toolbox = AzureAIProjectToolbox(
toolbox_name="my-toolbox",
credential=credential,
)
tools = await toolbox.get_tools()
model = init_chat_model("azure_ai:gpt-5-mini", credential=credential)
agent = create_agent(
model=model,
tools=tools,
system_prompt=(
"You are a helpful assistant with access to a set of tools from "
"Azure AI Foundry. Use them to help the user with their requests."
),
)
return agent
API 参考
from langchain_azure_ai.tools import (
AzureAIProjectToolbox,
AzureOpenAIModelImageGenTool,
AzureOpenAITranscriptionsTool,
)
from langchain_azure_ai.tools.builtin import (
CodeInterpreterTool,
FileSearchTool,
ImageGenerationTool,
McpTool,
WebSearchTool,
)