Zum Inhalt springen
API-Referenz
API-Referenz

OpenAI Chat Completions

Eine Anfrage über RunAPIs OpenAI-kompatible Chat-Completions-Operation senden.

01

Übersicht

Verwende RunAPI's OpenAI-kompatible Chat Completions-Operation mit einem kompatiblen Modell sowie den Anfrage- und Antwortstrukturen des Protokolls.

Schnellstart

  1. Erstellen Sie einen API-Schlüssel und setzen Sie ihn als RUNAPI_API_KEY.
  2. Wählen Sie ein kompatibles Modell, senden Sie dann die dokumentierten Pfadparameter und den JSON-Body.
  3. Lesen Sie die für diesen Vorgang angezeigte JSON-Antwort oder die server-sent events und behandeln Sie Protokollfehler.

Endpunkt

POST /v1/chat/completions
Basis-URL
https://runapi.ai
Vertragsversion
v1
Bevorzugte Authentifizierung
Authorization: Bearer YOUR_API_TOKEN
02

Authentifizierungsträger

RunAPI akzeptiert jeden der unten aufgeführten Träger. Generierte Beispiele verwenden den bevorzugten Header des Protokolls.

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

Kompatible Modelle

Öffnen Sie eine Modellseite für aktuelle Preise, Ratenlimits und Details zur kommerziellen Nutzung.

60 kompatible Modelle anzeigen
04

Anfrage-Vertrag

JSON-Body

Es werden nur Felder aufgeführt, für die sowohl öffentliche Protokoll- als auch RunAPI-Unterstützung belegt ist. Weitere JSON-Body-Felder werden durchgereicht.

JSON-Body9 Felder
max_tokens["integer", "null"]
Optional

Kompatibles Generierungstoken-Limit für unterstützte Modelle beibehalten; die Modellunterstützung kann variieren.

messagesarray
Erforderlich

Gesprächsnachrichten im OpenAI Chat Completions-Format.

messages[].content["string", "array"]
Optional
messages[].rolestring
Erforderlich
modelstring
Erforderlich

RunAPI-Modellbezeichner, der vom kompatiblen Modellkatalog zurückgegeben wird.

stream["boolean", "null"]
Optional

Chat-Completions-Chunks als Server-Sent Events zurückgeben.

Standard: false
temperature["number", "null"]
Optional

Sampling-Temperatur; die Modellunterstützung kann variieren.

toolsarray
Optional

Tools, die das Modell aufrufen darf.

top_p["number", "null"]
Optional

Nucleus-Sampling-Wahrscheinlichkeit; die Modellunterstützung kann variieren.

Anfragebeispiel

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

Synchrone Antwort

HTTP 200

Schema

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

Beispiel

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-Ereignis: Chunk

text/event-stream

Schema

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

Beispiel

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

Protokollfehler: invalid_request

HTTP 400

Schema

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

Beispiel

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

Verwendung

Schema

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

Beispiel

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

Generierte Code-Beispiele

Jedes cURL-, JavaScript- und Python-Beispiel sendet die validierte Anfrage aus diesem Vertrag, ohne ein protokollspezifisches SDK zu benötigen.

Installieren

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