AI代理框架示例

基于Azure AI Foundry CB Agent框架构建的示例代理,用于快速部署和运行AI代理服务。

  • by djetchev
  • 本地部署
  • 更新于
在 GitHub 查看 djetchev/foundry-agents-mcp-scenarios

使用说明

铸造代理mcp场景

基于Azure AI Foundry CB Agent框架构建的示例代理。

存储库结构

examples/
  foundry-cbagent-hello/               # Minimal "Hello, World!" agent
    main.py                            # Agent implementation (HelloWorldAgent)
    requirements.txt                   # Python dependencies
    Dockerfile                         # Container image for deployment
    azure.yaml                         # azd deployment manifest
    infra/                             # Bicep infrastructure-as-code
      main.bicep                       #   Orchestrator (RG + modules)
      main.parameters.json             #   Maps azd env vars → Bicep params
      modules/
        hub-dependencies.bicep         #   Storage Account + Key Vault
        hub.bicep                      #   AI Foundry Hub + AI Services connection
        project.bicep                  #   AI Foundry Project
        container-registry.bicep       #   Azure Container Registry
        container-app.bicep            #   Container App Environment + Container App

先决条件

快速启动--在本地运行

cd examples/foundry-cbagent-hello
python -m venv .venv
source .venv/bin/activate   # On Windows: .venv\Scripts\activate
pip install -r requirements.txt
python main.py

代理服务器在端口上启动 8088. 用以下方法进行测试:

curl -X POST http://localhost:8088/runs \
  -H "Content-Type: application/json" \
  -d '{"input": "hello"}'

预期响应:

{
  "id": "hello-world",
  "output": [
    {
      "status": "completed",
      "content": [{ "text": "Hello, World!", "type": "output_text" }],
      "type": "message",
      "role": "assistant"
    }
  ],
  "object": "response"
}

部署到Azure

建筑

azd provision 创建a 新资源组 使用以下资源。 您现有的AI服务资源仍保留在其原始资源组中 通过集线器连接引用:

rg-<env-name>  (new, managed by azd)
├── Storage Account              ← required by Hub
├── Key Vault                    ← required by Hub
├── AI Foundry Hub               ← connects to your existing AI Services
│   └── AI Services Connection   (AAD auth, cross-RG reference)
├── AI Foundry Project           ← child of Hub
├── Container Registry (ACR)     ← stores agent Docker images
├── User-Assigned Managed Identity ← AcrPull on the registry
├── Container App Environment    ← hosting environment
└── Container App                ← runs the agent container (port 8088)

部署步骤

有 三个步骤:提供基础设施,构建映像,然后 更新容器应用程序。整个流程大约需要5分钟。

1.登录并配置环境

cd examples/foundry-cbagent-hello

azd auth login
azd init -e <env-name>            # e.g. cbagent-hello-dev

azd env set AZURE_SUBSCRIPTION_ID "<subscription-id>"
azd env set AZURE_LOCATION        "westus2"

# Resource ID of your existing AI Services account
azd env set AI_SERVICES_RESOURCE_ID \
  "/subscriptions/<sub-id>/resourceGroups/<rg>/providers/Microsoft.CognitiveServices/accounts/<name>"

# Endpoint of the same AI Services account
azd env set AI_SERVICES_ENDPOINT "https://<name>.cognitiveservices.azure.com/"

2.提供基础设施(~3-4分钟)

azd provision

这将创建资源组、集线器、项目、ACR、容器应用程序 环境、托管身份、角色分配和容器应用程序 单ARM部署。

3.构建并推送代理映像(约30秒)

使用ACR Build在云中构建Docker镜像(不需要本地Docker):

az acr build \
  --registry <acr-name> \
  --image agent:latest \
  ./examples/foundry-cbagent-hello

ACR名称打印为 azd provision 输出 (AZURE_CONTAINER_REGISTRY_NAME).您还可以通过以下方式检索它 azd env get-value AZURE_CONTAINER_REGISTRY_NAME.

推送图像后,更新容器应用程序:

azd deploy

验证

FQDN=$(azd env get-value AGENT_FQDN)

# Health check
curl https://$FQDN/liveness        # → HTTP 200

# Run the agent
curl -X POST https://$FQDN/runs \
  -H "Content-Type: application/json" \
  -d '{"input": "hello"}'

更新代理

代码更改后,重建并重新部署:

az acr build --registry <acr-name> --image agent:latest ./examples/foundry-cbagent-hello
azd deploy

拆除

要删除所有已配置的资源,请执行以下操作:

azd down

这将删除 rg-<env-name> 资源组及其内部的所有内容。 您的原始AI服务资源是 不 影响。