MetaLlama 大模型从入门到部署实战指南
MetaLlama 系列大模型涵盖 7B 至 70B 参数规模,包含基础版与对话优化版。本文详细介绍 LLaMA 及 Code Llama 模型特性,提供基于 Hugging Face 的获取方式。重点讲解多种本地部署方案,包括 llama.cpp 量化运行、Ollama 服务化以及通过 LangChain 集成调用。内容涵盖代码示例、环境配置及常见错误处理,适合开发者快速上手大模型应用开发。

MetaLlama 系列大模型涵盖 7B 至 70B 参数规模,包含基础版与对话优化版。本文详细介绍 LLaMA 及 Code Llama 模型特性,提供基于 Hugging Face 的获取方式。重点讲解多种本地部署方案,包括 llama.cpp 量化运行、Ollama 服务化以及通过 LangChain 集成调用。内容涵盖代码示例、环境配置及常见错误处理,适合开发者快速上手大模型应用开发。

LLaMA 是一个基础语言模型的集合,参数范围从 7B 到 65B。我们在数万亿个 Token 上训练我们的模型,并表明可以专门使用公开可用的数据集来训练最先进的模型,而无需诉诸专有的和无法访问的数据集。特别是,LLaMA-13B 在大多数基准测试中都优于 GPT-3 (175B)。
我们开发并发布了 Llama 2,这是一组经过预训练和微调的大型语言模型 (LLM),其参数规模从 70 亿到 700 亿不等。我们经过微调的大语言模型(称为 Llama 2-Chat)针对对话用例进行了优化。我们的模型在我们测试的大多数基准上都优于开源聊天模型,并且根据我们对有用性和安全性的人工评估,可能是闭源模型的合适替代品。
Code Llama 是一个基于 Llama 2 的大型代码语言模型系列,在开放模型、填充功能、对大输入上下文的支持以及编程任务的零样本指令跟踪能力中提供最先进的性能。我们提供多种风格来覆盖广泛的应用程序:基础模型 (Code Llama)、Python 专业化 (Code Llama - Python) 和指令跟随模型 (Code Llama - Instruct),每个模型都有 7B、13B 和 34B 参数。所有模型均在 16k 个标记序列上进行训练,并在最多 100k 个标记的输入上显示出改进。
| Base Model | Python | Instruct | |
|---|---|---|---|
| 7B | codellama/CodeLlama-7b-hf | codellama/CodeLlama-7b-Python-hf | codellama/CodeLlama-7b-Instruct-hf |
| 13B | codellama/CodeLlama-13b-hf | codellama/CodeLlama-13b-Python-hf | codellama/CodeLlama-13b-Instruct-hf |
| 34B | codellama/CodeLlama-34b-hf | codellama/CodeLlama-34b-Python-hf | codellama/CodeLlama-34b-Instruct-hf |
申请地址通常通过 Hugging Face 进行。申请通过后,如果邮箱一致,会提示已经授权。在 Hugging Face 上下载模型权重后,需确保本地环境配置正确。
主要有以下几种使用方式:
torchrun --nproc_per_node 1 example_text_completion.py \
--ckpt_dir llama-2-7b/ \
--tokenizer_path tokenizer.model \
--max_seq_len 128 --max_batch_size 4
注意: Windows 和 Mac 上基本跑不起来,因为 torchrun 依赖 NCCL。如果遇到 RuntimeError: Distributed package doesn't have NCCL built in 错误,建议改用其他推理方案。
Port of Facebook's LLaMA model in C/C++。因为很多同学受限于个人电脑的环境,没法运行完整的 llama 模型。llama.cpp 提供了一个非常好的移植版本,可以降低电脑的硬件要求,方便个人电脑运行与测试。
git clone https://github.com/ggerganov/llama.cpp.git
cd llama.cpp
make
通过对模型进行转化,可以降低资源消耗。
# obtain the original LLaMA model weights and place them in ./models
ls ./models
# 65B 30B 13B 7B tokenizer_checklist.chk tokenizer.model
# install Python dependencies
python3 -m pip install -r requirements.txt
# convert the 7B model to ggml FP16 format
python3 convert.py models/7B/
# [Optional] for models using BPE tokenizers
python convert.py models/7B/ --vocabtype bpe
# quantize the model to 4-bits (using q4_0 method)
./quantize ./models/7B/ggml-model-f16.gguf ./models/7B/ggml-model-q4_0.gguf q4_0
# update the gguf filetype to current if older version is unsupported by another application
./quantize ./models/7B/ggml-model-q4_0.gguf ./models/7B/ggml-model-q4_0-v2.gguf COPY
# run the inference
./main -m ./models/7B/ggml-model-q4_0.gguf -n 128
此步可以省略,直接下载别人转换好的量化模型即可。
命令行交互模式:
./main -m ./models/llama-2-7b.Q4_0.gguf -i -n 256 --color
开启 server 模式,访问 API:
./server -m ./models/llama-2-7b.Q4_0.gguf
pip install llama-cpp-python
mac m1 上构建的时候需要加上特殊的参数:
CMAKE_ARGS="-DLLAMA_METAL=on -DCMAKE_OSX_ARCHITECTURES=arm64" FORCE_CMAKE=1 pip install -U llama-cpp-python --no-cache-dir --force-reinstall
pip install llama-cpp-python[server]
python -m llama_cpp.server --model models/llama-2-7b.Q4_0.gguf
python -m llama_cpp.server --model models/llama-2-7b.Q4_0.gguf --n_gpu_layers 1
访问文档可以看到 API 的文档,与 OpenAI 兼容。
ollama serve codellama:7b
日志输出示例:
2023/10/08 02:31:04 images.go:987: total blobs: 6
2023/10/08 02:31:04 images.go:994: total unused blobs removed: 0
2023/10/08 02:31:04 routes.go:535: Listening on 127.0.0.1:11434
def test_llama_cpp_local():
"""
使用本地模型
:return:
"""
from langchain.llms import LlamaCpp
llm = LlamaCpp(
model_path="/Users/seveniruby/projects/llama.cpp/models/llama-2-7b.Q4_0.gguf",
n_ctx=2048,
n_threads=4
)
output = llm("Q: 法国的首都在哪里\n A: ", echo=True, max_tokens=6, temperature=0)
print(output)
def test_llama_api():
from langchain.llms import OpenAI
# 配置为兼容 OpenAI 格式的本地 API
llm = OpenAI(
openai_api_key="sk-",
openai_api_base="http://localhost:8000/v1"
)
output = llm("Q: 法国的首都在哪里\n A: ")
print(output)
def test_pipeline():
from transformers import pipeline
pipe = pipeline(
"text-generation",
model="meta-llama/Llama-2-7b-hf",
torch_dtype=torch.float16,
device='mps', # 按需改成你的 cuda 或者 cpu
revision='main',
)
print(pipe('法国的首都在哪里'))

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online