Service 服务AI 模型服务

/chat/completions

OpenAI 兼容端点,让 OpenAI SDK、curl 和兼容客户端调用任何注册模型。

POST /v1/ai/chat/completions 是 AIService 的 OpenAI 兼容端点。

工作原理

第三方工具
  → POST /v1/ai/chat/completions
  → { model, messages, stream, ... }
  → AIService 解析 model → Provider action
  → Provider openai action(或自动透传)
  → 上游 API
  → Response 原样返回

自动透传

当 Provider 有 baseURL + envKey 但没有 openai action 时:

  • 请求 body 原样转发到 {baseURL}/chat/completions
  • body.model 被替换为 passthroughModel(有则替换)
  • 上游 Response(JSON 或 SSE)原样返回
  • 不经过 ai-sdk,零转换开销

适用:DeepSeek 等 OpenAI 兼容的 API。

自定义转换

当 OpenAI 兼容上游需要自定义请求或响应处理时,可以提供 openai 方法:

import { Provider, type Context, type AIProviderChargedResponse } from "@downcity/city";

class OpenAICustomProvider extends Provider {
  constructor() {
    super({
      id: "openai-custom",
      baseURL: "https://api.openai.com/v1",
      envKey: "OPENAI_API_KEY",
    });
  }

  async openai(ctx: Context): Promise<AIProviderChargedResponse> {
    // 按上游要求定制 OpenAI body,并把响应归一回来
    const body = customizeOpenAIBody(ctx.input);
    const apiKey = ctx.env("OPENAI_API_KEY");
    const response = await fetch(`${this.baseURL}/chat/completions`, {
      method: "POST",
      headers: {
        authorization: `Bearer ${apiKey}`,
        "content-type": "application/json",
      },
      body: JSON.stringify(body),
    });
    return ctx.input.stream
      ? { response: normalizeStream(response) }
      : { response: normalizeJSON(await response.json()) };
  }
}

const openaiCustom = new OpenAICustomProvider();