Categories:

半结构化和结构化数据函数 (Array/Object)

PROMPT

PROMPT 函数构造一个结构化的 OBJECT,其中包含一个模板字符串和一个实参列表。此对象用于动态格式化消息、构造结构化提示或存储格式化数据,以供进一步处理(例如通过 Cortex AI 函数)。

语法

SELECT PROMPT('<template_string>', <expr_1> [ , <expr_2>, ... ] )
    FROM <table>;

实参

必填:

template_string

一个包含编号占位符的字符串,例如 {0},其中数字至少为 0 且小于指定的表达式数。第一个表达式被替换为 {0},第二个表达式被替换为 {1},依此类推。

expr_1 [ , expr_2, ... ]

表达式的值最终被替换为模板字符串,以代替编号占位符。这些可以是列名或其他表达式。值可以是可强制转换为字符串或 FILE 的任何类型(例如,VARCHAR、NUMBER 等)。

返回

具有以下结构的 SQL OBJECT:

{
  'template': '<template_string>',
  'args': ARRAY(<value_1>, <value_2>, ...)
}

args 数组包含 PROMPT 函数调用中指定的表达式的值。

使用说明

  • PROMPT 本身不执行任何字符串格式。它旨在构造一个供 Cortex AI 函数使用的对象。

  • It is an error to use a placeholder in the template string that does not have a corresponding expression, but it is not an error to provide expressions that are not used in the template string.

示例

基本用法

SELECT PROMPT('Hello, {0}! Today is {1}.', 'Alice', 'Monday');

输出:

{
    'template': 'Hello, {0}! Today is {1}.',
    'args': ['Alice', 'Monday']
}

Use with Cortex AI_FILTER

WITH reviews AS (
    SELECT 'Wow... Loved this place.' AS review, 5 AS rating
    UNION ALL
    SELECT 'Crust is not good.', 2 AS rating
)
SELECT * FROM reviews
WHERE AI_FILTER(PROMPT('The reviewer enjoyed the restaurant: {0}, Rating: {1}', review, rating));

与 Cortex COMPLETE 和 FILE 列结合使用

AI_COMPLETE('claude-4-sonnet',
    PROMPT('Classify the input image {0} in no more than 2 words. Respond in JSON', img_file)) AS image_classification
FROM image_table;

有关更多示例,请参阅 AI_COMPLETE(提示对象)