类别:

:doc:`/sql-reference/functions-string`(大型语言模型)

CLASSIFY_TEXT (SNOWFLAKE.CORTEX)

将自由形式的文本分类到您提供的类别中。

语法

SNOWFLAKE.CORTEX.CLASSIFY_TEXT( <input> , <list_of_categories>, [ <options> ] )
Copy

实参

必填:

input

要分类的字符串。输入字符串区分大小写。对于使用不同大小写的同一字符串,可能会得到不同的结果。

list_of_categories

表示类别的数组。必须包含最少两个、最多 100 个唯一类别。类别区分大小写。如果不满足这些要求,函数将返回错误。有关错误的完整列表,请参阅 错误条件

类别可以是简单字符串或 SQL 对象;所有类别必须是同一类型。使用对象时,可以提供每个类别的描述和示例,提供有助于提高分类准确性的上下文。不需要为每个类别提供描述或示例;您可以自由地为每个类别提供描述、示例、两者或两者都不提供。

  • label:类别的名称。此键是必需的。

  • description:类别的描述。描述长度不应超过约 25 个单词(1-2 句话)。此键是可选的。

  • examples:代表该类别的示例数组。通常情况下,所需的示例不超过 5 个,但每个类别限制为 20 个示例。每个类别的示例数量不必相同。此键是可选的。

备注

描述和示例算作输入词元,会增加分类操作的成本。在 成本注意事项 中阅读更多信息。

可选:

options

包含分类操作的可选配置(键/值对)的对象。目前,唯一可用的键是:

  • task_description:包含文本分类任务的简短解释的字符串。任务描述长度不应超过 50 个单词(3-4 句话)。

返回

返回包含 JSON 对象的字符串。JSON 对象包含输入提示被分类的类别。如果给出无效实参,则返回错误。有关错误的详细信息,请参阅 错误条件

访问控制要求

用户必须使用已被授予 SNOWFLAKE.CORTEX_USER 数据库角色 的角色。有关此权限的更多信息,请参阅 所需权限

使用说明

为了获得最佳性能,请遵循以下指导原则:

  • 输入和类别使用纯英语文本。

  • 限制输入文本中非纯英语的文本数量。例如,尝试限制文本输入中的代码片段或日志等内容。

  • 文本不应包含非开源的代码或格式(公司特定语言、专有格式等)。函数不会返回错误,但结果可能与您预期的不同。

  • 不要在类别标签中使用缩写、特殊字符或行话。

  • 类别应具有描述性。例如,使用 Xa4s3category 1 等类别不会产生好的结果。

  • 类别应相互排斥。

  • 当输入文本和类别之间的关系不明确或差别较小时,添加清晰的任务描述可以提高准确性。

  • 在描述不明确或选择特定标签时应遵循特定逻辑的情况下,添加标签描述可以提高准确性。在撰写描述时,应侧重于某一标签区别于其他标签的关键方面。

  • 示例有助于提高准确性。

示例

使用必需的实参

这些示例说明了如何仅搭配必需的实参来使用 CLASSIFY_TEXT 函数。

以下示例将提示分为两个类别之一,travelcooking

SELECT SNOWFLAKE.CORTEX.CLASSIFY_TEXT('One day I will see the world', ['travel', 'cooking']);
Copy
{
  "label": "travel"
}

以下示例创建了一个表 text_classification_table,其中包含文本列以及该文本的可能类别列。对表的每一行调用 CLASSIFY_TEXT 函数,可对文本列中的字符串进行分类。

CREATE OR REPLACE TEMPORARY TABLE text_classification_table AS
SELECT 'France' AS input, ['North America', 'Europe', 'Asia'] AS classes
UNION ALL
SELECT 'Singapore', ['North America', 'Europe', 'Asia']
UNION ALL
SELECT 'one day I will see the world', ['travel', 'cooking', 'dancing']
UNION ALL
SELECT 'my lobster bisque is second to none', ['travel', 'cooking', 'dancing'];

SELECT input,
       classes,
       SNOWFLAKE.CORTEX.CLASSIFY_TEXT(input, classes)['label'] as classification
FROM text_classification_table;
Copy

使用可选实参

这些示例说明了如何将 CLASSIFY_TEXT 函数与类别描述和示例和/或任务描述结合使用。

以下示例将提示分类为三个类别(旅行、烹饪或健身)之一,仅提供任务描述:

SELECT SNOWFLAKE.CORTEX.CLASSIFY_TEXT(
  'When I am not at work, I love creating recipes using every day ingredients',
  ['travel', 'cooking', 'fitness'],
  {
    'task_description': 'Return a classification of the Hobby identified in the text'
  }
);
Copy
{
  "label": "cooking"
}

以下示例使用所有选项将提示分类为旅行、烹饪或健身类别之一。

SELECT SNOWFLAKE.CORTEX.CLASSIFY_TEXT(
  'I love running every morning before the world wakes up',
  [{
    'label': 'travel',
    'description': 'Hobbies related to going from one place to another',
    'examples': ['I like flying to Europe', 'Every summer we go to Italy' , 'I love traveling to learn new cultures']
  },{
    'label': 'cooking',
    'description': 'Hobbies related to preparing food',
    'examples': ['I like learning about new ingredients', 'You must bring your soul to the recipe' , 'Baking is my therapy']
    },{
    'label': 'fitness',
    'description': 'Hobbies related to being active and healthy',
    'examples': ['I cannot live without my Strava app', 'Running is life' , 'I go to the Gym every day']
    }],
  {'task_description': 'Return a classification of the Hobby identified in the text'})
Copy
{
  "label": "fitness"
}

以下示例使用所有选项将提示分类为三个类别(旅行、烹饪或健身)之一。但是,有些类别的描述或示例被省略,示例数量也各不相同。

SELECT SNOWFLAKE.CORTEX.CLASSIFY_TEXT(
  'I love running every morning before the world wakes up',
  [{
    'label': 'travel',
    'description': 'Hobbies related to going from one place to another',
    'examples': ['I like flying to Europe']
  },{
    'label': 'cooking',
    'examples': ['I like learning about new ingredients', 'You must bring your soul to the recipe' , 'Baking is my therapy']
    },{
    'label': 'fitness',
    'description': 'Hobbies related to being active and healthy'
    }],
  {'task_description': 'Return a classification of the Hobby identified in the text'})
Copy
{
  "label": "fitness"
}
语言: 中文