聊天补全(Completions)
简洁描述:文本补全接口,兼容传统 completions 格式(如
gpt-3.5-turbo-instruct系列)。本页包含请求参数说明、示例与返回示例。
概览
- 请求方法:
POST - 路径:
/v1/completions - 内容类型:
application/json
认证方式
- 使用标准 Header 认证:
Authorization: Bearer sk-xxx。
请求参数说明
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| model | string | 是 | 模型名称,例如 gpt-3.5-turbo-instruct |
| prompt / messages | string 或 array | 是 | 输入提示(本端点常用 prompt 字段;若使用 messages,请参照聊天接口格式) |
| temperature | number | 否 | 采样温度,0-2,越大越随机 |
| top_p | number | 否 | 核采样阈值 |
| max_tokens | number | 否 | 生成最大 tokens |
| n | integer | 否 | 返回候选完成数量 |
| stream | boolean | 否 | 是否流式返回 |
| presence_penalty | number | 否 | 新主题惩罚 (-2.0~2.0) |
| frequency_penalty | number | 否 | 重复惩罚 (-2.0~2.0) |
| logprobs | integer | 否 | 返回每个 token 的概率信息 |
| user | string | 否 | 终端用户标识,用于滥用检测 |
示例
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 }
}