SuperAPI AI 工具集

azure-aigateway

部署与运维

查看项目

更新于 10-14 05:34

技能介绍

用途

azure-aigateway 用于将 Azure API Management(APIM)配置为 AI 网关,从而对 AI 模型、MCP 工具和智能体进行统一治理。它围绕三类治理场景展开:模型治理、工具治理和智能体治理,并覆盖配置与测试环节。

主要能力

模型治理

  • 语义缓存(semantic caching)
  • 令牌限制(token limits)
  • AI 负载均衡(load balance AI)
  • 令牌用量追踪(track token usage)

工具治理

  • MCP 限流(rate limit MCP)
  • 工具保护(protect my tools)
  • 工具配置(configure my tool)
  • 将 API 转换为 MCP(convert API to MCP)

智能体治理

  • 内容安全(content safety)
  • 越狱检测(jailbreak detection)
  • 过滤有害内容(filter harmful content)

配置与测试

  • 添加 Azure OpenAI 后端
  • 配置模型
  • 添加 AI Foundry 模型
  • 测试 AI 网关、通过网关调用 OpenAI

策略速查

策略 用途 详情位置
azure-openai-token-limit 成本控制 Model Policies
azure-openai-semantic-cache-lookup/store 语义缓存,60-80% 成本节省 Model Policies
azure-openai-emit-token-metric 可观测性 Model Policies
llm-content-safety 安全与合规 Agent Policies
rate-limit-by-key MCP/工具保护 Tool Policies

使用方式

获取网关详情

# Get gateway URL
az apim show --name <apim-name> --resource-group <rg> --query "gatewayUrl" -o tsv

# List backends (AI models)
az apim backend list --service-name <apim-name> --resource-group <rg> \
  --query "[].{id:name, url:url}" -o table

# Get subscription key
az apim subscription keys list \
  --service-name <apim-name> --resource-group <rg> --subscription-id <sub-id>

测试 AI 端点

GATEWAY_URL=$(az apim show --name <apim-name> --resource-group <rg> --query "gatewayUrl" -o tsv)

curl -X POST "${GATEWAY_URL}/openai/deployments/<deployment>/chat/completions?api-version=2024-02-01" \
  -H "Content-Type: application/json" \
  -H "Ocp-Apim-Subscription-Key: <key>" \
  -d '{"messages": [{"role": "user", "content": "Hello"}], "max_tokens": 100}'

添加 AI 后端

完整步骤见 references/patterns.md。

# Discover AI resources
az cognitiveservices account list --query "[?kind=='OpenAI']" -o table

# Create backend
az apim backend create --service-name <apim> --resource-group <rg> \
  --backend-id openai-backend --protocol http --url "https://<aoai>.openai.azure.com/openai"

# Grant access (managed identity)
az role assignment create --assignee <apim-principal-id> \
  --role "Cognitive Services User" --scope <aoai-resource-id>

应用 AI 治理策略

在 <inbound> 中推荐的策略顺序:

  1. Authentication - Managed identity to backend
  2. Semantic Cache Lookup - Check cache before calling AI
  3. Token Limits - Cost control
  4. Content Safety - Filter harmful content
  5. Backend Selection - Load balancing
  6. Metrics - Token usage tracking

完整示例见 references/policies.md。

适用场景

  • 需要对 AI 模型调用进行成本控制与令牌用量追踪
  • 希望通过语义缓存降低调用成本
  • 需要对 MCP 工具或 API 进行限流和保护
  • 需要为智能体添加内容安全与越狱检测
  • 需要将 API 转换为 MCP,并接入 Azure OpenAI 或 AI Foundry 模型
  • 需要通过网关测试 AI 端点

限制与排查

  • 需先部署 APIM,可使用 azure-prepare 技能完成部署,具体部署指南参见 APIM deployment guide。
  • 令牌限制返回 429:可提高 tokens-per-minute 或添加负载均衡。
  • 无缓存命中:可降低 score-threshold 至 0.7。
  • 内容误报:可提高类别阈值(5-6)。
  • 后端认证 401:需为 APIM 授予 “Cognitive Services User” 角色。
  • 详细排查见 references/troubleshooting.md。

参考

  • Detailed Policies - Full policy examples
  • Configuration Patterns - Step-by-step patterns
  • Troubleshooting - Common issues
  • AI-Gateway Samples
  • GenAI Gateway Docs
  • SDK Quick References:Content Safety(Python | TypeScript)、API Management(Python | .NET)