重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
agents-v2-py by sickn33/antigravity-awesome-skills
npx skills add https://github.com/sickn33/antigravity-awesome-skills --skill agents-v2-py使用 Azure AI Projects SDK 中的 ImageBasedHostedAgentDefinition 构建基于容器的托管代理。
pip install azure-ai-projects>=2.0.0b3 azure-identity
最低 SDK 版本: 需要 2.0.0b3 或更高版本以支持托管代理。
AZURE_AI_PROJECT_ENDPOINT=https://<resource>.services.ai.azure.com/api/projects/<project>
在创建托管代理之前:
AcrPull 角色enablePublicHostingEnvironment=true广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
azure-ai-projects>=2.0.0b3始终使用 DefaultAzureCredential:
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
credential = DefaultAzureCredential()
client = AIProjectClient(
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
credential=credential
)
import os
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import (
ImageBasedHostedAgentDefinition,
ProtocolVersionRecord,
AgentProtocol,
)
client = AIProjectClient(
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
credential=DefaultAzureCredential()
)
agent = client.agents.create_version(
agent_name="my-hosted-agent",
definition=ImageBasedHostedAgentDefinition(
container_protocol_versions=[
ProtocolVersionRecord(protocol=AgentProtocol.RESPONSES, version="v1")
],
cpu="1",
memory="2Gi",
image="myregistry.azurecr.io/my-agent:latest",
tools=[{"type": "code_interpreter"}],
environment_variables={
"AZURE_AI_PROJECT_ENDPOINT": os.environ["AZURE_AI_PROJECT_ENDPOINT"],
"MODEL_NAME": "gpt-4o-mini"
}
)
)
print(f"Created agent: {agent.name} (version: {agent.version})")
versions = client.agents.list_versions(agent_name="my-hosted-agent")
for version in versions:
print(f"Version: {version.version}, State: {version.state}")
client.agents.delete_version(
agent_name="my-hosted-agent",
version=agent.version
)
| 参数 | 类型 | 必需 | 描述 |
|---|---|---|---|
container_protocol_versions | list[ProtocolVersionRecord] | 是 | 代理支持的协议版本 |
image | str | 是 | 完整的容器镜像路径(注册表/镜像:标签) |
cpu | str | 否 | CPU 分配(例如,"1", "2") |
memory | str | 否 | 内存分配(例如,"2Gi", "4Gi") |
tools | list[dict] | 否 | 代理可用的工具 |
environment_variables | dict[str, str] | 否 | 容器的环境变量 |
container_protocol_versions 参数指定您的代理支持哪些协议:
from azure.ai.projects.models import ProtocolVersionRecord, AgentProtocol
# RESPONSES 协议 - 标准代理响应
container_protocol_versions=[
ProtocolVersionRecord(protocol=AgentProtocol.RESPONSES, version="v1")
]
可用协议:
| 协议 | 描述 |
|---|---|
AgentProtocol.RESPONSES | 用于代理交互的标准响应协议 |
为您的容器指定 CPU 和内存:
definition=ImageBasedHostedAgentDefinition(
container_protocol_versions=[...],
image="myregistry.azurecr.io/my-agent:latest",
cpu="2", # 2 个 CPU 核心
memory="4Gi" # 4 GiB 内存
)
资源限制:
| 资源 | 最小值 | 最大值 | 默认值 |
|---|---|---|---|
| CPU | 0.5 | 4 | 1 |
| 内存 | 1Gi | 8Gi | 2Gi |
向您的托管代理添加工具:
tools=[{"type": "code_interpreter"}]
tools=[
{"type": "code_interpreter"},
{
"type": "mcp",
"server_label": "my-mcp-server",
"server_url": "https://my-mcp-server.example.com"
}
]
tools=[
{"type": "code_interpreter"},
{"type": "file_search"},
{
"type": "mcp",
"server_label": "custom-tool",
"server_url": "https://custom-tool.example.com"
}
]
将配置传递给您的容器:
environment_variables={
"AZURE_AI_PROJECT_ENDPOINT": os.environ["AZURE_AI_PROJECT_ENDPOINT"],
"MODEL_NAME": "gpt-4o-mini",
"LOG_LEVEL": "INFO",
"CUSTOM_CONFIG": "value"
}
最佳实践: 切勿硬编码密钥。使用环境变量或 Azure Key Vault。
import os
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import (
ImageBasedHostedAgentDefinition,
ProtocolVersionRecord,
AgentProtocol,
)
def create_hosted_agent():
"""使用自定义容器镜像创建托管代理。"""
client = AIProjectClient(
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
credential=DefaultAzureCredential()
)
agent = client.agents.create_version(
agent_name="data-processor-agent",
definition=ImageBasedHostedAgentDefinition(
container_protocol_versions=[
ProtocolVersionRecord(
protocol=AgentProtocol.RESPONSES,
version="v1"
)
],
image="myregistry.azurecr.io/data-processor:v1.0",
cpu="2",
memory="4Gi",
tools=[
{"type": "code_interpreter"},
{"type": "file_search"}
],
environment_variables={
"AZURE_AI_PROJECT_ENDPOINT": os.environ["AZURE_AI_PROJECT_ENDPOINT"],
"MODEL_NAME": "gpt-4o-mini",
"MAX_RETRIES": "3"
}
)
)
print(f"Created hosted agent: {agent.name}")
print(f"Version: {agent.version}")
print(f"State: {agent.state}")
return agent
if __name__ == "__main__":
create_hosted_agent()
import os
from azure.identity.aio import DefaultAzureCredential
from azure.ai.projects.aio import AIProjectClient
from azure.ai.projects.models import (
ImageBasedHostedAgentDefinition,
ProtocolVersionRecord,
AgentProtocol,
)
async def create_hosted_agent_async():
"""异步创建托管代理。"""
async with DefaultAzureCredential() as credential:
async with AIProjectClient(
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
credential=credential
) as client:
agent = await client.agents.create_version(
agent_name="async-agent",
definition=ImageBasedHostedAgentDefinition(
container_protocol_versions=[
ProtocolVersionRecord(
protocol=AgentProtocol.RESPONSES,
version="v1"
)
],
image="myregistry.azurecr.io/async-agent:latest",
cpu="1",
memory="2Gi"
)
)
return agent
| 错误 | 原因 | 解决方案 |
|---|---|---|
ImagePullBackOff | ACR 拉取权限被拒绝 | 授予项目托管身份 AcrPull 角色 |
InvalidContainerImage | 镜像未找到 | 验证镜像路径和标签是否存在于 ACR 中 |
CapabilityHostNotFound | 未配置能力主机 | 创建账户级能力主机 |
ProtocolVersionNotSupported | 无效的协议版本 | 使用 AgentProtocol.RESPONSES 和版本 "v1" |
latest此技能适用于执行概述中描述的工作流程或操作。
每周安装次数
69
代码仓库
GitHub 星标数
28.1K
首次出现
2026年2月13日
安全审计
安装于
opencode68
codex68
gemini-cli67
github-copilot67
amp67
kimi-cli67
Build container-based hosted agents using ImageBasedHostedAgentDefinition from the Azure AI Projects SDK.
pip install azure-ai-projects>=2.0.0b3 azure-identity
Minimum SDK Version: 2.0.0b3 or later required for hosted agent support.
AZURE_AI_PROJECT_ENDPOINT=https://<resource>.services.ai.azure.com/api/projects/<project>
Before creating hosted agents:
AcrPull role on the ACRenablePublicHostingEnvironment=trueazure-ai-projects>=2.0.0b3Always use DefaultAzureCredential:
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
credential = DefaultAzureCredential()
client = AIProjectClient(
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
credential=credential
)
import os
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import (
ImageBasedHostedAgentDefinition,
ProtocolVersionRecord,
AgentProtocol,
)
client = AIProjectClient(
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
credential=DefaultAzureCredential()
)
agent = client.agents.create_version(
agent_name="my-hosted-agent",
definition=ImageBasedHostedAgentDefinition(
container_protocol_versions=[
ProtocolVersionRecord(protocol=AgentProtocol.RESPONSES, version="v1")
],
cpu="1",
memory="2Gi",
image="myregistry.azurecr.io/my-agent:latest",
tools=[{"type": "code_interpreter"}],
environment_variables={
"AZURE_AI_PROJECT_ENDPOINT": os.environ["AZURE_AI_PROJECT_ENDPOINT"],
"MODEL_NAME": "gpt-4o-mini"
}
)
)
print(f"Created agent: {agent.name} (version: {agent.version})")
versions = client.agents.list_versions(agent_name="my-hosted-agent")
for version in versions:
print(f"Version: {version.version}, State: {version.state}")
client.agents.delete_version(
agent_name="my-hosted-agent",
version=agent.version
)
| Parameter | Type | Required | Description |
|---|---|---|---|
container_protocol_versions | list[ProtocolVersionRecord] | Yes | Protocol versions the agent supports |
image | str | Yes | Full container image path (registry/image:tag) |
cpu | str | No |
The container_protocol_versions parameter specifies which protocols your agent supports:
from azure.ai.projects.models import ProtocolVersionRecord, AgentProtocol
# RESPONSES protocol - standard agent responses
container_protocol_versions=[
ProtocolVersionRecord(protocol=AgentProtocol.RESPONSES, version="v1")
]
Available Protocols:
| Protocol | Description |
|---|---|
AgentProtocol.RESPONSES | Standard response protocol for agent interactions |
Specify CPU and memory for your container:
definition=ImageBasedHostedAgentDefinition(
container_protocol_versions=[...],
image="myregistry.azurecr.io/my-agent:latest",
cpu="2", # 2 CPU cores
memory="4Gi" # 4 GiB memory
)
Resource Limits:
| Resource | Min | Max | Default |
|---|---|---|---|
| CPU | 0.5 | 4 | 1 |
| Memory | 1Gi | 8Gi | 2Gi |
Add tools to your hosted agent:
tools=[{"type": "code_interpreter"}]
tools=[
{"type": "code_interpreter"},
{
"type": "mcp",
"server_label": "my-mcp-server",
"server_url": "https://my-mcp-server.example.com"
}
]
tools=[
{"type": "code_interpreter"},
{"type": "file_search"},
{
"type": "mcp",
"server_label": "custom-tool",
"server_url": "https://custom-tool.example.com"
}
]
Pass configuration to your container:
environment_variables={
"AZURE_AI_PROJECT_ENDPOINT": os.environ["AZURE_AI_PROJECT_ENDPOINT"],
"MODEL_NAME": "gpt-4o-mini",
"LOG_LEVEL": "INFO",
"CUSTOM_CONFIG": "value"
}
Best Practice: Never hardcode secrets. Use environment variables or Azure Key Vault.
import os
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import (
ImageBasedHostedAgentDefinition,
ProtocolVersionRecord,
AgentProtocol,
)
def create_hosted_agent():
"""Create a hosted agent with custom container image."""
client = AIProjectClient(
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
credential=DefaultAzureCredential()
)
agent = client.agents.create_version(
agent_name="data-processor-agent",
definition=ImageBasedHostedAgentDefinition(
container_protocol_versions=[
ProtocolVersionRecord(
protocol=AgentProtocol.RESPONSES,
version="v1"
)
],
image="myregistry.azurecr.io/data-processor:v1.0",
cpu="2",
memory="4Gi",
tools=[
{"type": "code_interpreter"},
{"type": "file_search"}
],
environment_variables={
"AZURE_AI_PROJECT_ENDPOINT": os.environ["AZURE_AI_PROJECT_ENDPOINT"],
"MODEL_NAME": "gpt-4o-mini",
"MAX_RETRIES": "3"
}
)
)
print(f"Created hosted agent: {agent.name}")
print(f"Version: {agent.version}")
print(f"State: {agent.state}")
return agent
if __name__ == "__main__":
create_hosted_agent()
import os
from azure.identity.aio import DefaultAzureCredential
from azure.ai.projects.aio import AIProjectClient
from azure.ai.projects.models import (
ImageBasedHostedAgentDefinition,
ProtocolVersionRecord,
AgentProtocol,
)
async def create_hosted_agent_async():
"""Create a hosted agent asynchronously."""
async with DefaultAzureCredential() as credential:
async with AIProjectClient(
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
credential=credential
) as client:
agent = await client.agents.create_version(
agent_name="async-agent",
definition=ImageBasedHostedAgentDefinition(
container_protocol_versions=[
ProtocolVersionRecord(
protocol=AgentProtocol.RESPONSES,
version="v1"
)
],
image="myregistry.azurecr.io/async-agent:latest",
cpu="1",
memory="2Gi"
)
)
return agent
| Error | Cause | Solution |
|---|---|---|
ImagePullBackOff | ACR pull permission denied | Grant AcrPull role to project's managed identity |
InvalidContainerImage | Image not found | Verify image path and tag exist in ACR |
CapabilityHostNotFound | No capability host configured | Create account-level capability host |
ProtocolVersionNotSupported | Invalid protocol version | Use AgentProtocol.RESPONSES with version |
latest in productionThis skill is applicable to execute the workflow or actions described in the overview.
Weekly Installs
69
Repository
GitHub Stars
28.1K
First Seen
Feb 13, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
opencode68
codex68
gemini-cli67
github-copilot67
amp67
kimi-cli67
Azure 配额管理指南:服务限制、容量验证与配额增加方法
122,900 周安装
| CPU allocation (e.g., "1", "2") |
memory | str | No | Memory allocation (e.g., "2Gi", "4Gi") |
tools | list[dict] | No | Tools available to the agent |
environment_variables | dict[str, str] | No | Environment variables for the container |
"v1"