Przejdź do treści
Dokumentacja API
Dokumentacja API

OpenAI Chat Completions

Wyślij żądanie przez operację Chat Completions kompatybilną z OpenAI w RunAPI.

01

Omówienie

Użyj operacji Chat Completions zgodnej z OpenAI w RunAPI z kompatybilnym modelem oraz kształtami żądań i odpowiedzi protokołu.

Szybki start

  1. Utwórz klucz API i ustaw go jako RUNAPI_API_KEY.
  2. Wybierz zgodny model, a następnie wyślij udokumentowane parametry ścieżki i treść JSON.
  3. Odczytaj odpowiedź JSON lub zdarzenia wysyłane przez serwer pokazane dla tej operacji i obsłuż błędy protokołu.

Punkt końcowy

POST /v1/chat/completions
Bazowy URL
https://runapi.ai
Wersja kontraktu
v1
Preferowane uwierzytelnianie
Authorization: Bearer YOUR_API_TOKEN
02

Nośniki uwierzytelniania

RunAPI akceptuje każdy z wymienionych poniżej protokołów. Wygenerowane przykłady używają preferowanego nagłówka danego protokołu.

header - Preferowane
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

Zgodne modele

Otwórz stronę modelu, aby zapoznać się z aktualnymi cenami, limitami żądań i szczegółami dotyczącymi użytku komercyjnego.

Pokaż 60 zgodnych modeli
04

Kontrakt żądania

Treść JSON

Wymieniane są tylko pola mające zarówno publiczną dokumentację protokołu, jak i potwierdzenie obsługi przez RunAPI. Dodatkowe pola treści JSON są przekazywane bez zmian.

Treść JSON9 pola
max_tokens["integer", "null"]
Opcjonalne

Limit tokenów generacji zachowany dla zgodności z obsługiwanymi modelami; obsługa modeli może się różnić.

messagesarray
Wymagane

Wiadomości rozmowy w formacie OpenAI Chat Completions.

messages[].content["string", "array"]
Opcjonalne
messages[].rolestring
Wymagane
modelstring
Wymagane

Identyfikator modelu RunAPI zwrócony przez kompatybilny katalog modeli.

stream["boolean", "null"]
Opcjonalne

Zwróć fragmenty Chat Completions jako zdarzenia wysyłane przez serwer.

Domyślna wartość: false
temperature["number", "null"]
Opcjonalne

Temperatura próbkowania; obsługa przez model może się różnić.

toolsarray
Opcjonalne

Narzędzia, które model może wywoływać.

top_p["number", "null"]
Opcjonalne

Prawdopodobieństwo próbkowania jądra; obsługa przez model może być różna.

Przykład żądania

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

Odpowiedź synchroniczna

HTTP 200

Schemat

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"
}

Przykład

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

Zdarzenie SSE: chunk

text/event-stream

Schemat

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"
}

Przykład

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

Błąd protokołu: invalid_request

HTTP 400

Schemat

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"
}

Przykład

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

Użycie

Schemat

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"
}

Przykład

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

Wygenerowane przykłady kodu

Każdy przykład cURL, JavaScript i Python wysyła zwalidowane żądanie z tej specyfikacji bez konieczności używania SDK właściwego dla protokołu.

Instalacja

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())