Vanna.AI 借助 Qdrant 实现检索增强的 SQL 生成

Vanna 是一个 Python 包,通过检索增强把训练数据注入 LLM 提示词,从而针对数据库生成 SQL。它支持以 Qdrant 作为向量存储,可训练 DDL、业务文档和示例查询,并在提问时检索最相关的训练片段。

## 范纳.AI. [范纳](<https://vanna.ai/>) 是一个 Python 软件包,该软件包使用 retrieved Usupation 帮助您为您使用 LLMs 的数据库生成精确的 SQL 查询 。 Vanna在两个简单的步骤中工作 - 在您的数据上训练一个 RAG “ 模型”, 然后询问将返回 SQL 的询问, 以便自动在您的数据库中运行 。 Qdrant 作为支持矢量存储器,用于摄取和检索您的RAG数据. ## 安装 ```console pip install 'vanna[qdrant]' ``` ## 设置 跟着 [使用 Qdrant 的 Vanna 示例](<https://try.vanna.ai/docs/duckdb-other-llm-qdrant/index.html>) 来查看如何连接一个向量商店和 LLM 。 将使用 OpenAI 进行演示。 ```python 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 数据库](<https://try.vanna.ai/docs/FAQ/#can-i-use-this-with-my-sql-database>) 你选择。 例如Postgres(英语:Postgres). 维基语录链接:名人名言 - 文学作品 - 谚语 - 谚语 - 谚语 ```python vn.connect_to_postgres(host='my-host', dbname='my-dbname', user='my-user', password='my-password', port='my-port') ``` 您现在可以使用 SQL 来培训并开始查询您的数据库 。 ```python # 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>") ``` ## 进一步阅读 - [从Vanna.AI开始](<https://try.vanna.ai/docs/app/>) - [Vanna.AI 文档](<https://vanna.ai/docs/>) - [源代码](<https://github.com/vanna-ai/vanna/tree/main/src/vanna/integrations/qdrant>)

查看原始来源