跳至正文
跳到正文
Wener Notes

MaaS API

约 7 分钟阅读
标签:MaaSAPI

本文梳理 MaaS(Model as a Service)中常见的线协议分类(ApiType)、端点路径映射(Endpoint Path)、能力抽象(ModelCapability)以及与统一 Model Service 接口的对应关系,用于规范网关路由、客户端适配、模型目录声明和用量计费。

相关文档:

  • 服务接口与注册表抽象:
  • 协议细节与字段对照:( · · · )
  • 计量与双边计费模型:

分层模型:Capability ↔ ApiType ↔ Endpoint

在多模型、多上游环境下,应把调用方要完成的能力、规范协议标识和具体 HTTP 路径拆成三层:

text
[1. Model Service / Capability]   业务语义:generateMessage / embed / rerank / generateImage / countTokens / listModel
               │
               ▼
[2. ApiType (规范协议标识)]         有限枚举:openai-chat-completions / anthropic-messages / google-generative-ai / bedrock-converse ...
               │                  作为路由、模型目录、Adapter 选择与 Billing Profile 的稳定主键
               ▼
[3. Endpoint Path + Method]       HTTP 契约:POST /{version}/chat/completions、POST /model/{model}/converse-stream ...

为什么需要独立的规范 ApiType,而不是直接存 URL 路径或宽泛别名(如 openai、responses):

  1. 同一能力对应多种线协议:例如 message-generation 可以走 OpenAI Chat Completions、OpenAI Responses、Anthropic Messages、Gemini GenerateContent、Bedrock Converse 等不同协议。
  2. 同一 ApiType 可包含多个操作路径:例如流式与非流式在 Google/Bedrock 上是两个 path(:generateContent vs :streamGenerateContent、/converse vs /converse-stream),资源类 API(Files、Uploads、Batches)包含多条 CRUD 子路径,但它们属于同一协议族和适配契约。
  3. 跨系统稳定标识:网关路由、客户端配置、模型目录(ModelInfo.apis)和计费日志(ModelRequestUsage.api / upstream_api)需要同一套无歧义的协议枚举。

ApiType 与 Endpoint 映射总表

下表按 ApiFamily 列出规范 ApiType、对应的模型能力(资源/控制面端点为 -)、HTTP Method、规范路径模板及 operation。路径中 {version} 在匹配时通常可省略,允许前置自定义 prefix(如 /openai/v1/chat/completions)。

OpenAI Family(openai)

typecapabilitymethodpathoperation
openai-chat-completionsmessage-generationPOST/{version}/chat/completionschat/completions
openai-completionstext-completionPOST/{version}/completionscompletions
openai-responsesmessage-generationPOST/{version}/responsesresponses
openai-response-compactmessage-generationPOST/{version}/responses/compactresponses/compact
openai-responses-input-tokenstoken-countingPOST/{version}/responses/input_tokensresponses/input_tokens
openai-models-GET/{version}/modelsmodels
openai-models-GET/{version}/models/{model}models/retrieve
openai-embeddingsembeddingPOST/{version}/embeddingsembeddings
openai-image-generationsimage-generationPOST/{version}/images/generationsimages/generations
openai-image-editsimage-editingPOST/{version}/images/editsimages/edits
openai-audio-speechaudio-generationPOST/{version}/audio/speechaudio/speech
openai-audio-transcriptionsaudio-transcriptionPOST/{version}/audio/transcriptionsaudio/transcriptions
openai-files-GET / POST/{version}/filesfiles
openai-files-GET / DELETE/{version}/files/{file_id}files/retrieve
openai-files-GET/{version}/files/{file_id}/contentfiles/content
openai-uploads-POST/{version}/uploadsuploads
openai-uploads-POST/{version}/uploads/{upload_id}/partsuploads/parts
openai-uploads-POST/{version}/uploads/{upload_id}/completeuploads/complete
openai-uploads-POST/{version}/uploads/{upload_id}/canceluploads/cancel
openai-batches-GET / POST/{version}/batchesbatches/list · batches/create
openai-batches-GET/{version}/batches/{batch_id}batches/retrieve
openai-batches-POST/{version}/batches/{batch_id}/cancelbatches/cancel

Anthropic Family(anthropic)

typecapabilitymethodpathoperation
anthropic-messagesmessage-generationPOST/{version}/messagesmessages
anthropic-messages-count-tokenstoken-countingPOST/{version}/messages/count_tokensmessages/count_tokens
anthropic-messages-batches-GET / POST/{version}/messages/batchesmessages/batches/list · messages/batches/create
google-vertex-anthropic-messagesmessage-generationPOST/{version}/projects/{project}/locations/{location}/publishers/anthropic/models/{model}:rawPredictrawPredict
google-vertex-anthropic-messagesmessage-generationPOST/{version}/projects/{project}/locations/{location}/publishers/anthropic/models/{model}:streamRawPredictstreamRawPredict
google-vertex-anthropic-messages-count-tokenstoken-countingPOST/{version}/projects/{project}/locations/{location}/publishers/anthropic/models/count-tokens:rawPredictrawPredict

Google Family(google)

typecapabilitymethodpathoperation
google-generative-aimessage-generationPOST/{version}/models/{model}:generateContentgenerateContent
google-generative-aimessage-generationPOST/{version}/models/{model}:streamGenerateContentstreamGenerateContent
google-generative-ai-embeddingsembeddingPOST/{version}/models/{model}:embedContentembedContent
google-generative-ai-embeddingsembeddingPOST/{version}/models/{model}:batchEmbedContentsbatchEmbedContents
google-generative-ai-count-tokenstoken-countingPOST/{version}/models/{model}:countTokenscountTokens
google-generative-ai-cached-contents-GET / POST / PATCH / DELETE/{version}/cachedContentscachedContents
google-vertexmessage-generationPOST/{version}/projects/{project}/locations/{location}/publishers/google/models/{model}:generateContentgenerateContent
google-vertexmessage-generationPOST/{version}/projects/{project}/locations/{location}/publishers/google/models/{model}:streamGenerateContentstreamGenerateContent

Amazon Bedrock Family(bedrock)

typecapabilitymethodpathoperation
bedrock-conversemessage-generationPOST/model/{model}/converseconverse
bedrock-conversemessage-generationPOST/model/{model}/converse-streamconverse-stream
bedrock-invokemessage-generationPOST/model/{model}/invokeinvoke
bedrock-invokemessage-generationPOST/model/{model}/invoke-with-response-streaminvoke-with-response-stream

其他常用与扩展 Family(rerank / sdapi / 自定义)

typefamilycapabilitymethodpathoperation
rerankrerankrerankingPOST/{version}/rerank · /{version}/rerankingrerank · reranking
sdapi-txt2imgsdapiimage-generationPOST/sdapi/{version}/txt2imgtxt2img
sdapi-img2imgsdapiimage-editingPOST/sdapi/{version}/img2imgimg2img
general自定义 / 透传-*任意上游路径逃生路径(仅记录原始路径与上下文)

业务专用或私有协议(如结构化决策 decision、视频生成异步任务、专用 OCR 端点等)按相同结构登记独立的 ApiType,不要复用 openai-chat-completions 等无关标识。

Capability 与 Model Service 映射

参照 ,业务层按语义能力调用,协议差异收敛在 Adapter 层:

ModelCapabilityIModelService 方法常见 ApiType
message-generationgenerateMessage / streamMessageopenai-chat-completions、openai-responses、anthropic-messages、google-generative-ai、google-vertex、google-vertex-anthropic-messages、bedrock-converse、bedrock-invoke
text-completion传统文本补全(兼容)openai-completions
embeddingembedopenai-embeddings、google-generative-ai-embeddings
rerankingrerankrerank
image-generationgenerateImageopenai-image-generations、sdapi-txt2img
image-editingeditImageopenai-image-edits、sdapi-img2img
audio-generationTTS / 语音合成openai-audio-speech
audio-transcriptionSTT / 语音转写openai-audio-transcriptions
token-countingcountTokensanthropic-messages-count-tokens、openai-responses-input-tokens、google-generative-ai-count-tokens、google-vertex-anthropic-messages-count-tokens
decisiondecide结构化决策专用协议
null(资源与控制面)listModel / getModel 等openai-models、openai-files、openai-uploads、openai-batches、anthropic-messages-batches、google-generative-ai-cached-contents

路径解析与协议差异规则

实现网关路由识别或日志解析(parseApiEndpoint(pathOrUrl, method))时,需处理四类典型协议差异:

差异维度OpenAI / Anthropic / RerankGoogle Gemini / VertexAmazon Bedrock
模型 ID 位置请求 JSON Body 的 model 字段嵌入路径 models/{model}:{op}嵌入路径 /model/{model}/{op}({model} 可能含 / 或 :,如 ARN / Inference Profile,需 URL decode 并取首尾段之间)
流式区分方式请求体 stream: true路径后缀 :streamGenerateContent / :streamRawPredict路径后缀 /converse-stream / /invoke-with-response-stream
版本号形式通常为 /v1/...、/v3/...,网关场景可能省略版本或带前缀/v1/...、/v1beta/...(正则 ^v[0-9]+(?:[a-z]+[0-9]*)?$)路径无版本段
同路径多动作需结合 HTTP Method 区分(如 GET /batches 列表 vs POST /batches 创建)动作在冒号后 :{operation}动作在末段 /{operation}

规范值、兼容别名与废弃别名

为避免配置、路由和历史账单出现碎片化命名,建议把 ApiType 治理分成三类:

  • 规范值(Canonical):所有新配置、Provider 注册、API 目录输出、用量事件一律写入规范值。
  • 兼容别名(Legacy Alias):历史已落库或旧服务发出的别名(例如 bedrock-converse-stream → bedrock-converse),读取时统一归一化(normalizeApiType),新写入不再产出。
  • 废弃别名(Retired Alias):语义过宽或存在歧义的旧写法(例如 openai、openai-chat、responses),配置校验阶段直接拒绝,不再隐式猜协议。

核心数据结构(概念模型)

TypeScript
type ApiHttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';

type ApiFamily =
  | 'openai'
  | 'anthropic'
  | 'bedrock'
  | 'google'
  | 'rerank'
  | 'sdapi'
  | string;

type ModelCapability =
  | 'message-generation'
  | 'text-completion'
  | 'embedding'
  | 'image-generation'
  | 'image-editing'
  | 'reranking'
  | 'token-counting'
  | 'audio-generation'
  | 'audio-transcription'
  | 'decision';

interface ApiEndpointDef {
  readonly type: ApiType;
  /** 规范路径模板,动态段用 {name} 表示;匹配时 {version} 可省略 */
  readonly path: string;
  readonly operation: string;
  readonly methods?: readonly ApiHttpMethod[];
}

interface ApiTypeDef {
  readonly type: ApiType;
  readonly title: string;
  readonly family: ApiFamily;
  readonly capability: ModelCapability | null;
  readonly endpoints: readonly ApiEndpointDef[];
}

/** 从请求 URL / Path 解析出的结构化端点信息 */
interface ParsedApiEndpoint {
  readonly raw: string;       // 去除 query/fragment 后的路径
  readonly type: ApiType;
  readonly version: string;   // 如 v1、v1beta,无则为空
  readonly prefix: string;    // version 之前的路由前缀
  readonly operation: string; // 如 chat/completions、converse-stream
  readonly model: string;     // 路径内嵌的模型 ID(若存在)
}

在注册表、网关与计费中的联动

  1. Provider 与模型注册(ModelProviderRegistry)
    • Provider 层声明默认协议 api 及支持的协议集合 apis,并通过 endpoints?: Partial<Record<ApiType, string>> 覆盖同源上游的非标准路径。
    • 模型层(ModelInfo.apis)可单独覆盖该模型支持的 ApiType,避免把 Provider 支持的所有协议误报给仅支持单一端点的模型(如纯 Embedding 或仅支持 Responses 的模型)。
  2. 网关协议转换(Adapter / Router)
    • 用 (client_api, upstream_api) 二元组唯一标识一次转发是同协议透传还是跨协议适配;缺少对应能力或无法无损转换时显式报错,不静默丢弃约束。
  3. 用量计量与计费(Usage & Billing)
    • 中的 ModelRequestUsage 同时记录 api(客户侧)与 upstream_api(上游侧)。
    • Normalization 层结合 upstream_provider + upstream_api + upstream_model 选择对应的 Token 包含口径(Inclusive vs Exclusive)与工具计费规则,确保跨协议转发和流式响应的计量准确。

参考

  • Provider 官方入口:、、、、、

关联信息

反向链接、本文链接的其他页面和外部资料。

站内链接

  • Amazon Bedrock MaaS API
    笔记 · Amazon Bedrock

    InvokeModel / InvokeModelWithResponseStream · 适合单次调用、模型原生 body、大 payload、非消息型/特殊模型 · Converse / ConverseStream · 新的统一消息接口

  • Anthropic MaaS API
    笔记 · Anthropic

    Anthropic Beta Flags · Anthropic FAQ · Anthropic API · https://platform.claude.com/docs/en/api/overview · SDK · https://github.com/anthropics/anthropic-sdk-typescript

  • Design Service Interface
    笔记 · Design Service Interface - Model Service

    资源方法使用「动词 + 单数资源名」,例如 、。是列表还是单个结果由动词和返回类型表达,便于搜索和生成。 · 能力方法按语义命名,例如 、,不受资源名规则约束。 · 按调用方要完成的操作命名。上游路由、鉴权和请求编码属于 Adapter;换协议不应该要求调用方重写业务输入。

  • Gemini API
    笔记 · Gemini

    Gemini Developer API / Google AI Studio API · generativelanguage.googleapis.com · API key · Google Generative AI API · https://ai.google.dev/api/rest

  • MaaS API Spec
    笔记 · MaaS API Spec

    Session / Thread · Turn · Message · Message Part / Content Part · Chunk Part · | topic | for | · | google | Gemini API、Vertex AI、Google tools / thinking / cache |

  • MaaS Spec Messages
    笔记 · Messages

    梳理 MaaS 需要适配的 message / item / content block / part 类型。 · 明确各协议的 role、type、基础结构和跨协议映射边界。 · 避免把 provider 的中间状态错误地压平成纯文本,尤其是 tool call、tool result、reasoning/thought、citation、multi...

  • MaaS SSE
    笔记 · SSE

    第一个 chunk 通常只有 ,没有内容。 · 后续 chunk 通常有 ,没有 。 · 最后的 chunk 里 和 可能分开出现。 · 第一个 chunk 和最后一个 chunk 通常不应该包含正常 content。 · 有些供应商为了紧凑,会在第一个 chunk 包含内容。

  • MaaS Tool Call
    笔记 · Tool Call

    Tool call / function calling 是模型请求外部能力的通用机制。 · MaaS 网关需要处理不同厂商的 tool schema、tool choice、tool result role、tool call id 和 streaming chunk 差异。

  • MaaS Usage
    笔记 · Usage

    MaaS Usage 用于描述模型请求的资源消耗与计费统计。不同 Provider 的字段命名、cache 口径、reasoning 口径和 streaming 返回时机不同,设计统一统计时应同时保留: · 原始 Provider usage:用于供应商账单核对和协议排障。

  • MaaS Usage & Billing
    笔记 · MaaS Usage & Billing

    通用按量计费通常是「单边事件 → 单一计量项 → 乘以单价」。MaaS 在此基础上增加了五类复杂度: · 双边主体并存:同一笔请求同时对应客户侧收入(Customer Rating)与上游供应侧成本(Provider Rating),两者共享原始用量事实,但协议、价格表、折扣、抵扣与异常规则可以不同。

  • Microsoft Foundry
    笔记 · Microsoft Foundry

    旧名称 Azure AI Foundry、Azure AI Studio、Azure AI Services · https://ai.azure.com/ · , 。 · OpenAI · Base URL: · | Surface | Endpoint shape | 定位 |

  • OpenAI MaaS API
    笔记 · OpenAI

    OpenAI API · https://platform.openai.com/docs/api-reference/chat/create · Responses / OpenResponses · https://www.openresponses.org/

  • Vertex AI MaaS API
    笔记 · Vertex AI

    Vertex AI 使用 Google Cloud project/location、IAM/ADC、quota、VPC-SC 和 regional/global endpoint。 · API host · Regional: · Global:

最近更新commit 8f6dcd8编辑