通用对话接口
curl --request POST \
--url https://api.gregapi.com/v1/chat/completions \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "gpt-4o-mini",
"messages": [
{
"role": "system",
"content": "你是 GregAPI 的产品助理。"
},
{
"role": "user",
"content": "用三句话介绍 GregAPI。"
}
],
"temperature": 0.7
}
'import requests
url = "https://api.gregapi.com/v1/chat/completions"
payload = {
"model": "gpt-4o-mini",
"messages": [
{
"role": "system",
"content": "你是 GregAPI 的产品助理。"
},
{
"role": "user",
"content": "用三句话介绍 GregAPI。"
}
],
"temperature": 0.7
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: 'gpt-4o-mini',
messages: [
{role: 'system', content: '你是 GregAPI 的产品助理。'},
{role: 'user', content: '用三句话介绍 GregAPI。'}
],
temperature: 0.7
})
};
fetch('https://api.gregapi.com/v1/chat/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://api.gregapi.com/v1/chat/completions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => 'gpt-4o-mini',
'messages' => [
[
'role' => 'system',
'content' => '你是 GregAPI 的产品助理。'
],
[
'role' => 'user',
'content' => '用三句话介绍 GregAPI。'
]
],
'temperature' => 0.7
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.gregapi.com/v1/chat/completions"
payload := strings.NewReader("{\n \"model\": \"gpt-4o-mini\",\n \"messages\": [\n {\n \"role\": \"system\",\n \"content\": \"你是 GregAPI 的产品助理。\"\n },\n {\n \"role\": \"user\",\n \"content\": \"用三句话介绍 GregAPI。\"\n }\n ],\n \"temperature\": 0.7\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.gregapi.com/v1/chat/completions")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"gpt-4o-mini\",\n \"messages\": [\n {\n \"role\": \"system\",\n \"content\": \"你是 GregAPI 的产品助理。\"\n },\n {\n \"role\": \"user\",\n \"content\": \"用三句话介绍 GregAPI。\"\n }\n ],\n \"temperature\": 0.7\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.gregapi.com/v1/chat/completions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"gpt-4o-mini\",\n \"messages\": [\n {\n \"role\": \"system\",\n \"content\": \"你是 GregAPI 的产品助理。\"\n },\n {\n \"role\": \"user\",\n \"content\": \"用三句话介绍 GregAPI。\"\n }\n ],\n \"temperature\": 0.7\n}"
response = http.request(request)
puts response.read_body大语言模型(LLM)
通用对话接口
通过 GregAPI 兼容 OpenAI 的对话补全与工具调用接口。
POST
/
v1
/
chat
/
completions
通用对话接口
curl --request POST \
--url https://api.gregapi.com/v1/chat/completions \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "gpt-4o-mini",
"messages": [
{
"role": "system",
"content": "你是 GregAPI 的产品助理。"
},
{
"role": "user",
"content": "用三句话介绍 GregAPI。"
}
],
"temperature": 0.7
}
'import requests
url = "https://api.gregapi.com/v1/chat/completions"
payload = {
"model": "gpt-4o-mini",
"messages": [
{
"role": "system",
"content": "你是 GregAPI 的产品助理。"
},
{
"role": "user",
"content": "用三句话介绍 GregAPI。"
}
],
"temperature": 0.7
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: 'gpt-4o-mini',
messages: [
{role: 'system', content: '你是 GregAPI 的产品助理。'},
{role: 'user', content: '用三句话介绍 GregAPI。'}
],
temperature: 0.7
})
};
fetch('https://api.gregapi.com/v1/chat/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://api.gregapi.com/v1/chat/completions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => 'gpt-4o-mini',
'messages' => [
[
'role' => 'system',
'content' => '你是 GregAPI 的产品助理。'
],
[
'role' => 'user',
'content' => '用三句话介绍 GregAPI。'
]
],
'temperature' => 0.7
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.gregapi.com/v1/chat/completions"
payload := strings.NewReader("{\n \"model\": \"gpt-4o-mini\",\n \"messages\": [\n {\n \"role\": \"system\",\n \"content\": \"你是 GregAPI 的产品助理。\"\n },\n {\n \"role\": \"user\",\n \"content\": \"用三句话介绍 GregAPI。\"\n }\n ],\n \"temperature\": 0.7\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.gregapi.com/v1/chat/completions")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"gpt-4o-mini\",\n \"messages\": [\n {\n \"role\": \"system\",\n \"content\": \"你是 GregAPI 的产品助理。\"\n },\n {\n \"role\": \"user\",\n \"content\": \"用三句话介绍 GregAPI。\"\n }\n ],\n \"temperature\": 0.7\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.gregapi.com/v1/chat/completions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"gpt-4o-mini\",\n \"messages\": [\n {\n \"role\": \"system\",\n \"content\": \"你是 GregAPI 的产品助理。\"\n },\n {\n \"role\": \"user\",\n \"content\": \"用三句话介绍 GregAPI。\"\n }\n ],\n \"temperature\": 0.7\n}"
response = http.request(request)
puts response.read_bodyPOST /v1/chat/completions 是最常用的对话生成接口,支持流式输出、函数调用(tools/functions)以及 JSON mode。
请求示例
curl -X POST "https://api.gregapi.com/v1/chat/completions" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o-mini",
"messages": [
{"role": "system", "content": "你是 GregAPI 的产品助理。"},
{"role": "user", "content": "用三句话介绍 GregAPI。"}
],
"temperature": 0.7
}'
常见用法
- 流式输出:在请求体中设置
stream: true。cURL 请添加-N选项,Python 端可组合requests.post(..., stream=True)逐行读取。 - 函数调用:通过
tools与tool_choice描述可调用的函数,后续在响应中解析tool_calls并执行业务逻辑。 - JSON 约束:配合
response_format设置为{"type": "json_schema"},可让模型严格返回结构化数据。
GregAPI 扩展字段
以下字段是 GregAPI 在标准 OpenAI 响应体基础上的扩展,用于暴露上游模型的计费明细。usage.prompt_tokens_details
| 字段 | 含义 | 适用模型 |
|---|---|---|
cached_tokens | 命中缓存的输入 token 数 | 所有支持缓存的模型 |
cached_write_tokens | 缓存写入总量(= 5m + 1h) | Claude |
cached_write_5m_tokens | 写入 5 分钟有效期缓存的 token 数 | Claude |
cached_write_1h_tokens | 写入 1 小时有效期缓存的 token 数 | Claude |
prompt_tokens 为输入 token 总量(含缓存命中与缓存写入)。缓存写入相关字段为 omitempty,GPT 等无缓存写入的模型不会输出。
{
"usage": {
"prompt_tokens": 58518,
"prompt_tokens_details": {
"cached_tokens": 11944,
"cached_write_tokens": 46109,
"cached_write_5m_tokens": 29762,
"cached_write_1h_tokens": 16347
}
}
}
授权
Bearer authentication header of the form Bearer <token>, where <token> is your auth token.
请求体
application/json
响应
200
成功返回对话补全结果