Catégories :

Fonctions de chaîne et fonctions binaires (Fonctions AI)

AI_COUNT_TOKENS

Note

AI_COUNT_TOKENS est la version actualisée de COUNT_TOKENS (SNOWFLAKE.CORTEX). Pour obtenir les dernières fonctionnalités, utilisez AI_COUNT_TOKENS.

Renvoie une estimation du nombre de jetons dans une invite pour le grand modèle de langage spécifié ou la fonction spécifique à la tâche spécifiée. Pour les fonctions qui peuvent prendre des entrées supplémentaires qui affectent le nombre de jetons, telles que le nom du modèle ou les catégories/libellés, ces entrées peuvent également être spécifiées.

Syntaxe

La syntaxe peut varier selon la fonction utilisée. En général, vous transmettez le nom de la fonction, le nom du modèle le cas échéant, le texte d’entrée et toute option supplémentaire qui affecte le nombre de jetons.

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 utilise des variantes syntaxiques spécifiques pour certaines fonctions. Par exemple :

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

Voir Exemples pour les modèles d’utilisation spécifiques à la fonction.

Arguments

Obligatoire :

nom_fonction

Chaîne contenant le nom de la fonction sur laquelle vous souhaitez baser le nombre de jetons, par exemple 'ai_complete' ou 'ai_sentiment'. Le nom de la fonction doit commencer par « ai_ » et utiliser uniquement des lettres minuscules.

Une liste complète des fonctions prises en charge est disponible dans la table Disponibilité régionale.

input_text ou input_text_1 input_text_2

Saisisser du texte pour compter les jetons.

Facultatif :

model_name

Chaîne contenant le nom du modèle sur lequel vous souhaitez baser le contenu du jeton. Requis si la fonction spécifiée par function_name vous oblige à choisir le modèle à utiliser, tel que AI_COMPLETE ou AI_EMBED.

A list of available LLM models is available in the Disponibilité régionale table. However, not all models are currently supported. Snowflake intends to add support for additional models over time.

Pour AI_COMPLETE, les modèles suivants ne sont pas pris en charge :

  • claude-4-opus

  • claude-4-sonnet

  • claude-3-7-sonnet

  • claude-3-5-sonnet

  • openai-gpt-4.1

  • openai-o4-mini

categories

Un tableau de valeurs VARIANT qui spécifient une ou plusieurs catégories ou étiquettes à utiliser, pour les fonctions qui nécessitent ces données. Les catégories sont incluses dans le nombre de jetons d’entrée.

options

Une VARIANT qui spécifie des options supplémentaires qui affectent la manière dont la fonction traite l’entrée. Pour les fonctions qui prennent deux entrées de texte, comme AI_SIMILARITY, les options sont utilisées pour spécifier le modèle.

Renvoie

Une valeur INTEGER qui est le nombre de jetons de texte d’entrée calculé à l’aide des valeurs de paramètre données.

Notes sur l’utilisation

  • 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 n’entraîne que des coûts de calcul et ne facture pas en fonction du nombre de jetons.

  • COUNT_TOKENS est disponible dans toutes les régions, même pour les modèles non disponibles dans une région donnée.

Exemples

Exemple AI_COMPLETE

L’instruction SQL suivante compte le nombre de jetons dans une invite pour AI_COMPLETE et le modèle 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

Réponse :

158

Exemple AI_EMBED

L’instruction SQL suivante compte le nombre de jetons dans le texte incorporé à l’aide de la fonction AI_EMBED et du modèle 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

Réponse :

142

Exemples AI_CLASSIFY

Cet exemple calcule le nombre total de jetons d’entrée requis pour la classification de texte avec l’entrée et les étiquettes données :

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

Réponse :

187

L’exemple suivant ajoute des descriptions par étiquette et une description globale des tâches à l’exemple précédent :

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

Réponse :

254

L’exemple suivant s’appuie sur les deux exemples précédents en ajoutant des exemples d’étiquettes :

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

Réponse :

298

Exemples AI_SENTIMENT

L’instruction SQL suivante compte le nombre de jetons dans le texte en cours d’analyse pour le sentiment en utilisant la fonction 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

Réponse :

139

L’exemple suivant ajoute des étiquettes à l’exemple précédent :

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

Réponse :

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

Réponse :

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

Réponse :

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

Réponse :

51