Skip to content

Completions 文本补全 ​

根据提示词生成文本补全。

参考文档: https://platform.openai.com/docs/api-reference/completions/create

请求 ​

POST /v1/completions

认证 ​

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

请求体 (application/json) ​

参数类型必填说明
modelstring是模型 ID
promptstring/array是生成补全的提示词
max_tokensinteger否最大生成 token 数
temperaturenumber否采样温度
top_pnumber否核采样参数
ninteger否生成补全数量
streamboolean否是否流式响应
stopstring/array否停止序列
suffixstring否补全后追加的后缀
echoboolean否是否回显提示词
presence_penaltynumber否存在惩罚
frequency_penaltynumber否频率惩罚
logit_biasobject否token 偏置
userstring否用户标识

请求示例 ​

bash
curl -X POST "https://your-api-base/v1/completions" \
  -H "Authorization: Bearer sk-xxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-3.5-turbo-instruct",
    "prompt": "Say this is a test",
    "max_tokens": 7,
    "temperature": 0
  }'
python
from openai import OpenAI

client = OpenAI(
    api_key="sk-xxxxxx",
    base_url="https://your-api-base/v1"
)

response = client.completions.create(
    model="gpt-3.5-turbo-instruct",
    prompt="Say this is a test",
    max_tokens=7,
    temperature=0
)

print(response.choices[0].text)

响应 ​

200 成功响应 ​

json
{
  "id": "cmpl-abc123",
  "object": "text_completion",
  "created": 1677858242,
  "model": "gpt-3.5-turbo-instruct",
  "choices": [
    {
      "text": "\n\nThis is a test!",
      "index": 0,
      "finish_reason": "length"
    }
  ],
  "usage": {
    "prompt_tokens": 5,
    "completion_tokens": 7,
    "total_tokens": 12
  }
}