- 类别:
:doc:`/sql-reference/functions-string`(AI 函数)
AI_COUNT_TOKENS¶
备注
AI_COUNT_TOKENS 是 COUNT_TOKENS (SNOWFLAKE.CORTEX) 的更新版本。要获得最新功能,请使用 AI_COUNT_TOKENS。
Returns an estimate of the number of tokens in a prompt for the specified large language model or task-specific function. For functions that can take additional inputs that affect token count, such as model name or categories/labels, those inputs can also be specified.
语法¶
The syntax can vary based on the function used. In general, you pass the function name, model name if applicable, input text, and any additional options that affect token count.
AI_COUNT_TOKENS uses specific syntax variations for some functions. For example:
See Examples for function specific usage patterns.
实参¶
必填:
function_name包含要作为词元计数基础的函数名称的字符串,例如
'ai_complete'或'ai_sentiment'。函数的名称必须以“ai_”开头,并且只能使用小写字母。可用性 表提供了支持的函数的完整列表。
input_textorinput_text_1,input_text_2输入文本,计算其中的令牌。
可选:
model_name包含要作为词元内容基础的模型名称的字符串。如果
function_name指定的函数要求您选择要使用的模型(例如 AI_COMPLETE 或 AI_EMBED),则为必需项。A list of available LLM models is available in the 可用性 table. However, not all models are currently supported. Snowflake intends to add support for additional models over time.
AI_COMPLETE 不支持以下模型:
claude-4-opus
claude-4-sonnet
claude-3-7-sonnet
claude-3-5-sonnet
openai-gpt-4.1
openai-o4-mini
categoriesVARIANT 值的数组,为需要此数据的函数指定要使用的一个或多个类别或标签。类别计入输入词元计数。
optionsA VARIANT that specifies additional options that affect how the function processes the input. For functions that take two text inputs, such as AI_SIMILARITY, options are used to specify the model.
返回¶
INTEGER 值,使用给定参数值计算的输入文本词元数。
使用说明¶
Although function names are usually written in all uppercase, use only lowercase letters in function and model names.
COUNT_TOKENS does not work with LLM functions in the SNOWFLAKE.CORTEX namespace or with fine-tuned models. You must specify a function name that begins with "ai_".
COUNT_TOKENS accepts only text, not image, audio, or video inputs.
COUNT_TOKENS only incurs compute costs and does not bill based on token count.
即使某些模型在特定区域不可用,COUNT_TOKENS 也可在所有区域使用。
示例¶
AI_COMPLETE 示例¶
以下 SQL 语句计算 AI_COMPLETE 和 llama3.3-70b 模型提示中的词元数:
响应:
AI_EMBED 示例¶
以下 SQL 语句计算使用 AI_EMBED 函数和 nv-embed-qa-4' 模型嵌入的文本中的词元数:
响应:
AI_CLASSIFY 示例¶
此示例计算使用给定输入和标签进行文本分类所需的输入词元总数:
响应:
以下示例在上一个示例的基础上添加了每个标签的描述和总体任务描述:
响应:
以下示例在前两个示例的基础上添加了标签示例:
响应:
AI_SENTIMENT 示例¶
以下 SQL 语句计算使用 AI_SENTIMENT 函数对其进行情绪分析的文本中的词元数量:
响应:
以下示例为上一个示例添加了标签:
响应:
AI_SIMILARITY examples¶
The following SQL statement counts the number of tokens in an AI_SIMILARITY call that uses the default model.
响应:
The following SQL statement counts the number of tokens in an AI_SIMILARITY that uses the e5-base-v2 model:
响应:
AI_TRANSLATE example¶
The following SQL statement counts the number of tokens used by AI_TRANSLATE when translating text from English to German.
响应:
法律声明¶
请参阅 Snowflake AI 和 ML。