ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

LLM 工作流上线清单:预算、权限、观测与降级

LLM 工作流上线清单:预算、权限、观测与降级 LLM 工作流上线清单预算、权限、观测与降级LLM 工作流上线前要回答四个问题一次任务最多花多少、能调用哪些工具、出了问题看哪里、下游不可用如何降级。把答案写进配置和测试比口头约定可靠。1. LLM 自动化工作流部署三大架构隐患将 LLM 接入自动化工作流时如果没有在部署拓扑与配置层做好隔离通常会出现以下三大生产危机API 密钥明文暴露与泄露风险把 API Key 直接写在代码或普通 ConfigMap 里没有走 KMSKey Management Service或 Secret 挂载。缺乏多 Key 轮换与 Rate Limit 流量削峰单 Key 在高并发下很快触发 429 Too Many Requests 错误导致整个自动化 Workflow 报废。阻塞式调用缺乏背压LLM 响应时间波动较大若同步请求持续占用 Worker线程池可能排队。应测量响应分布和在途请求数再配置并发上限、超时与队列拒绝策略。2. 带有多 Key 轮换与背压限制的 Gateway 代码实现为了收口 API 密钥配置并在 Worker 队列与模型 API 之间加入缓冲区在全栈 Node.js/TypeScript 服务中实现了一个带 Key 轮换与并发背压的代理中间件。下面的代码展示了如何在生产环境下安全注入配置并平滑分发 LLM 请求import http from http; import { EventEmitter } from events; interface KeyConfig { apiKey: string; weight: number; isAlive: boolean; } export class LLMGatewayProxy { private keys: KeyConfig[] []; private currentKeyIndex 0; private activeRequests 0; private maxConcurrent: number; constructor(apiKeysEnv: string, maxConcurrent: number 20) { this.maxConcurrent maxConcurrent; // 1. 从收口的配置中解析多 API Key 环境变量 (格式: key1,key2,key3) const rawKeys apiKeysEnv.split(,).map(k k.trim()).filter(Boolean); if (rawKeys.length 0) { throw new Error(❌ [FATAL] 部署配置错误: 未找到可用的大模型 API Keys!); } this.keys rawKeys.map(key ({ apiKey: key, weight: 1, isAlive: true })); } // 轮换算法获取有效 API Key private getNextApiKey(): string { const aliveKeys this.keys.filter(k k.isAlive); if (aliveKeys.length 0) { throw new Error(❌ 所有配置的模型 API Keys 均已离线或限流); } this.currentKeyIndex (this.currentKeyIndex 1) % aliveKeys.length; return aliveKeys[this.currentKeyIndex].apiKey; } // 标记触发 Rate Limit 的 Key 暂时熔断 public markKeyFailure(apiKey: string) { const target this.keys.find(k k.apiKey apiKey); if (target) { target.isAlive false; console.warn(⚠️ API Key [***${apiKey.slice(-4)}] 触发限流进入 60 秒熔断状态); setTimeout(() { target.isAlive true; console.log(✅ API Key [***${apiKey.slice(-4)}] 熔断解除恢复工作); }, 60000); } } // 带有并发背压控制的流式转发器 public async dispatchRequest(prompt: string): Promisestring { if (this.activeRequests this.maxConcurrent) { throw new Error(⚠️ 网关触发背压隔离高并发排队已满降级处理); } this.activeRequests; const selectedKey this.getNextApiKey(); try { const response await this.executeHttpRequest(selectedKey, prompt); return response; } catch (err: any) { if (err.message.includes(429)) { this.markKeyFailure(selectedKey); // 自动重试一次 (使用下一个健康的 Key) return this.dispatchRequest(prompt); } throw err; } finally { this.activeRequests--; } } private executeHttpRequest(apiKey: string, prompt: string): Promisestring { return new Promise((resolve, reject) { // 模拟调用第三方大模型 API setTimeout(() { if (Math.random() 0.05) { reject(new Error(429 Too Many Requests)); } else { resolve([LLM Response for prompt: ${prompt.slice(0, 15)}...]); } }, 150); }); } }3. K8s 部署配置治理与诊断检查在 Kubernetes 生产部署清单中绝对不能把 API Key 明文写入 Deployment 环境变量。必须使用 K8s Secret 加密存储并在 Container 中以环境变量形式只读注入# deployment.yaml 生产部署配置治理范例 apiVersion: apps/v1 kind: Deployment metadata: name: llm-workflow-worker namespace: production spec: replicas: 4 template: metadata: labels: app: llm-workflow-worker spec: containers: - name: worker image: registry.internal/llm-worker:v1.2.0 env: - name: LLM_API_KEYS valueFrom: secretKeyRef: name: llm-api-secrets key: API_KEYS_LIST - name: MAX_CONCURRENT_PER_POD value: 25 resources: limits: cpu: 2 memory: 2Gi requests: cpu: 500m memory: 512Mi部署上线前在终端运行配置扫描与连通性诊断命令# 1. 检查集群 Secret 中 API Key 格式与安全权限 kubectl get secret llm-api-secrets -n production -o jsonpath{.data.API_KEYS_LIST} | base64 --decode # 2. 模拟突发高并发请求检测网关背压与多 Key 轮换表现 npx autocannon -c 30 -d 15 -m POST \ -H Content-Type: application/json \ -b {prompt:测试工作流吞吐} \ http://localhost:3000/api/v1/workflow/dispatch4. 优化效果与部署总结通过重构智能工作流的生产部署拓扑与网关 Key 管理可以在高并发峰值下的稳健性明显跃升运维评估维度治理前 (硬编码单 Key 直连)治理后 (代理网关 多 Key 轮换 K8s Secret)API Key 泄露风险高 (经常随镜像打入)治理后结果 (代理网关 多 Key 轮换 K8s Secret)429 Rate Limit 触发率治理前基线 (硬编码单 Key 直连)治理后结果 (代理网关 多 Key 轮换 K8s Secret)Worker 节点资源利用率治理前基线 (硬编码单 Key 直连)治理后结果 (代理网关 多 Key 轮换 K8s Secret)高并发工作流吞吐量治理前基线 (硬编码单 Key 直连)治理后结果 (代理网关 多 Key 轮换 K8s Secret)部署前必备 CheckList密钥不落盘API Key 统一收口在 KMS 或加密 Secret代码与 Docker 镜像中不要出现任何sk-开头的字符串。必须有代理网关做 Token 计数与轮换Worker 节点不要直接连外部模型 API中间加一层网关负责限流、熔断与多 Key 负载均衡。工作流必须异步化耗时较长的大模型调用一定要放入 Redis / RabbitMQ 等消息队列中解耦前端通过 SSE 或 Websocket 接收结果防止 HTTP 连接长时间悬挂。
返回列表