创建文本补全请求,适用于单轮文本生成任务。
- 接口地址:
POST /v1/completions - 认证方式: Bearer Token
- Content-Type: application/json
| 参数 | 类型 | 说明 |
|---|
| model | string | 模型名称 |
| prompt | string/array | 提示文本 |
| 参数 | 类型 | 默认值 | 说明 |
|---|
| suffix | string | null | 文本后缀 |
| max_tokens | integer | 16 | 最大生成 token 数 |
| temperature | number | 1.0 | 采样温度,范围 0-2 |
| top_p | number | 1.0 | 核采样参数 |
| n | integer | 1 | 生成数量 |
| stream | boolean | false | 是否流式返回 |
| logprobs | integer | null | 返回的 log 概率数量 |
| echo | boolean | false | 是否返回 prompt |
| stop | string/array | null | 停止生成的标记 |
| presence_penalty | number | 0 | 存在惩罚 |
| frequency_penalty | number | 0 | 频率惩罚 |
| best_of | integer | 1 | 生成候选项数量 |
| logit_bias | object | null | token 偏好设置 |
| user | string | null | 用户标识 |
curl https://aigc.aochengcloud.com/v1/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
-d '{
"model": "gpt-3.5-turbo-instruct",
"prompt": "写一首关于春天的诗:",
"max_tokens": 100
}'
curl https://aigc.aochengcloud.com/v1/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
-d '{
"model": "gpt-3.5-turbo-instruct",
"prompt": "将以下英文翻译成中文:\n\nHello, how are you?",
"temperature": 0.3,
"max_tokens": 50,
"stop": ["\n\n"]
}'
curl https://aigc.aochengcloud.com/v1/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
-d '{
"model": "gpt-3.5-turbo-instruct",
"prompt": "Once upon a time",
"stream": true,
"max_tokens": 200
}'
{
"id": "cmpl-8VYRlGZS4uPpnS3FyW32vM",
"object": "text_completion",
"created": 1704067200,
"model": "gpt-3.5-turbo-instruct",
"choices": [
{
"text": "\n\n春风拂面暖阳斜,\n花开满园香万家。",
"index": 0,
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 20,
"total_tokens": 30
}
}
data: {"id":"cmpl-8VYRlGZS4uPpnS3FyW32vM","object":"text_completion","created":1704067200,"model":"gpt-3.5-turbo-instruct","choices":[{"text":"\n\n","index":0,"logprobs":null,"finish_reason":null}]}
data: {"id":"cmpl-8VYRlGZS4uPpnS3FyW32vM","object":"text_completion","created":1704067200,"model":"gpt-3.5-turbo-instruct","choices":[{"text":"春","index":0,"logprobs":null,"finish_reason":null}]}
data: {"id":"cmpl-8VYRlGZS4uPpnS3FyW32vM","object":"text_completion","created":1704067200,"model":"gpt-3.5-turbo-instruct","choices":[{"text":"风","index":0,"logprobs":null,"finish_reason":null}]}
data: [DONE]
| 字段 | 类型 | 说明 |
|---|
| id | string | 请求的唯一标识 |
| object | string | 对象类型 |
| created | integer | 创建时间戳 |
| model | string | 使用的模型 |
| choices | array | 生成的文本列表 |
| usage | object | token 用量统计 |
| 字段 | 类型 | 说明 |
|---|
| text | string | 生成的文本 |
| index | integer | 文本索引 |
| logprobs | object | log 概率 |
| finish_reason | string | 结束原因 |
import openai
client = openai.OpenAI(
api_key="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
base_url="https://aigc.aochengcloud.com/v1"
)
response = client.completions.create(
model="gpt-3.5-turbo-instruct",
prompt="写一首关于春天的诗:",
max_tokens=100
)
print(response.choices[0].text)
stream = client.completions.create(
model="gpt-3.5-turbo-instruct",
prompt="Once upon a time",
stream=True,
max_tokens=200
)
for chunk in stream:
print(chunk.choices[0].text, end="")
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
baseURL: 'https://aigc.aochengcloud.com/v1'
});
const response = await openai.completions.create({
model: 'gpt-3.5-turbo-instruct',
prompt: '写一首关于春天的诗:',
max_tokens: 100
});
console.log(response.choices[0].text);
response = client.completions.create(
model="gpt-3.5-turbo-instruct",
prompt="人工智能的发展趋势是",
max_tokens=200
)
response = client.completions.create(
model="gpt-3.5-turbo-instruct",
prompt="将以下英文翻译成中文:\n\nHello, how are you?",
temperature=0.3,
max_tokens=50
)
response = client.completions.create(
model="gpt-3.5-turbo-instruct",
prompt="用Python写一个快速排序函数:",
max_tokens=300
)
response = client.completions.create(
model="gpt-3.5-turbo-instruct",
prompt="请将以下文本总结为一句话:\n\n" + long_text,
max_tokens=100
)
控制输出的随机性:
- 0: 确定性输出,适合事实性任务
- 0.5: 平衡创造性和准确性
- 1.0: 适中
- 2.0: 高创造性,可能不准确
控制生成文本的长度:
- 短文本:50-100
- 中等长度:200-500
- 长文本:1000-2000
设置停止生成的标记:
{
"stop": ["\n\n", "END"]
}
| 错误码 | 说明 | 处理方式 |
|---|
| 400 | 请求参数错误 | 检查请求参数 |
| 401 | 认证失败 | 检查 API Key |
| 429 | 请求频率超限 | 降低请求频率 |
| 500 | 服务器错误 | 稍后重试 |