MaaS API
本文梳理 MaaS(Model as a Service)中常见的线协议分类(ApiType)、端点路径映射(Endpoint Path)、能力抽象(ModelCapability)以及与统一 Model Service 接口的对应关系,用于规范网关路由、客户端适配、模型目录声明和用量计费。
相关文档:
- 服务接口与注册表抽象:Design Service Interface - Model Service笔记Design Service Interface资源方法使用「动词 + 单数资源名」,例如 、。是列表还是单个结果由动词和返回类型表达,便于搜索和生成。 · 能力方法按语义命名,例如 、,不受资源名规则约束。 · 按调用方要完成的操作命名。上游路由、鉴权和请求编码属于 Adapter;换协议不应该要求调用方重写业务输入。打开:/notes/dev/design/service/model
- 协议细节与字段对照:MaaS API Spec笔记MaaS API SpecSession / Thread · Turn · Message · Message Part / Content Part · Chunk Part · | topic | for | · | google | Gemini API、Vertex AI、Google tools / thinking / cache |打开:/notes/ai/maas/spec(Messages笔记MaaS Spec Messages梳理 MaaS 需要适配的 message / item / content block / part 类型。 · 明确各协议的 role、type、基础结构和跨协议映射边界。 · 避免把 provider 的中间状态错误地压平成纯文本,尤其是 tool call、tool result、reasoning/thought、citation、multi...打开:/notes/ai/maas/spec/messages · Tool Call笔记MaaS Tool CallTool call / function calling 是模型请求外部能力的通用机制。 · MaaS 网关需要处理不同厂商的 tool schema、tool choice、tool result role、tool call id 和 streaming chunk 差异。打开:/notes/ai/maas/spec/tool-call · SSE笔记MaaS SSE第一个 chunk 通常只有 ,没有内容。 · 后续 chunk 通常有 ,没有 。 · 最后的 chunk 里 和 可能分开出现。 · 第一个 chunk 和最后一个 chunk 通常不应该包含正常 content。 · 有些供应商为了紧凑,会在第一个 chunk 包含内容。打开:/notes/ai/maas/spec/sse · Usage笔记MaaS UsageMaaS Usage 用于描述模型请求的资源消耗与计费统计。不同 Provider 的字段命名、cache 口径、reasoning 口径和 streaming 返回时机不同,设计统一统计时应同时保留: · 原始 Provider usage:用于供应商账单核对和协议排障。打开:/notes/ai/maas/spec/usage)
- 计量与双边计费模型:MaaS Usage & Billing笔记MaaS Usage & Billing通用按量计费通常是「单边事件 → 单一计量项 → 乘以单价」。MaaS 在此基础上增加了五类复杂度: · 双边主体并存:同一笔请求同时对应客户侧收入(Customer Rating)与上游供应侧成本(Provider Rating),两者共享原始用量事实,但协议、价格表、折扣、抵扣与异常规则可以不同。打开:/notes/ai/maas/billing
分层模型:Capability ↔ ApiType ↔ Endpoint
在多模型、多上游环境下,应把调用方要完成的能力、规范协议标识和具体 HTTP 路径拆成三层:
[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):
- 同一能力对应多种线协议:例如
message-generation可以走 OpenAI Chat Completions、OpenAI Responses、Anthropic Messages、Gemini GenerateContent、Bedrock Converse 等不同协议。 - 同一
ApiType可包含多个操作路径:例如流式与非流式在 Google/Bedrock 上是两个 path(:generateContentvs:streamGenerateContent、/conversevs/converse-stream),资源类 API(Files、Uploads、Batches)包含多条 CRUD 子路径,但它们属于同一协议族和适配契约。 - 跨系统稳定标识:网关路由、客户端配置、模型目录(
ModelInfo.apis)和计费日志(ModelRequestUsage.api/upstream_api)需要同一套无歧义的协议枚举。
ApiType 与 Endpoint 映射总表
下表按 ApiFamily 列出规范 ApiType、对应的模型能力(资源/控制面端点为 -)、HTTP Method、规范路径模板及 operation。路径中 {version} 在匹配时通常可省略,允许前置自定义 prefix(如 /openai/v1/chat/completions)。
OpenAI Family(openai)
| type | capability | method | path | operation |
|---|---|---|---|---|
openai-chat-completions | message-generation | POST | /{version}/chat/completions | chat/completions |
openai-completions | text-completion | POST | /{version}/completions | completions |
openai-responses | message-generation | POST | /{version}/responses | responses |
openai-response-compact | message-generation | POST | /{version}/responses/compact | responses/compact |
openai-responses-input-tokens | token-counting | POST | /{version}/responses/input_tokens | responses/input_tokens |
openai-models | - | GET | /{version}/models | models |
openai-models | - | GET | /{version}/models/{model} | models/retrieve |
openai-embeddings | embedding | POST | /{version}/embeddings | embeddings |
openai-image-generations | image-generation | POST | /{version}/images/generations | images/generations |
openai-image-edits | image-editing | POST | /{version}/images/edits | images/edits |
openai-audio-speech | audio-generation | POST | /{version}/audio/speech | audio/speech |
openai-audio-transcriptions | audio-transcription | POST | /{version}/audio/transcriptions | audio/transcriptions |
openai-files | - | GET / POST | /{version}/files | files |
openai-files | - | GET / DELETE | /{version}/files/{file_id} | files/retrieve |
openai-files | - | GET | /{version}/files/{file_id}/content | files/content |
openai-uploads | - | POST | /{version}/uploads | uploads |
openai-uploads | - | POST | /{version}/uploads/{upload_id}/parts | uploads/parts |
openai-uploads | - | POST | /{version}/uploads/{upload_id}/complete | uploads/complete |
openai-uploads | - | POST | /{version}/uploads/{upload_id}/cancel | uploads/cancel |
openai-batches | - | GET / POST | /{version}/batches | batches/list · batches/create |
openai-batches | - | GET | /{version}/batches/{batch_id} | batches/retrieve |
openai-batches | - | POST | /{version}/batches/{batch_id}/cancel | batches/cancel |
Anthropic Family(anthropic)
| type | capability | method | path | operation |
|---|---|---|---|---|
anthropic-messages | message-generation | POST | /{version}/messages | messages |
anthropic-messages-count-tokens | token-counting | POST | /{version}/messages/count_tokens | messages/count_tokens |
anthropic-messages-batches | - | GET / POST | /{version}/messages/batches | messages/batches/list · messages/batches/create |
google-vertex-anthropic-messages | message-generation | POST | /{version}/projects/{project}/locations/{location}/publishers/anthropic/models/{model}:rawPredict | rawPredict |
google-vertex-anthropic-messages | message-generation | POST | /{version}/projects/{project}/locations/{location}/publishers/anthropic/models/{model}:streamRawPredict | streamRawPredict |
google-vertex-anthropic-messages-count-tokens | token-counting | POST | /{version}/projects/{project}/locations/{location}/publishers/anthropic/models/count-tokens:rawPredict | rawPredict |
Google Family(google)
| type | capability | method | path | operation |
|---|---|---|---|---|
google-generative-ai | message-generation | POST | /{version}/models/{model}:generateContent | generateContent |
google-generative-ai | message-generation | POST | /{version}/models/{model}:streamGenerateContent | streamGenerateContent |
google-generative-ai-embeddings | embedding | POST | /{version}/models/{model}:embedContent | embedContent |
google-generative-ai-embeddings | embedding | POST | /{version}/models/{model}:batchEmbedContents | batchEmbedContents |
google-generative-ai-count-tokens | token-counting | POST | /{version}/models/{model}:countTokens | countTokens |
google-generative-ai-cached-contents | - | GET / POST / PATCH / DELETE | /{version}/cachedContents | cachedContents |
google-vertex | message-generation | POST | /{version}/projects/{project}/locations/{location}/publishers/google/models/{model}:generateContent | generateContent |
google-vertex | message-generation | POST | /{version}/projects/{project}/locations/{location}/publishers/google/models/{model}:streamGenerateContent | streamGenerateContent |
Amazon Bedrock Family(bedrock)
| type | capability | method | path | operation |
|---|---|---|---|---|
bedrock-converse | message-generation | POST | /model/{model}/converse | converse |
bedrock-converse | message-generation | POST | /model/{model}/converse-stream | converse-stream |
bedrock-invoke | message-generation | POST | /model/{model}/invoke | invoke |
bedrock-invoke | message-generation | POST | /model/{model}/invoke-with-response-stream | invoke-with-response-stream |
其他常用与扩展 Family(rerank / sdapi / 自定义)
| type | family | capability | method | path | operation |
|---|---|---|---|---|---|
rerank | rerank | reranking | POST | /{version}/rerank · /{version}/reranking | rerank · reranking |
sdapi-txt2img | sdapi | image-generation | POST | /sdapi/{version}/txt2img | txt2img |
sdapi-img2img | sdapi | image-editing | POST | /sdapi/{version}/img2img | img2img |
general | 自定义 / 透传 | - | * | 任意上游路径 | 逃生路径(仅记录原始路径与上下文) |
业务专用或私有协议(如结构化决策
decision、视频生成异步任务、专用 OCR 端点等)按相同结构登记独立的ApiType,不要复用openai-chat-completions等无关标识。
Capability 与 Model Service 映射
参照 Model Service 接口设计笔记Design Service Interface资源方法使用「动词 + 单数资源名」,例如 、。是列表还是单个结果由动词和返回类型表达,便于搜索和生成。 · 能力方法按语义命名,例如 、,不受资源名规则约束。 · 按调用方要完成的操作命名。上游路由、鉴权和请求编码属于 Adapter;换协议不应该要求调用方重写业务输入。打开:/notes/dev/design/service/model,业务层按语义能力调用,协议差异收敛在 Adapter 层:
ModelCapability | IModelService 方法 | 常见 ApiType |
|---|---|---|
message-generation | generateMessage / streamMessage | openai-chat-completions、openai-responses、anthropic-messages、google-generative-ai、google-vertex、google-vertex-anthropic-messages、bedrock-converse、bedrock-invoke |
text-completion | 传统文本补全(兼容) | openai-completions |
embedding | embed | openai-embeddings、google-generative-ai-embeddings |
reranking | rerank | rerank |
image-generation | generateImage | openai-image-generations、sdapi-txt2img |
image-editing | editImage | openai-image-edits、sdapi-img2img |
audio-generation | TTS / 语音合成 | openai-audio-speech |
audio-transcription | STT / 语音转写 | openai-audio-transcriptions |
token-counting | countTokens | anthropic-messages-count-tokens、openai-responses-input-tokens、google-generative-ai-count-tokens、google-vertex-anthropic-messages-count-tokens |
decision | decide | 结构化决策专用协议 |
null(资源与控制面) | listModel / getModel 等 | openai-models、openai-files、openai-uploads、openai-batches、anthropic-messages-batches、google-generative-ai-cached-contents |
路径解析与协议差异规则
实现网关路由识别或日志解析(parseApiEndpoint(pathOrUrl, method))时,需处理四类典型协议差异:
| 差异维度 | OpenAI / Anthropic / Rerank | Google Gemini / Vertex | Amazon 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),配置校验阶段直接拒绝,不再隐式猜协议。
核心数据结构(概念模型)
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(若存在)
}在注册表、网关与计费中的联动
- Provider 与模型注册(ModelProviderRegistry)
- Provider 层声明默认协议
api及支持的协议集合apis,并通过endpoints?: Partial<Record<ApiType, string>>覆盖同源上游的非标准路径。 - 模型层(
ModelInfo.apis)可单独覆盖该模型支持的ApiType,避免把 Provider 支持的所有协议误报给仅支持单一端点的模型(如纯 Embedding 或仅支持 Responses 的模型)。
- Provider 层声明默认协议
- 网关协议转换(Adapter / Router)
- 用
(client_api, upstream_api)二元组唯一标识一次转发是同协议透传还是跨协议适配;缺少对应能力或无法无损转换时显式报错,不静默丢弃约束。
- 用
- 用量计量与计费(Usage & Billing)
- MaaS Usage & Billing笔记MaaS Usage & Billing通用按量计费通常是「单边事件 → 单一计量项 → 乘以单价」。MaaS 在此基础上增加了五类复杂度: · 双边主体并存:同一笔请求同时对应客户侧收入(Customer Rating)与上游供应侧成本(Provider Rating),两者共享原始用量事实,但协议、价格表、折扣、抵扣与异常规则可以不同。打开:/notes/ai/maas/billing 中的
ModelRequestUsage同时记录api(客户侧)与upstream_api(上游侧)。 - Normalization 层结合
upstream_provider + upstream_api + upstream_model选择对应的 Token 包含口径(Inclusive vs Exclusive)与工具计费规则,确保跨协议转发和流式响应的计量准确。
- MaaS Usage & Billing笔记MaaS Usage & Billing通用按量计费通常是「单边事件 → 单一计量项 → 乘以单价」。MaaS 在此基础上增加了五类复杂度: · 双边主体并存:同一笔请求同时对应客户侧收入(Customer Rating)与上游供应侧成本(Provider Rating),两者共享原始用量事实,但协议、价格表、折扣、抵扣与异常规则可以不同。打开:/notes/ai/maas/billing 中的
参考
- Design Service Interface - Model Service笔记Design Service Interface资源方法使用「动词 + 单数资源名」,例如 、。是列表还是单个结果由动词和返回类型表达,便于搜索和生成。 · 能力方法按语义命名,例如 、,不受资源名规则约束。 · 按调用方要完成的操作命名。上游路由、鉴权和请求编码属于 Adapter;换协议不应该要求调用方重写业务输入。打开:/notes/dev/design/service/model
- MaaS Usage & Billing笔记MaaS Usage & Billing通用按量计费通常是「单边事件 → 单一计量项 → 乘以单价」。MaaS 在此基础上增加了五类复杂度: · 双边主体并存:同一笔请求同时对应客户侧收入(Customer Rating)与上游供应侧成本(Provider Rating),两者共享原始用量事实,但协议、价格表、折扣、抵扣与异常规则可以不同。打开:/notes/ai/maas/billing
- MaaS API Spec笔记MaaS API SpecSession / Thread · Turn · Message · Message Part / Content Part · Chunk Part · | topic | for | · | google | Gemini API、Vertex AI、Google tools / thinking / cache |打开:/notes/ai/maas/spec
- Provider 官方入口:OpenAI笔记OpenAI MaaS APIOpenAI API · https://platform.openai.com/docs/api-reference/chat/create · Responses / OpenResponses · https://www.openresponses.org/打开:/notes/ai/maas/openai、Anthropic笔记Anthropic MaaS APIAnthropic Beta Flags · Anthropic FAQ · Anthropic API · https://platform.claude.com/docs/en/api/overview · SDK · https://github.com/anthropics/anthropic-sdk-typescript打开:/notes/ai/maas/anthropic、Gemini笔记Gemini APIGemini Developer API / Google AI Studio API · generativelanguage.googleapis.com · API key · Google Generative AI API · https://ai.google.dev/api/rest打开:/notes/ai/maas/gemini、Vertex AI笔记Vertex AI MaaS APIVertex AI 使用 Google Cloud project/location、IAM/ADC、quota、VPC-SC 和 regional/global endpoint。 · API host · Regional: · Global:打开:/notes/ai/maas/vertex、Amazon Bedrock笔记Amazon Bedrock MaaS APIInvokeModel / InvokeModelWithResponseStream · 适合单次调用、模型原生 body、大 payload、非消息型/特殊模型 · Converse / ConverseStream · 新的统一消息接口打开:/notes/ai/maas/bedrock、Microsoft Foundry笔记Microsoft Foundry旧名称 Azure AI Foundry、Azure AI Studio、Azure AI Services · https://ai.azure.com/ · , 。 · OpenAI · Base URL: · | Surface | Endpoint shape | 定位 |打开:/notes/ai/maas/foundry
关联信息
反向链接、本文链接的其他页面和外部资料。
站内链接
- 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: