Skip to content

聊天补全(Completions)

简洁描述:文本补全接口,兼容传统 completions 格式(如 gpt-3.5-turbo-instruct 系列)。本页包含请求参数说明、示例与返回示例。


概览

  • 请求方法:POST
  • 路径:/v1/completions
  • 内容类型:application/json

认证方式

  • 使用标准 Header 认证:Authorization: Bearer sk-xxx

请求参数说明

参数类型必填描述
modelstring模型名称,例如 gpt-3.5-turbo-instruct
prompt / messagesstring 或 array输入提示(本端点常用 prompt 字段;若使用 messages,请参照聊天接口格式)
temperaturenumber采样温度,0-2,越大越随机
top_pnumber核采样阈值
max_tokensnumber生成最大 tokens
ninteger返回候选完成数量
streamboolean是否流式返回
presence_penaltynumber新主题惩罚 (-2.0~2.0)
frequency_penaltynumber重复惩罚 (-2.0~2.0)
logprobsinteger返回每个 token 的概率信息
userstring终端用户标识,用于滥用检测

示例

curl 示例

bash
curl -X POST "https://api.gpt.ge/v1/completions" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-xxxx" \
  -d '{
    "model": "gpt-3.5-turbo-instruct",
    "prompt": "天气非常好",
    "temperature": 0.7,
    "max_tokens": 100,
    "top_p": 1,
    "frequency_penalty": 0,
    "presence_penalty": 0,
    "logprobs": 1
  }'

JavaScript (fetch) 示例

javascript
fetch('https://api.gpt.ge/v1/completions', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer sk-xxxx' },
  body: JSON.stringify({ model: 'gpt-3.5-turbo-instruct', prompt: '天气非常好', temperature: 0.7, max_tokens: 100, logprobs: 1 })
}).then(r => r.json()).then(console.log)

Python 示例(requests)

python
import requests

payload = {
  'model': 'gpt-3.5-turbo-instruct',
  'prompt': '天气非常好',
  'temperature': 0.7,
  'max_tokens': 100,
  'logprobs': 1
}

resp = requests.post('https://api.gpt.ge/v1/completions', headers={'Content-Type':'application/json','Authorization':'Bearer sk-xxxx'}, json=payload)
print(resp.json())

返回示例(200)

json
{
  "id": "cmpl-A1tJLfyQgj1j2GpvhA6QcMCZKwRtw",
  "object": "text_completion",
  "created": 1725014307,
  "model": "gpt-3.5-turbo-instruct",
  "choices": [
    {
      "text": ",适合出门旅行\n\nYes, the weather is very good today, perfect for a trip.",
      "index": 0,
      "logprobs": {
        "tokens": [",", "Yes", ",", " the", " weather", " is", " very", " good", " today", ",", " perfect", " for", " a", " trip", "."],
        "token_logprobs": [-0.80, -3.95, -0.01, -0.00, -0.09, -0.00, -0.32, -1.13, -0.98, -0.15, -0.25, -0.00, -1.63, -0.29, -0.39]
      },
      "finish_reason": "stop"
    }
  ],
  "usage": { "prompt_tokens": 6, "completion_tokens": 25, "total_tokens": 31 }
}