实战PG vector构建DBA个人知识库之一:基础环境搭建以及大模型部署
陈旭,捷银消费金融有限公司DBA
大家好,今天和大家聊聊PG vector 构建个人知识库。
可能大多数朋友和我一样,工作了几年之后,累计了大量的邮件,PPT,word 文档,PDF 资料, 很难有时间自己整理。
到后来可以采用麦库笔记(已停服),印象笔记, 墨天轮博客来记录总结一些自己学习的知识和分享一些工作中的经验。
到了AI时代,我想做的是把手头的一些资料,笔记通过LLM+RAG的方式重新来归纳到新的知识库管理平台中。
本次实践设计到的一些技术堆栈:(技术面有点广,但是每项都不深入。。。)
前台技术:html, js, vue
后端技术:Python , Longchain 框架
数据库技术:PG 16 以及 pg_vector 插件
机器部署篇:私有机器部署或者租用云服务部署LLM已经embedding 模型
LLM模型:Llama3-Chinese
Embedding 模型:text2vec-large-chinese
AI 开发助手:CHATGPT 4 , 通义千问平台
数据集:麦库笔记,印象笔记, wiki 网页, word 技术文档…
作为实战PG的开场篇,我们需要搭建一下大模型的环境,需要物理机1一台(需要带有GPU的显卡),如何朋友们没有机器的话,可以租用云服务…
或者部署在自己的笔记本上(总之根据个人的财力量力而行就行)
我们这里采用的是算力云的服务: https://www.autodl.com/home
关于模型的GPU选型问题可以参考: https://www.autodl.com/docs/gpu/
我们去算力市场租用一台机器: https://www.autodl.com/market/list 我们选择一台 RTX 3090的机器 , 每个小时才1.66 元
我们选择按流量计费,这样在不用的时候可以选择关闭机器来节省一部分经费。
关于基础镜像我们选择:pytorch 1.7.0 版本对应的 cuda版本 11.0
我们可以通过web终端来访问这台机器:
查看显卡信息:nvidia-smi
对于大模型的选择,我们可以选择一个支持中文的大模型,我们访问一下魔搭社区 https://www.modelscope.cn/home
最终我们选择了 Llama3-Chinese 版本 https://www.modelscope.cn/models/seanzhang/Llama3-Chinese
Llama3-Chinese是以Meta-Llama-3-8B为底座,使用 DORA + LORA+ 的训练方法,在50w高质量中文多轮SFT数据 + 10w英文多轮SFT数据 + 2000单轮自我认知数据训练而来的大模型。
模型下载界面: 主要的模型文件分为5个文件,大小不到20GB。
我们在刚才租用的算力云的服务器上,设置一下资源下载加速:
root@autodl-container-16e1448282-c9f3dde0:~/autodl-tmp# source /etc/network_turbo
设置成功
注意:仅限于学术用途,不承诺稳定性保证
安装git-lfs
root@autodl-container-16e1448282-c9f3dde0:~/autodl-tmp# curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
Detected operating system asUbuntu/bionic.
Checkingfor curl...
Detected curl...
Checkingfor gpg...
Detected gpg...
Detected apt version as1.6.12ubuntu0.1
Running apt-get update...done.
Installing apt-transport-https...done.
Installing/etc/apt/sources.list.d/github_git-lfs.list...done.
Importing packagecloud gpg key...Packagecloud gpg key imported to /etc/apt/keyrings/github_git-lfs-archive-keyring.gpg
done.
Running apt-get update...done.
The repository is setup!You can now install packages.
root@autodl-container-16e1448282-c9f3dde0:~/autodl-tmp# apt install git-lfs
Readingpackage lists...Done
Building dependency tree
Reading state information...Done
The following NEW packages will be installed:
git-lfs
0 upgraded,1 newly installed,0 to remove and176not upgraded.
Need to get7,168 kB of archives.
Afterthis operation,15.6 MB of additional disk space will be used.
Get:1 https://packagecloud.io/github/git-lfs/ubuntu bionic/main amd64 git-lfs amd64 3.2.0 [7,168 kB]
Fetched7,168 kB in3s(2,139 kB/s)
debconf: delaying package configuration, since apt-utils isnot installed
Selecting previously unselected package git-lfs.
(Reading database ...42112 files and directories currently installed.)
Preparing to unpack .../git-lfs_3.2.0_amd64.deb ...
Unpacking git-lfs (3.2.0)...
Setting up git-lfs (3.2.0)...
Git LFS initialized.
下载大模型文件:总大小在30GB左右
root@autodl-container-16e1448282-c9f3dde0:~/autodl-tmp/LLM# git clone https://www.modelscope.cn/seanzhang/Llama3-Chinese.git
Cloninginto'Llama3-Chinese'...
remote:Enumerating objects:43,done.
remote:Counting objects:100%(43/43),done.
remote:Compressing objects:100%(41/41),done.
remote:Total43(delta 11), reused 0(delta 0), pack-reused 0
Unpacking objects:100%(43/43),done.
Filtering content:100%(5/5),14.95GiB|50.68MiB/s,done.
root@autodl-container-16e1448282-c9f3dde0:~/autodl-tmp/LLM# ls -lhtr
total 4.0K
drwxr-xr-x 4 root root 4.0KJul2216:23Llama3-Chinese
root@autodl-container-16e1448282-c9f3dde0:~/autodl-tmp/LLM# du -h ./ --max-depth=1
30G./Llama3-Chinese
30G./
安装依赖的包
pip install transformers
测试部署的大模型代码
from transformers importAutoTokenizer,AutoModelForCausalLM
model_id ="/root/autodl-tmp/LLM/Llama3-Chinese"
tokenizer =AutoTokenizer.from_pretrained(model_id)
model =AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="auto", device_map="auto")
messages =[
{"role":"system","content":"You are a helpful assistant."},
{"role":"user","content":"""鲁迅(1881年9月25日—1936年10月19日),原名周樟寿,后改名周树人,
字豫山,后改字豫才,浙江绍兴人。
中国著名文学家、思想家、革命家、教育家、美术家、书法家、民主战士,新文化运动的重要参与者,中国现代文学的奠基人之一.
请问鲁迅活了多少岁?"""},
]
input_ids = tokenizer.apply_chat_template(
messages, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
outputs = model.generate(
input_ids,
max_new_tokens=2048,
do_sample=True,
temperature=0.7,
top_p=0.95,
)
response = outputs[0][input_ids.shape[-1]:]
print(tokenizer.decode(response, skip_special_tokens=True))
测试一下大模型的理解计算能力:
输入为:鲁迅(1881年9月25日—1936年10月19日),原名周樟寿,后改名周树人,
字豫山,后改字豫才,浙江绍兴人。
中国著名文学家、思想家、革命家、教育家、美术家、书法家、民主战士,新文化运动的重要参与者,中国现代文学的奠基人之一.
提问是:请问鲁迅活了多少岁?
大模型的回答是:鲁迅活了55岁。答案源于计算:(1881年9月25日—1936年10月19日)\
最后我们尝试为本地部署的大模型发布一个API的接口, 这里我们用PYTHON自带 fastAPI 包实现:
安装包:
pip3 install fastapi
发布一个查询接口:
from transformers importAutoTokenizer,AutoModelForCausalLM
import uvicorn
from fastapi importFastAPI
app =FastAPI()
model_id ="/root/autodl-tmp/LLM/Llama3-Chinese"
tokenizer =AutoTokenizer.from_pretrained(model_id)
model =AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="auto", device_map="auto")
@app.get("/llm_query/{query}")
def llm_query(query):
message =[
{"role":"system","content":"You are a helpful assistant."},
{"role":"user","content":"""{}""".format(query)},
]
input_ids = tokenizer.apply_chat_template(
message, add_generation_prompt=True, return_tensors="pt"
).to(model.device)
outputs = model.generate(
input_ids,
max_new_tokens=2048,
do_sample=True,
temperature=0.7,
top_p=0.95,
)
response = outputs[0][input_ids.shape[-1]:]
return tokenizer.decode(response, skip_special_tokens=True);
if __name__ =='__main__':
print('-----start-------')
#init_load()
uvicorn.run(app, host="0.0.0.0", port=8868)
启动程序发布接口:
root@autodl-container-16e1448282-c9f3dde0:~/autodl-tmp/scripts# vi LLMHandler.py
root@autodl-container-16e1448282-c9f3dde0:~/autodl-tmp/scripts# python LLMHandler.py &
[1]1404
root@autodl-container-16e1448282-c9f3dde0:~/autodl-tmp/scripts#
root@autodl-container-16e1448282-c9f3dde0:~/autodl-tmp/scripts# Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
Loading checkpoint shards:100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████|5/5[00:03<00:00,1.26it/s]
-----start-------
INFO:Started server process [1404]
INFO:Waitingfor application startup.
INFO:Application startup complete.
INFO:Uvicorn running on http://0.0.0.0:8868 (Press CTRL+C to quit)
发布服务后,我们还需要打开端口,参考: https://www.autodl.com/docs/port/
点击自定义服务:
浏览器调用测试一下:
输入问题:你好,分享一下夏天的解暑方式?
LLM回答:“夏天的解暑方式有很多种,比如饮用清凉饮料,多喝水,做些轻松的户外活动,利用空调等冷却设备等等。您有什么特别的需求或偏好吗?”
当然你也可以写一段小代码调用LLM的接口:
最后总结:
1.我们完成大模型Llama3-Chinese 在算力云上的私有化部署
2.我们通过fastAPI发布了大模型的调用接口