Per una guida dettagliata sul conteggio dei token utilizzando l'API Gemini, incluso il modo in cui vengono conteggiati immagini, audio e video, consulta la Guida al conteggio dei token e l'apposita ricetta di Cookbook.
Metodo: Models.countTokens
- Endpoint
- Parametri percorso
- Corpo della richiesta
- Corpo della risposta
- Ambiti di autorizzazione
- Richiesta di esempio
- GenerateContentRequest
Esegue il tokenizzatore di un modello sull'input Content
e restituisce il conteggio dei token. Per scoprire di più sui token, consulta la guida ai token.
Endpoint
pubblica
https://generativelanguage.googleapis.com/v1beta/{model=models/*}:countTokens
Parametri del percorso
model
string
Obbligatorio. Il nome della risorsa del modello. Serve da ID per il modello.
Questo nome deve corrispondere a un nome di modello restituito dal metodo models.list
.
Formato: models/{model}
Prende il formato models/{model}
.
Corpo della richiesta
Il corpo della richiesta contiene dati con la seguente struttura:
contents[]
object (Content
)
(Facoltativo) L'input fornito al modello come prompt. Questo campo viene ignorato se viene impostato il criterio generateContentRequest
.
generateContentRequest
object (GenerateContentRequest
)
(Facoltativo) L'input complessivo fornito a Model
. Sono inclusi il prompt e altre informazioni di reindirizzamento del modello, come le istruzioni di sistema e/o le dichiarazioni di funzione per le chiamate di funzione. Model
s/Content
e generateContentRequest
s si escludono a vicenda. Puoi inviare Model
+ Content
o un generateContentRequest
, ma mai entrambi.
Esempio di richiesta
Testo
Python
Node.js
Vai
Conchiglia
Kotlin
Swift
Dart
Java
Chat
Python
Node.js
Vai
Conchiglia
Kotlin
Swift
Dart
Java
Contenuti multimediali incorporati
Python
Node.js
Vai
Conchiglia
Kotlin
Swift
Dart
Java
Video
Python
Node.js
Vai
Conchiglia
Python
Cache
Python
Node.js
Vai
Istruzione di sistema
Python
Node.js
Vai
Kotlin
Swift
Dart
Java
Strumenti
Python
Node.js
Kotlin
Swift
Dart
Java
Corpo della risposta
Una risposta da models.countTokens
.
Restituisce il valore tokenCount
del modello per prompt
.
In caso di esito positivo, il corpo della risposta contiene dati con la seguente struttura:
totalTokens
integer
Il numero di token in cui Model
tokenizza prompt
. Sempre non negativo.
Rappresentazione JSON |
---|
{ "totalTokens": integer } |
GenerateContentRequest
Richiesta di generare un completamento dal modello.
model
string
Obbligatorio. Il nome dell'evento Model
da utilizzare per generare il completamento.
Formato: name=models/{model}
.
contents[]
object (Content
)
Obbligatorio. I contenuti della conversazione corrente con il modello.
Per le query a turno singolo, si tratta di una singola istanza. Per le query in più passaggi come la chat, si tratta di un campo ripetuto che contiene la cronologia della conversazione e l'ultima richiesta.
tools[]
object (Tool
)
(Facoltativo) Un elenco di Tools
che Model
può utilizzare per generare la risposta successiva.
Un Tool
è una porzione di codice che consente al sistema di interagire con sistemi esterni per eseguire un'azione o un insieme di azioni al di fuori delle conoscenze e dell'ambito di Model
. I valori di tipo Tool
supportati sono Function
e codeExecution
. Per saperne di più, consulta le Chiamate di funzione e le guide sull'esecuzione del codice.
toolConfig
object (ToolConfig
)
(Facoltativo) Configurazione dello strumento per qualsiasi Tool
specificato nella richiesta. Per un esempio di utilizzo, consulta la guida alla chiamata di funzione.
safetySettings[]
object (SafetySetting
)
(Facoltativo) Un elenco di istanze SafetySetting
univoche per il blocco di contenuti non sicuri.
Questa norma verrà applicata in modo forzato a GenerateContentRequest.contents
e GenerateContentResponse.candidates
. Non dovrebbe essere presente più di un'impostazione per ogni tipo di SafetyCategory
. L'API bloccherà tutti i contenuti e le risposte che non rispetteranno le soglie impostate da queste impostazioni. Questo elenco sostituisce le impostazioni predefinite per ogni SafetyCategory
specificato in safetySettings. Se non è presente alcun valore SafetySetting
per un determinato SafetyCategory
nell'elenco, l'API utilizzerà l'impostazione di sicurezza predefinita per la categoria in questione. Sono supportate le categorie di danni HARM_CATEGORY_HATE_SPEECH, HARM_CATEGORY_SEXUALLY_EXPLICIT, HARM_CATEGORY_DANGEROUS_CONTENT e HARM_CATEGORY_HARASSMENT. Consulta la guida per informazioni dettagliate sulle impostazioni di sicurezza disponibili. Consulta anche le indicazioni sulla sicurezza per scoprire come incorporare le considerazioni sulla sicurezza nelle tue applicazioni di IA.
systemInstruction
object (Content
)
(Facoltativo) Lo sviluppatore ha impostato le istruzioni di sistema. Al momento, solo testo.
generationConfig
object (GenerationConfig
)
(Facoltativo) Opzioni di configurazione per la generazione e gli output del modello.
cachedContent
string
(Facoltativo) Il nome dei contenuti memorizzati nella cache da utilizzare come contesto per la previsione. Formato: cachedContents/{cachedContent}
Rappresentazione JSON |
---|
{ "model": string, "contents": [ { object ( |