Skip to content

DF1018/llama-langchain

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation


                                                     __----~~~~~~~~~~~------___
                                    .  .   ~~//====......          __--~ ~~
                    -.            \_|//     |||\\  ~~~~~~::::... /~
                 ___-==_       _-~o~  \/    |||  \\            _/~~-
         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
   .~       .~       |   \\ -_    /  /-   /   ||      \   /
  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
           '         ~-|      /|    |-~\~~       __--~~
                       |-~~-_/ |    |   ~\_   _-~            /\
                            /  \     \__   \/~                \__
                        _--~ _/ | .-~~____--~-/                  ~~==.
                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
                                  -_     ~\      ~~---l__i__i__i--~~_/
                                  _-~-__   ~)  \--______________--~~
                                //.-~~~-~_--~- |-------~~~~~~~~
                                       //.-~~~--\
                                神獸保佑,程式碼沒Bug!
  

在這裡感謝HERO的幫助

文件夾 - folder tree

langchain_llama  
    -model (放llama ,text2vec-base-chinese的位置)  
    -script (放llama_langChain_embedding.py)  
        -data (放資料 .txt)  
    Dockerfile  
    requirement.txt  

llama的model 可以去DELL 或 4060的studio的server 那邊拿

DELL(Dell7920) ------ /mnt/hdd1_4t/james_df/langchain/models
4060(PC0137) ------ /mnt/sda1/DF/llama_chain/models

llama model選用

我是用了

這兩個TW中文llama2

7B比較輕量 一開始預備初始化會比較快 跟 推理會比較快 當然回答精準度不夠好 應該是原作者預訓練不夠多 導致LLM的中文理解能力不夠好

13B是比較吃空間大 一開始預備初始化比較慢 跟 推理會比較7B慢一點點 當然回答精準度比7B好一點點

目前(2024/01/08)

我只找到這兩個比較靠譜的中文LLM

餘下的都是簡體字大陸的組織訓練出來的LLM

langchain embeddings的部分

為什麼用這個model 有兩個原因

  1. 這個model是輕量級 還有是專用在中文embeddings的 無論是繁體字或簡體字
  2. 大部分用家都一致好評 (主要是這個) text2vec-base-chinese 表现最好

千萬不要用llama-cpp跟langchain 或者 把llama變成ggml

GGML的講解 Chatbot开发三剑客:LLAMA、LangChain和Python

ggml雖然是輕量級 壓縮過的框架

簡單的推理/普通問問題 的執行速度是快的

對langchain來說 慢到死.....................

以下都是cpp慢到死的評論
Extremely slow?
Llama-cpp-python is slower than llama.cpp by more than 25%. Let's get it resolved
Speed too slow

我自己也試用cpp的程式 也可能是我寫不對 所以執行速度可能有點奇怪
可以看我的程式碼
llama_cpp.py

在embeddings的向量庫方面 我用了

Chroma向量数据库完全手册

但是在用linux docker的時候 無法更新sqlite3
Chroma requires SQLite > 3.35, if you encounter issues with having too low of a SQLite version please try the following.

所以我用了
sys.modules["sqlite3"] = sys.modules.pop("pysqlite3")
覆寫預設的 sqlite3 函式庫

chroma的覆寫sqlite3

llama & langchain 的結合

整個langchain可以參考Langchain - Mistral 7B 模型實作day-1

量化模型的設定

gpu的設定

38-60的程式碼 都是量化llama2

Alt text

目前量化到int4/4bit,如果之後可以跑更好的設備,建議跑16bit以上

可以參考 如何利用量化在低資源情況下執行70B的LLaMA2模型?

model的設定

model的設定

HuggingFacePipeline

創建一個HuggingFacePipeline實例,用於後續的語言生成
huggingface pipeline

載入文本 & embedding

content&embedding

llama的system prompt

system prompt格式可以參考以下Make a chain的部份
YT Customizing the Prompt - RetrievalQA withLLaMA2-7B .ipynb

llama_system_prompt

定義prompt模板 & 創建一個RetrievalQA

Alt text

執行QA,得到回應

Alt text

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages