OpenAI API

下面是我们支持的官方 API 接口。实际和官方接口open in new window无异。

TIP

本页面中所有 你的api_key 都代表你获取到的 api_key,实际请求的时候请替换成真实 api_key。

鉴权方式

将原 sk-开头的api_key 修改成我们平台专属的 sb-开头 api_key

Authorization: Bearer 你的api_key

聊天

TIP

现在传入gpt-3.5-turbo我们后端会默认替换为gpt-3.5-turbo-0613,因为 0613 速度是 0314 的 6 倍,价格也更实惠。

  • POST 请求地址: https://api.openai-sb.com/v1/chat/completions

例如你要调用gpt-4模型:就将请求 json 的 model 设为gpt-4

目前聊天接口支持的模型有gpt-4gpt-3.5-turbo

不支持gpt-4-32k

如果你不知道你在做什么的,不推荐使用gpt-4-0314gpt-3.5-turbo-0301

  • 请求头示例:
{
  "Authorization": "Bearer 你的api_key",
  "Content-Type": "application/json"
}
  • 请求体示例 JSON 格式:
{
  "model": "gpt-3.5-turbo",
  "messages": [
    {
      "role": "user",
      "content": "树上有9只鸟,猎人开枪打死1只,树上还剩几只鸟?"
    }
  ]
}
  • 代码示例:
const fetch = require("node-fetch");

fetch("https://api.openai-sb.com/v1/chat/completions", {
  method: "POST",
  headers: {
    Authorization: "Bearer 你的api_key",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "gpt-3.5-turbo",
    stream: false,
    messages: [
      {
        role: "user",
        content: "树上有9只鸟,猎人开枪打死1只,树上还剩几只鸟?",
      },
    ],
  }),
});
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.openai-sb.com/v1/chat/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\r\n    \"model\": \"gpt-3.5-turbo\",\r\n    \"stream\": false,\r\n    \"messages\": [\r\n        {\r\n            \"role\": \"user\",\r\n            \"content\": \"你好\"\r\n        }\r\n    ]\r\n}");

$headers = array();
$headers[] = 'Authorization: Bearer 你的api_key';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

文本补全

  • 请求地址: POST https://api.openai-sb.com/v1/completions

  • 代码示例:

const fetch = require("node-fetch");

fetch("https://api.openai-sb.com/v1/completions", {
  method: "POST",
  headers: {
    Authorization: "Bearer 你的api_key",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "text-davinci-003",
    prompt: "Say this is a test",
    max_tokens: 7,
    temperature: 0,
    top_p: 1,
    n: 1,
    stream: false,
    logprobs: null,
    stop: "\n",
  }),
});
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.openai-sb.com/v1/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\r\n    \"model\": \"text-davinci-003\",\r\n    \"prompt\": \"Say this is a test\",\r\n    \"max_tokens\": 7,\r\n    \"temperature\": 0,\r\n    \"top_p\": 1,\r\n    \"n\": 1,\r\n    \"stream\": false,\r\n    \"logprobs\": null,\r\n    \"stop\": \"n\"\r\n}");

$headers = array();
$headers[] = 'Authorization: Bearer 你的api_key';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

审核

  • 请求地址: POST https://api.openai-sb.com/v1/moderations

  • 代码示例:

const fetch = require("node-fetch");

fetch("https://api.openai-sb.com/v1/moderations", {
  method: "POST",
  headers: {
    Authorization: "Bearer 你的api_key",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ input: "I want to kill them." }),
});
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.openai-sb.com/v1/moderations');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\r\n    \"input\": \"I want to kill them.\"\r\n}");

$headers = array();
$headers[] = 'Authorization: Bearer 你的api_key';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

嵌入

  • 请求地址: POST https://api.openai-sb.com/v1/embeddings

  • 代码示例:

const fetch = require("node-fetch");

fetch("https://api.openai-sb.com/v1/embeddings", {
  method: "POST",
  headers: {
    Authorization: "Bearer 你的api_key",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    input: "The food was delicious and the waiter...",
    model: "text-embedding-ada-002",
  }),
});

更多接口正在开发中...