使用 Azure AI 项目 Python SDK (azure-ai-projects) 构建 AI 应用。适用于处理 Foundry 项目客户端、使用 PromptAgentDefinition 创建版本化代理、运行评估、管理连接/部署/数据集/索引,或使用 OpenAI 兼容客户端。这是高级 Foundry SDK - 如需低级代理操作,请使用 azure-ai-agents-python 技能。
Azure AI Projects Python SDK(Foundry SDK). 在 Azure AI 创始人上使用 azure-ai- project S是一项面向实际任务的技能,主要用于客户操作概览;Acce 操作。
从功能定位来看,该技能强调把分散的操作要求整理成清晰、可复用的处理流程,使用户能够围绕既定目标快速准备输入、选择执行方式并获得结构化结果。实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;
若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。
使用 azure-ai-projects SDK 在 Azure AI Foundry 上构建 AI 应用程序。
pip install azure-ai-projects azure-identity
AZURE_AI_PROJECT_ENDPOINT="https://.services.ai.azure.com/api/projects/"
AZURE_AI_MODEL_DEPLOYMENT_NAME="gpt-4o-mini"
import os
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
credential = DefaultAzureCredential()
client = AIProjectClient(
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
credential=credential,
)
| 操作 | 访问路径 | 用途 |
|---|---|---|
client.agents |
.agents.* |
Agent 的增删改查、版本管理、会话线程(thread)、运行(run) |
client.connections |
.connections.* |
列出/获取项目连接(connection) |
client.deployments |
.deployments.* |
列出模型部署(model deployment) |
client.datasets |
.datasets.* |
数据集(dataset)管理 |
client.indexes |
.indexes.* |
索引(index)管理 |
client.evaluations |
.evaluations.* |
执行评估(evaluation) |
client.red_teams |
.red_teams.* |
红队(red team)操作 |
from azure.ai.projects import AIProjectClient
client = AIProjectClient(
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
credential=DefaultAzureCredential(),
)
# 使用 Foundry 原生操作
agent = client.agents.create_agent(
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
name="my-agent",
instructions="You are helpful.",
)
# 从项目获取兼容 OpenAI 的客户端
openai_client = client.get_openai_client()
# 使用标准 OpenAI API
response = openai_client.chat.completions.create(
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
messages=[{"role": "user", "content": "Hello!"}],
)
agent = client.agents.create_agent(
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
name="my-agent",
instructions="You are a helpful assistant.",
)
from azure.ai.agents import CodeInterpreterTool, FileSearchTool
agent = client.agents.create_agent(
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
name="tool-agent",
instructions="You can execute code and search files.",
tools=[CodeInterpreterTool(), FileSearchTool()],
)
from azure.ai.projects.models import PromptAgentDefinition
# 创建带版本的 Agent
agent_version = client.agents.create_version(
agent_name="customer-support-agent",
definition=PromptAgentDefinition(
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
instructions="You are a customer support specialist.",
tools=[], # 根据需要添加工具
),
version_label="v1.0",
)
详见 references/agents.md 中关于 Agent 的详细模式说明。
| 工具 | 类名 | 适用场景 |
|---|---|---|
| 代码解释器 | CodeInterpreterTool |
执行 Python 代码、生成文件 |
| 文件搜索 | FileSearchTool |
对已上传文档执行 RAG |
| Bing 接地 | BingGroundingTool |
网页搜索(需配置 connection) |
| Azure AI Search | AzureAISearchTool |
搜索您创建的索引 |
| 函数调用 | FunctionTool |
调用您的 Python 函数 |
| OpenAPI | OpenApiTool |
调用 REST API |
| MCP | McpTool |
对接 Model Context Protocol 服务器 |
| 内存搜索 | MemorySearchTool |
搜索 Agent 的内存存储(memory store) |
| SharePoint | SharepointGroundingTool |
搜索 SharePoint 内容 |
所有工具的使用模式详见 references/tools.md。
# 1. 创建 thread
thread = client.agents.threads.create()
# 2. 添加消息
client.agents.messages.create(
thread_id=thread.id,
role="user",
content="What's the weather like?",
)
# 3. 创建并处理 run
run = client.agents.runs.create_and_process(
thread_id=thread.id,
agent_id=agent.id,
)
# 4. 获取响应
if run.status == "completed":
messages = client.agents.messages.list(thread_id=thread.id)
for msg in messages:
if msg.role == "assistant":
print(msg.content[0].text.value)
# 列出所有 connection
connections = client.connections.list()
for conn in connections:
print(f"{conn.name}: {conn.connection_type}")
# 获取指定 connection
connection = client.connections.get(connection_name="my-search-connection")
连接相关使用模式详见 references/connections.md。
# 列出可用的模型部署
deployments = client.deployments.list()
for deployment in deployments:
print(f"{deployment.name}: {deployment.model}")
部署相关使用模式详见 references/deployments.md。
# 列出数据集
datasets = client.datasets.list()
# 列出索引
indexes = client.indexes.list()
数据操作相关说明详见 references/datasets-indexes.md。
# 使用 OpenAI 客户端执行评估
openai_client = client.get_openai_client()
# 使用内置评估器创建评估任务
eval_run = openai_client.evals.runs.create(
eval_id="my-eval",
name="quality-check",
data_source={
"type": "custom",
"item_references": [{"item_id": "test-1"}],
},
testing_criteria=[
{"type": "fluency"},
{"type": "task_adherence"},
],
)
评估相关使用模式详见 references/evaluation.md。
from azure.ai.projects.aio import AIProjectClient
async with AIProjectClient(
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
credential=DefaultAzureCredential(),
) as client:
agent = await client.agents.create_agent(...)
# ... 异步操作
异步使用模式详见 references/async-patterns.md。
# 为 Agent 创建内存存储
memory_store = client.agents.create_memory_store(
name="conversation-memory",
)
# 将其附加至 Agent,实现持久化记忆能力
agent = client.agents.create_agent(
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
name="memory-agent",
tools=[MemorySearchTool()],
tool_resources={"memory": {"store_ids": [memory_store.id]}},
)
async with AIProjectClient(...) as client:client.agents.delete_agent(agent.id)create_and_process,对实时用户体验(UX)则优先采用 流式响应(streaming)| 功能 | azure-ai-projects |
azure-ai-agents |
|---|---|---|
| 抽象层级 | 高层级(面向 Foundry) | 低层级(面向 Agents) |
| 客户端类型 | AIProjectClient |
AgentsClient |
| 版本支持 | create_version() |
不支持 |
| 连接(Connections) | 支持 | 不支持 |
| 部署(Deployments) | 支持 | 不支持 |
| 数据集 / 索引 | 支持 | 不支持 |
| 评估(Evaluation) | 通过 OpenAI 客户端支持 | 不支持 |
| 适用场景 | 需完整集成 Azure AI Foundry 的应用 | 独立的 Agent 应用 |