Szczegółowy przewodnik po liczeniu tokenów przy użyciu interfejsu Gemini API, w tym o sposobie zliczania obrazów, dźwięku i filmów, znajdziesz w przewodniku po liczeniu tokenów oraz w powiązanym z nim przepisie w Książce kucharskiej.
Metoda: Modele.countTokens
- Punkt końcowy
- Parametry ścieżki
- Treść żądania
- Treść odpowiedzi
- Zakresy autoryzacji
- Przykładowe żądanie
- GenerateContentRequest
Uruchamia tokenizację modelu dla danych wejściowych Content
i zwraca liczbę tokenów. Więcej informacji o tokenach znajdziesz w przewodniku po tokenach.
Punkt końcowy
post
https://generativelanguage.googleapis.com/v1beta/{model=models/*}:countTokens
Parametry ścieżki
model
string
Wymagane. Nazwa zasobu modelu. Służy on jako identyfikator, którego model ma używać.
Ta nazwa powinna być zgodna z nazwą modelu zwracaną przez metodę models.list
.
Format: models/{model}
Przyjmuje postać models/{model}
.
Treść żądania
Treść żądania zawiera dane o następującej strukturze:
contents[]
object (Content
)
Opcjonalnie: Dane wejściowe podane modelowi w postaci promptu. To pole jest ignorowane po ustawieniu pola generateContentRequest
.
generateContentRequest
object (GenerateContentRequest
)
Opcjonalnie: Ogólne dane wejściowe przekazywane do funkcji Model
. Obejmuje to prompt, a także inne informacje związane ze sterowaniem modelem, takie jak instrukcje systemowe lub deklaracje funkcji dotyczące wywoływania funkcji. Elementy Model
/Content
i generateContentRequest
wzajemnie się wykluczają. Możesz wysłać Model
+ Content
lub generateContentRequest
, ale nie oba te elementy naraz.
Przykładowe żądanie
Tekst
Python
Node.js
Przeczytaj
Pudrowy róż
Kotlin
Swift
Dart
Java
Czat
Python
Node.js
Przeczytaj
Pudrowy róż
Kotlin
Swift
Dart
Java
Multimedia w treści
Python
Node.js
Przeczytaj
Pudrowy róż
Kotlin
Swift
Dart
Java
Wideo
Python
Node.js
Przeczytaj
Pudrowy róż
Python
Cache (Pamięć podręczna)
Python
Node.js
Przeczytaj
Instrukcja systemowa
Python
Node.js
Przeczytaj
Kotlin
Swift
Dart
Java
Narzędzia
Python
Node.js
Kotlin
Swift
Dart
Java
Treść odpowiedzi
Odpowiedź od użytkownika models.countTokens
.
Zwraca tokenCount
modelu dla prompt
.
W przypadku powodzenia treść żądania zawiera dane o następującej strukturze:
totalTokens
integer
Liczba tokenów, do których Model
tokenizuje token prompt
. Zawsze nieujemna.
Zapis JSON |
---|
{ "totalTokens": integer } |
GenerateContentRequest
Żądanie wygenerowania ukończenia z modelu.
model
string
Wymagane. Nazwa obiektu Model
, która ma być używana do generowania uzupełniania.
Format: name=models/{model}
.
contents[]
object (Content
)
Wymagane. Treść bieżącej rozmowy z modelem.
W przypadku zapytań jednoetapowych jest to pojedyncza instancja. W przypadku zapytań wieloetapowych, takich jak czat, jest to pole powtarzane, które zawiera historię rozmowy i ostatnie żądanie.
tools[]
object (Tool
)
Opcjonalnie: Lista właściwości Tools
, których Model
może użyć do wygenerowania następnej odpowiedzi.
Tool
to fragment kodu, który umożliwia systemowi wchodzenie w interakcję z zewnętrznymi systemami w celu wykonania działania lub zestawu działań poza zakresem wiedzy i zakresu Model
. Obsługiwane elementy typu Tool
to Function
i codeExecution
. Więcej informacji znajdziesz w przewodnikach dotyczących wywoływania funkcji i wykonania kodu.
toolConfig
object (ToolConfig
)
Opcjonalnie: Konfiguracja narzędzia dla dowolnych Tool
określonych w żądaniu. Przykład użycia znajdziesz w przewodniku dotyczącym wywoływania funkcji.
safetySettings[]
object (SafetySetting
)
Opcjonalnie: Lista unikalnych instancji SafetySetting
do blokowania niebezpiecznych treści.
Ta zmiana zostanie zastosowana w GenerateContentRequest.contents
i GenerateContentResponse.candidates
. Każdy typ SafetyCategory
powinien mieć maksymalnie 1 ustawienie. Interfejs API zablokuje treści i odpowiedzi, które nie osiągną progów ustawionych przez te ustawienia. Ta lista zastępuje domyślne ustawienia dla każdego elementu SafetyCategory
określonego w ustawieniach bezpieczeństwa. Jeśli na liście nie ma atrybutu SafetySetting
dla danego elementu SafetyCategory
, interfejs API użyje dla tej kategorii domyślnego ustawienia bezpieczeństwa. Kategorie szkodliwego działania: HARM_CATEGORY_HATE_SPEECH, HARM_CATEGORY_SEXUALLY_EXPLICIT, HARM_CATEGORY_DANGEROUS_CONTENT, HARM_CATEGORY_HARASSMENT są obsługiwane. Szczegółowe informacje o dostępnych ustawieniach bezpieczeństwa znajdziesz w przewodniku. Aby dowiedzieć się, jak uwzględniać kwestie bezpieczeństwa w aplikacjach AI, zapoznaj się ze wskazówkami dotyczącymi bezpieczeństwa.
systemInstruction
object (Content
)
Opcjonalnie: Programista skonfigurował instrukcje systemowe. Obecnie tylko tekstowe.
generationConfig
object (GenerationConfig
)
Opcjonalnie: Opcje konfiguracji generowania modeli i ich danych wyjściowych.
cachedContent
string
Opcjonalnie: Nazwa treści zapisana w pamięci podręcznej, która może służyć jako kontekst do wyświetlania podpowiedzi. Format: cachedContents/{cachedContent}
Zapis JSON |
---|
{ "model": string, "contents": [ { object ( |