Vanna.AI
Vanna 是一个 Python 包,它利用检索增强生成技术,帮助您使用大型语言模型(LLM)为您的数据库生成准确的 SQL 查询。
Vanna 的工作分为两个简单的步骤:首先,在您的数据上训练一个 RAG “模型”;然后,您可以提出问题,它将返回 SQL 查询,这些查询可以设置为在您的数据库上自动运行。
Qdrant 可用作支持向量存储,用于摄取和检索您的 RAG 数据。
安装
pip install 'vanna[qdrant]'
设置
您可以设置一个 Vanna 代理,将 Qdrant 作为您的向量存储,并使用 Vanna 支持的任何 LLM。
我们将使用 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>")