Vanna.AI
Vanna 是一个 Python 软件包,它使用检索增强技术来帮助您使用 LLM 为数据库生成准确的 SQL 查询。
Vanna 的工作流程简单分为两步:首先,在您的数据上训练一个 RAG“模型”;然后,提出问题,Vanna 将返回可设置为在数据库上自动运行的 SQL 查询。
Qdrant 可用作支持向量存储,用于摄取和检索您的 RAG 数据。
安装
pip install 'vanna[qdrant]'
设置
您可以使用 Qdrant 作为向量存储以及 Vanna 支持的任何 LLM 来设置 Vanna 代理。
我们将使用 OpenAI 进行演示。
from vanna.openai import OpenAI_Chat
from vanna.qdrant import Qdrant_VectorStore
from qdrant_client import QdrantClient
class MyVanna(Qdrant, OpenAI_Chat):
def __init__(self, config=None):
Qdrant_VectorStore.__init__(self, config=config)
OpenAI_Chat.__init__(self, config=config)
vn = MyVanna(config={
'client': QdrantClient(...),
'api_key': sk-...,
'model': gpt-4-...,
})
使用方法
实例化 Vanna 代理后,您可以将其连接到 您选择的任何 SQL 数据库。
例如,Postgres。
vn.connect_to_postgres(host='my-host', dbname='my-dbname', user='my-user', password='my-password', port='my-port')
您现在可以训练并开始使用 SQL 查询数据库。
# You can add DDL statements that specify table names, column names, types, and potentially relationships
vn.train(ddl="""
CREATE TABLE IF NOT EXISTS my-table (
id INT PRIMARY KEY,
name VARCHAR(100),
age INT
)
""")
# You can 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.
vn.train(sql="SELECT * FROM my-table WHERE name = 'John Doe'")
# You can remove training data if there's obsolete/incorrect information.
vn.remove_training_data(id='1-ddl')
# Whenever you ask a new question, Vanna will retrieve 10 most relevant pieces of training data and use it as part of the LLM prompt to generate the SQL.
vn.ask(question="<YOUR_QUESTION>")