Cortex Code CLI 工作流程示例

本主题提供了常见任务的工作流程示例,以帮助您充分利用 Cortex Code CLI。它涵盖了数据发现、合成数据生成、构建仪表板和创建 Cortex Agents。

用例:数据发现和查询

本节将介绍如何创建合成数据集并执行基本分析以生成仪表板。

连接到 Snowflake 账户

cortex -c <your-demo-account>
Copy

或者以交互方式连接:

> connect to <my demo account>
Copy

发现和探索数据

搜索数据目录、了解沿袭并查找相关表:

> Find all tables related to customers that I have write access to
Copy

确保您拥有具备正确权限的正确角色

> What privileges does my role have on this database?
Copy

诊断访问问题并了解角色权限:

> Why am I getting a permissions error?
Copy

生成合成数据

以下是为不同用例生成合成数据的一些示例。

金融科技公司的欺诈分析:

> Generate realistic looking synthetic data into <database name>. Create a table of 10000
  financial transactions where ~0.5% of them are fraudulent. Include Amount, Location,
  Merchant, and Time. Make the fraudulent ones look suspicious based on location or amount.
Copy

药物试验数据:

> Make a dummy dataset for a clinical trial of a new blood pressure medication. List 100
  patients, their age, their dosage group (Placebo vs. 10mg), and their blood pressure
  readings over 4 weeks.
Copy

客户流失数据:

> Create a customer churn dataset for a telecom company showing customer usage for 100000
  customers. Include basic demographic data such as fake names, phone numbers, US city and
  state. Also include data usage (GB), call minutes, contract length, and whether they
  cancelled their service (churn). Ensure there's a customer_id column that's unique.
  Create the data locally and then upload it to Snowflake.
Copy

对此数据执行基本查询

> Calculate the Churn Rate grouped by state and contract length. Order the results by the
  highest churn rate first so I can see the most risky regions and contract types.
Copy
> I want to identify the heaviest data users who are also churning.
Copy

构建交互式仪表板

创建和部署具有图表、筛选器和交互功能的 Streamlit 应用程序。

小技巧

打开您喜欢的示例仪表板(或在网上找一个),并将其复制到剪贴板。您可以将图像直接粘贴到 Cortex Code 中(按 Ctrl+V)作为设计参考。

> Build an interactive Streamlit dashboard on this data with state filters and use the
  conversation so far for examples of the kinds of charts to show. Use the attached image
  as a template for visuals and branding.
Copy

验证仪表板运行正常且外观良好后,将其上传到 Snowflake:

> Ensure that the Streamlit app will work with Snowflake and upload it to Snowflake.
  Give me a link to access the dashboard when it's done.
Copy

恭喜!您现在应该有一个运行正常的 Streamlit 仪表板来显示您创建的数据集。

用例:构建 Cortex Agents

本节将介绍如何创建 Cortex Agent,以回答有关 Snowflake Intelligence 中数据的问题。我们将使用客户通话记录来扩充现有的合成数据。

为 Cortex Analyst 创建语义视图

创建语义视图,以便将 Cortex Analyst 与数据结合使用。对它提出的所有问题使用默认值:

> Write a Semantic View named DEMO_TELECOM_CHURN_ANALYTICS for Cortex Analyst based on
  this data. Use the semantic-view optimization skill.
Copy

创建 Cortex Search Service

首先,生成包含客户服务通话的合成数据:

> Generate a new table called customer_call_logs. Generate 50 realistic customer service
  transcripts (2-3 sentences each) as PDF files. Some should be angry complaints about
  coverage, others should be questions about billing. Then use the AI_PARSE_DOCUMENT
  function to extract the text and layout information from the PDFs into the TRANSCRIPT_TEXT
  column. Split text into chunks for better search quality.
Copy

然后创建 Cortex Search Service,用于为转录文本编制索引:

> Create a Cortex Search Service named CALL_LOGS_SEARCH that indexes these transcripts.
  It should index the TRANSCRIPT_TEXT column and filter by CUSTOMER_ID.
Copy

创建 Cortex Agent

构建同时使用 Analyst 和 Search 服务的 Cortex Agent:

> Build a Cortex Agent that has access to two tools:
  - cortex_analyst: For querying the TELECOM_CUSTOMERS SQL table.
  - cortex_search: For searching the CALL_LOGS_SEARCH service.

  Write a system prompt for this agent:
  - Persona: You are a Senior Retention Specialist.
  - Routing Logic: If the user asks for 'metrics', 'counts', or 'averages', use the
    Analyst tool. If the user asks for 'sentiment', 'reasons', or 'summaries of calls',
    use the Search tool.
  - Output Format: Always verify the customer ID before answering. If the risk score is
    high, end the response with a recommended retention offer (e.g., 'Offer 10% discount').
  - Constraint: Never reveal the raw CHURN_RISK_SCORE to the user; interpret it as 'Low',
    'Medium', or 'High'.
Copy

部署到 Snowflake Intelligence

将代理部署到 Snowflake Intelligence:

> Let's deploy this agent to Snowflake Intelligence.
Copy

恭喜!您已成功创建并部署了 Snowflake Intelligence 代理。

现在,您应该能够在 Snowflake Intelligence 中访问此代理,并向其询问以下问题:

  • “客户在通话中抱怨什么?”

  • “显示月费超过 100 美元的高风险客户”

另请参阅

Cortex Code CLI

开始安装和首次提示

Skill

语义模型、代理和文档的专业技能

Cortex Analyst

Cortex Analyst 文档