引言
近年来,大型语言模型(LLM)在自然语言处理领域引起了广泛关注。Llama.cpp 是其中一款开源项目,提供了在本地设备上运行 LLM 的能力。本文将介绍如何在 LangChain 中使用 llama-cpp-python,帮助开发者在本地执行推理任务。
主要内容
1. 安装 llama-cpp-python
Llama-cpp-python 是 Llama.cpp 的 Python 绑定,它提供多种安装选项以满足不同的硬件需求。
CPU 安装
使用以下命令安装:
%pip install --upgrade --quiet llama-cpp-python
GPU 加速安装
可以使用如 OpenBLAS、cuBLAS 等 BLAS 后端来加速处理速度:
!CMAKE_ARGS="-DLLAMA_CUBLAS=on" FORCE_CMAKE=1 pip install llama-cpp-python
若已安装纯 CPU 版本,需重新安装:
!CMAKE_ARGS="-DLLAMA_CUBLAS=on" FORCE_CMAKE=1 pip install --upgrade --force-reinstall llama-cpp-python --no-cache-dir
Metal 支持(MacOS)
对于 Apple Silicon 芯片,使用 Metal 框架:
!CMAKE_ARGS="-DLLAMA_METAL=on" FORCE_CMAKE=1 pip install llama-cpp-python
2. 使用 LangChain 和 llama-cpp-python
在 LangChain 中,LlamaCpp 提供了一个接口来调用本地 LLM 模型。以下是一个基本示例:
from langchain_community.llms import LlamaCpp
from langchain_core.callbacks import CallbackManager, StreamingStdOutCallbackHandler
from langchain_core.prompts import PromptTemplate
# 设置输出模板和回调管理器
template = """Question: {question}
Answer: Let's work this out in a step by step way to be sure we have the right answer."""
prompt = PromptTemplate.from_template(template)
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
# 初始化 LLM 设置
llm = LlamaCpp(
model_path="/Users/rlm/Desktop/Code/llama.cpp/models/openorca-platypus2-13b.gguf.q4_0.bin",
temperature=0.75,
max_tokens=2000,
top_p=1,
callback_manager=callback_manager,
verbose=True, # 必须设置为 True 以启用回调管理器
)
question = """
Question: A rap battle between Stephen Colbert and John Oliver
"""
llm.invoke(question)
代码示例
以下是如何在 LangChain 中使用 LlamaCpp 来回答一个问题的代码示例:
llm = LlamaCpp(
model_path="./ggml-model-q4_0.bin",
callback_manager=callback_manager,
verbose=True
)
llm_chain = prompt | llm
question = "What NFL team won the Super Bowl in the year Justin Bieber was born?"
llm_chain.invoke({"question": question})
常见问题和解决方案
- 模型文件加载失败:确保模型路径和文件正确无误。
- GPU 使用不当:确认已正确安装相应的 BLAS 后端,并调试
n_gpu_layers和n_batch参数。 - 性能问题:首次调用可能较慢,可通过调整
n_gpu_layers提升性能。
总结和进一步学习资源
通过结合使用 llama-cpp-python 和 LangChain,开发者能够灵活地在本地执行 LLM 推理任务。以下资源可帮助您进一步探索:
参考资料
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
---END---