对话式分析集成

本文档介绍了 Google Cloud Cortex Framework 如何与 BigQuery 中的对话式分析和 Conversational Analytics API 集成,以直接在已部署的 Cortex Framework 数据产品之上预配具有领域感知能力的 BigQuery 数据智能体。使用 uv run cortex-create-ca-agents CLI 工具,您可以声明式地配置、验证和同步自然语言数据智能体,将其作为代码与 config/config.yaml 一起管理。在 BigQuery 中预配数据智能体后,您还可以在 Gemini 中发布和注册这些智能体,以便组织中的业务用户可以发现和查询它们。如需了解详情,请参阅 Gemini 集成。

主要优势

通过 Cortex Framework 预配 BigQuery 对话式分析数据智能体具有以下优势:

  • 自动收集语义上下文:自动将每个引用数据产品的 annotations/*.yaml 文件中的表和列商家说明导入智能体的 BigQuery 知识源,从而提高 SQL 生成准确率,而无需手动重新输入。
  • 环境可移植的已验证查询 (goldenQueries):将已验证的示例 SQL 查询(在 goldenQueries 下配置,以前在 BigQuery 中称为黄金查询)中的逻辑 {<product>.project_id} 和 {<product>.dataset_id} 占位符(以及驼峰式大小写 {<product>.projectId} 和 {<product>.datasetId} 变体)重写为每个环境(开发、预演或生产)的物理 BigQuery 标识符。
  • 零成本预检 SQL 验证:针对 goldenQueries(位于 data.bigQueryLocation 中)中的每个已配置查询执行 BigQuery 试运行编译检查,并在进行任何 API 更改之前验证估算的处理字节数是否在配置的字节数限制范围内。
  • 幂等生命周期管理:通过使用 cortex-framework-version 系统标签跟踪托管资源、在更新期间保留现有 DataAgent 资源上客户添加的标签,以及不影响用户创建的代理,在重复的 CI/CD 运行中安全地协调代理定义。
  • 灵活的位置和自动区域路由:从可选的 conversationalAnalytics.location(推荐:global,用于在 BigQuery Agents Hub 界面中显示代理)自动解析全球、多区域或区域对话式分析 API 端点,或默认为小写的 data.bigQueryLocation。

此集成如何运作

在 Dataform 流水线在 BigQuery 中具体化目标表和视图后,运行 uv run cortex-create-ca-agents。在每次运行期间,该工具都会完成以下阶段:

  1. 上下文组装。读取 config/config.yaml 中的 conversationalAnalytics,将目标 BigQuery 表和视图解析为知识来源,附加 annotations/*.yaml 中的说明,并组合指令、联接路径、术语表和经过验证的查询 (goldenQueries)。
  2. 预检验证。检查 buildEnvironment.buildProjectId 中的必需 Google Cloud API,通过对对话式分析 API 端点进行 get_location 调用来验证解析的代理位置,确认目标数据集、表和视图是否存在,并针对字节数上限试运行 data.bigQueryLocation 中扩展的 goldenQueries。
  3. 智能体同步。连接到 conversationalAnalytics.location(如果省略,则为小写的 data.bigQueryLocation)的已解析对话式分析 API 端点,并就地创建或更新每个已启用的数据智能体(如果设置了 --dry-run,则报告计划的更改,而不更改资源)。

位置信息替换和区域级端点路由

您可以在 config/config.yaml 中设置可选的 conversationalAnalytics.location 值(标准化为小写)。如果省略,Cortex Framework 默认使用 data.bigQueryLocation 的小写值。

Cortex Framework 会将对话式分析 API 请求路由到与解析的位置匹配的端点,并在创建或更新代理之前使用 get_location 检查可用性:

  • 全球(global或留空):geminidataanalytics.googleapis.com
  • 单区域(包含连字符,例如 us-east4 或 europe-west2):geminidataanalytics-LOCATION.googleapis.com
  • 多区域(不带连字符,例如 us 或 eu):geminidataanalytics.LOCATION.rep.googleapis.com

准备工作

在运行 uv run cortex-create-ca-agents 之前,请验证以下前提条件:

启用 Google Cloud API

在前提条件中完成本地环境、身份验证和核心项目设置。然后,在 buildEnvironment.buildProjectId 项目中启用所需的 API,或在运行 uv run cortex-create-ca-agents 时传递 --enable-apis:

gcloud services enable bigquery.googleapis.com \
    geminidataanalytics.googleapis.com \
    cloudaicompanion.googleapis.com \
    --project=BUILD_PROJECT_ID

替换以下内容:

  • BUILD_PROJECT_ID:在 buildEnvironment.buildProjectId 中配置的 Google Cloud 项目 ID,例如:my-cortex-project。

获取所需的角色

如需获得预配和查询 BigQuery 对话分析数据智能体所需的权限,请让管理员向您授予以下 IAM 角色:

如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。

您也可以通过自定义角色或其他预定义角色来获取所需的权限。

部署和运行数据产品流水线

首先运行 uv run cortex-build-and-deploy(或 uv run cortex-deploy)并执行 Dataform 工作流操作,以便所有目标表和视图都存在于 BigQuery 中。如需查看说明,请参阅部署后步骤。

预构建的数据智能体模板

Cortex Framework 在 src/agents/cortex/ 下提供了三个预构建的数据智能体模板(并捆绑在已安装的 cortex Python 软件包中)。每个模板都包含一个完整的 agent_settings.yaml、一个网域 system_instruction.md、显式 schemaRelationships、业务 glossaryTerms 和参数化验证查询 (goldenQueries):

模板 agentSettings路径 所需的数据产品和目录 分析范围
财务分析智能体 ../src/agents/cortex/sap/financial_analysis/agent_settings.yaml financial_statements、accounts_payable_insights、accounts_receivable_insights、cost_and_profit_centers 和 budget_allocations(在 SAP 部署中默认全部启用)。 SAP 资产负债表和损益表差异、营运资金(DPO、DSO、AP 和 AR 周转率)、提前付款现金折扣防御、暂存或付款冻结的 AP 账单,以及成本中心预算超支(5 个经过验证的查询,已启用 Python 分析)。
SAP 入站物流风险代理 ../src/agents/cortex/sap_bdc/inbound_logistics_risk/agent_settings.yaml supplier_plant_weather_risk_insights 和 supplier_inbound_delivery_weather_risk_insights (cortex_samples),由 sap_bdc_catalog、ah_weathernext_catalog 和 ah_dnb_catalog 目录提供支持。 入站交付执行、关键物料短缺、供应商准时足额交付 (OTIF)、基于调度的 P50 和 P90 动态预计到达时间、陆地和海上天气或港口延误,以及 Dun & Bradstreet 提单匹配层级(7 个经过验证的查询,已启用 Python 分析)。
销售业绩和收入预测助理 ../src/agents/cortex/sap_sfdc/sales_performance_revenue_forecast/agent_settings.yaml SAP sales_performance (cortex_samples)、sales_documents、customers 和 materials;Salesforce opportunity_pipeline、sales_activities、leads_capture_conversions、case_management 和 accounts。 跨系统 SAP 和 Salesforce 商业收入展望桥梁、已实现账单收入与未完成订单的对比、概率加权管道覆盖率、潜在客户来源转化效果、客户流失同比恢复情况以及升级的支持风险(4 个经过验证的查询,已启用 Python 分析)。

在部署所需的数据产品和目录后,将任何预构建模板添加到 config/config.yaml 并设置 enabled: true:

buildEnvironment:
  buildProjectId: BUILD_PROJECT_ID

conversationalAnalytics:
  enabled: true
  location: global
  agents:
    - agentId: cortex-sap-financial-analysis-agent
      displayName: "Financial Analysis Agent"
      enabled: true
      contextVersion: published
      agentSettings: "../src/agents/cortex/sap/financial_analysis/agent_settings.yaml"

    - agentId: cortex-sap-inbound-logistics-risk-analyst
      displayName: "SAP Inbound Logistics Risk Agent"
      enabled: false # Enable after deploying sap_bdc_catalog, ah_weathernext_catalog, ah_dnb_catalog, and weather-risk samples
      contextVersion: published
      agentSettings: "../src/agents/cortex/sap_bdc/inbound_logistics_risk/agent_settings.yaml"

    - agentId: cortex-sales-revenue-forecast-assistant
      displayName: "Sales Performance & Revenue Forecast Assistant"
      enabled: false # Enable after deploying sap_sales_performance (cortex_samples) plus the required SAP and Salesforce products
      contextVersion: published
      agentSettings: "../src/agents/cortex/sap_sfdc/sales_performance_revenue_forecast/agent_settings.yaml"

替换以下内容:

  • BUILD_PROJECT_ID:托管数据智能体资源的 Google Cloud 项目 ID,例如:my-cortex-project。

配置数据智能体

向 config/config.yaml 添加顶级 conversationalAnalytics 部分。agents 下的每个条目都可以使用 agentSettings 指向模块化外部 YAML 文件,也可以使用 inlineAgentSettings 内嵌定义其上下文。

选项 1:外部 agent_settings.yaml

在 config/config.yaml 中注册智能体:

buildEnvironment:
  buildProjectId: BUILD_PROJECT_ID

conversationalAnalytics:
  enabled: true
  location: global
  agents:
    - agentId: sap-procurement-spend-agent
      enabled: true
      contextVersion: published
      agentSettings: agents/procurement_spend/agent_settings.yaml

替换以下内容:

  • BUILD_PROJECT_ID:托管数据智能体资源的 Google Cloud 项目 ID,例如:my-cortex-project。

使用智能体的数据产品(知识来源)、系统指令、联接关系、业务术语库和经过验证的查询 (goldenQueries) 创建 config/agents/procurement_spend/agent_settings.yaml:

displayName: "SAP Procurement & Supplier Spend Agent"
description: "Conversational agent for analyzing SAP purchase orders, vendors, and material spend."
labels:
  domain: "procurement"
  source: "sap"

dataProducts:
  - id: sap_purchasing_documents
    includeTables:
      - purchasing_document_headers
      - purchasing_document_items
  - id: sap_vendors
    includeTables:
      - vendors
  - id: sap_materials
    includeTables:
      - materials

systemInstruction: "system_instruction.md"

schemaRelationships:
  - leftTable:
      dataProduct: sap_purchasing_documents
      table: purchasing_document_headers
      columns: ["client_mandt", "purchasing_document_number_ebeln"]
    rightTable:
      dataProduct: sap_purchasing_documents
      table: purchasing_document_items
      columns: ["client_mandt", "purchasing_document_number_ebeln"]
    confidenceScore: 1.0
  - leftTable:
      dataProduct: sap_purchasing_documents
      table: purchasing_document_headers
      columns: ["client_mandt", "vendor_account_number_lifnr"]
    rightTable:
      dataProduct: sap_vendors
      table: vendors
      columns: ["client_mandt", "account_number_of_vendor_or_creditor_lifnr"]
    confidenceScore: 1.0

glossaryTerms:
  - displayName: "Vendor Account Number (LIFNR)"
    description: "Unique SAP supplier identifier joining purchase orders to vendor master records."
    labels: ["sap", "procurement"]

goldenQueries:
  - question: "Who are the top 10 vendors by net order value in {currency}?"
    inlineSql: |
      SELECT
        v.name1_name1 AS vendor_name,
        SUM(i.net_order_value_in_po_currency_netwr) AS total_net_order_value
      FROM `{sap_purchasing_documents.project_id}.{sap_purchasing_documents.dataset_id}.purchasing_document_headers` AS p
      INNER JOIN `{sap_purchasing_documents.project_id}.{sap_purchasing_documents.dataset_id}.purchasing_document_items` AS i
        ON p.client_mandt = i.client_mandt
        AND p.purchasing_document_number_ebeln = i.purchasing_document_number_ebeln
      INNER JOIN `{sap_vendors.project_id}.{sap_vendors.dataset_id}.vendors` AS v
        ON p.client_mandt = v.client_mandt
        AND p.vendor_account_number_lifnr = v.account_number_of_vendor_or_creditor_lifnr
      WHERE p.currency_key_waers = @currency
      GROUP BY 1
      ORDER BY total_net_order_value DESC
      LIMIT 10
    parameters:
      - name: currency
        dataType: STRING
        description: "Three-letter ISO currency code (for example, 'USD' or 'EUR')"

options:
  enablePythonAnalysis: true
  maxBilledBytes: 10737418240

方法 2:自包含 inlineAgentSettings

对于紧凑的单产品代理,您可以直接在 config/config.yaml 中定义 inlineAgentSettings:

buildEnvironment:
  buildProjectId: BUILD_PROJECT_ID

conversationalAnalytics:
  enabled: true
  location: global
  agents:
    - agentId: sfdc-opportunity-pipeline-agent
      displayName: "Salesforce Opportunity Pipeline Agent"
      enabled: true
      contextVersion: published
      inlineAgentSettings:
        description: "Answers pipeline, win-rate, and stage velocity questions on Salesforce opportunities."
        labels:
          domain: "sales"
          source: "sfdc"
        dataProducts:
          - id: sfdc_opportunity_pipeline
            includeTables:
              - opportunity_pipeline
              - opportunity_pipeline_overview
        inlineSystemInstruction: |
          You are a sales operations analyst assistant. Base pipeline totals on
          non-closed open opportunities unless the user asks for historical closed deals.
        options:
          enablePythonAnalysis: false
          maxBilledBytes: 10737418240

替换以下内容:

  • BUILD_PROJECT_ID:托管数据智能体资源的 Google Cloud 项目 ID,例如:my-cortex-project。

配置解析和验证规则

在解析和验证 conversationalAnalytics 定义时,uv run cortex-create-ca-agents 会强制执行以下规则:

  • 三层 agentSettings 路径解析:直接使用绝对路径。相对 agentSettings 路径会按顺序针对以下各项进行解析:
    1. 包含 config.yaml 的目录(例如 config/)。
    2. 工作区根目录。
    3. 只要路径包含 agents 段,已安装的 cortex Python wheel 软件包内的捆绑 agents/ 目录(因此预构建路径(例如 ../src/agents/cortex/...)在源代码检出和 wheel 安装中均可正常运行)。
  • 灵活的 dataProducts[].id 查找:dataProducts 中的每个条目(以及 schemaRelationships 和 goldenQueries 中的匹配引用)都可以指定 data.modules.products 中的显式 moduleId(例如 sap_customers)或明确的规范叶 module_name(modulePath 的最后一段,例如 customers)。如果多个已启用的产品模块共享同一个叶 module_name,则解析会引发 AmbiguousProductReferenceError,您必须引用显式 moduleId。
  • 非破坏性 contextVersion 更新(published 与 staging 相比):contextVersion 默认为 published。更新会发送一个仅涵盖 display_name、description、labels 和活跃上下文槽(data_analytics_agent.published_context 或 data_analytics_agent.staging_context)的目标 protobuf FieldMask,而另一个上下文槽保持不变。如需在 Gemini 中注册数据智能体,您必须使用 contextVersion: published 为其预配。
  • 经过验证的查询 (goldenQueries) 字节限制和参数类型:
    • 每个经过验证的查询的 BigQuery 试运行估算值 (total_bytes_processed) 必须等于或低于 min(options.maxBilledBytes, 500,000,000,000)(500 GB API 上限;options.maxBilledBytes 默认为 10737418240,即 10 GiB)。
    • 占位符支持 snake_case({<product>.project_id}、{<product>.dataset_id})和 camelCase({<product>.projectId}、{<product>.datasetId})形式,并且每个引用的产品都必须出现在代理的 dataProducts 中。
    • 支持的 parameters[].dataType 值(STRING、INT64、FLOAT64、NUMERIC、BIGNUMERIC、BOOL、DATE、DATETIME、TIME、TIMESTAMP、BYTES、GEOGRAPHY、JSON)会自动规范化别名:INTEGER 变为 INT64,FLOAT 变为 FLOAT64,BOOLEAN 变为 BOOL。

如需了解完整的字段规范和验证限制,请参阅对话式分析 (conversationalAnalytics)。

预配和同步代理

在 BigQuery 中具体化目标数据产品表和视图后,请先使用 --dry-run 预览预检验证和计划的同步操作,然后再应用更改:

export CORTEX_CONFIG_FILE=config/config.yaml
uv run cortex-create-ca-agents --config $CORTEX_CONFIG_FILE --dry-run

--dry-run 会运行所有只读预检(API、get_location、数据集和表是否存在,以及 goldenQueries 的零费用 BigQuery 试运行),并报告每个代理的同步状态(NEEDS_CREATION、NEEDS_UPDATE、NO_CHANGE 或 NOT_MANAGED),而不会创建或更新代理或发出遥测数据。--dry-run 与 --enable-apis 互斥。

如需创建或更新已启用的数据智能体,请运行 uv run cortex-create-ca-agents:

export CORTEX_CONFIG_FILE=config/config.yaml
uv run cortex-create-ca-agents --config $CORTEX_CONFIG_FILE

如需在运行期间自动启用任何缺少的必需 Google Cloud API,请添加 --enable-apis:

export CORTEX_CONFIG_FILE=config/config.yaml
uv run cortex-create-ca-agents \
    --config $CORTEX_CONFIG_FILE \
    --enable-apis

如需了解所有受支持的 CLI 标志,请参阅 CLI 创建对话式分析智能体。

验证数据智能体并与之对话

如需验证预配的数据智能体并测试自然语言查询,请执行以下操作:

  1. 在 Google Cloud 控制台中,前往 BigQuery Studio 代理页面。

    前往“代理”

  2. 选择在buildEnvironment.buildProjectId中配置的 Google Cloud 项目。

  3. 选择代理目录标签页,然后找到与您配置的 displayName 匹配的代理。(如果您希望智能体显示在智能体中心界面中,请设置为 conversationalAnalytics.location: global。)

  4. 发起对话,并提出一个已配置的经过验证的查询 (goldenQueries),以验证 SQL 生成和结果。

如需在 Gemini 中向组织内的业务用户提供经过验证的数据智能体,请参阅 Gemini 集成。

后续步骤