POST /v1/completions

补全(Completions)

传统文本补全接口,兼容 OpenAI Completions 格式

接口说明

补全接口用于对一段文本进行续写或补全。此接口为传统接口,新应用建议优先使用 Chat 接口。

实际可用模型名称以控制台配置为准,部分模型可能只支持聊天接口。

认证

使用 Bearer Token 认证,格式:Authorization: Bearer sk-xxxxxx

请求体参数

参数 类型 必需 说明
model string 是 模型名称,如 gpt-3.5-turbo-instruct
prompt string | array 是 要补全的文本提示
max_tokens integer 否 最大生成 token 数,默认 16
temperature number 否 采样温度 (0-2),默认 1
top_p number 否 核采样参数 (0-1),默认 1
n integer 否 生成的补全数量,默认 1
stream boolean 否 是否流式响应,默认 false
stop string | array 否 停止生成的标记序列,最多 4 个
presence_penalty number 否 存在惩罚 (-2.0 到 2.0),默认 0
frequency_penalty number 否 频率惩罚 (-2.0 到 2.0),默认 0

请求示例

cURL
curl -X POST "https://your-base-url/v1/completions" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-xxxxxx" \
  -d '{
    "model": "gpt-3.5-turbo-instruct",
    "prompt": "人工智能是什么?请简单介绍一下。",
    "max_tokens": 128,
    "temperature": 0.7
  }'

响应示例

200 OK
{
  "id": "cmpl-abc123",
  "object": "text_completion",
  "created": 1700000000,
  "model": "gpt-3.5-turbo-instruct",
  "choices": [
    {
      "text": "人工智能(AI)是计算机科学的一个分支,旨在创建能够模拟人类智能的系统...",
      "index": 0,
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 12,
    "completion_tokens": 45,
    "total_tokens": 57
  }
}
在线测试 直接在此页面测试 API
Authorization
Body