Vai al contenuto
Riferimento API
Riferimento API

OpenAI Chat Completions

Invia una richiesta tramite l'operazione Chat Completions compatibile con OpenAI di RunAPI.

01

Panoramica

Usa l'operazione Chat Completions compatibile con OpenAI di RunAPI con un modello compatibile e le forme di richiesta e risposta del protocollo.

Avvio rapido

  1. Crea una Chiave API e impostala come RUNAPI_API_KEY.
  2. Scegli un modello compatibile, quindi invia i parametri di percorso e il corpo JSON documentati.
  3. Leggi la risposta JSON o gli eventi inviati dal server mostrati per questa operazione e gestisci gli errori di protocollo.

Endpoint

POST /v1/chat/completions
URL di base
https://runapi.ai
Versione del contratto
v1
Autenticazione preferita
Authorization: Bearer YOUR_API_TOKEN
02

Vettori di autenticazione

RunAPI accetta ciascun protocollo elencato di seguito. I campioni generati utilizzano l'header preferito del protocollo.

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

Modelli compatibili

Apri la pagina di un modello per i prezzi correnti, i limiti di frequenza e i dettagli sull'uso commerciale.

Mostra 60 modelli compatibili
04

Contratto della richiesta

Corpo JSON

Sono elencati solo i campi con evidenza di supporto sia del protocollo pubblico che di RunAPI. I campi aggiuntivi del corpo JSON vengono trasmessi senza modifiche.

Corpo JSON9 campi
max_tokens["integer", "null"]
Opzionale

Limite di token per la generazione di compatibilità mantenuto per i modelli supportati; il supporto del modello può variare.

messagesarray
Obbligatorio

Messaggi della conversazione in formato OpenAI Chat Completions.

messages[].content["string", "array"]
Opzionale
messages[].rolestring
Obbligatorio
modelstring
Obbligatorio

Identificatore del modello di RunAPI restituito dal catalogo modelli compatibile.

stream["boolean", "null"]
Opzionale

Restituisce i chunk di Chat Completions come server-sent events.

Predefinito: false
temperature["number", "null"]
Opzionale

Temperatura di campionamento; il supporto varia in base al modello.

toolsarray
Opzionale

Strumenti che il modello può chiamare.

top_p["number", "null"]
Opzionale

Probabilità di campionamento nucleus; il supporto del modello può variare.

Esempio di richiesta

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

Risposta sincrona

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

Esempio

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

Evento SSE: 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"
}

Esempio

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

Errore di protocollo: 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"
}

Esempio

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

Utilizzo

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

Esempio

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

Esempi di Codice Generati

Ogni esempio cURL, JavaScript e Python invia la richiesta validata di questo contratto senza richiedere un SDK specifico per il protocollo.

Installa

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