Vanna 简介
Vanna 是一个基于 MIT 许可的开源 Python RAG(检索增强生成)框架,用于 SQL 生成和相关功能。它允许用户在数据上训练一个 RAG “模型”,然后提问问题,这将生成在数据库上运行的 SQL 查询语句,并将查询结果通过表格和图表的方式展示给用户。
安装环境
CentOS 7.X
1.安装 anaconda3
wget https:
//repo
.anaconda.com
/archive/Anaconda3-2024
.02-1-Linux-x86_64.sh
sh Anaconda3-2024.02-1-Linux-x86_64.sh
source
.
/anaconda3/bin/activate
2.更新pip
pip install --upgrade pip
Successfully installed pip-24.0
3.安装模块包
pip install pandas -i Simple Index
pip install openai -i Simple Index
pip install pymysql -i Simple Index
pip install 'vanna[chromadb,openai]' -i Simple Index
- 通过conda环境启动jupyter(pyhton开发环境),命令行执行
jupyter-lab
或者
jupyter notebook --allow-root
- 申请Vanna的api_key
打开网址Log in to Vanna.AI,输入邮箱,获取Vanna.AI for free Key,示例如下:
from vanna.remote import VannaDefault
vn = VannaDefault(model='vanna-dxs', api_key='57d72396****')
- 运行python模型
##初始化 Vanna 对象
from vanna.remote import VannaDefault
vn = VannaDefault(model='data', api_key='d6647*****')
##连接数据库
import pandas as pd
import pymysql
# MySQL数据库连接参数
conn_details = {
"host": "192.168.1.56",
"port": 13306,
"user": "root",
"password": "123@123",
"database": "vanna",
"charset": "utf8mb4",
}
# 建立MySQL数据库连接
conn = pymysql.connect(**conn_details)
# 定义一个函数,用于执行SQL查询并返回一个Pandas DataFrame
def run_sql(sql: str) -> pd.DataFrame:
df = pd.read_sql_query(sql, conn)
return df
# 将函数设置到vn.run_sql中
vn.run_sql = run_sql
vn.run_sql_is_set = True
##训练数据
vn.train(ddl="""
CREATE TABLE IF NOT EXISTS users (
id INT PRIMARY KEY COMMENT '用户ID',
username VARCHAR(50) COMMENT '用户名',
email VARCHAR(100) COMMENT '电子邮件',
age INT COMMENT '年龄',
gender VARCHAR(10) COMMENT '性别(男/女)',
city VARCHAR(50) COMMENT '城市'
) COMMENT='用户信息表' CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
""")
##插入示例数据
INSERT INTO users (id,username,email,age,gender,city) VALUES
(1,'张三','[email protected]',30,'男','北京'),
(2,'李四','[email protected]', 25,'女','上海'),
(3,'王五','[email protected]',40,'男','广州'),
(4,'赵六','[email protected]',35,'女','深圳'),
(5,'小明','[email protected]',28,'男','成都'),
(6,'小红','[email protected]',45,'女','重庆'),
(7,'小华','[email protected]',32,'男','天津'),
(8,'小丽','[email protected]',27,'女','南京'),
(9,'小李','[email protected]',38,'男','武汉'),
(10,'小美','[email protected]',33,'女','西安');
- 打开web访问界面
from vanna.flask import VannaFlaskApp
VannaFlaskApp(vn).run()
返回如下: Your app is running at: http://localhost:8084 * Serving Flask app 'vanna.flask' * Debug mode: off
- 打开Vanna界面
其它安装参考:
安装Vanna模块
[yangwx@localhost bin]$ ./pip install vanna
Collecting vanna
Downloading vanna-0.0.33-py3-none-any.whl.metadata (8.8 kB)
Requirement already satisfied: requests in /opt/anaconda3/lib/python3.7/site-packages (from vanna) (2.19.1)
......
Installing collected packages: kaleido, tenacity, tabulate, sqlparse, plotly, vanna
Successfully installed kaleido-0.2.1 plotly-5.18.0 sqlparse-0.4.4 tabulate-0.9.0 tenacity-8.2.3 vanna-0.0.33
更新安装pandas
[yangwx@localhost bin]$ ./pip install --upgrade pandas
Requirement already satisfied: pandas in /opt/anaconda3/lib/python3.7/site-packages (0.23.4)
Collecting pandas
Downloading pandas-1.1.5-cp37-cp37m-manylinux1_x86_64.whl.metadata (4.7 kB)
Successfully installed numpy-1.21.6 pandas-1.1.5
安装openAI
./pip install openai
openai-0.8.0
安装chromadb
./pip install chromadb --ignore-installed
Downloading filelock-3.12.2-py3-none-any.whl (10 kB)
Using cached fsspec-2023.1.0-py3-none-any.whl (143 kB)
Installing collected packages: pypika, mpmath, monotonic, flatbuffers, zipp, websockets, uvloop, urllib3, typing-extensions, tqdm, sympy, sniffio, six, pyyaml, python-dotenv, protobuf, packaging, overrides, numpy, idna, humanfriendly, httptools, grpcio, graphlib-backport, fsspec, filelock, exceptiongroup, charset-normalizer, certifi, bcrypt, backoff, requests, python-dateutil, pydantic-core, pulsar-client, importlib-resources, importlib-metadata, h11, coloredlogs, chroma-hnswlib, anyio, annotated-types, watchfiles, starlette, pydantic, posthog, onnxruntime, huggingface_hub, click, uvicorn, typer, tokenizers, fastapi, chromadb
开放页面IP访问配置:
修改配置文件,让界面通过IP访问
先执行如下命令,重新生成配置文件
jupyter notebook --generate-config
将c.NotebookApp.ip =localhost修改为c.NotebookApp.ip = '*'
vim /home/yangwx/.jupyter/jupyter_notebook_config.py
参考链接:
版权归原作者 weixin_46451201 所有, 如有侵权,请联系我们删除。