Appearance
Completions 文本补全
根据提示词生成文本补全。
参考文档: https://platform.openai.com/docs/api-reference/completions/create
请求
POST /v1/completions
认证
使用 Bearer Token 认证,格式: Authorization: Bearer sk-xxxxxx
请求体 (application/json)
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| model | string | 是 | 模型 ID |
| prompt | string/array | 是 | 生成补全的提示词 |
| max_tokens | integer | 否 | 最大生成 token 数 |
| temperature | number | 否 | 采样温度 |
| top_p | number | 否 | 核采样参数 |
| n | integer | 否 | 生成补全数量 |
| stream | boolean | 否 | 是否流式响应 |
| stop | string/array | 否 | 停止序列 |
| suffix | string | 否 | 补全后追加的后缀 |
| echo | boolean | 否 | 是否回显提示词 |
| presence_penalty | number | 否 | 存在惩罚 |
| frequency_penalty | number | 否 | 频率惩罚 |
| logit_bias | object | 否 | token 偏置 |
| user | string | 否 | 用户标识 |
请求示例
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
}
}