后端与接口
需要在 Azure API Management 上配置…
当使用 Azure API Management 作为 AI Gateway 对 AI 模型、MCP 工具和代理进行治理、配置、测试和排障时适用。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 38/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 59/100 ×0.15
适用场景
需要在 Azure API Manage特定
解决思路
- 若需部署 APIM,使用 azure-prepare skill,并参考 APIM deployment guide。
- 配置 `azure-openai-token-limit` 策略进行成本控制。
- 配置 `azure-openai-semantic-cache-lookup/store` 策略实现语义缓存,可节省 60-80% 成本。
- 配置 `azure-openai-emit-token-metric` 策略做可观测性和令牌指标。
- 配置 `llm-content-safety` 策略满足安全与合规。
- 配置 `rate-limit-by-key` 策略保护 MCP/工具。
- 获取网关 URL:`az apim show --name <apim-name> --resource-group <rg> --query "gatewayUrl" -o tsv`。
- 列出后端(AI 模型):`az apim backend list --service-name <apim-name> --resource-group <rg> --query "[].{id:name, url:url}" -o table`。
- 获取订阅密钥:`az apim subscription keys list --service-name <apim-name> --resource-group <rg> --subscription-id <sub-id>`。
- 测试 AI endpoint:先执行 `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 Backend:发现 AI 资源:`az cognitiveservices account list --query "[?kind=='OpenAI']" -o table`。
- 添加 AI Backend:创建 backend:`az apim backend create --service-name <apim> --resource-group <rg> --backend-id openai-backend --protocol http --url "https://<aoai>.openai.azure.com/openai"`。
- 添加 AI Backend:用 managed identity 授予访问:`az role assignment create --assignee <apim-principal-id> --role "Cognitive Services User" --scope <aoai-resource-id>`。
- 应用 AI 治理策略时,在 `<inbound>` 中按推荐顺序配置第 1 项:认证 - 使用托管标识访问后端。
- 应用 AI 治理策略时,在 `<inbound>` 中按推荐顺序配置第 2 项:语义缓存查找 - 在调用 AI 前检查缓存。
- 应用 AI 治理策略时,在 `<inbound>` 中按推荐顺序配置第 3 项:令牌限额 - 成本控制。
- 应用 AI 治理策略时,在 `<inbound>` 中按推荐顺序配置第 4 项:内容安全 - 过滤有害内容。
- 应用 AI 治理策略时,在 `<inbound>` 中按推荐顺序配置第 5 项:后端选择 - 负载均衡。
- 应用 AI 治理策略时,在 `<inbound>` 中按推荐顺序配置第 6 项:指标 - 令牌用量跟踪。
- 排障 Token limit 429:增加 `tokens-per-minute` 或添加负载均衡。
- 排障无缓存命中:将 `score-threshold` 降到 0.7。
- 排障内容误报:提高类别阈值到 5-6。
- 排障后端认证 401:给 APIM 授予 "Cognitive Services User" 角色。
适用边界 · 注意事项
- 不适用于非 Azure API Management 的网关,或非 Azure AI 服务的治理场景。
- 需要 Azure CLI(`az`)、Azure API Management 实例和可用的 AI 后端资源;缺少这些依赖时不能直接按本经验执行。
- 只遇到与 Azure APIM AI Gateway 无关的通用 API 网关问题时,不应使用本经验。