跳到主要內容
API 參考
API 參考

OpenAI Chat Completions

透過 RunAPI 相容 OpenAI 的 Chat Completions 操作傳送請求。

01

概覽

使用 RunAPI 的 OpenAI 相容「Chat Completions」操作,搭配相容的模型及該協定的請求與回應格式。

快速入門

  1. 建立 API 金鑰並將其設定為 RUNAPI_API_KEY。
  2. 選擇相容的模型,然後傳送文件中指定的路徑參數與 JSON 本文。
  3. 讀取此操作顯示的 JSON 回應或伺服器傳送事件,並處理協定錯誤。

端點

POST /v1/chat/completions
基礎 URL
https://runapi.ai
合約版本
v1
建議的身分驗證方式
Authorization: Bearer YOUR_API_TOKEN
02

身分驗證載體

RunAPI 接受以下列出的每種傳輸協定。產生的範例使用該協定偏好的標頭。

header - 建議
Authorization: Bearer YOUR_API_TOKEN
header
x-api-key: YOUR_API_TOKEN
header
x-goog-api-key: YOUR_API_TOKEN
query
?key=YOUR_API_TOKEN
03

相容模型

開啟模型頁面以查看目前的定價、速率限制及商業用途詳情。

顯示 60 個相容模型
04

請求合約

JSON 請求內文

僅列出同時具備公開協定與 RunAPI 支援依據的欄位。其他 JSON 本文欄位將直接傳遞。

JSON 主體9 個欄位
max_tokens["integer", "null"]
選填

為支援模型保留的相容性生成 token 上限;模型支援情況可能有所不同。

messagesarray
必填

OpenAI Chat Completions 格式的對話訊息。

messages[].content["string", "array"]
選填
messages[].rolestring
必填
modelstring
必填

由相容模型目錄回傳的 RunAPI 模型識別碼。

stream["boolean", "null"]
選填

以伺服器傳送事件的形式回傳 Chat Completions 資料區塊。

預設值: false
temperature["number", "null"]
選填

取樣溫度;模型支援程度可能有所不同。

toolsarray
選填

模型可呼叫的工具。

top_p["number", "null"]
選填

Nucleus 取樣機率;模型支援程度可能有所不同。

請求範例

JSON
{
  "messages": [
    {
      "content": "Summarize one practical improvement for an API deployment.",
      "role": "user"
    }
  ],
  "model": "gpt-5.4",
  "stream": false
}
05

同步回應

HTTP 200

結構描述

JSON
{
  "additionalProperties": true,
  "properties": {
    "choices": {
      "type": "array"
    },
    "created": {
      "type": "integer"
    },
    "id": {
      "type": "string"
    },
    "model": {
      "type": "string"
    },
    "object": {
      "const": "chat.completion",
      "type": "string"
    },
    "usage": {
      "additionalProperties": true,
      "properties": {
        "completion_tokens": {
          "type": "integer"
        },
        "prompt_tokens": {
          "type": "integer"
        },
        "total_tokens": {
          "type": "integer"
        }
      },
      "required": [
        "prompt_tokens",
        "completion_tokens",
        "total_tokens"
      ],
      "type": "object"
    }
  },
  "required": [
    "id",
    "object",
    "created",
    "model",
    "choices"
  ],
  "type": "object"
}

範例

JSON
{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "Hello! How can I help today?",
        "role": "assistant"
      }
    }
  ],
  "created": 1785196800,
  "id": "chatcmpl_example",
  "model": "gpt-5.4",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 8,
    "prompt_tokens": 12,
    "total_tokens": 20
  }
}
06

SSE 事件:chunk

text/event-stream

結構描述

JSON
{
  "additionalProperties": true,
  "properties": {
    "choices": {
      "type": "array"
    },
    "created": {
      "type": "integer"
    },
    "id": {
      "type": "string"
    },
    "model": {
      "type": "string"
    },
    "object": {
      "const": "chat.completion.chunk",
      "type": "string"
    },
    "usage": {
      "oneOf": [
        {
          "additionalProperties": true,
          "properties": {
            "completion_tokens": {
              "type": "integer"
            },
            "prompt_tokens": {
              "type": "integer"
            },
            "total_tokens": {
              "type": "integer"
            }
          },
          "required": [
            "prompt_tokens",
            "completion_tokens",
            "total_tokens"
          ],
          "type": "object"
        },
        {
          "type": "null"
        }
      ]
    }
  },
  "required": [
    "id",
    "object",
    "choices"
  ],
  "type": "object"
}

範例

JSON
{
  "choices": [
    {
      "delta": {
        "content": "Hello"
      },
      "finish_reason": null,
      "index": 0
    }
  ],
  "id": "chatcmpl_example",
  "object": "chat.completion.chunk",
  "usage": null
}
07

協定錯誤:invalid_request

HTTP 400

結構描述

JSON
{
  "additionalProperties": true,
  "properties": {
    "error": {
      "additionalProperties": true,
      "properties": {
        "code": {
          "type": [
            "string",
            "null"
          ]
        },
        "message": {
          "type": "string"
        },
        "param": {
          "type": [
            "string",
            "null"
          ]
        },
        "type": {
          "type": "string"
        }
      },
      "required": [
        "message",
        "type"
      ],
      "type": "object"
    }
  },
  "required": [
    "error"
  ],
  "type": "object"
}

範例

JSON
{
  "error": {
    "code": null,
    "message": "messages is required",
    "param": "messages",
    "type": "invalid_request_error"
  }
}
08

用量

結構描述

JSON
{
  "additionalProperties": true,
  "properties": {
    "completion_tokens": {
      "type": "integer"
    },
    "prompt_tokens": {
      "type": "integer"
    },
    "total_tokens": {
      "type": "integer"
    }
  },
  "required": [
    "prompt_tokens",
    "completion_tokens",
    "total_tokens"
  ],
  "type": "object"
}

範例

JSON
{
  "completion_tokens": 8,
  "prompt_tokens": 12,
  "total_tokens": 20
}
09

產生的程式碼範例

每個 cURL、JavaScript 和 Python 範例均會從此契約傳送經驗證的請求,無需特定協定的 SDK。

安裝

Shell
pip install requests
PYTHON
import requests

response = requests.post(
    "https://runapi.ai/v1/chat/completions",
    headers={"Authorization": "Bearer YOUR_API_TOKEN", "Content-Type": "application/json"},
    json={"model": "gpt-5.4", "messages": [{"role": "user", "content": "Summarize one practical improvement for an API deployment."}], "stream": False}
)
response.raise_for_status()
print(response.json())