跳转至

文本生成

POST /api/v5/chat/completions

该接口用于实现文本生成功能,是基于 OpenAI 兼容的对话补全(Chat Completions)协议的接口。通过提交模型名称与对话消息,支持流式(SSE)与非流式两种返回方式,并可通过温度、采样参数等配置生成结果,返回生成内容及 Token 用量统计。

访问令牌支持说明

  • AtomGit 用户访问令牌(AtomGit user access tokens)

是否允许匿名访问

此接口允许匿名访问

请求参数

参数名 位置 类型 必填 说明
Authorization header string 否 用户个人令牌;允许匿名访问,可不携带;携带时以 Bearer <token> 形式传入

Request Body:

请求体为 OpenAI 兼容的 Chat Completions 请求 JSON,字段如下:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "model": {
      "description": "模型名称",
      "type": "string"
    },
    "messages": {
      "description": "对话消息列表",
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "stream": {
      "description": "是否流式返回(SSE);为 true 时响应为 text/event-stream,否则为 application/json",
      "type": "boolean",
      "default": false
    },
    "temperature": {
      "description": "温度",
      "type": "number",
      "default": 0.7
    },
    "top_p": {
      "description": "top_p 采样参数",
      "type": "number",
      "default": 0.7
    },
    "top_k": {
      "description": "top_k 采样参数",
      "type": "integer",
      "default": 50
    },
    "frequency_penalty": {
      "description": "频率惩罚",
      "type": "number",
      "default": 1.0
    },
    "maxTokens": {
      "description": "最大 Token 数",
      "type": "integer"
    }
  },
  "required": ["model", "messages"]
}

响应参数

字段 类型 必返 可为空 说明
trace_id string 是 否 请求的唯一标识
choices array 是 否 生成结果列表
ext string 是 是 扩展字段,通常为 null
usage object 是 否 Token 用量统计

choices 元素对象

字段 类型 必返 可为空 说明
choices[].index integer 是 否 结果序号
choices[].finish_reason string 否 是 结束原因,可为 null
choices[].delta object 是 否 增量内容

choices[].delta 对象

字段 类型 必返 可为空 说明
choices[].delta.content string 否 是 生成的内容片段

usage 对象

字段 类型 必返 可为空 说明
usage.prompt_tokens integer 是 否 提示词消耗的 token 数量
usage.completion_tokens integer 是 否 补全内容消耗的 token 数量
usage.total_tokens integer 是 否 总 token 数量
usage.think_tokens integer 否 是 思考过程消耗的 token 数量

失败响应

该接口无业务失败响应,失败时以 HTTP 状态码体现(见「HTTP 响应状态码」)。

HTTP 响应状态码

状态码 说明
200 成功(OK)
401 需要认证(Requires authentication)
403 禁止访问(Forbidden)
404 请求路径不存在
405 请求方法不支持
429 请求过于频繁,触发限流
500 系统内部错误

请求示例

参数说明

示例为非流式调用。<model> 替换为模型名称,<messages> 替换为对话消息列表。该接口允许匿名访问,可不携带令牌;也可携带 Authorization: Bearer <token> 进行身份标识(<token> 替换为你的 AtomGit 用户访问令牌)。

cURL

curl -X POST 'https://api.atomgit.com/api/v5/chat/completions' \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json' \
  -d '{
    "model": "<model>",
    "messages": ["<message>"],
    "temperature": 0.7,
    "stream": false
  }'

JavaScript

// 原生 fetch (Node.js 18+ / 浏览器)
const response = await fetch("https://api.atomgit.com/api/v5/chat/completions", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "Accept": "application/json",
  },
  body: JSON.stringify({
    model: "<model>",
    messages: ["<message>"],
    temperature: 0.7,
    stream: false,
  }),
});

if (!response.ok) {
  throw new Error(`请求失败: ${response.status}`);
}

const data = await response.json();
console.log(data);

Python

import requests

resp = requests.post(
    "https://api.atomgit.com/api/v5/chat/completions",
    headers={
        "Content-Type": "application/json",
        "Accept": "application/json",
    },
    json={
        "model": "<model>",
        "messages": ["<message>"],
        "temperature": 0.7,
        "stream": False,
    },
    timeout=10,
)
resp.raise_for_status()

print(resp.json())

响应示例

状态码: 200

{
  "trace_id": "045560649c7c483e80eb196e2bc95d38",
  "choices": [
    {
      "index": 0,
      "finish_reason": null,
      "delta": {
        "content": "这是流式返回的内容片段"
      }
    }
  ],
  "ext": null,
  "usage": {
    "prompt_tokens": 11,
    "completion_tokens": 146,
    "total_tokens": 157,
    "think_tokens": null
  }
}

错误码

错误码 说明
401 未认证或凭证无效(缺少/无效的访问凭证)
400 参数错误或请求无法解析
404 请求路径不存在
405 请求方法不支持
429 请求过于频繁,触发限流
500 系统内部错误

本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 gitcode-dev/atomgit-api-doc