企业集成最佳实践(Enterprise Integration)

总览

在企业环境中引入与运营大模型,需要兼顾可靠性、可观测性、成本与合规,并与现有系统(认证、数据平台、消息与工作流引擎)稳健集成。此页提供架构参考与工程实践,以帮助你落地生产级 AI 能力。

架构模式

提示:建议把“生成”与“检索/工具调用”解耦,分别度量与优化。

可靠性与弹性

// Node.js(简化:重试 + 超时 + 断路器)
import OpenAI from "openai";
const client = new OpenAI({ apiKey: process.env.API_KEY, baseURL: process.env.API_BASE });

async function callLLM(payload, { retries = 2, timeoutMs = 15000 } = {}) {
  let attempt = 0;
  while (attempt <= retries) {
    attempt++;
    try {
      const ctl = new AbortController();
      const t = setTimeout(() => ctl.abort(), timeoutMs);
      const res = await client.chat.completions.create({ ...payload, signal: ctl.signal });
      clearTimeout(t);
      return res;
    } catch (e) {
      if (attempt > retries) throw e;
      const backoff = Math.min(1000 * 2 ** attempt, 8000);
      await new Promise(r => setTimeout(r, backoff));
    }
  }
}

可观测性与质量

成本优化

// 估算 token 与预算(示意)
function estimateTokens(text) { return Math.ceil(text.length / 4); }
function estimateCost(tokens, pricePer1k) { return (tokens / 1000) * pricePer1k; }

数据与平台集成

变更管理与发布

示例:调用封装(缓存 + 回退)

// Node.js(示意)
const cache = new Map();
async function generate(key, payload) {
  if (cache.has(key)) return cache.get(key);
  try {
    const res = await callLLM(payload);
    cache.set(key, res); // 仅缓存非错误结果
    return res;
  } catch (e) {
    // 兜底:降级为规则模板或备份供应商
    return { choices: [{ message: { content: "抱歉,服务繁忙,请稍后再试。" } }] };
  }
}