カテゴリ:

:doc:`/sql-reference/functions-string`(AI 関数)

AI_COUNT_TOKENS

注釈

AI_COUNT_TOKENS は COUNT_TOKENS (SNOWFLAKE.CORTEX) の更新されたバージョンです。最新の関数については、 AI_COUNT_TOKENS を使用してください。

指定された大規模言語モデルまたはタスク固有の関数のプロンプトのトークン数の推定値を返します。モデル名、カテゴリやラベルなど、トークン数に影響する追加の入力を受け取ることができる関数の場合は、それらの入力も指定できます。

構文

構文は、使用される関数によって異なります。通常、関数名、該当する場合はモデル名、入力テキスト、トークン数に影響する追加オプションを渡します。

AI_COUNT_TOKENS(<function_name>, <input_text> )
AI_COUNT_TOKENS( <function_name>, <model_name> , <input_text> )
AI_COUNT_TOKENS( <function_name>, <input_text>, <options> )
AI_COUNT_TOKENS( <function_name>, <model_name>, <input_text>, <options> )
Copy

AI_COUNT_TOKENS は、一部の関数に対して特定の構文バリエーションを使用します。例:

AI_COUNT_TOKENS( 'ai_similarity', <input_text_1>, <input_text_2>, <options> )
AI_COUNT_TOKENS( 'ai_classify', <input_text>, <categories> )
AI_COUNT_TOKENS( 'ai_translate', <input_text>, <source_language>, <target_language> )
Copy

関数固有の使用パターンについては、 をご参照ください。

引数

必須:

関数名

'ai_complete' または 'ai_sentiment' など、トークン数の基にしたい関数の名前を含む文字列。関数の名前は「ai_」で始まり、小文字のみを使用する必要があります。

サポートされている関数の完全なリストは、リージョンの可用性 テーブルで入手できます。

input_text または input_text_1input_text_2

トークンを数えるテキストを入力。

オプション:

model_name

トークンのコンテンツの基にしたいモデルの名前を含む文字列。AI_COMPLETE または AI_EMBED など、function_name で指定される関数で使用するモデルを選択する必要がある場合は、必須。

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

categories

このデータを必要とする関数の場合は、使用する1つ以上のカテゴリまたはラベルを指定する VARIANT 値の配列。カテゴリは入力トークン数に含まれます。

options

関数が入力を処理する方法に影響する追加オプションを指定する VARIANT 。AI_SIMILARITY など、2つのテキスト入力を受け取る関数の場合、オプションはモデルの指定に使用されます。

戻り値

所定のパラメーター値を使って計算された入力テキストのトークンの数である 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 はコンピュートコストのみが発生し、トークン数に基づいて請求されることはありません。

  • COUNT_TOKENS は、特定のリージョンで利用できないモデルであっても、すべてのリージョンで利用可能です。

AI_COMPLETE の例

次の SQL ステートメントは、AI_COMPLETE と llama3.3-70b モデルのプロンプトについてトークン数をカウントします。

SELECT AI_COUNT_TOKENS('ai_complete', 'llama3.3-70b', 'Summarize the insights from this
call transcript in 20 words: "I finally splurged on these after months of hesitation about
the price, and I\'m mostly impressed. The Nulu fabric really is as buttery-soft as everyone says,
and they\'re incredibly comfortable for yoga and lounging. The high-rise waistband stays put
and doesn\'t dig in, which is rare for me. However, I\'m already seeing some pilling after
just a few wears, and they definitely require gentle care. They\'re also quite delicate -
I snagged them slightly on my gym bag zipper. Great for low-impact activities, but I wouldn\'t
recommend for high-intensity workouts. Worth it for the comfort factor"');
Copy

応答:

158

AI_EMBED の例

次の SQL ステートメントは、AI_EMBED 関数と nv-embed-qa-4' モデルを使用して埋め込まれているテキストについてトークン数をカウントします。

SELECT AI_COUNT_TOKENS('ai_embed', 'nv-embed-qa-4', '"I finally splurged on these after months
of hesitation about the price, and I\'m mostly impressed. The Nulu fabric really is as buttery-soft
as everyone says, and they\'re incredibly comfortable for yoga and lounging. The high-rise waistband
stays put and doesn\'t dig in, which is rare for me. However, I\'m already seeing some pilling after
just a few wears, and they definitely require gentle care. They\'re also quite delicate - I snagged
them slightly on my gym bag zipper. Great for low-impact activities, but I wouldn\'t recommend for
high-intensity workouts. Worth it for the comfort factor"');
Copy

応答:

142

AI_CLASSIFY 例

この例では、与えられた入力とラベルを使用して、テキスト分類に必要な入力トークンの合計数を計算します。

SELECT AI_COUNT_TOKENS('ai_classify',
  'One day I will see the world and learn to cook my favorite dishes',
  [
      {'label': 'travel'},
      {'label': 'cooking'},
      {'label': 'reading'},
      {'label': 'driving'}
  ]
);
Copy

応答:

187

次の例では、前の例にラベルごとの説明とタスク全体の説明を追加します。

SELECT AI_COUNT_TOKENS('ai_classify',
  'One day I will see the world and learn to cook my favorite dishes',
  [
    {'label': 'travel', 'description': 'content related to traveling'},
    {'label': 'cooking','description': 'content related to food preparation'},
    {'label': 'reading','description': 'content related to reading'},
    {'label': 'driving','description': 'content related to driving a car'}
  ],
  {
    'task_description': 'Determine topics related to the given text'
  };
Copy

応答:

254

次の例では、前の2つの例にラベルの例を追加して構築しています。

SELECT AI_COUNT_TOKENS('ai_classify',
  'One day I will see the world and learn to cook my favorite dishes',
  [
    {'label': 'travel', 'description': 'content related to traveling'},
    {'label': 'cooking','description': 'content related to food preparation'},
    {'label': 'reading','description': 'content related to reading'},
    {'label': 'driving','description': 'content related to driving a car'}
  ],
  {
    'task_description': 'Determine topics related to the given text',
    'examples': [
      {
        'input': 'i love traveling with a good book',
        'labels': ['travel', 'reading'],
        'explanation': 'the text mentions traveling and a good book which relates to reading'
      }
    ]
  }
);
Copy

応答:

298

AI_SENTIMENT 例

次の SQL ステートメントは、AI_SENTIMENT 関数を使用してセンチメント分析の対象となるテキストについてトークン数をカウントします。

SELECT AI_COUNT_TOKENS('ai_sentiment',
  'This place makes the best truffle pizza in the world! Too bad I cannot afford it');
Copy

応答:

139

次の例では、前の例にラベルを追加します。

SELECT AI_COUNT_TOKENS('ai_sentiment',
  'This place makes the best truffle pizza in the world! Too bad I cannot afford it',
  [
    {'label': 'positive'},
    {'label': 'negative'},
    {'label': 'neutral'}
  ]
);
Copy

応答:

148

AI_SIMILARITY examples

The following SQL statement counts the number of tokens in an AI_SIMILARITY call that uses the default model.

SELECT AI_COUNT_TOKENS('ai_similarity',
  'The plot is fast and the characters feel real. This book kept me awake all night
  because the mystery is so deep. I love how the author  handles the ending. It is a
  great read for anyone who likes suspense.',
  'The story is quick and the people feel true. This novel kept me awake all night
  because the puzzle is so big. I love how the writer handles the finale. It is a
  solid choice for anyone who enjoys suspense.');
Copy

応答:

101

The following SQL statement counts the number of tokens in an AI_SIMILARITY that uses the e5-base-v2 model:

SELECT AI_COUNT_TOKENS('ai_similarity',
  'The plot is fast and the characters feel real. This book kept me awake all night
  because the mystery is so deep. I love how the author handles the ending. It is a
  great read for anyone who likes suspense.',
  'The story is quick and the people feel true. This novel kept me awake all night
  because the puzzle is so big. I love how the writer handles the finale. It is a
  solid choice for anyone who enjoys suspense.', {'model': 'e5-base-v2'})
Copy

応答:

92

AI_TRANSLATE example

The following SQL statement counts the number of tokens used by AI_TRANSLATE when translating text from English to German.

SELECT AI_COUNT_TOKENS('ai_translate',
  'The plot is fast and the characters feel real. This book kept me awake all night
  because the mystery is so deep. I love how the author handles the ending. It is a
  great read for anyone who likes suspense.', 'en', 'de');
Copy

応答:

51