環(huán)境
操作系統(tǒng):RockyLinux
容器:Docker
大語言模型:Deepseek-r1-70b
安裝向量數(shù)據(jù)庫 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管理,參考:http://www.lxweimin.com/p/9939df72356e?v=1740562209751
- 安裝python包
pip install 'vanna[chromadb,openai,mysql]'
- 自定義vanna服務(wù)
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)
# 刪除所有訓(xùn)練數(shù)據(jù)
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 '學(xué)號',
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 '學(xué)號',
st_name varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '學(xué)生姓名',
st_age int DEFAULT NULL COMMENT '學(xué)生年齡'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='學(xué)生表';
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
- 啟動服務(wù)
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
=》 文件內(nèi)容寫:/usr/local/lib/
如果執(zhí)行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