日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

本地搭建属于自己的ChatGPT:基于PyTorch+ChatGLM-6b+Streamlit+QDrant+DuckDuckGo

發布時間:2024/1/8 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 本地搭建属于自己的ChatGPT:基于PyTorch+ChatGLM-6b+Streamlit+QDrant+DuckDuckGo 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


本地部署chatglm及緩解時效性問題的思路:

模型使用chatglm-6b 4bit,推理使用hugging face,前端應用使用streamlit或者gradio。

微調對顯存要求較高,還沒試驗。可以結合LoRA進行微調。

緩解時效性問題:通過本地數據庫或者搜索引擎獲取特有數據或者實時數據作為生成模型的上下文。

  • 向量數據庫實現思路:先將特有數據轉換為embedding存入向量數據庫,在調用模型生成問答時,先將query轉換成embedding,然后從數據庫查詢相近的結果作為上下文。embedding生成可以使用sentence_transformer庫,向量數據庫可以使用qdrant或者milvus。
  • 搜索引擎實現思路:在調用大模型生成問答時,先用搜索引擎搜索相關的詞條,將詞條內容或者摘要作為上下文輸入到模型。搜索引擎可以使用duckduckgo_search庫。

1.運行環境配置

windows 11

32G 內存

GTX 3080Ti

1.1 PyTorch

安裝anaconda或者miniconda

創建虛擬環境:

conda create -n chatbot python=3.9

激活虛擬環境:

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 nvidia

2)hugging face

conda install -c huggingface transformers

3)streamlit

pip install streamlit pip install streamlit-chat

4)sentencepiece 和 cpm-kernels

pip install sentencepiece pip install cpm-kernels

5)sentence-transformers

conda install -c conda-forge sentence-transformers

6)qdrant-client

pip install qdrant-client

7)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

導出虛擬環境的依賴包命令:

conda env export > freeze.ymlpip list --format=freeze > ./requirements.txt

1.3 Docker

用于運行QDrant數據庫:

可以參考: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.大模型構建

2.1 開源模型

ChatGLM

從github下載chatglm-6b工程:THUDM/ChatGLM-6B

從抱抱臉下載chatglm-6b-int4模型:THUDM/chatglm-6b-int4

官方介紹:

ChatGLM-6B 是一個開源的、支持中英雙語問答的對話語言模型,基于 General Language Model (GLM) 架構,具有 62 億參數。結合模型量化技術,用戶可以在消費級的顯卡上進行本地部署(INT4 量化級別下最低只需 6GB 顯存)。ChatGLM-6B 使用了和 ChatGLM 相同的技術,針對中文問答和對話進行了優化。經過約 1T 標識符的中英雙語訓練,輔以監督微調、反饋自助、人類反饋強化學習等技術的加持,62 億參數的 ChatGLM-6B 已經能生成相當符合人類偏好的回答。

ChatGLM-6B-INT4 是 ChatGLM-6B 量化后的模型權重。具體的,ChatGLM-6B-INT4 對 ChatGLM-6B 中的 28 個 GLM Block 進行了 INT4 量化,沒有對 Embedding 和 LM Head 進行量化。量化后的模型理論上 6G 顯存(使用 CPU 即內存)即可推理,具有在嵌入式設備(如樹莓派)上運行的可能。

在 CPU 上運行時,會根據硬件自動編譯 CPU Kernel ,請確保已安裝 GCC 和 OpenMP (Linux一般已安裝,對于Windows則需手動安裝),以獲得最佳并行計算能力。

其他大模型

模型作者開源協議鏈接
BLOOMGoogleApache-2.0https://huggingface.co/bigscience/bloom
ColossoalAIApache-2.0https://colossalai.org/zh-Hans/
LLaMahttps://github.com/facebookresearch/llama
Alpacahttps://crfm.stanford.edu/2023/03/13/alpaca.html
T5https://huggingface.co/docs/transformers/model_doc/t5
CerebrasApache-2.0https://huggingface.co/cerebras/Cerebras-GPT-6.7B
文心一言
通義千問
盤古

2.2 微調

對顯存要求較高,暫未試驗。

制作微調數據集

可以參考:

huang1332/finetune_dataset_maker

基于LoRA/P-Tuning進行微調

可以參考:

極低資源微調大模型方法LoRA以及BLOOM-LORA實現代碼

ChatGLM-6B/ptuning

mymusise/ChatGLM-Tuning

2.3 推理

Hugging Face

from transformers import AutoModel, AutoTokenizer

模型采樣算法

ChatGPT有兩個重要的參數是temperature和top_p,HuggingFace的AutoModel有兩個類似的參數是temperature和top_k。上述這三個方法都是采樣方法,用于因果語言模型中在給定上下文的情景下預測下一個單詞出現的概率。

在進行預訓練時,往往使用“完形填空”的形式,例如給定上文預測下文。基于貪心策略的思路是選擇下文單詞概率最大的單詞,但是這樣會讓大模型的注意力只集中在最近的幾個單詞(token)上,導致最終模型生成效果會非常生硬和可預測。

為了讓模型具有一定的創新性(隨機性),可以使用基于分布采樣的生成采樣算法。

Top-k采樣從排名前 k (即采樣列表的大小為k)的token種進行抽樣,允許其他分數或概率較高的token也有機會被選中。在很多情況下,這種抽樣帶來的隨機性有助于提高生成質量

Top-k采樣的缺點是k的取值不好確定,無法保證最優。所以ChatGPT引入了動態設置k大小的策略——即刻采樣(Nucleus Sampling)。top-p 值通常設置為比較高的值(如0.75),目的是限制低概率token的長尾。可以同時使用top-k和top-p,top-p策略在top-k策略之后生效。

溫度采樣受統計熱力學的啟發,高溫意味著更可能遇到低能態。在概率模型中,logits扮演著能量的角色,我們可以通過將logits除以溫度來實現溫度采樣,然后將其輸入Softmax并獲得采樣概率。

總的來說,溫度參數用來調整候選詞的概率分布。溫度越低,模型對其首選越有信心;溫度>1度會降低信心,模型不確定性增加,趨近于正無窮的溫度相當于均勻采樣(候選詞的概率都相同,完全隨機)。通常,溫度設在[0.7, 0.9]之間是創造性任務最常見的溫度。

參考:ChatGPT模型采樣算法詳解

3.前端應用

3.1 Streamlit

ChatGLM工程中提供了兩個demo,基于streamlit的是其中之一,另一個是基于gradio的。

https://streamlit.io/

3.2 LangChain

LangChain是一個用于開發由語言模型驅動的應用程序的框架。它提供了一套工具、組件和接口,可簡化創建由大型語言模型 (LLM) 和聊天模型提供支持的應用程序的過程。LangChain 可以輕松管理與語言模型的交互,將多個組件鏈接在一起,并集成額外的資源,例如 API 和數據庫。

https://docs.langchain.com/docs/

https://zhuanlan.zhihu.com/p/620529542

3.3 展示效果

4.時效性問題解決方案

核心思路:通過本地數據庫或者搜索引擎獲取特有數據或者實時數據作為生成模型的上下文。

向量數據庫實現思路:先將特有數據轉換為embedding存入向量數據庫,在調用模型生成問答時,先將query轉換成embedding,然后從數據庫查詢相近的結果作為上下文。

1)embedding生成可以使用sentence_transformer庫

2)向量數據庫可以使用qdrant或者milvus

搜索引擎實現思路:在調用大模型生成問答時,先用搜索引擎搜索相關的詞條,將詞條內容或者摘要作為上下文輸入到模型。

1)搜索引擎庫可以使用duckduckgo_search包

大模型使用chatglm-6b 4bit,推理使用hugging face,前端應用使用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-v1

4.2 向量數據庫構建

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實現本地知識庫解決方案-利用向量數據庫和GPT向量接口-實現智能回復并限制ChatGPT回答的范圍

4.3 搜索引擎構建

主要使用查詢新聞的接口:

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正在回復:")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 + ",請參考以下內容生成答案:" + 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)

總結

以上是生活随笔為你收集整理的本地搭建属于自己的ChatGPT:基于PyTorch+ChatGLM-6b+Streamlit+QDrant+DuckDuckGo的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。