本地搭建属于自己的ChatGPT:基于PyTorch+ChatGLM-6b+Streamlit+QDrant+DuckDuckGo
 
本地部署chatglm及緩解時效性問題的思路:
模型使用chatglm-6b 4bit,推理使用hugging face,前端應(yīng)用使用streamlit或者gradio。
微調(diào)對顯存要求較高,還沒試驗??梢越Y(jié)合LoRA進行微調(diào)。
緩解時效性問題:通過本地數(shù)據(jù)庫或者搜索引擎獲取特有數(shù)據(jù)或者實時數(shù)據(jù)作為生成模型的上下文。
- 向量數(shù)據(jù)庫實現(xiàn)思路:先將特有數(shù)據(jù)轉(zhuǎn)換為embedding存入向量數(shù)據(jù)庫,在調(diào)用模型生成問答時,先將query轉(zhuǎn)換成embedding,然后從數(shù)據(jù)庫查詢相近的結(jié)果作為上下文。embedding生成可以使用sentence_transformer庫,向量數(shù)據(jù)庫可以使用qdrant或者milvus。
 - 搜索引擎實現(xiàn)思路:在調(diào)用大模型生成問答時,先用搜索引擎搜索相關(guān)的詞條,將詞條內(nèi)容或者摘要作為上下文輸入到模型。搜索引擎可以使用duckduckgo_search庫。
 
1.運行環(huán)境配置
windows 11
32G 內(nèi)存
GTX 3080Ti
1.1 PyTorch
安裝anaconda或者miniconda
創(chuàng)建虛擬環(huán)境:
conda create -n chatbot python=3.9激活虛擬環(huán)境:
conda activate chatbot主要依賴的包:
1)pytorch-gpu
Currently, PyTorch on Windows only supports Python 3.7-3.9; Python 2.x is not supported.
conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia2)hugging face
conda install -c huggingface transformers3)streamlit
pip install streamlit pip install streamlit-chat4)sentencepiece 和 cpm-kernels
pip install sentencepiece pip install cpm-kernels5)sentence-transformers
conda install -c conda-forge sentence-transformers6)qdrant-client
pip install qdrant-client7)duckduckgo_search
pip install -U duckduckgo_search參考:
Start Locally | PyTorch
Installation (huggingface.co)
Installation - Streamlit Docs
Installation — Sentence-Transformers documentation (sbert.net)
Install - Qdrant
1.2 requirements
安裝:
# 建議用這個 conda env create -f freeze.ymlpip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt導(dǎo)出虛擬環(huán)境的依賴包命令:
conda env export > freeze.ymlpip list --format=freeze > ./requirements.txt1.3 Docker
用于運行QDrant數(shù)據(jù)庫:
 
可以參考:Install Docker Desktop on Windows | Docker Documentation
1.4 QDrant
可以參考:https://github.com/qdrant/qdrant
1.5 報錯及處理
streamlit報錯1
報錯信息:
AttributeError: module 'click' has no attribute 'get_os_args'解決措施:
pip install -U click==8參考:https://github.com/streamlit/streamlit/issues/4555
streamlit報錯2
報錯信息:
AttributeError: module 'streamlit' has no attribute 'cache_resource'解決措施:
# 去掉這個裝飾器或者替換為 @st.cache參考:https://discuss.streamlit.io/t/attributeerror-module-streamlit-has-no-attribute-cache/25155
2.大模型構(gòu)建
2.1 開源模型
ChatGLM
從github下載chatglm-6b工程:THUDM/ChatGLM-6B
從抱抱臉下載chatglm-6b-int4模型:THUDM/chatglm-6b-int4
官方介紹:
ChatGLM-6B 是一個開源的、支持中英雙語問答的對話語言模型,基于 General Language Model (GLM) 架構(gòu),具有 62 億參數(shù)。結(jié)合模型量化技術(shù),用戶可以在消費級的顯卡上進行本地部署(INT4 量化級別下最低只需 6GB 顯存)。ChatGLM-6B 使用了和 ChatGLM 相同的技術(shù),針對中文問答和對話進行了優(yōu)化。經(jīng)過約 1T 標識符的中英雙語訓(xùn)練,輔以監(jiān)督微調(diào)、反饋自助、人類反饋強化學(xué)習(xí)等技術(shù)的加持,62 億參數(shù)的 ChatGLM-6B 已經(jīng)能生成相當符合人類偏好的回答。
ChatGLM-6B-INT4 是 ChatGLM-6B 量化后的模型權(quán)重。具體的,ChatGLM-6B-INT4 對 ChatGLM-6B 中的 28 個 GLM Block 進行了 INT4 量化,沒有對 Embedding 和 LM Head 進行量化。量化后的模型理論上 6G 顯存(使用 CPU 即內(nèi)存)即可推理,具有在嵌入式設(shè)備(如樹莓派)上運行的可能。
在 CPU 上運行時,會根據(jù)硬件自動編譯 CPU Kernel ,請確保已安裝 GCC 和 OpenMP (Linux一般已安裝,對于Windows則需手動安裝),以獲得最佳并行計算能力。
其他大模型
| BLOOM | Apache-2.0 | https://huggingface.co/bigscience/bloom | |
| ColossoalAI | Apache-2.0 | https://colossalai.org/zh-Hans/ | |
| LLaMa | https://github.com/facebookresearch/llama | ||
| Alpaca | https://crfm.stanford.edu/2023/03/13/alpaca.html | ||
| T5 | https://huggingface.co/docs/transformers/model_doc/t5 | ||
| Cerebras | Apache-2.0 | https://huggingface.co/cerebras/Cerebras-GPT-6.7B | |
| 文心一言 | |||
| 通義千問 | |||
| 盤古 | 
2.2 微調(diào)
對顯存要求較高,暫未試驗。
制作微調(diào)數(shù)據(jù)集
可以參考:
huang1332/finetune_dataset_maker
基于LoRA/P-Tuning進行微調(diào)
可以參考:
極低資源微調(diào)大模型方法LoRA以及BLOOM-LORA實現(xiàn)代碼
ChatGLM-6B/ptuning
mymusise/ChatGLM-Tuning
2.3 推理
Hugging Face
from transformers import AutoModel, AutoTokenizer模型采樣算法
ChatGPT有兩個重要的參數(shù)是temperature和top_p,HuggingFace的AutoModel有兩個類似的參數(shù)是temperature和top_k。上述這三個方法都是采樣方法,用于因果語言模型中在給定上下文的情景下預(yù)測下一個單詞出現(xiàn)的概率。
在進行預(yù)訓(xùn)練時,往往使用“完形填空”的形式,例如給定上文預(yù)測下文。基于貪心策略的思路是選擇下文單詞概率最大的單詞,但是這樣會讓大模型的注意力只集中在最近的幾個單詞(token)上,導(dǎo)致最終模型生成效果會非常生硬和可預(yù)測。
為了讓模型具有一定的創(chuàng)新性(隨機性),可以使用基于分布采樣的生成采樣算法。
Top-k采樣從排名前 k (即采樣列表的大小為k)的token種進行抽樣,允許其他分數(shù)或概率較高的token也有機會被選中。在很多情況下,這種抽樣帶來的隨機性有助于提高生成質(zhì)量。
Top-k采樣的缺點是k的取值不好確定,無法保證最優(yōu)。所以ChatGPT引入了動態(tài)設(shè)置k大小的策略——即刻采樣(Nucleus Sampling)。top-p 值通常設(shè)置為比較高的值(如0.75),目的是限制低概率token的長尾??梢酝瑫r使用top-k和top-p,top-p策略在top-k策略之后生效。
溫度采樣受統(tǒng)計熱力學(xué)的啟發(fā),高溫意味著更可能遇到低能態(tài)。在概率模型中,logits扮演著能量的角色,我們可以通過將logits除以溫度來實現(xiàn)溫度采樣,然后將其輸入Softmax并獲得采樣概率。
總的來說,溫度參數(shù)用來調(diào)整候選詞的概率分布。溫度越低,模型對其首選越有信心;溫度>1度會降低信心,模型不確定性增加,趨近于正無窮的溫度相當于均勻采樣(候選詞的概率都相同,完全隨機)。通常,溫度設(shè)在[0.7, 0.9]之間是創(chuàng)造性任務(wù)最常見的溫度。
參考:ChatGPT模型采樣算法詳解
3.前端應(yīng)用
3.1 Streamlit
ChatGLM工程中提供了兩個demo,基于streamlit的是其中之一,另一個是基于gradio的。
https://streamlit.io/
3.2 LangChain
LangChain是一個用于開發(fā)由語言模型驅(qū)動的應(yīng)用程序的框架。它提供了一套工具、組件和接口,可簡化創(chuàng)建由大型語言模型 (LLM) 和聊天模型提供支持的應(yīng)用程序的過程。LangChain 可以輕松管理與語言模型的交互,將多個組件鏈接在一起,并集成額外的資源,例如 API 和數(shù)據(jù)庫。
https://docs.langchain.com/docs/
https://zhuanlan.zhihu.com/p/620529542
3.3 展示效果
4.時效性問題解決方案
核心思路:通過本地數(shù)據(jù)庫或者搜索引擎獲取特有數(shù)據(jù)或者實時數(shù)據(jù)作為生成模型的上下文。
向量數(shù)據(jù)庫實現(xiàn)思路:先將特有數(shù)據(jù)轉(zhuǎn)換為embedding存入向量數(shù)據(jù)庫,在調(diào)用模型生成問答時,先將query轉(zhuǎn)換成embedding,然后從數(shù)據(jù)庫查詢相近的結(jié)果作為上下文。
1)embedding生成可以使用sentence_transformer庫
2)向量數(shù)據(jù)庫可以使用qdrant或者milvus
搜索引擎實現(xiàn)思路:在調(diào)用大模型生成問答時,先用搜索引擎搜索相關(guān)的詞條,將詞條內(nèi)容或者摘要作為上下文輸入到模型。
1)搜索引擎庫可以使用duckduckgo_search包
大模型使用chatglm-6b 4bit,推理使用hugging face,前端應(yīng)用使用streamlit或者gradio。
4.1 embedding模型
模型介紹:Pretrained Models — Sentence-Transformers
模型下載:Models - Hugging Face
本項目中使用:multi-qa-MiniLM-L6-cos-v1。
git clone https://huggingface.co/sentence-transformers/multi-qa-MiniLM-L6-cos-v14.2 向量數(shù)據(jù)庫構(gòu)建
def dataset2qdrant(root_path, file_path, embed_length: int = 384):client = QdrantClient("localhost", port=2023)collection_name = "data_collection"client.recreate_collection(collection_name=collection_name,vectors_config=VectorParams(size=embed_length, distance=Distance.COSINE))count = 0file_dir = os.path.join(root_path, file_path)for root_path, dirs, files in os.walk(file_dir):for file in tqdm.tqdm(files):file_path = os.path.join(root_path, file)with open(file_path, "r", encoding="utf-8") as f:text = f.readlines()for per_line in text:parts = per_line.split("##")item = text2embedding(parts[1])client.upsert(collection_name=collection_name,wait=True,points=[PointStruct(id=count, vector=list([float(x) for x in item.tolist()]),payload={"title": parts[0], "response": parts[1]})])count += 1參考:基于GPT3.5實現(xiàn)本地知識庫解決方案-利用向量數(shù)據(jù)庫和GPT向量接口-實現(xiàn)智能回復(fù)并限制ChatGPT回答的范圍
4.3 搜索引擎構(gòu)建
主要使用查詢新聞的接口:
from typing import Listfrom duckduckgo_search import ddg_newsdef get_news(keywords: str) -> List[dict]:return ddg_news(keywords, safesearch='Off', time='w', max_results=5)4.4 增加上下文后的效果
增加上下文作為prompt一部分后的效果:
 
 
 
5.主要代碼
5.1 功能介紹
import streamlit as st from streamlit_chat import messagefrom inference import load_llm_model from search_engine import get_news from gen_embedding import text2embedding from vector_database import result4searchMAX_TURNS = 20 MAX_BOXES = MAX_TURNS * 2st.set_page_config(layout="wide")def generate_answer(root_path, prompt, history):# 加載模型tokenizer, model = load_llm_model(root_path, "ChatGLM-6B\\chatglm-6b-int4")with container:if len(history) > 0:for i, (query, response) in enumerate(history):message(query, avatar_style="big-smile", key=str(i) + "_user")message(response, avatar_style="bottts", key=str(i))message(prompt, avatar_style="big-smile", key=str(len(history)) + "_user")st.write("AI正在回復(fù):")with st.empty():for response, history in model.stream_chat(tokenizer,prompt,history,max_length=max_length,top_p=top_p,temperature=temperature):query, response = history[-1]st.write(response)return historydef button_reset_event():st.session_state["state"] = []if __name__ == "__main__":model_root_path = "D:\\GitHub\\LLM-Weights\\"container = st.container()# chatbot logo and titlest.image("main_page_logo.png", width=64)st.title("A Chatbot powered by ChatGLM-6b")max_length = st.sidebar.slider('max_length', 0, 4096, 2048, step=1)top_p = st.sidebar.slider('top_p', 0.0, 1.0, 0.6, step=0.01)temperature = st.sidebar.slider('temperature', 0.0, 1.0, 0.95, step=0.01)st.session_state["state"] = []# create a prompt text for the text generationprompt_text = st.text_area(label="用戶命令輸入",height=100,placeholder="請在這兒輸入您的命令")# set buttoncol1, col2 = st.columns([0.1, 0.9], gap="small")with col1:button_send = st.button("send", key="generate_answer")with col2:button_reset = st.button("reset", on_click=button_reset_event())if button_send:# news from web search enginesearch_news = get_news(prompt_text)if (search_news is not None) and len(search_news[0]) >= 1:relevant_news = get_news(prompt_text)[0]["body"]else:relevant_news = ""# knowledge from databasedatabase_answer = result4search(text2embedding(prompt_text))[0]if database_answer is not None:relevant_answer = database_answer["response"]else:relevant_answer = ""prompt_text = "問題:" + prompt_text + ",請參考以下內(nèi)容生成答案:" + relevant_news + "。" + relevant_answerwith st.spinner("AI正在思考,請稍等........"):st.session_state["state"] = generate_answer(model_root_path,prompt_text,st.session_state["state"])5.2 代碼下載
chopinchenx/Bubble: A private chatbot deployed on local. (github.com)
總結(jié)
以上是生活随笔為你收集整理的本地搭建属于自己的ChatGPT:基于PyTorch+ChatGLM-6b+Streamlit+QDrant+DuckDuckGo的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: python求数独全解
 - 下一篇: webshell、一句话木马的权限问题