Chat Completions
Usa CometAPI POST /v1/chat/completions per inviare conversazioni multi-messaggio e ottenere risposte LLM con controlli per Streaming, temperature e max_tokens.
CometAPI instrada Chat Completions verso più provider — inclusi OpenAI, Claude e Gemini — attraverso un’unica interfaccia compatibile con OpenAI. Passa da un modello all’altro modificando il parametroDocumentation Index
Fetch the complete documentation index at: https://apidoc.cometapi.com/llms.txt
Use this file to discover all available pages before exploring further.
model; la maggior parte degli SDK compatibili con OpenAI funziona impostando base_url su https://api.cometapi.com/v1.
Ruoli dei messaggi
| Role | Description |
|---|---|
system | Imposta il comportamento e la personalità dell’assistente. Va posizionato all’inizio della conversazione. |
developer | Sostituisce system per i modelli più recenti (o1+). Fornisce istruzioni che il modello deve seguire indipendentemente dall’input dell’utente. |
user | Messaggi dell’utente finale. |
assistant | Risposte precedenti del modello, usate per mantenere la cronologia della conversazione. |
tool | Risultati di chiamate a tool/funzioni. Deve includere tool_call_id corrispondente alla chiamata tool originale. |
Invia input multimodale
Molti modelli supportano immagini e audio insieme al testo. Per inviare messaggi Multimodal, usa il formato array percontent:
detail controlla la profondità dell’analisi dell’immagine:
low— più veloce, usa meno Tokens (costo fisso)high— analisi dettagliata, vengono consumati più Tokensauto— decide il modello (predefinito)
Risposte in Streaming
Per ricevere output incrementale, impostastream su true. La risposta viene fornita come Server-Sent Events (SSE), dove ogni evento contiene un oggetto chat.completion.chunk:
Richiedere output strutturato
Per forzare il model a restituire JSON valido che corrisponda a uno schema specifico, usaresponse_format:
json_schema) garantisce che l’output corrisponda esattamente al tuo schema. La modalità JSON Object (json_object) garantisce solo JSON valido — la struttura non viene applicata.Chiamare strumenti e funzioni
Per consentire al model di chiamare funzioni esterne, fornisci le definizioni degli strumenti:finish_reason: "tool_calls" e l’array message.tool_calls conterrà il nome della funzione e gli argomenti. A quel punto esegui la funzione e rimanda il risultato come messaggio tool con il tool_call_id corrispondente.
Note tra provider
Supporto dei parametri tra provider
Supporto dei parametri tra provider
| Parameter | OpenAI GPT | Claude (via compat) | Gemini (via compat) |
|---|---|---|---|
temperature | 0–2 | 0–1 | 0–2 |
top_p | 0–1 | 0–1 | 0–1 |
n | 1–128 | solo 1 | 1–8 |
stop | Fino a 4 | Fino a 4 | Fino a 5 |
tools | ✅ | ✅ | ✅ |
response_format | ✅ | ✅ (json_schema) | ✅ |
logprobs | ✅ | ❌ | ❌ |
reasoning_effort | serie o, GPT-5.1+ | ❌ | ❌ (usa thinking per Gemini native) |
max_tokens vs max_completion_tokens
max_tokens vs max_completion_tokens
max_tokens— Il parametro legacy. Funziona con la maggior parte dei modelli ma è deprecato per i modelli OpenAI più recenti.max_completion_tokens— Il parametro consigliato per i modelli GPT-4.1, serie GPT-5 e serie o. Obbligatorio per i modelli di reasoning poiché include sia gli output tokens sia i reasoning tokens.
system vs developer role
system vs developer role
system— Il ruolo di istruzione tradizionale. Funziona con tutti i modelli.developer— Introdotto con i modelli o1. Fornisce un’adesione alle istruzioni più forte per i modelli più recenti. Ripiega sul comportamento disystemnei modelli meno recenti.
developer per nuovi progetti destinati ai modelli GPT-4.1+ o della serie o.FAQ
Come gestire i rate limit?
Quando incontri429 Too Many Requests, implementa un backoff esponenziale:
Come mantenere il contesto della conversazione?
Includi l’intera cronologia della conversazione nell’arraymessages:
Cosa significa finish_reason?
| Value | Meaning |
|---|---|
stop | Completamento naturale o raggiungimento di una sequenza di stop. |
length | Raggiunto il limite di max_tokens o max_completion_tokens. |
tool_calls | Il model ha invocato una o più chiamate a tool/function. |
content_filter | L’output è stato filtrato a causa della policy sui contenuti. |
Come controllare i costi?
- Usa
max_completion_tokensper limitare la lunghezza dell’output. - Scegli modelli convenienti in termini di costo (ad esempio,
gpt-5.4-miniogpt-5.4-nanoper attività più semplici). - Mantieni i prompt concisi — evita contesto ridondante.
- Monitora l’utilizzo dei token nel campo
usagedella risposta.
Autorizzazioni
Bearer token authentication. Use your CometAPI key.
Corpo
Model ID to use for this request. See the Models page for current options.
"gpt-4.1"
A list of messages forming the conversation. Each message has a role (system, user, assistant, or developer) and content (text string or multimodal content array).
If true, partial response tokens are delivered incrementally via server-sent events (SSE). The stream ends with a data: [DONE] message.
Sampling temperature between 0 and 2. Higher values (e.g., 0.8) produce more random output; lower values (e.g., 0.2) make output more focused and deterministic. Recommended to adjust this or top_p, but not both.
0 <= x <= 2Nucleus sampling parameter. The model considers only the tokens whose cumulative probability reaches top_p. For example, 0.1 means only the top 10% probability tokens are considered. Recommended to adjust this or temperature, but not both.
0 <= x <= 1Number of completion choices to generate for each input message. Defaults to 1.
Up to 4 sequences where the API will stop generating further tokens. Can be a string or an array of strings.
Maximum number of tokens to generate in the completion. The total of input + output tokens is capped by the model's context length.
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.
-2 <= x <= 2Number between -2.0 and 2.0. Positive values penalize tokens proportionally to how often they have appeared, reducing verbatim repetition.
-2 <= x <= 2A 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.
A unique identifier for your end-user. Helps with abuse detection and monitoring.
An upper bound for the number of tokens to generate, including visible output tokens and reasoning tokens. Use this instead of max_tokens for GPT-4.1+, GPT-5 series, and o-series models.
Specifies the output format. Use {"type": "json_object"} for JSON mode, or {"type": "json_schema", "json_schema": {...}} for strict structured output.
A list of tools the model may call. Currently supports function type tools.
Controls how the model selects tools. auto (default): model decides. none: no tools. required: must call a tool.
Whether to return log probabilities of the output tokens.
Number of most likely tokens to return at each position (0-20). Requires logprobs to be true.
0 <= x <= 20Controls the reasoning effort for o-series and GPT-5.1+ models.
low, medium, high Options for streaming. Only valid when stream is true.
Specifies the processing tier.
auto, default, flex, priority Risposta
Successful chat completion response.
Unique completion identifier.
"chatcmpl-abc123"
chat.completion "chat.completion"
Unix timestamp of creation.
1774412483
The model used (may include version suffix).
"gpt-5.4-2025-07-16"
Array of completion choices.
"default"
"fp_490a4ad033"