当前位置:   article > 正文

AI大模型环境—Vanna安装分享(text-2-sql)_vanna部署离线大模型

vanna部署离线大模型

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

4. 通过conda环境启动jupyter(pyhton开发环境),命令行执行

jupyter-lab

或者

jupyter notebook --allow-root

5. 申请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****')

6. 运行python模型

  1. ##初始化 Vanna 对象
  2. from vanna.remote import VannaDefault
  3. vn = VannaDefault(model='data', api_key='d6647*****')
  4. ##连接数据库
  5. import pandas as pd
  6. import pymysql
  7. # MySQL数据库连接参数
  8. conn_details = {
  9. "host": "192.168.1.56",
  10. "port": 13306,
  11. "user": "root",
  12. "password": "123@123",
  13. "database": "vanna",
  14. "charset": "utf8mb4",
  15. }
  16. # 建立MySQL数据库连接
  17. conn = pymysql.connect(**conn_details)
  18. # 定义一个函数,用于执行SQL查询并返回一个Pandas DataFrame
  19. def run_sql(sql: str) -> pd.DataFrame:
  20. df = pd.read_sql_query(sql, conn)
  21. return df
  22. # 将函数设置到vn.run_sql中
  23. vn.run_sql = run_sql
  24. vn.run_sql_is_set = True
  25. ##训练数据
  26. vn.train(ddl="""
  27. CREATE TABLE IF NOT EXISTS users (
  28. id INT PRIMARY KEY COMMENT '用户ID',
  29. username VARCHAR(50) COMMENT '用户名',
  30. email VARCHAR(100) COMMENT '电子邮件',
  31. age INT COMMENT '年龄',
  32. gender VARCHAR(10) COMMENT '性别(男/女)',
  33. city VARCHAR(50) COMMENT '城市'
  34. ) COMMENT='用户信息表' CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  35. """)
  36. ##插入示例数据
  37. INSERT INTO users (id,username,email,age,gender,city) VALUES
  38. (1,'张三','zhangsan@example.com',30,'男','北京'),
  39. (2,'李四','lisi@example.com', 25,'女','上海'),
  40. (3,'王五','wangwu@example.com',40,'男','广州'),
  41. (4,'赵六','zhaoliu@example.com',35,'女','深圳'),
  42. (5,'小明','xiaoming@example.com',28,'男','成都'),
  43. (6,'小红','xiaohong@example.com',45,'女','重庆'),
  44. (7,'小华','xiaohua@example.com',32,'男','天津'),
  45. (8,'小丽','xiaoli@example.com',27,'女','南京'),
  46. (9,'小李','xiaoli2@example.com',38,'男','武汉'),
  47. (10,'小美','xiaomei@example.com',33,'女','西安');

7. 打开web访问界面 

  1. from vanna.flask import VannaFlaskApp
  2. VannaFlaskApp(vn).run()
返回如下:
Your app is running at:
http://localhost:8084
 * Serving Flask app 'vanna.flask'
 * Debug mode: off

8. 打开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

参考链接:

https://pypi.org/project/vanna/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/659888
推荐阅读
相关标签
  

闽ICP备14008679号