In [ ]:
%pip install vanna
%pip install snowflake-connector-python
In [2]:
import vanna as vn
import snowflake.connector
登录¶
创建登录并获取API密钥就像输入您的电子邮件(在您运行此单元格后)并输入我们发送给您的代码一样简单。如果您没有看到代码,请检查您的垃圾邮件文件夹。
In [3]:
api_key = vn.get_api_key('my-email@example.com')
vn.set_api_key(api_key)
设置你的模型¶
您需要选择一个全局唯一的模型名称。尝试使用您的公司名称或其他唯一字符串。所有来自模型的数据都是隔离的——没有泄漏。
In [4]:
vn.set_model('my-model') # Enter your model name here. This is a globally unique identifier for your model.
自动训练¶
如果您想使用自动训练,Vanna 包可以爬取您的数据库以获取元数据来训练您的模型。您可以在此处输入您的 Snowflake 凭据。这些详细信息仅在您的笔记本中引用。这些数据库凭据永远不会发送到 Vanna 的服务器。
In [5]:
vn.connect_to_snowflake(account='my-account', username='my-username', password='my-password', database='my-database')
In [6]:
training_plan = vn.get_training_plan_experimental(filter_databases=['SNOWFLAKE_SAMPLE_DATA'], filter_schemas=['TPCH_SF1'])
training_plan
Trying query history Trying INFORMATION_SCHEMA.COLUMNS for SNOWFLAKE_SAMPLE_DATA
Out[6]:
Train on SQL: What are the top 10 customers ranked by total sales? Train on SQL: What are the top 10 customers in terms of total sales? Train on SQL: What are the top two customers with the highest total sales for each region? Train on SQL: What are the top 5 customers with the highest total sales? Train on SQL: What is the total quantity of each product sold in each region, ordered by region name and total quantity in descending order? Train on SQL: What is the number of orders for each week, starting from the most recent week? Train on SQL: What countries are in the region 'EUROPE'? Train on Information Schema: SNOWFLAKE_SAMPLE_DATA.TPCH_SF1 SUPPLIER Train on Information Schema: SNOWFLAKE_SAMPLE_DATA.TPCH_SF1 LINEITEM Train on Information Schema: SNOWFLAKE_SAMPLE_DATA.TPCH_SF1 CUSTOMER Train on Information Schema: SNOWFLAKE_SAMPLE_DATA.TPCH_SF1 PARTSUPP Train on Information Schema: SNOWFLAKE_SAMPLE_DATA.TPCH_SF1 PART Train on Information Schema: SNOWFLAKE_SAMPLE_DATA.TPCH_SF1 ORDERS Train on Information Schema: SNOWFLAKE_SAMPLE_DATA.TPCH_SF1 REGION Train on Information Schema: SNOWFLAKE_SAMPLE_DATA.TPCH_SF1 NATION
In [ ]:
vn.train(plan=training_plan)
查看训练数据¶
任何时候你都可以查看模型中包含的训练数据
In [7]:
vn.get_training_data()
Out[7]:
| id | training_data_type | question | content | |
|---|---|---|---|---|
| 0 | 15-doc | 文档 | 无 | 这是PARTSUPP表中的一个表。\n\n... |
| 1 | 11-doc | 文档 | 无 | 这是CUSTOMER表中的一个表。\n\n... |
| 2 | 14-doc | documentation | None | 这是ORDERS表中的一个表。\n\nfo... |
| 3 | 1244-sql | sql | 前10名客户的名称是什么? | SELECT c.c_name as customer_name\nFROM snowf... |
| 4 | 1242-sql | sql | 按总金额计算的前5名客户是哪些... | SELECT c.c_name AS customer_name, SUM(l.l_quan... |
| 5 | 17-doc | 文档 | 无 | 这是REGION表中的一个表。\n\nfo... |
| 6 | 16-doc | 文档 | 无 | 这是PART表中的一个表。\n\n以下... |
| 7 | 1243-sql | sql | 哪些是消费最高的前10名客户... | SELECT c.c_name as customer_name,\n sum(... |
| 8 | 1239-sql | sql | 基于他们的...,前100名客户是哪些 | SELECT c.c_name as customer_name,\n sum(... |
| 9 | 13-doc | 文档 | 无 | 这是SUPPLIER表中的一个表。\n\n... |
| 10 | 1241-sql | sql | 按总金额计算的前10名客户是哪些... | SELECT c.c_name as customer_name,\n sum(... |
| 11 | 12-doc | 文档 | 无 | 这是LINEITEM表中的一个表。\n\n... |
| 12 | 18-doc | 文档 | 无 | 这是NATION表中的一个表。\n\n该表... |
| 13 | 1248-sql | sql | 每个国家有多少客户? | SELECT n.n_name as country,\n count(*) a... |
| 14 | 1240-sql | sql | 每周的订单数量是多少? | SELECT date_trunc('week', o_orderdate) as week... |
移除训练数据¶
如果您错误地添加了一些训练数据,您可以将其删除。模型性能直接与训练数据的质量相关。
In [ ]:
vn.remove_training_data(id='my-training-data-id')