模型简介
模型特点
模型能力
使用案例
🚀 金融聊天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}
}



