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