Để xem hướng dẫn chi tiết về cách đếm mã thông báo bằng Gemini API, bao gồm cả cách đếm hình ảnh, âm thanh và video, hãy xem Hướng dẫn cách đếm mã thông báo và công thức dạy nấu ăn đi kèm.
Phương thức: model.countTokens
- Điểm cuối
- Tham số đường dẫn
- Nội dung yêu cầu
- Nội dung phản hồi
- Phạm vi uỷ quyền
- Ví dụ về yêu cầu
- GenerateContentRequest
Chạy trình tạo mã thông báo của mô hình trên đầu vào Content
và trả về số lượng mã thông báo. Hãy tham khảo hướng dẫn về mã thông báo để tìm hiểu thêm về mã thông báo.
Điểm cuối
bài đăng
https://generativelanguage.googleapis.com/v1beta/{model=models/*}:countTokens
Tham số đường dẫn
model
string
Bắt buộc. Tên tài nguyên của mô hình. Giá trị này đóng vai trò là mã nhận dạng cho Mô hình sử dụng.
Tên này phải khớp với tên mô hình được phương thức models.list
trả về.
Định dạng: models/{model}
. Định dạng này có dạng models/{model}
.
Nội dung yêu cầu
Nội dung yêu cầu chứa dữ liệu có cấu trúc sau:
contents[]
object (Content
)
Không bắt buộc. Giá trị nhập vào được cung cấp cho mô hình ở dạng câu lệnh. Trường này sẽ bị bỏ qua khi bạn đặt generateContentRequest
.
generateContentRequest
object (GenerateContentRequest
)
Không bắt buộc. Thông tin đầu vào tổng thể được cung cấp cho Model
. Trong đó bao gồm lời nhắc cũng như các thông tin điều khiển mô hình khác như hướng dẫn hệ thống và/hoặc nội dung khai báo hàm để gọi hàm. Model
/Content
và generateContentRequest
loại trừ lẫn nhau. Bạn có thể gửi Model
+ Content
hoặc generateContentRequest
, nhưng không được gửi cả hai.
Yêu cầu mẫu
Văn bản
Python
Node.js
Go
Vỏ
Kotlin
Swift
Dart
Java
Chat (Trò chuyện)
Python
Node.js
Go
Vỏ
Kotlin
Swift
Dart
Java
Nội dung nghe nhìn cùng dòng
Python
Node.js
Go
Vỏ
Kotlin
Swift
Dart
Java
Video
Python
Node.js
Go
Vỏ
Python
Bộ nhớ đệm
Python
Node.js
Go
Hướng dẫn hệ thống
Python
Node.js
Go
Kotlin
Swift
Dart
Java
Công cụ
Python
Node.js
Kotlin
Swift
Dart
Java
Nội dung phản hồi
Phản hồi của models.countTokens
.
Phương thức này trả về tokenCount
của mô hình cho prompt
.
Nếu thành công, phần nội dung phản hồi sẽ chứa dữ liệu có cấu trúc sau:
totalTokens
integer
Số lượng mã thông báo mà Model
tạo mã thông báo prompt
. Luôn luôn là số âm.
Biểu diễn dưới dạng JSON |
---|
{ "totalTokens": integer } |
GenerateContentRequest
Yêu cầu tạo một lượt hoàn thành từ mô hình.
model
string
Bắt buộc. Tên của Model
được dùng để tạo hoàn thành.
Định dạng: name=models/{model}
.
contents[]
object (Content
)
Bắt buộc. Nội dung của cuộc trò chuyện hiện tại với mô hình.
Đối với truy vấn một lượt, đây chỉ là một phiên bản. Đối với các truy vấn nhiều lượt như trò chuyện, đây là trường lặp lại chứa nhật ký trò chuyện và yêu cầu mới nhất.
tools[]
object (Tool
)
Không bắt buộc. Danh sách Tools
mà Model
có thể dùng để tạo phản hồi tiếp theo.
Tool
là một đoạn mã cho phép hệ thống tương tác với các hệ thống bên ngoài để thực hiện một hành động hoặc một tập hợp hành động, nằm ngoài phạm vi kiến thức và phạm vi của Model
. Các Tool
được hỗ trợ là Function
và codeExecution
. Hãy tham khảo hướng dẫn Gọi hàm và Thực thi mã để tìm hiểu thêm.
toolConfig
object (ToolConfig
)
Không bắt buộc. Cấu hình công cụ cho mọi Tool
được chỉ định trong yêu cầu. Tham khảo Hướng dẫn gọi hàm để xem ví dụ về cách sử dụng.
safetySettings[]
object (SafetySetting
)
Không bắt buộc. Danh sách các thực thể SafetySetting
riêng biệt dùng để chặn nội dung không an toàn.
Thay đổi này sẽ được thực thi trên GenerateContentRequest.contents
và GenerateContentResponse.candidates
. Không được có nhiều hơn một chế độ cài đặt cho mỗi loại SafetyCategory
. API này sẽ chặn mọi nội dung và phản hồi không đáp ứng ngưỡng do các chế độ cài đặt này đặt ra. Danh sách này ghi đè các chế độ cài đặt mặc định cho từng SafetyCategory
được chỉ định trong phần safetySettings. Nếu không có SafetySetting
cho một SafetyCategory
nhất định được cung cấp trong danh sách, API sẽ sử dụng chế độ cài đặt an toàn mặc định cho danh mục đó. Các danh mục gây hại HARM_CATEGORY_HATE_SPEECH, HARM_CATEGORY_SEXUALLY_EXPLICIT, HARM_CATEGORY_DANGEROUS_CONTENT, HARM_CATEGORY_HARASSMENT được hỗ trợ. Tham khảo hướng dẫn để biết thông tin chi tiết về các chế độ cài đặt an toàn có sẵn. Ngoài ra, hãy tham khảo Hướng dẫn về an toàn để tìm hiểu cách tích hợp những yếu tố cần cân nhắc về an toàn vào các ứng dụng dựa trên AI.
systemInstruction
object (Content
)
Không bắt buộc. (Các) hướng dẫn hệ thống dành cho nhà phát triển. Hiện tại, chỉ có văn bản.
generationConfig
object (GenerationConfig
)
Không bắt buộc. Các tuỳ chọn cấu hình để tạo mô hình và đầu ra.
cachedContent
string
Không bắt buộc. Tên của nội dung được lưu vào bộ nhớ đệm để dùng làm ngữ cảnh cung cấp thông tin dự đoán. Định dạng: cachedContents/{cachedContent}
Biểu diễn dưới dạng JSON |
---|
{ "model": string, "contents": [ { object ( |