ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;margin: 0px 8px 1em;letter-spacing: 0.1em;color: rgb(33, 37, 41);padding: 8px 12px;background: rgba(237, 242, 255, 0.8);border-radius: 8px;">MindSQL 是一个文本到 SQL 生成的 Python 包,开源、且支持 RAG,能帮助我们构建 Chat2SQL 的相关应用。 ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;margin: 1.5em 8px;color: rgb(63, 63, 63);"> ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 14px;border-radius: 8px;display: block;margin: 0.1em auto 0.5em;border-width: 0px;border-style: solid;border-color: initial;border-image-width: initial;border-image-outset: initial;border-image-repeat: initial;border-image-source: linear-gradient(to right, rgb(143, 65, 233), rgb(108, 89, 245), rgb(164, 102, 255), rgb(232, 111, 255), rgb(255, 132, 213), rgb(234, 160, 147), rgb(255, 173, 51));border-image-slice: 1;height: auto !important;" title="null" src="https://api.ibos.cn/v4/weapparticle/accesswximg?aid=104109&url=aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X3BuZy9hZzdaM2RDcTdMbEtnVDRRTTdDRno3ZmRWNVRacFhmdzZ3Tm9OVTFXendEMWUzZUlYMGpUaWNxR2h6UGowb2tITXZxOER3cUhka1hESGN4OWJmYWhXRHcvNjQwP3d4X2ZtdD1wbmcmYW1w;from=appmsg"/>
ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;margin: 1em 8px;letter-spacing: 0.1em;color: rgb(33, 37, 41);padding: 8px 12px;background: rgba(237, 242, 255, 0.8);border-radius: 8px;">除了常规一些开源软件的特点:•开源 软件包及其不同的前端集成均免费供任何人使用。 •掌控之中 您可以按照自己想要的任何方式构建它,并将所有业务数据连接并集成起来。
ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;margin: 1em 8px;letter-spacing: 0.1em;color: rgb(33, 37, 41);padding: 8px 12px;background: rgba(237, 242, 255, 0.8);border-radius: 8px;">MindSQL 还解决了企业应用两大难点:准确率不高和不能支持高并发的问题: ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;font-weight: bold;margin: 2em 8px 0.75em;border-left: 5px solid rgb(161, 27, 218);color: rgb(63, 63, 63);padding: 6px 8px;">高度准确
ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;margin: 1em 8px;letter-spacing: 0.1em;color: rgb(33, 37, 41);padding: 8px 12px;background: rgba(237, 242, 255, 0.8);border-radius: 8px;">使用 MindsSQL 实现高精度取决于所提供的训练数据的质量,更多的训练数据可提高高精度。 ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;font-weight: bold;margin: 2em 8px 0.75em;border-left: 5px solid rgb(161, 27, 218);color: rgb(63, 63, 63);padding: 6px 8px;">高并发
ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;margin: 1em 8px;letter-spacing: 0.1em;color: rgb(33, 37, 41);padding: 8px 12px;background: rgba(237, 242, 255, 0.8);border-radius: 8px;">MindSQL 的强大架构可自动扩展以每分钟处理数千个请求。 ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 20px;font-weight: bold;display: table;margin: 2em auto 1.5em;padding: 6px 0px 6px 16.75px;background: linear-gradient(135deg, rgb(113, 23, 234), rgba(113, 23, 234, 0.667), rgba(234, 96, 96, 0.533), rgba(217, 57, 205, 0.267), rgba(217, 57, 205, 0));color: rgb(255, 255, 255);border-radius: 8px;width: 318.25px;">工作原理MindSQL 是一个 Python 库,它使用 RAG(检索增强生成) 在大型语言模型的帮助下创建适合您的数据库的精确 SQL 查询。
MindSQL 遵循简单的两步程序来简化您的数据库交互:
• 首先,它将数据库的具体信息索引到向量存储中,从而创建适合您数据的 RAG“模型”。 • 随后,它会根据您的查询制定 SQL 查询,从而实现在数据库上的无缝执行。 为了充分利用 MindSQL 的功能,您主要需要与两个关键功能进行交互:
•minds.index(...):此功能在初始步骤中起着关键作用,允许您将特定于数据库的详细信息索引到向量存储中。此信息可作为后续 SQL 查询生成的基础语料库。 •minds.ask_db(...):此功能有助于直接与数据库通信。它允许您提出问题,并利用存储在向量存储中的上下文来构建 SQL 查询,以准确解决您的问题。 MindSQL 由三个基础组件组成,每个组件都继承并实现了 MindSQLCore 类提供的 API:
•向量存储 :向量存储包专注于存储和组织索引数据库上下文,作为查询生成的重要参考语料库。 •LLMs :LLMs 包负责将大型语言模型集成到 MindSQL 框架中,增强该库理解和生成上下文相关的 SQL 查询的能力。 安装使用 MindSQL的使用非常简单,首先安装 python 包:
pipinstallmindsql 然后环境变量设置,可以通过命令行或者.env文件来设置:
•API_KEY : 用于使用 OpenAI、Gemini、LLAMA 等 LLM 进行身份验证的 API 密钥 •EXAMPLE_PATH : 您可能想要用于批量索引的任何示例 JSON 文件或数据的路径 常见数据库 DB_URL 和格式如下:
•mysql://username:password@host:port/database •postgresql://username:password@host:port/database •mongodb://username:password@host:port/database 程序很简单,先引入相关包:
frommindsql.coreimportMindSQLCore frommindsql.databasesimportSqlite frommindsql.llmsimportGoogleGenAi frommindsql.vectorstoresimportChromaDB传入LLM配置,和向量数据库,指定数据库类型即可创建MindSQLCore实例。
# Choose the Vector Store. LLM and DB You Want to Work With And # Create MindSQLCore Instance With Configured Llm, Vectorstore, And Database minds = MindSQLCore( llm=GoogleGenAi(config={"api_key":"YOUR-API-KEY"}), vectorstore=ChromaDB(), database=Sqlite() )使用数据库连接字符串连接数据库:
connection=minds.database.create_connection(url="YOUR_DATABASE_CONNECTION_URL") 把所有的数据结构都进行索引,存入向量数据库:
minds.index_all_ddls(connection=connection,db_name='NAME_OF_THE_DB') 从指定的示例路径批量索引问题(RAG知识库),如果没有示例问题也可以不执行此步骤:
minds.index(bulk=True,path="your-qsn-sql-example.json") 然后就可以向数据库提出问题了,
response = minds.ask_db( question="YOUR_QUESTION", connection=connection, visualize=True )在Python中还可以从响应中提取并显示图表,
chart = response["chart"] chart.show()最后别忘了关闭数据库连接:
connection.close() 完整的程序如下:
frommindsql.coreimportMindSQLCore frommindsql.databasesimportSqlite frommindsql.llmsimportGoogleGenAi frommindsql.vectorstoresimportChromaDB config = {"api_key":"YOUR-API-KEY"} minds = MindSQLCore( llm=GoogleGenAi(config=config), vectorstore=ChromaDB(), database=Sqlite() ) connection = minds.database.create_connection(url="YOUR_DATABASE_CONNECTION_URL") minds.index_all_ddls(connection=connection, db_name='NAME_OF_THE_DB') minds.index(bulk=True, path="your-qsn-sql-example.json") response = minds.ask_db( question="YOUR_QUESTION", connection=connection, visualize=True ) chart = response["chart"] chart.show() connection.close()查询数据 ask_db(...)方法作为查询系统和从底层数据中获取见解的主要接口,其结构如下:
result = ask_db( question ="员工的平均工资是多少", connection = my_connection, table_names = ["employees"], visualize =False )table_names参数是可选的, 可用于在需要时指定相关表。 如果提供了table_names, MindSQL 将不会从向量数据库中检索相关表, 而是使用提供的表名来获取相关DDL 。
将visualize参数设置为 True, 用户可以请求使用 plotly 图表对查询结果进行可视化(除了做Demo,这个参数一般是关闭的)。
手动构建索引知识库 前面提到了使用示例文件来构建索引知识库,我们还可以在任意时候使用代码来构建。
构建索引有如下多种方式:
问答对 添加问题-SQL 对时,您可以直接指示系统如何解释特定的用户查询。 这是通过将问题与其对应的 SQL 查询关联起来来实现的, 从而使系统能够理解用户的意图并准确检索相关数据。
index( question ="平均工资是多少?", sql ="SELECT AVG(工资) FROM 员工" )添加 DDL 语句 添加数据定义语言 (DDL) 语句, 也就是定义数据库对象的结构,例如表、列和数据类型。 通过将 DDL 语句合并到系统的知识库中,您可以提供必要的架构信息, 从而增强系统有效解释和响应数据库相关查询的能力。
index(ddl="CREATETABLEemployees(idINT,nameVARCHAR(50),salaryFLOAT)") 添加文档字符串 添加文档字符串涉及提供对于系统理解其所运行的领域至关重要的特定上下文信息。 此文档可以包括有关业务逻辑、 行业特定术语或准确解释用户查询所需的任何其他相关信息的详细信息。
index(documentation="员工工资以美元($)计算") 批量数据添加 为了高效地集成来自外部源的多个问题 SQL 对,请使用带有 bulk=True 标志的索引方法并指定 JSON 文件 (data.json) 的路径。 这样可以快速提取数据并将其集成到系统的知识库中。
index(bulk=True,path="data.json") JSON文件的数据结构:
[ { "Question":"员工的平均工资是多少?", "SQLQuery":"SELECT AVG(salary) FROM employees" }, ]最后 测试下来在400多张表的情况下,简单查询基本都可以,但是在相近字段和相近表的选择上有时候会出错。
此时指定表的查询 效果比较好,不过在实际使用中需要上层应用的支持,比如先用 AI 进行表选择。
但是实际生产中靠的还是私有训练数据,大多数实际客户的Chat2BI场景有限,这部分的准确率可以逼近 100%。