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 凭据。这些详细信息仅在您的 Notebook 中引用。这些数据库凭据绝不会发送到 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 | 训练数据类型 | 问题 | 内容 | |
---|---|---|---|---|
0 | 15-doc | 文档 | 无 | 这是 PARTSUPP 表中的一个表。\n\n该 ... |
1 | 11-doc | 文档 | 无 | 这是 CUSTOMER 表中的一个表。\n\n该 ... |
2 | 14-doc | 文档 | 无 | 这是 ORDERS 表中的一个表。\n\n该 表... |
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\n该 表... |
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')