跳到主要内容
极客日志极客日志
首页博客AI提示词GitHub精选代理工具
搜索
|注册
博客列表
PythonAI算法

MetaLlama 大模型从入门到部署实战指南

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

赛博行者发布于 2025/2/6更新于 2026/5/713 浏览
MetaLlama 大模型从入门到部署实战指南

MetaLlama 大模型介绍

LLaMA 是一个基础语言模型的集合,参数范围从 7B 到 65B。我们在数万亿个 Token 上训练我们的模型,并表明可以专门使用公开可用的数据集来训练最先进的模型,而无需诉诸专有的和无法访问的数据集。特别是,LLaMA-13B 在大多数基准测试中都优于 GPT-3 (175B)。

Llama 2 大模型介绍

我们开发并发布了 Llama 2,这是一组经过预训练和微调的大型语言模型 (LLM),其参数规模从 70 亿到 700 亿不等。我们经过微调的大语言模型(称为 Llama 2-Chat)针对对话用例进行了优化。我们的模型在我们测试的大多数基准上都优于开源聊天模型,并且根据我们对有用性和安全性的人工评估,可能是闭源模型的合适替代品。

Code Llama 模型

Code Llama 是一个基于 Llama 2 的大型代码语言模型系列,在开放模型、填充功能、对大输入上下文的支持以及编程任务的零样本指令跟踪能力中提供最先进的性能。我们提供多种风格来覆盖广泛的应用程序:基础模型 (Code Llama)、Python 专业化 (Code Llama - Python) 和指令跟随模型 (Code Llama - Instruct),每个模型都有 7B、13B 和 34B 参数。所有模型均在 16k 个标记序列上进行训练,并在最多 100k 个标记的输入上显示出改进。

Code Llama 主要模型列表

Base ModelPythonInstruct
7Bcodellama/CodeLlama-7b-hfcodellama/CodeLlama-7b-Python-hfcodellama/CodeLlama-7b-Instruct-hf
13Bcodellama/CodeLlama-13b-hfcodellama/CodeLlama-13b-Python-hfcodellama/CodeLlama-13b-Instruct-hf
34Bcodellama/CodeLlama-34b-hfcodellama/CodeLlama-34b-Python-hfcodellama/CodeLlama-34b-Instruct-hf

申请与获取模型

申请地址通常通过 Hugging Face 进行。申请通过后,如果邮箱一致,会提示已经授权。在 Hugging Face 上下载模型权重后,需确保本地环境配置正确。

使用模型

主要有以下几种使用方式:

  • 使用官方的 API
  • 使用第三方封装 API(llama.cpp-python, ollama)
  • 使用 LangChain
  • 使用 Hugging Face 的 Transformers

官方示例运行

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 错误,建议改用其他推理方案。

llama.cpp

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

llama-cpp-python

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

启动 API 模式

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

  • 官网
  • GitHub
  • Docker
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

基于 LangChain 使用 Llama

使用 LangChain 调用本地模型

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)

使用 LangChain 结合 API 服务

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)

基于 LangChain 与 Hugging Face

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('法国的首都在哪里'))

部署建议与注意事项

  1. 硬件要求:量化后的模型(如 Q4_0)可以在消费级显卡甚至 CPU 上运行,但推理速度会受限于内存带宽。建议至少配备 8GB 显存以流畅运行 7B 模型。
  2. 安全合规:Code Llama 等模型生成的代码可能受第三方许可约束,使用时请注意版权风险。同时,应用安全缓解措施以防止生成有害内容。
  3. 上下文长度:不同模型支持的上下文窗口不同,Code Llama 支持 16k 标记序列,部分变体支持更长。请根据任务需求选择合适的模型版本。
  4. 持续更新:大模型技术迭代迅速,建议关注官方仓库以获取最新的模型权重和修复补丁。

目录

  1. MetaLlama 大模型介绍
  2. Llama 2 大模型介绍
  3. Code Llama 模型
  4. Code Llama 主要模型列表
  5. 申请与获取模型
  6. 使用模型
  7. 官方示例运行
  8. llama.cpp
  9. 下载与编译
  10. 模型转换
  11. obtain the original LLaMA model weights and place them in ./models
  12. 65B 30B 13B 7B tokenizer_checklist.chk tokenizer.model
  13. install Python dependencies
  14. convert the 7B model to ggml FP16 format
  15. [Optional] for models using BPE tokenizers
  16. quantize the model to 4-bits (using q4_0 method)
  17. update the gguf filetype to current if older version is unsupported by another application
  18. run the inference
  19. 运行模式
  20. llama-cpp-python
  21. 启动 API 模式
  22. Ollama
  23. 基于 LangChain 使用 Llama
  24. 使用 LangChain 调用本地模型
  25. 使用 LangChain 结合 API 服务
  26. 基于 LangChain 与 Hugging Face
  27. 部署建议与注意事项
  • 💰 8折买阿里云服务器限时8折了解详情
  • GPT-5.5 超高智商模型1元抵1刀ChatGPT中转购买
  • 代充Chatgpt Plus/pro 帐号了解详情
  • 🤖 一键搭建Deepseek满血版了解详情
  • 一键打造专属AI 智能体了解详情
极客日志微信公众号二维码

微信扫一扫,关注极客日志

微信公众号「极客日志V2」,在微信中扫描左侧二维码关注。展示文案:极客日志V2 zeeklog

更多推荐文章

查看全部
  • Faster Whisper 语音识别高效实现与使用指南
  • FPGA 快速傅里叶变换(FFT)实现与配置
  • ThreadLocal 原理、使用场景及内存泄漏问题详解
  • Neo4j 社区版安装与使用指南
  • Java AI 辅助开发电商系统核心功能模块实战指南
  • whisper.cpp 模型选型实测:从 tiny 到 large-v3-turbo 速度与准确率对比
  • GitHub 7 款 Claude Skills 开源项目:Skill Creator、Superpowers 与 Code Review 实战指南
  • Python 爬虫实战:抓取小红书穿搭笔记数据
  • 2026 主流 AI 编程助手深度评测:文心快码、Copilot 与 Cursor
  • Python 基于 Vue 的黄山旅游网站设计与实现
  • 数据结构:顺序表与链表经典算法实战
  • Cursor 在 C/C++ 开发中的实战使用指南
  • OpenClaw Memory 本地模式配置指南:Ubuntu CUDA cuDNN llama.cpp
  • GitHub 使用指南:环境配置与版本控制流程
  • 专科生自学 Python 转行爬虫开发实战经历与面试复盘
  • MCAP:机器人数据容器的全面实践指南
  • OpenSpiel 进阶教程:用 C++ 与 Python 实现自定义博弈算法
  • openEuler 系统安装 Docker 容器完整教程
  • 黑客圈子真的都是闷声发大财的土豪吗?
  • Flutter 使用 eip55 库实现鸿蒙系统以太坊地址校验适配

相关免费在线工具

  • 加密/解密文本

    使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online

  • RSA密钥对生成器

    生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online

  • Mermaid 预览与可视化编辑

    基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online

  • 随机西班牙地址生成器

    随机生成西班牙地址(支持马德里、加泰罗尼亚、安达卢西亚、瓦伦西亚筛选),支持数量快捷选择、显示全部与下载。 在线工具,随机西班牙地址生成器在线工具,online

  • Gemini 图片去水印

    基于开源反向 Alpha 混合算法去除 Gemini/Nano Banana 图片水印,支持批量处理与下载。 在线工具,Gemini 图片去水印在线工具,online

  • curl 转代码

    解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online