ดูคำแนะนำโดยละเอียดเกี่ยวกับการนับโทเค็นโดยใช้ Gemini API รวมถึงวิธีนับรูปภาพ เสียง และวิดีโอได้ในคู่มือการนับโทเค็น และสูตรตำราอาหารที่แนบมาด้วย
เมธอด: collections.countTokens
- ปลายทาง
- พารามิเตอร์เส้นทาง
- เนื้อหาคำขอ
- เนื้อหาการตอบกลับ
- ขอบเขตการให้สิทธิ์
- ตัวอย่างคำขอ
- GenerateContentRequest
เรียกใช้เครื่องมือแปลงข้อมูลเป็นโทเค็นของโมเดลบนอินพุต Content
และแสดงผลจำนวนโทเค็น โปรดดูคู่มือโทเค็นเพื่อดูข้อมูลเพิ่มเติมเกี่ยวกับโทเค็น
ปลายทาง
โพสต์
https://generativelanguage.googleapis.com/v1beta/{model=models/*}:countTokens
พารามิเตอร์เส้นทาง
model
string
ต้องระบุ ชื่อทรัพยากรของโมเดล ซึ่งจะเป็นรหัสให้โมเดลใช้งาน
ชื่อนี้ควรตรงกับชื่อโมเดลที่แสดงผลโดยเมธอด models.list
รูปแบบ: models/{model}
จะอยู่ในรูปแบบ models/{model}
เนื้อหาของคำขอ
เนื้อหาของคำขอมีข้อมูลที่มีโครงสร้างต่อไปนี้
contents[]
object (Content
)
ไม่บังคับ อินพุตที่ระบุให้กับโมเดลเป็นพรอมต์ ระบบจะไม่สนใจช่องนี้เมื่อตั้งค่า generateContentRequest
generateContentRequest
object (GenerateContentRequest
)
ไม่บังคับ อินพุตโดยรวมที่ให้กับ Model
ซึ่งรวมถึงข้อความแจ้งและข้อมูลการบังคับควบคุมโมเดลอื่นๆ เช่น วิธีการของระบบ และ/หรือการประกาศฟังก์ชันสำหรับการเรียกใช้ฟังก์ชัน Model
วินาที/Content
และ generateContentRequest
ไม่เกี่ยวข้องกัน คุณจะส่ง Model
+ Content
หรือ generateContentRequest
ก็ได้ แต่จะส่งทั้ง 2 รายการไม่ได้
ตัวอย่างคำขอ
ข้อความ
Python
Node.js
Go
เปลือกหอย
Kotlin
Swift
Dart
Java
แชท
Python
Node.js
Go
เปลือกหอย
Kotlin
Swift
Dart
Java
สื่อในบรรทัด
Python
Node.js
Go
เปลือกหอย
Kotlin
Swift
Dart
Java
วิดีโอ
Python
Node.js
Go
เปลือกหอย
Python
แคช
Python
Node.js
Go
คำแนะนำของระบบ
Python
Node.js
Go
Kotlin
Swift
Dart
Java
เครื่องมือ
Python
Node.js
Kotlin
Swift
Dart
Java
เนื้อหาการตอบกลับ
การตอบกลับจาก models.countTokens
โดยจะแสดงผล tokenCount
ของโมเดลสำหรับ prompt
หากทำสำเร็จ เนื้อหาการตอบกลับจะมีข้อมูลซึ่งมีโครงสร้างดังต่อไปนี้
totalTokens
integer
จำนวนโทเค็นที่ Model
แปลง prompt
เป็นโทเค็น ไม่ติดลบเสมอ
การแสดง JSON |
---|
{ "totalTokens": integer } |
GenerateContentRequest
คำขอเพื่อสร้างเสร็จสมบูรณ์จากโมเดล
model
string
ต้องระบุ ชื่อของ Model
ที่จะใช้ในการสร้างการดำเนินการให้เสร็จสมบูรณ์
รูปแบบ: name=models/{model}
contents[]
object (Content
)
ต้องระบุ เนื้อหาของการสนทนาปัจจุบันกับโมเดล
สำหรับการค้นหาแบบเลี้ยวเดียว อินสแตนซ์นี้เป็นอินสแตนซ์เดียว สำหรับคำค้นหาแบบหลายรอบ เช่น แชท ช่องนี้จะเป็นช่องที่ซ้ำซึ่งมีประวัติการสนทนาและคำขอล่าสุด
tools[]
object (Tool
)
ไม่บังคับ รายการของ Tools
ที่ Model
อาจใช้เพื่อสร้างคำตอบถัดไป
Tool
คือโค้ดที่ช่วยให้ระบบโต้ตอบกับระบบภายนอกเพื่อดำเนินการหรือชุดการทำงานนอกเหนือจากความรู้และขอบเขตของ Model
ได้ Tool
ที่รองรับคือ Function
และ codeExecution
โปรดดูข้อมูลเพิ่มเติมที่การเรียกใช้ฟังก์ชันและคำแนะนำการเรียกใช้โค้ด
toolConfig
object (ToolConfig
)
ไม่บังคับ การกำหนดค่าเครื่องมือสำหรับ Tool
ที่ระบุไว้ในคำขอ ดูตัวอย่างการใช้งานได้ที่คู่มือการเรียกใช้ฟังก์ชัน
safetySettings[]
object (SafetySetting
)
ไม่บังคับ รายการอินสแตนซ์ SafetySetting
ที่ไม่ซ้ำกันสำหรับการบล็อกเนื้อหาที่ไม่ปลอดภัย
นโยบายนี้จะบังคับใช้ในวันที่ GenerateContentRequest.contents
และ GenerateContentResponse.candidates
ประเภท SafetyCategory
แต่ละประเภทไม่ควรมีการตั้งค่ามากกว่า 1 รายการ API จะบล็อกเนื้อหาและการตอบกลับทั้งหมดที่ไม่ตรงตามเกณฑ์ที่การตั้งค่าเหล่านี้กำหนดไว้ รายการนี้จะลบล้างการตั้งค่าเริ่มต้นสำหรับแต่ละ SafetyCategory
ที่ระบุใน safetySettings หากไม่มี SafetySetting
สำหรับ SafetyCategory
ที่ระบุในรายการ API จะใช้การตั้งค่าความปลอดภัยเริ่มต้นสำหรับหมวดหมู่นั้น สนับสนุนหมวดหมู่อันตราย HARM_CATEGORY_HATE_SPEECH, HARM_CATEGORY_SEXUALLY_EXPLICIT, HARM_CATEGORY_DANGEROUS_CONTENT, HARM_CATEGORY_HARASSMENT ดูข้อมูลโดยละเอียดเกี่ยวกับการตั้งค่าความปลอดภัยที่ใช้ได้ในคำแนะนำ นอกจากนี้ โปรดดูคำแนะนำด้านความปลอดภัยเพื่อดูวิธีนำข้อควรพิจารณาด้านความปลอดภัยไปใช้กับแอปพลิเคชัน AI ของคุณ
systemInstruction
object (Content
)
ไม่บังคับ นักพัฒนาแอปตั้งค่าคำสั่งของระบบ ขณะนี้มีเพียงข้อความเท่านั้น
generationConfig
object (GenerationConfig
)
ไม่บังคับ ตัวเลือกการกำหนดค่าสำหรับการสร้างและเอาต์พุตโมเดล
cachedContent
string
ไม่บังคับ ชื่อของเนื้อหาที่แคชเพื่อใช้เป็นบริบทในการแสดงการคาดคะเน รูปแบบ: cachedContents/{cachedContent}
การแสดง JSON |
---|
{ "model": string, "contents": [ { object ( |