カテゴリ:

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

AI_COUNT_TOKENS

注釈

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

指定された大規模言語モデルまたはタスク固有の関数のプロンプトのトークン数を返します。

構文

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

引数

必須:

関数名

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

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

input_text

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

オプション:

model_name

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

利用可能な LLM モデルのリストは リージョンの可用性 テーブルで利用可能です。ただし、現在すべてのモデルがサポートされているわけではありません。

AI_COMPLETE の場合、以下のモデルはサポートされていません。

  • claude-4-opus

  • claude-4-sonnet

  • claude-3-7-sonnet

  • claude-3-5-sonnet

  • openai-gpt-4.1

  • openai-o4-mini

AI_EMBED の場合、以下のモデルはサポートされていません。

  • snowflake-arctic-embed-l-v2.0-8k

categories

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

戻り値

所定のパラメーター値を使って計算された入力テキストのトークンの数である INTEGER 値。

使用上の注意

  • 関数名とモデル名には小文字のみを使用します。

  • COUNT_TOKENS は、SNOWFLAKE.CORTEX 名前空間の LLM 関数または微調整済みモデルでは機能しません。「AI_」で始まる関数名を指定する必要があります。

  • COUNT_TOKENS はテキストのみを受け入れ、画像や音声入力は受け入れません。

  • 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