Files
taiji-AI-PAD/Docs/前后端调试接口说明/API-ModelGateway服务.md
T
2025-12-31 10:56:00 +00:00

4.9 KiB

Model Gateway (LiteLLM) 服务 API

基础URL: http://localhost:4000

返回 API接口文档


目录

  1. 健康检查
  2. 列出可用模型
  3. Chat Completions
  4. 流式 Chat Completions
  5. API Key 管理

认证

所有 API 请求都需要在请求头中包含 API Key:

Authorization: Bearer <api_key>

1. 健康检查

功能: 检查 LiteLLM 网关健康状态

请求方式: GET /health

请求头:

  • Authorization: Bearer <api_key>

响应字段:

字段 类型 说明
healthy_endpoints array 健康的端点列表
unhealthy_endpoints array 不健康的端点列表
healthy_count int 健康端点数量
unhealthy_count int 不健康端点数量

2. 列出可用模型

功能: 获取所有可用的模型列表

请求方式: GET /v1/models

请求头:

  • Authorization: Bearer <api_key>

响应字段:

字段 类型 说明
data array 模型列表
data[].id string 模型 ID
data[].object string 对象类型
data[].created int 创建时间戳
data[].owned_by string 所有者
object string 响应类型

可用模型:

模型名称 说明
gpt-3.5-turbo GPT-3.5 Turbo
openrouter-gpt-3.5-turbo GPT-3.5 Turbo (OpenRouter)
openrouter-gpt-4o-mini GPT-4o Mini
openrouter-claude-3.5-sonnet Claude 3.5 Sonnet
openrouter-claude-3-opus Claude 3 Opus
test-model 测试用模型

3. Chat Completions

功能: 发送聊天完成请求

请求方式: POST /v1/chat/completions

请求头:

  • Authorization: Bearer <api_key>
  • Content-Type: application/json

请求体参数:

参数 类型 必填 说明
model string 是 模型名称
messages array 是 消息数组
messages[].role string 是 角色: system, user, assistant
messages[].content string 是 消息内容
temperature float 否 采样温度,范围 0-2,默认 0.7
max_tokens int 否 最大生成 token 数
stream boolean 否 是否流式返回,默认 false

响应字段:

字段 类型 说明
id string 响应 ID
object string 对象类型
created int 创建时间戳
model string 使用的模型
choices array 响应选项
choices[].index int 选项索引
choices[].message object 响应消息
choices[].message.role string 角色
choices[].message.content string 内容
choices[].finish_reason string 结束原因
usage object Token 使用情况
usage.prompt_tokens int 输入 token 数
usage.completion_tokens int 输出 token 数
usage.total_tokens int 总 token 数

错误响应:

状态码 说明
401 认证失败
402 余额不足

4. 流式 Chat Completions

功能: 发送流式聊天完成请求

请求方式: POST /v1/chat/completions

请求头:

  • Authorization: Bearer <api_key>
  • Content-Type: application/json

请求体参数: 同 Chat Completions,设置 stream: true

响应格式: Server-Sent Events (SSE)

每个事件格式:

data: {"id":"...","object":"chat.completion.chunk","choices":[{"delta":{"content":"..."}}]}

结束标记:

data: [DONE]

5. API Key 管理

5.1 创建 API Key

功能: 生成新的 API Key

请求方式: POST /key/generate

请求体参数:

参数 类型 必填 说明
models array 否 可用模型列表
max_budget float 否 最大预算
budget_duration string 否 预算周期
metadata object 否 元数据

响应字段:

字段 类型 说明
key string 生成的 API Key
models array 可用模型列表
max_budget float 最大预算
budget_duration string 预算周期

5.2 获取 API Key 信息

功能: 获取当前 API Key 的信息

请求方式: GET /key/info

请求头:

  • Authorization: Bearer <api_key>

响应字段:

字段 类型 说明
key string API Key
models array 可用模型列表
max_budget float 最大预算
budget_duration string 预算周期
spent_budget float 已用预算
remaining_budget float 剩余预算

5.3 删除 API Key

功能: 删除指定的 API Key

请求方式: DELETE /key/delete

请求体参数:

参数 类型 必填 说明
keys array 是 要删除的 API Key 列表

返回 API接口文档