Skip to main content
POST
Invia conversazioni tramite l’API Chat Completions compatibile con OpenAI. Configura il tuo SDK con base_url="https://api.cometapi.com/v1" e la tua chiave API CometAPI. Gli esempi di base, relativi alle immagini, di Streaming e di output strutturato usano gpt-6-astra. L’esempio di function calling usa gpt-5.6-sol, mentre l’esempio di probabilità logaritmica usa gpt-4.1.
Usa il riferimento OpenAI Chat Completions per le definizioni dei parametri. Per il tool calling Astra di GPT-6, usa Responses, seguendo le indicazioni sui modelli di OpenAI.

Ruoli dei messaggi

Inserisci le istruzioni dell’applicazione in un messaggio developer prima dei messaggi utente quando usi gli esempi Astra di GPT-6.

Invia input Multimodal

Usa un array di parti di contenuto per inviare un’immagine con del testo:
Usa image_url.detail per selezionare il livello di dettaglio dell’elaborazione delle immagini. Questo esempio usa high.

Risposte in streaming

Per ricevere output incrementale, imposta stream su true. La risposta viene fornita come eventi inviati dal server (SSE)). Il testo arriva in oggetti chat.completion.chunk; questo esempio abbreviato include il chunk di utilizzo finale:
Per includere statistiche sull’utilizzo dei token nelle risposte in streaming, imposta stream_options.include_usage su true. I dati di utilizzo vengono visualizzati in un chunk finale prima di [DONE]. I chunk possono avere un array choices vuoto; controllalo prima di accedere a choices[0].

Richiedere output strutturato

Usa response_format per richiedere output strutturato:
La modalità JSON Schema specifica la struttura richiesta. La modalità JSON Object richiede JSON valido senza imporre il tuo schema. Verifica la presenza di un rifiuto o di una risposta limitata in lunghezza prima di utilizzare il risultato.

Chiamare strumenti e funzioni

Usa gpt-5.6-sol con una definizione di funzione:
Una chiamata di funzione restituisce finish_reason: "tool_calls" e un array message.tool_calls. Analizza arguments della funzione, codificato in JSON, esegui la funzione, quindi aggiungi il messaggio dell’assistente e un messaggio di risultato tool con il relativo tool_call_id.

Scegliere i parametri della richiesta

Usa reasoning_effort e max_completion_tokens con Astra GPT-6. Gli esempi Functions e Logprobs mostrano le opzioni di richiesta rispettivamente per gpt-5.6-sol e gpt-4.1.Per i formati di richiesta specifici di Claude o Gemini, consulta Anthropic Messages e Gemini Generate content.
Usa max_completion_tokens per limitare i Tokens generati negli esempi Astra GPT-6. Questo include il ragionamento e l’output visibile, quindi lascia spazio per entrambi. max_tokens è un parametro legacy.
Usa developer per le istruzioni dell’applicazione negli esempi Astra GPT-6. Mantieni i contenuti dell’utente finale nei messaggi user e conserva le risposte precedenti dell’assistente quando prosegui una conversazione.

Domande frequenti

Come gestire i limiti di frequenza?

Quando si verifica 429 Too Many Requests, implementa un backoff esponenziale:

Come mantenere il contesto della conversazione?

Includi l’intera cronologia della conversazione nell’array messages:

Cosa finish_reason significa?

Come controllare i costi?

  1. Usa max_completion_tokens per limitare la lunghezza dell’output.
  2. Confronta i prezzi dei modelli e scegli un modello che soddisfi i requisiti del tuo carico di lavoro.
  3. Mantieni i prompt concisi: evita il contesto ridondante.
  4. Monitora l’utilizzo dei token nel campo di risposta usage.

Autorizzazioni

Authorization
string
header
obbligatorio

Bearer token authentication. Use your CometAPI key.

Corpo

application/json
model
string
predefinito:gpt-6-astra
obbligatorio

Model ID to use for this request. See the Models page for current options.

Esempio:

"gpt-6-astra"

messages
object[]
obbligatorio

Conversation messages, including instructions, user input, assistant replies, and tool results.

stream
boolean

If true, partial response tokens are delivered incrementally via server-sent events (SSE). The stream ends with a data: [DONE] message.

temperature
number

Sampling temperature. Omit this field for GPT-6 Astra.

Intervallo richiesto: 0 <= x <= 2
top_p
number

Nucleus sampling threshold. Omit this field for GPT-6 Astra. For sampling overrides, adjust either top_p or temperature.

Intervallo richiesto: 0 <= x <= 1
n
integer

Number of completion choices to generate for each input message. Defaults to 1.

stop

Stop string or list of up to four strings, for models that support stop sequences.

max_tokens
integer
deprecato

Legacy output-token limit. Use max_completion_tokens for the GPT-6 Astra examples.

presence_penalty
number

Number between -2.0 and 2.0. Positive values penalize tokens based on whether they have already appeared, encouraging the model to explore new topics.

Intervallo richiesto: -2 <= x <= 2
frequency_penalty
number

Number between -2.0 and 2.0. Positive values penalize tokens proportionally to how often they have appeared, reducing verbatim repetition.

Intervallo richiesto: -2 <= x <= 2
logit_bias
object

A JSON object mapping token IDs to bias values from -100 to 100. The bias is added to the model's logits before sampling. Values between -1 and 1 subtly adjust likelihood; -100 or 100 effectively ban or force selection of a token.

user
string

A unique identifier for your end-user. Helps with abuse detection and monitoring.

max_completion_tokens
integer

Maximum generated tokens, including visible output and reasoning. Leave enough room for both, as in the image-input example.

response_format
object

Specifies the output format. Use {"type": "json_object"} for JSON mode, or {"type": "json_schema", "json_schema": {...}} for strict structured output.

tools
object[]

Function definitions for a model that supports tool calling on Chat Completions. The function example uses GPT-5.6 Sol; use Responses for GPT-6 Astra tool calls.

tool_choice

Controls how the model selects tools. auto (default): model decides. none: no tools. required: must call a tool.

logprobs
boolean

Return token log probabilities. Use gpt-4.1 as shown in the Logprobs example.

top_logprobs
integer

Number of most likely tokens to return at each position (0-20). Requires logprobs to be true.

Intervallo richiesto: 0 <= x <= 20
reasoning_effort
string

Reasoning effort supported by the selected model. The GPT-6 Astra examples use low; the GPT-5.6 Sol function example uses none. See the model reference for other supported levels.

stream_options
object

Options for streaming. Only valid when stream is true.

service_tier
enum<string>

Specifies the processing tier.

Opzioni disponibili:
auto,
default,
flex,
priority

Risposta

Successful chat completion response.

id
string

Unique completion identifier.

Esempio:

"chatcmpl_example"

object
enum<string>

Object type. Non-streaming responses use chat.completion.

Opzioni disponibili:
chat.completion
Esempio:

"chat.completion"

created
integer

Unix timestamp of creation.

Esempio:

1788763703

model
string

The model used (may include version suffix).

Esempio:

"gpt-6-astra"

choices
object[]

Array of completion choices.

usage
object

Token accounting for this request. Billing uses these counts.

service_tier
string

Service tier that processed the request, when returned.

system_fingerprint
string | null

Model configuration fingerprint, when returned.

Ultima modifica il 8 settembre 2026