curl --request POST \
--url https://api.moonshot.cn/v1/tokenizers/estimate-token-count \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "kimi-k2.5",
"messages": [
{
"role": "system",
"content": "<string>",
"name": null,
"partial": false
}
]
}
'{
"data": {
"total_tokens": 80
}
}估算给定消息和模型所需的 Token 数量。输入结构与聊天补全几乎相同。
curl --request POST \
--url https://api.moonshot.cn/v1/tokenizers/estimate-token-count \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "kimi-k2.5",
"messages": [
{
"role": "system",
"content": "<string>",
"name": null,
"partial": false
}
]
}
'{
"data": {
"total_tokens": 80
}
}estimate-token-count 的输入结构体和 chat completion 基本一致。Documentation Index
Fetch the complete documentation index at: https://platform.kimi.com/docs/llms.txt
Use this file to discover all available pages before exploring further.
纯文本调用示例
curl 'https://api.moonshot.cn/v1/tokenizers/estimate-token-count' \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $MOONSHOT_API_KEY" \
-d '{
"model": "kimi-k2.6",
"messages": [
{
"role": "system",
"content": "你是 Kimi,由 Moonshot AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言。"
},
{
"role": "user",
"content": "你好,我叫李雷,1+1等于多少?"
}
]
}'
包含视觉的调用示例
import os
import base64
import json
import requests
api_key = os.environ.get("MOONSHOT_API_KEY")
endpoint = "https://api.moonshot.cn/v1/tokenizers/estimate-token-count"
image_path = "image.png"
with open(image_path, "rb") as f:
image_data = f.read()
# 我们使用标准库 base64.b64encode 函数将图片编码成 base64 格式的 image_url
image_url = f"data:image/{os.path.splitext(image_path)[1].lstrip('.')};base64,{base64.b64encode(image_data).decode('utf-8')}"
payload = {
"model": "kimi-k2.6",
"messages": [
{
"role": "system",
"content": "你是 Kimi,由 Moonshot AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言。"
},
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": image_url,
},
},
{
"type": "text",
"text": "请描述图片的内容。",
},
],
}
]
}
response = requests.post(
endpoint,
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
},
data=json.dumps(payload)
)
print(response.json())
data.total_tokens 作为计算结果。模型 ID
kimi-k2.6, kimi-k2.5, kimi-k2-0905-preview, kimi-k2-0711-preview, kimi-k2-turbo-preview, moonshot-v1-8k, moonshot-v1-32k, moonshot-v1-128k, moonshot-v1-auto, moonshot-v1-8k-vision-preview, moonshot-v1-32k-vision-preview, moonshot-v1-128k-vision-preview 包含迄今为止对话的消息列表。每个元素格式为 {"role": "user", "content": "你好"}。role 支持 system、user、assistant 其一,content 不得为空
Show child attributes
Token 数量估算结果
Show child attributes
此页面对您有帮助吗?