Cortex Analyst 中的自定义指令

自定义指令让您可以更好地控制 SQL 生成。使用自然语言,可以明确告知 Cortex Analyst 如何从语义模型 YAML 文件生成 SQL 查询。例如,使用自定义指令来告知 Cortex Analyst 您所说的 performancefinancial year 是什么意思。这样一来,您可以通过合并自定义逻辑或其他元素来提高生成的 SQL 的准确性。

为了实现更精细的控制,您还可以为 SQL 生成管道中的各模块指定自定义指令。有关更多信息,请参阅 模块自定义指令

自定义指令的工作原理

Cortex Analyst 将 custom_instructions 字段引入语义模型 YAML 文件。此字段允许您将定义修改或添加应用于 SQL 生成。

有关语义模型语法的更多信息,请参阅 Cortex Analyst 语义模型规范

示例

要探索自定义指令的可能用例,请考虑以下示例。

格式化数据输出

确保输出中的所有数字都四舍五入到两位小数。

语义模型 YAML 文件中的 custom_instructions 字段

custom_instructions: "Ensure that all numeric columns are rounded to 2 decimal points in the output."
Copy

生成的 SQL 查询

SELECT
  ROUND(column_name, 2) AS column_name,
  ...
FROM
  your_table;
Copy

调整百分比

自动将百分比或比率计算乘以 100 以保持一致性。

语义模型 YAML 文件中的 custom_instructions 字段

custom_instructions: "For any percentage or rate calculation, multiply the result by 100."
Copy

生成的 SQL 查询

SELECT
  (column_a / column_b) * 100 AS percentage_rate,
  ...
FROM
  your_table;
Copy

添加默认筛选器

如果用户未指定筛选器,则应用筛选器(例如默认为去年)。

语义模型 YAML 文件中的 custom_instructions 字段

custom_instructions: "If no date filter is provided, apply a filter for the last year."
Copy

生成的 SQL 查询

SELECT
  ...
FROM
  your_table
WHERE
  date_column >= DATEADD(YEAR, -1, CURRENT_DATE);
Copy

关联列筛选器

根据用户输入对相关列应用其他筛选器。

语义模型 YAML 文件中的 custom_instructions 字段

custom_instructions: "If a filter is applied on column X, ensure that the same filter is applied to dimension Y."
Copy

生成的 SQL 查询

SELECT
  ...
FROM
  your_table
WHERE
  column_x = 'filter_value' AND
  dimension_y = 'filter_value';
Copy

模块自定义指令

module_custom_instructions 密钥设置在语义模型的顶层,为 SQL 生成管道中的特定组件定义自定义指令。此功能对于以下用例很有用:

  • 在生成 SQL 之前,定义影响用户问题解释方式的逻辑

  • 为分析师工作流程的不同部分维护单独的结构更清晰的指令

  • 随着使用量的增长,从现有 custom_instructions 过渡到模块化更明显的格式

module_custom_instructions 当前支持以下组件:

  • question_categorization:定义 Cortex Analyst 应如何对用户问题进行分类(例如,屏蔽某些主题或指导用户行为)。

  • sql_generation:指定 SQL 应如何生成(例如,数据格式和筛选)。

这两个组件中一个或两个组件的说明可以在 module_custom_instructions 键下设置。

重要

将任何现有 sql_generation 组件迁移到 custom_instructions 组件,如以下示例所示。

迁移现有自定义指令

如果您的模型已有 custom_instructions 字段,请将其内容迁移到 module_custom_instructions 下的 sql_generation 字段。

之前:

custom_instructions: "Ensure that all numeric columns are rounded to 2 decimal points."
Copy

之后:

module_custom_instructions:
  sql_generation: |
     "Ensure that all numeric columns are rounded to 2 decimal points."
Copy

屏蔽有关特定主题的问题

您可以使用 question_categorization 组件来屏蔽有关特定主题的问题。例如,如果您想屏蔽有关用户的问题,则可以设置以下指令。Cortex Analyst,然后通过一条消息来拒绝有关用户的问题,告诉他们联系管理员。

module_custom_instructions:
  question_categorization: |
     Reject all questions asking about users. Ask users to contact their admin.
Copy

您也可以使用问题分类指令来询问缺失的细节。在以下示例中,如果用户询问有关用户的情况,但未指定产品类型,Cortex Analyst 会请用户提供产品类型。

module_custom_instructions:
  question_categorization: |
    - If the question asks for users without providing a product_type, consider this question UNCLEAR and ask the user to specify product_type.
Copy

最佳实践

具体明确。

明确描述修改;例如,“添加固定值为 42 的列”或“包括列 X 的总和计算”。

从小处着手。

从简单的修改开始,例如添加静态列或默认筛选器,然后再转向更复杂的方案。

预览生成的 SQL 查询。

确保指令按预期应用,并且生成的 SQL 查询正确。

逐步迭代。

随着您熟悉该功能,尝试更复杂的用例。

语言: 中文