模型概述
模型特點
模型能力
使用案例
🚀 金融聊天GGUF模型
本項目提供了金融聊天模型的GGUF格式文件,可用於本地部署和運行金融領域的文本生成任務。藉助這些模型文件,用戶能夠在多種客戶端和庫中實現金融相關的對話和問答功能。
🚀 快速開始
本倉庫包含 finance-chat 模型的GGUF格式文件。GGUF是一種新的模型格式,被眾多客戶端和庫所支持。
✨ 主要特性
- 廣泛的兼容性:支持多種客戶端和庫,如 llama.cpp、text-generation-webui、Ollama 等。
- 多量化格式:提供多種不同的量化格式,用戶可按需選擇。
- 高性能:藉助 GPU 加速,實現高效的文本生成。
📦 安裝指南
安裝依賴庫
推薦使用 huggingface-hub
Python 庫下載模型文件:
pip3 install huggingface-hub
若要加速高速連接(1Gbit/s 或更高)下的下載,可安裝 hf_transfer
:
pip3 install hf_transfer
並設置環境變量 HF_HUB_ENABLE_HF_TRANSFER
為 1
:
HF_HUB_ENABLE_HF_TRANSFER=1 huggingface-cli download andrijdavid/finance-chat-GGUF finance-chat-f16.gguf --local-dir . --local-dir-use-symlinks False
在 Windows 命令行中,可按如下方式設置環境變量(以 PowerShell 為例,如使用 NVIDIA CUDA):
$env:CMAKE_ARGS = "-DLLAMA_OPENBLAS=on"
pip install llama-cpp-python
下載模型文件
自動下載
以下客戶端/庫會自動為你下載模型,並提供可用模型列表供你選擇:
- LM Studio
- LoLLMS Web UI
- Faraday.dev
在 text-generation-webui
中下載
在“Download Model”下,輸入模型倉庫地址 andrijdavid/finance-chat-GGUF
,並在下方輸入具體的文件名,如 finance-chat-f16.gguf
,然後點擊“Download”。
命令行下載
可使用以下命令將單個模型文件高速下載到當前目錄:
huggingface-cli download andrijdavid/finance-chat-GGUF finance-chat-f16.gguf --local-dir . --local-dir-use-symlinks False
若要一次下載多個文件,可使用通配符:
huggingface-cli download andrijdavid/finance-chat-GGUF --local-dir . --local-dir-use-symlinks False --include='*Q4_K*gguf'
更多關於 huggingface-cli
下載的文檔,請參考:HF -> Hub Python Library -> Download files -> Download from the CLI。
💻 使用示例
基礎用法
在 llama.cpp
中運行
確保你使用的是 d0cee0d 或更高版本的 llama.cpp
。
./main -ngl 35 -m finance-chat-f16.gguf --color -c 4096 --temp 0.7 --repeat_penalty 1.1 -n -1 -p "<PROMPT>"
-ngl
:設置要卸載到 GPU 的層數,若系統無 GPU 加速,可移除該參數。-c
:設置所需的序列長度,更長的序列長度需要更多資源,可按需調整。 若要進行聊天式對話,可將-p <PROMPT>
參數替換為-i -ins
。 更多參數及使用方法,請參考 the llama.cpp documentation。
在 text-generation-webui
中運行
具體說明可參考 text-generation-webui 文檔。
在 Python 代碼中使用
可使用 llama-cpp-python 或 ctransformers 庫從 Python 中使用 GGUF 模型。建議使用 llama-cpp-python
。
安裝 llama-cpp-python
包
根據系統不同,運行以下命令之一:
# 無 GPU 加速的基礎安裝
pip install llama-cpp-python
# 使用 NVIDIA CUDA 加速
CMAKE_ARGS="-DLLAMA_CUBLAS=on" pip install llama-cpp-python
# 使用 OpenBLAS 加速
CMAKE_ARGS="-DLLAMA_BLAS=ON -DLLAMA_BLAS_VENDOR=OpenBLAS" pip install llama-cpp-python
# 使用 CLBLast 加速
CMAKE_ARGS="-DLLAMA_CLBLAST=on" pip install llama-cpp-python
# 使用 AMD ROCm GPU 加速(僅適用於 Linux)
CMAKE_ARGS="-DLLAMA_HIPBLAS=on" pip install llama-cpp-python
# 使用 Metal GPU 加速(僅適用於 macOS 系統)
CMAKE_ARGS="-DLLAMA_METAL=on" pip install llama-cpp-python
簡單的 llama-cpp-python
示例代碼
from llama_cpp import Llama
# 設置要卸載到 GPU 的層數,若系統無 GPU 加速,設置為 0
llm = Llama(
model_path="./finance-chat-f16.gguf", # 先下載模型文件
n_ctx=32768, # 最大序列長度,更長的序列長度需要更多資源
n_threads=8, # CPU 線程數,可根據系統和性能調整
n_gpu_layers=35 # 要卸載到 GPU 的層數,若有 GPU 加速可用
)
# 簡單推理示例
output = llm(
"<PROMPT>", # 提示信息
max_tokens=512, # 生成最多 512 個 token
stop=["</s>"], # 示例停止標記,具體模型可能不同,請使用前檢查
echo=True # 是否回顯提示信息
)
# 聊天完成 API
llm = Llama(model_path="./finance-chat-f16.gguf", chat_format="llama-2") # 根據使用的模型設置聊天格式
llm.create_chat_completion(
messages = [
{"role": "system", "content": "You are a story writing assistant."},
{
"role": "user",
"content": "Write a story about llamas."
}
]
)
高級用法
使用 LangChain
以下是使用 llama-cpp-python
和 ctransformers
與 LangChain 結合的指南:
📚 詳細文檔
關於 GGUF 格式
GGUF 是 llama.cpp 團隊在 2023 年 8 月 21 日引入的新格式,用於替代不再被 llama.cpp 支持的 GGML 格式。以下是已知支持 GGUF 的客戶端和庫的不完全列表:
- llama.cpp:GGUF 的源項目,提供命令行界面(CLI)和服務器選項。
- text-generation-webui:最廣泛使用的 Web UI,功能豐富,支持 GPU 加速。
- Ollama:輕量級可擴展框架,用於本地構建和運行語言模型,具有簡單 API。
- KoboldCpp:全面的 Web UI,支持所有平臺和架構的 GPU 加速,以講故事功能著稱。
- GPT4All:免費開源的本地 GUI,支持 Windows、Linux 和 macOS,全 GPU 加速。
- LM Studio:適用於 Windows 和 macOS(Silicon)的直觀強大本地 GUI,支持 GPU 加速。
- LoLLMS Web UI:具有多種獨特功能的 Web UI,包含全面的模型庫。
- Faraday.dev:適用於 Windows 和 macOS 的有吸引力的基於字符的聊天 GUI,支持 GPU 加速。
- llama-cpp-python:Python 庫,支持 GPU 加速、LangChain 和 OpenAI 兼容的 API 服務器。
- candle:基於 Rust 的 ML 框架,注重性能,支持 GPU。
- ctransformers:Python 庫,支持 GPU 加速、LangChain 和 OpenAI 兼容的 AI 服務器。
- localGPT:開源項目,支持與文檔進行私密對話。
量化方法說明
點擊查看詳情
新的量化方法如下: - GGML_TYPE_Q2_K:“type-1” 2 位量化,每個超級塊包含 16 個塊,每個塊有 16 個權重。塊的縮放和最小值用 4 位量化,每個權重有效使用 2.5625 位。 - GGML_TYPE_Q3_K:“type-0” 3 位量化,每個超級塊包含 16 個塊,每個塊有 16 個權重。縮放用 6 位量化,每個權重使用 3.4375 位。 - GGML_TYPE_Q4_K:“type-1” 4 位量化,每個超級塊包含 8 個塊,每個塊有 32 個權重。縮放和最小值用 6 位量化,每個權重使用 4.5 位。 - GGML_TYPE_Q5_K:“type-1” 5 位量化,超級塊結構與 GGML_TYPE_Q4_K 相同,每個權重使用 5.5 位。 - GGML_TYPE_Q6_K:“type-0” 6 位量化,每個超級塊有 16 個塊,每個塊有 16 個權重。縮放用 8 位量化,每個權重使用 6.5625 位。原始模型信息
本項目的原始模型為 finance-chat,它基於 LLaMA-2-Chat-7B 開發,通過閱讀 comprehension 方法進行微調,在金融領域表現出色。
模型特點
- 領域適配:通過在特定領域語料上繼續預訓練,使模型具備豐富的金融領域知識。
- 高性能:在金融領域的問答任務中表現優異,可與大型特定領域模型如 BloombergGPT-50B 相媲美。
更新記錄
- 2024/1/16:🎉 研究論文 Adapting Large Language Models via Reading Comprehension 被 ICLR 2024 接收!🎉
- 2023/12/19:發佈基於 LLaMA-1-13B 開發的 13B 基礎模型。
- 2023/12/8:發佈基於 LLaMA-2-Chat-7B 開發的 聊天模型。
- 2023/9/18:發佈 論文、代碼、數據 和基於 LLaMA-1-7B 開發的 基礎模型。
特定領域模型
- LLaMA-1-7B:開發了三個特定領域模型,分別為 Biomedicine-LLM、Finance-LLM 和 Law-LLM。
- LLaMA-1-13B:將基礎模型擴展到 LLaMA-1-13B,開發了 Biomedicine-LLM-13B、Finance-LLM-13B 和 Law-LLM-13B。
- LLaMA-2-Chat:開源了不同領域的聊天模型,包括 Biomedicine-Chat、Finance-Chat 和 Law-Chat。
與金融聊天模型交互示例
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained("AdaptLLM/finance-chat")
tokenizer = AutoTokenizer.from_pretrained("AdaptLLM/finance-chat")
# 輸入問題
user_input = '''Use this fact to answer the question: Title of each class Trading Symbol(s) Name of each exchange on which registered
Common Stock, Par Value $.01 Per Share MMM New York Stock Exchange
MMM Chicago Stock Exchange, Inc.
1.500% Notes due 2026 MMM26 New York Stock Exchange
1.750% Notes due 2030 MMM30 New York Stock Exchange
1.500% Notes due 2031 MMM31 New York Stock Exchange
Which debt securities are registered to trade on a national securities exchange under 3M's name as of Q2 of 2023?'''
# 應用 LLaMA-2-Chat 演示的提示模板和系統提示
our_system_prompt = "\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.\n" # 請勿更改此內容
prompt = f"<s>[INST] <<SYS>>{our_system_prompt}<</SYS>>\n\n{user_input} [/INST]"
# # 若要應用自定義系統提示,可按以下方式集成
# your_system_prompt = "Please, check if the answer can be inferred from the pieces of context provided."
# prompt = f"<s>[INST] <<SYS>>{our_system_prompt}<</SYS>>\n\n{your_system_prompt}\n{user_input} [/INST]"
inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False).input_ids.to(model.device)
outputs = model.generate(input_ids=inputs, max_length=4096)[0]
answer_start = int(inputs.shape[-1])
pred = tokenizer.decode(outputs[answer_start:], skip_special_tokens=True)
print(f'### 用戶輸入:\n{user_input}\n\n### 助手輸出:\n{pred}')
特定領域任務
為方便重現實驗結果,上傳了每個特定領域任務的零樣本/少樣本輸入指令和輸出完成情況:biomedicine-tasks、finance-tasks 和 law-tasks。
注意:這些填充的指令是為未對齊的模型量身定製的,不適用於聊天模型所需的特定數據格式。
🔧 技術細節
本項目的模型基於 LLaMA-2-Chat-7B 開發,通過在金融領域的語料上進行繼續預訓練和微調,使其具備金融領域的專業知識。在量化方面,採用了多種量化方法,以平衡模型的大小和性能。
📄 許可證
本項目使用 llama2 許可證。
引用
如果您覺得本項目的工作有幫助,請引用以下文獻:
@article{adaptllm,
title = {Adapting Large Language Models via Reading Comprehension},
author = {Daixuan Cheng and Shaohan Huang and Furu Wei},
journal = {CoRR},
volume = {abs/2309.09530},
year = {2023}
}



