Completions(旧版)
curl --request POST \
--url https://openp.ai/v1/completionsimport requests
url = "https://openp.ai/v1/completions"
response = requests.post(url)
print(response.text)const options = {method: 'POST'};
fetch('https://openp.ai/v1/completions', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://openp.ai/v1/completions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://openp.ai/v1/completions"
req, _ := http.NewRequest("POST", url, nil)
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://openp.ai/v1/completions")
.asString();require 'uri'
require 'net/http'
url = URI("https://openp.ai/v1/completions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
response = http.request(request)
puts response.read_bodyOpenAI 兼容
Completions(旧版)
POST /v1/completions —— 传统文本补全接口
POST
/
v1
/
completions
Completions(旧版)
curl --request POST \
--url https://openp.ai/v1/completionsimport requests
url = "https://openp.ai/v1/completions"
response = requests.post(url)
print(response.text)const options = {method: 'POST'};
fetch('https://openp.ai/v1/completions', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://openp.ai/v1/completions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://openp.ai/v1/completions"
req, _ := http.NewRequest("POST", url, nil)
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://openp.ai/v1/completions")
.asString();require 'uri'
require 'net/http'
url = URI("https://openp.ai/v1/completions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
response = http.request(request)
puts response.read_body传统的文本补全接口,主要用于历史兼容。
新项目请使用 Chat Completions。
请求
curl https://openp.ai/v1/completions \
-H "Authorization: Bearer $OPENPAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo-instruct",
"prompt": "Once upon a time",
"max_tokens": 64,
"temperature": 0.7
}'
主要参数
| 字段 | 类型 | 说明 |
|---|---|---|
model | string | 模型 ID(gpt-3.5-turbo-instruct 等少数模型支持) |
prompt | string | array | 输入 prompt |
max_tokens | integer | 最大输出 |
temperature / top_p | number | 采样 |
n | integer | 候选数量 |
stream | boolean | SSE 流 |
stop | string | array | 停止序列 |
presence_penalty / frequency_penalty | number | 惩罚 |
echo | boolean | 在输出中包含 prompt |
best_of | integer | 服务端生成多个后挑最优 |
logprobs | integer | 返回 logprobs |
响应
{
"id": "cmpl-...",
"object": "text_completion",
"created": 1715750400,
"model": "gpt-3.5-turbo-instruct",
"choices": [
{
"text": ", there was a small castle...",
"index": 0,
"logprobs": null,
"finish_reason": "length"
}
],
"usage": {"prompt_tokens": 4, "completion_tokens": 64, "total_tokens": 68}
}
与 Chat 的迁移
把prompt 改成 messages 即可:
- "prompt": "Once upon a time"
+ "messages": [{"role": "user", "content": "Continue: Once upon a time"}]
gpt-5.5 等。⌘I