Untuk mengetahui panduan mendetail tentang cara menghitung token menggunakan Gemini API, termasuk cara penghitungan gambar, audio, dan video, lihat Panduan penghitungan token dan resep Cookbook yang menyertainya.
Metode: models.countTokens
- Endpoint
- Parameter jalur
- Isi permintaan
- Isi respons
- Cakupan otorisasi
- Contoh permintaan
- GenerateContentRequest
Menjalankan tokenizer model pada input Content
dan menampilkan jumlah token. Lihat panduan token untuk mempelajari token lebih lanjut.
Endpoint
posting
https://generativelanguage.googleapis.com/v1beta/{model=models/*}:countTokens
Parameter jalur
model
string
Wajib. Nama resource model. Ini berfungsi sebagai ID yang akan digunakan Model.
Nama ini harus cocok dengan nama model yang ditampilkan oleh metode models.list
.
Format: models/{model}
Formatnya adalah models/{model}
.
Isi permintaan
Isi permintaan memuat data dengan struktur berikut:
contents[]
object (Content
)
Opsional. Input yang diberikan ke model sebagai prompt. Kolom ini diabaikan jika generateContentRequest
ditetapkan.
generateContentRequest
object (GenerateContentRequest
)
Opsional. Keseluruhan input yang diberikan ke Model
. Hal ini mencakup perintah serta informasi pengarahan model lainnya seperti petunjuk sistem, dan/atau deklarasi fungsi untuk panggilan fungsi. Model
/Content
dan generateContentRequest
tidak sama-sama bersifat eksklusif. Anda dapat mengirim Model
+ Content
atau generateContentRequest
, tetapi tidak boleh keduanya.
Contoh permintaan
Teks
Python
Node.js
Go
Shell
Kotlin
Swift
Dart
Java
Chat
Python
Node.js
Go
Shell
Kotlin
Swift
Dart
Java
Media inline
Python
Node.js
Go
Shell
Kotlin
Swift
Dart
Java
Video
Python
Node.js
Go
Shell
Python
Cache
Python
Node.js
Go
Petunjuk Sistem
Python
Node.js
Go
Kotlin
Swift
Dart
Java
Alat
Python
Node.js
Kotlin
Swift
Dart
Java
Isi respons
Respons dari models.countTokens
.
Metode ini menampilkan tokenCount
model untuk prompt
.
Jika berhasil, isi respons memuat data dengan struktur berikut:
totalTokens
integer
Jumlah token yang menjadi token Model
sebagai token prompt
. Selalu non-negatif.
Representasi JSON |
---|
{ "totalTokens": integer } |
GenerateContentRequest
Permintaan untuk menghasilkan penyelesaian dari model.
model
string
Wajib. Nama Model
yang akan digunakan untuk menghasilkan penyelesaian.
Format: name=models/{model}
.
contents[]
object (Content
)
Wajib. Konten percakapan saat ini dengan model.
Untuk kueri satu putaran, ini adalah instance tunggal. Untuk kueri multi-giliran seperti chat, ini adalah kolom berulang yang berisi histori percakapan dan permintaan terbaru.
tools[]
object (Tool
)
Opsional. Daftar Tools
yang dapat digunakan Model
untuk membuat respons berikutnya.
Tool
adalah potongan kode yang memungkinkan sistem berinteraksi dengan sistem eksternal untuk melakukan tindakan, atau serangkaian tindakan, di luar pengetahuan dan cakupan Model
. Tool
yang didukung adalah Function
dan codeExecution
. Lihat panduan Panggilan fungsi dan Eksekusi kode untuk mempelajari lebih lanjut.
toolConfig
object (ToolConfig
)
Opsional. Konfigurasi alat untuk setiap Tool
yang ditentukan dalam permintaan. Lihat Panduan panggilan fungsi untuk contoh penggunaan.
safetySettings[]
object (SafetySetting
)
Opsional. Daftar instance SafetySetting
unik untuk memblokir konten tidak aman.
Hal ini akan diterapkan di GenerateContentRequest.contents
dan GenerateContentResponse.candidates
. Tidak boleh ada lebih dari satu setelan untuk setiap jenis SafetyCategory
. API akan memblokir konten dan respons apa pun yang gagal memenuhi nilai minimum yang ditetapkan oleh setelan ini. Daftar ini menggantikan setelan default untuk setiap SafetyCategory
yang ditentukan di safetySettings. Jika tidak ada SafetySetting
untuk SafetyCategory
tertentu yang disediakan dalam daftar, API akan menggunakan setelan keamanan default untuk kategori tersebut. Kategori bahaya HARM_CATEGORY_HATE_SPEECH, HARM_CATEGORY_SEXUALLY_EXPLICIT, HARM_CATEGORY_DANGEROUS_CONTENT, HARM_CATEGORY_HARASSMENT didukung. Lihat panduan untuk mendapatkan informasi mendetail tentang setelan keamanan yang tersedia. Lihat juga Panduan keamanan untuk mempelajari cara menyertakan pertimbangan keamanan dalam aplikasi AI.
systemInstruction
object (Content
)
Opsional. Menetapkan petunjuk sistem developer. Saat ini, teks saja.
generationConfig
object (GenerationConfig
)
Opsional. Opsi konfigurasi untuk pembuatan dan output model.
cachedContent
string
Opsional. Nama konten yang di-cache untuk digunakan sebagai konteks untuk menampilkan prediksi. Format: cachedContents/{cachedContent}
Representasi JSON |
---|
{ "model": string, "contents": [ { object ( |