你想使用哪个LLM?
你想在哪里存储“训练”数据?
-
Vanna Hosted Vector DB (Recommended)Use Vanna.AIs hosted vector database (pgvector) for free. This is usable across machines with no additional setup.
-
ChromaDBUse ChromaDBs open-source vector database for free locally. No additional setup is necessary -- all database files will be created and stored locally.
-
[Selected] MarqoUse Marqo locally for free. Requires additional setup. Or use their hosted option.
-
Other VectorDBUse any other vector database. Requires additional setup.
设置¶
In [ ]:
%pip install 'vanna[marqo,openai]'
In [ ]:
from vanna.openai.openai_chat import OpenAI_Chat
from vanna.marqo.marqo import Marqo
In [ ]:
class MyVanna(Marqo, OpenAI_Chat):
def __init__(self, config=None):
Marqo.__init__(self, config={'marqo_url': MARQO_URL, 'marqo_model': MARQO_MODEL})
OpenAI_Chat.__init__(self, config=config)
vn = MyVanna(config={'api_key': 'sk-...', 'model': 'gpt-4-...'})
你想查询哪个数据库?
In [ ]:
vn.connect_to_sqlite('my-database.sqlite')
训练¶
你只需要训练一次。除非你想添加更多的训练数据,否则不要再次训练。
In [ ]:
df_ddl = vn.run_sql("SELECT type, sql FROM sqlite_master WHERE sql is not null")
for ddl in df_ddl['sql'].to_list():
vn.train(ddl=ddl)
In [ ]:
# The following are methods for adding training data. Make sure you modify the examples to match your database.
# DDL statements are powerful because they specify table names, colume names, types, and potentially relationships
vn.train(ddl="""
CREATE TABLE IF NOT EXISTS my-table (
id INT PRIMARY KEY,
name VARCHAR(100),
age INT
)
""")
# Sometimes you may want to add documentation about your business terminology or definitions.
vn.train(documentation="Our business defines OTIF score as the percentage of orders that are delivered on time and in full")
# You can also add SQL queries to your training data. This is useful if you have some queries already laying around. You can just copy and paste those from your editor to begin generating new SQL.
vn.train(sql="SELECT * FROM my-table WHERE name = 'John Doe'")
In [ ]:
# At any time you can inspect what training data the package is able to reference
training_data = vn.get_training_data()
training_data
In [ ]:
# You can remove training data if there's obsolete/incorrect information.
vn.remove_training_data(id='1-ddl')
向AI提问¶
每当你提出一个新问题时,它将找到10个最相关的训练数据,并将其作为LLM提示的一部分来生成SQL。
In [ ]:
vn.ask(question=...)
启动用户界面¶

In [ ]:
from vanna.flask import VannaFlaskApp
app = VannaFlaskApp(vn)
app.run()