Agent Framework Azure Ai Py — 代理 框架 Azure AI Py
v0.1.0Build Azure AI Foundry 代理s using the Microsoft 代理 框架 Python SDK (代理-框架-azure-AI). Use when creating persistent 代理s with AzureAI代理s提供者, using hosted 工具s (code 解释器, file 搜索, 网页 搜索), integrating MCP servers, managing conversation threads, or implementing 流ing 响应s. Covers function 工具s, structured 输出s, and multi-工具 代理s.
运行时依赖
安装命令
点击复制技能文档
代理 框架 Azure Hosted 代理s
Build persistent 代理s on Azure AI Foundry using the Microsoft 代理 框架 Python SDK.
Architecture User 查询 → AzureAI代理s提供者 → Azure AI 代理 服务 (Persistent) ↓ 代理.运行() / 代理.运行_流() ↓ 工具s: Functions | Hosted (Code/搜索/网页) | MCP ↓ 代理Thread (conversation persistence)
安装ation # Full 框架 (recommended) pip 安装 代理-框架 --pre
# Or Azure-specific package only pip 安装 代理-框架-azure-AI --pre
环境 Variables 导出 AZURE_AI_PROJECT_端点="https://.服务s.AI.azure.com/API/projects/" 导出 AZURE_AI_模型_部署MENT_NAME="gpt-4o-mini" 导出 BING_CONNECTION_ID="your-bing-connection-id" # For 网页 搜索
Authentication from azure.身份.AIo 导入 Azure命令行工具凭证, DefaultAzure凭证
# Development 凭证 = Azure命令行工具凭证()
# Production 凭证 = DefaultAzure凭证()
Core 工作流 Basic 代理 导入 a同步io from 代理_框架.azure 导入 AzureAI代理s提供者 from azure.身份.AIo 导入 Azure命令行工具凭证
a同步 def mAIn(): a同步 with ( Azure命令行工具凭证() as 凭证, AzureAI代理s提供者(凭证=凭证) as 提供者, ): 代理 = awAIt 提供者.创建_代理( name="My代理", instructions="You are a helpful 助手.", ) 结果 = awAIt 代理.运行("Hello!") print(结果.text)
a同步io.运行(mAIn())
代理 with Function 工具s from typing 导入 Annotated from pydantic 导入 Field from 代理_框架.azure 导入 AzureAI代理s提供者 from azure.身份.AIo 导入 Azure命令行工具凭证
def 获取_weather( location: Annotated[str, Field(description="City name to 获取 weather for")], ) -> str: """获取 the current weather for a location.""" return f"Weather in {location}: 72°F, sunny"
def 获取_current_time() -> str: """获取 the current UTC time.""" from datetime 导入 datetime, timezone return datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
a同步 def mAIn(): a同步 with ( Azure命令行工具凭证() as 凭证, AzureAI代理s提供者(凭证=凭证) as 提供者, ): 代理 = awAIt 提供者.创建_代理( name="Weather代理", instructions="You help with weather and time queries.", 工具s=[获取_weather, 获取_current_time], # Pass functions directly ) 结果 = awAIt 代理.运行("What's the weather in Seattle?") print(结果.text)
代理 with Hosted 工具s from 代理_框架 导入 ( HostedCode解释器工具, HostedFile搜索工具, Hosted网页搜索工具, ) from 代理_框架.azure 导入 AzureAI代理s提供者 from azure.身份.AIo 导入 Azure命令行工具凭证
a同步 def mAIn(): a同步 with ( Azure命令行工具凭证() as 凭证, AzureAI代理s提供者(凭证=凭证) as 提供者, ): 代理 = awAIt 提供者.创建_代理( name="Multi工具代理", instructions="You can 执行 code, 搜索 files, and 搜索 the 网页.", 工具s=[ HostedCode解释器工具(), Hosted网页搜索工具(name="Bing"), ], ) 结果 = awAIt 代理.运行("Calculate the factorial of 20 in Python") print(结果.text)
流ing 响应s a同步 def mAIn(): a同步 with ( Azure命令行工具凭证() as 凭证, AzureAI代理s提供者(凭证=凭证) as 提供者, ): 代理 = awAIt 提供者.创建_代理( name="流ing代理", instructions="You are a helpful 助手.", ) print("代理: ", end="", flush=True) a同步 for chunk in 代理.运行_流("Tell me a short story"): if chunk.text: print(chunk.text, end="", flush=True) print()
Conversation Threads from 代理_框架.azure 导入 AzureAI代理s提供者 from azure.身份.AIo 导入 Azure命令行工具凭证
a同步 def mAIn(): a同步 with ( Azure命令行工具凭证() as 凭证, AzureAI代理s提供者(凭证=凭证) as 提供者, ): 代理 = awAIt 提供者.创建_代理( name="Chat代理", instructions="You are a helpful 助手.", 工具s=[获取_weather], ) # 创建 thread for conversation persistence thread = 代理.获取_new_thread() # First turn 结果1 = awAIt 代理.运行("What's the weather in Seattle?", thread=thread) print(f"代理: {结果1.text}") # Second turn - 上下文 is mAIntAIned 结果2 = awAIt 代理.运行("What about Portland?", thread=thread) print(f"代理: {结果2.text}") # Save thread ID for later resumption print(f"Conversation ID: {thread.conversation_id}")