Crea un completamento della chat
Usa CometAPI POST /v1/chat/completions per inviare conversazioni composte da più messaggi ai modelli di chat con Streaming, output strutturato e tool calling.
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.
Ruoli dei messaggi
Invia input Multimodal
Usa un array di parti di contenuto per inviare un’immagine con del testo:image_url.detail per selezionare il livello di dettaglio dell’elaborazione delle immagini. Questo esempio usa high.
Risposte in streaming
Per ricevere output incrementale, impostastream 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:
Richiedere output strutturato
Usaresponse_format per richiedere output strutturato:
Chiamare strumenti e funzioni
Usagpt-5.6-sol con una definizione di funzione:
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
Parametri specifici del modello
Parametri specifici del modello
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.max_tokens e max_completion_tokens
max_tokens e max_completion_tokens
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.Ruoli delle istruzioni
Ruoli delle istruzioni
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 verifica429 Too Many Requests, implementa un backoff esponenziale:
Come mantenere il contesto della conversazione?
Includi l’intera cronologia della conversazione nell’arraymessages:
Cosa finish_reason significa?
Come controllare i costi?
- Usa
max_completion_tokensper limitare la lunghezza dell’output. - Confronta i prezzi dei modelli e scegli un modello che soddisfi i requisiti del tuo carico di lavoro.
- Mantieni i prompt concisi: evita il contesto ridondante.
- Monitora l’utilizzo dei token nel campo di risposta
usage.
Autorizzazioni
Bearer token authentication. Use your CometAPI key.
Corpo
Model ID to use for this request. See the Models page for current options.
"gpt-6-astra"
Conversation messages, including instructions, user input, assistant replies, and tool results.
If true, partial response tokens are delivered incrementally via server-sent events (SSE). The stream ends with a data: [DONE] message.
Sampling temperature. Omit this field for GPT-6 Astra.
0 <= x <= 2Nucleus sampling threshold. Omit this field for GPT-6 Astra. For sampling overrides, adjust either top_p or temperature.
0 <= x <= 1Number of completion choices to generate for each input message. Defaults to 1.
Stop string or list of up to four strings, for models that support stop sequences.
Legacy output-token limit. Use max_completion_tokens for the GPT-6 Astra examples.
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.
Maximum generated tokens, including visible output and reasoning. Leave enough room for both, as in the image-input example.
Specifies the output format. Use {"type": "json_object"} for JSON mode, or {"type": "json_schema", "json_schema": {...}} for strict structured output.
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.
Controls how the model selects tools. auto (default): model decides. none: no tools. required: must call a tool.
Return token log probabilities. Use gpt-4.1 as shown in the Logprobs example.
Number of most likely tokens to return at each position (0-20). Requires logprobs to be true.
0 <= x <= 20Reasoning 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.
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_example"
Object type. Non-streaming responses use chat.completion.
chat.completion "chat.completion"
Unix timestamp of creation.
1788763703
The model used (may include version suffix).
"gpt-6-astra"
Array of completion choices.
Token accounting for this request. Billing uses these counts.
Service tier that processed the request, when returned.
Model configuration fingerprint, when returned.