环境
操作系统:RockyLinux
容器:Docker
大语言模型:Deepseek-r1-70b
安装向量数据库 chromadb (可省略)
- 拉取镜像
docker pull chromadb/chroma:latest
- 下载嵌入式向量模型
mkdir -p ~/.cache/chroma/onnx_models/all-MiniLM-L6-v2
wget https://chroma-onnx-models.s3.amazonaws.com/all-MiniLM-L6-v2/onnx.tar.gz
cp onnx.tar.gz ~/.cache/chroma/onnx_models/all-MiniLM-L6-v2/onnx.tar.gz
- 安装并启动chromadb
mkdir -p /data/chroma/data
# docker-compose配置
cat > /data/chroma/docker-compose.yaml <<"EOF"
services:
chromadb:
image: chromadb/chroma:latest
container_name: chromadb
restart: always
ports:
- "8000:8000"
volumes:
- /data/chroma/data:/study/ai/chroma
environment:
- IS_PERSISTENT=TRUE
- ANONYMIZED_TELEMETRY=TRUE
EOF
# 启动chroma
cd /data/chroma
docker-compose up -d
- 测试
有返回值代表成功
curl http://localhost:8000/docs
安装vanna
建议通过anaconda管理,参考:https://www.jianshu.com/p/9939df72356e?v=1740562209751
- 安装python包
pip install 'vanna[chromadb,openai,mysql]'
- 自定义vanna服务
cat > vanna-train.py <<"EOF"
from vanna.openai import OpenAI_Chat
from vanna.chromadb import ChromaDB_VectorStore
from openai import OpenAI
client = OpenAI(
api_key="EMPTY",
base_url="http://10.3.6.41:8000/v1"
)
class MyVanna(ChromaDB_VectorStore, OpenAI_Chat):
def __init__(self, config=None):
ChromaDB_VectorStore.__init__(self, config=config)
OpenAI_Chat.__init__(self, client=client, config=config)
vn = MyVanna(config={"model": "deepseek-r1-70B-4bit"})
vn.connect_to_mysql(host='10.3.23.191', dbname='text_sql', user='test_user', password='test12345', port=3306)
# 删除所有训练数据
train_data = vn.get_training_data()
id_list = train_data['id'].values
for i in range(len(id_list)):
vn.remove_training_data(id=id_list[i])
# # The information schema query may need some tweaking depending on your database. This is a good starting point.
# df_information_schema = vn.run_sql("SELECT * FROM INFORMATION_SCHEMA.COLUMNS")
#
# # This will break up the information schema into bite-sized chunks that can be referenced by the LLM
# plan = vn.get_training_plan_generic(df_information_schema)
# print(plan)
#
# # If you like the plan, then uncomment this and run it to train
# vn.train(plan=plan)
vn.train(ddl='''
CREATE TABLE score (
st_no varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '学号',
subject_no varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '课程号',
score int DEFAULT NULL COMMENT '成绩'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='成绩表';
CREATE TABLE student (
st_no varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '学号',
st_name varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '学生姓名',
st_age int DEFAULT NULL COMMENT '学生年龄'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='学生表';
CREATE TABLE subject (
subject_no varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '课程号',
subject_name varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '课程名'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='课程表';
''')
#vn.train(sql="", question="") #问题和sql对
# Sometimes you may want to 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. You can just copy and paste those from your editor to begin generating new SQL.
#vn.train(sql="SELECT * FROM my-table WHERE name = 'John Doe'")
# At any time you can inspect what training data the package is able to reference
training_data = vn.get_training_data()
print(training_data)
# vn.ask(question='有哪些表')
from vanna.flask import VannaFlaskApp
VannaFlaskApp(vn, allow_llm_to_see_data=True).run(port=8085, host='0.0.0.0', debug=True)
EOF
- 启动服务
python vanna-train.py
访问vanna
http://10.3.6.38:8085/
问答示例
问题
# 升级python到3.11
dnf install sqlite-devel
https://www.dbanote.com/Linux/Rocky9-4-update-python-Python3-11-9.html
https://blog.csdn.net/nibonnn/article/details/103999157
=》 文件内容写:/usr/local/lib/
如果执行python vanna-train.py的时候报错: RuntimeError: Your system has an unsupported version of sqlite3. Chroma requires sqlite3 >= 3.35.0.
解决方法:
pip install pysqlite3-binary
vi /usr/local/lib/python3.11/site-packages/chromadb/__init__.py
# 以下放到文件最头部
__import__('pysqlite3')
import sys
sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')
参考
https://cloud.tencent.com/developer/article/2426655
https://blog.csdn.net/beingstrong/article/details/136768519