<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>本地部署 | 林子杨的个人网站</title><link>https://ziyanglin.netlify.app/zh/tags/%E6%9C%AC%E5%9C%B0%E9%83%A8%E7%BD%B2/</link><atom:link href="https://ziyanglin.netlify.app/zh/tags/%E6%9C%AC%E5%9C%B0%E9%83%A8%E7%BD%B2/index.xml" rel="self" type="application/rss+xml"/><description>本地部署</description><generator>Source Themes Academic (https://sourcethemes.com/academic/)</generator><language>zh-Hans</language><lastBuildDate>Fri, 27 Jun 2025 02:00:00 +0000</lastBuildDate><image><url>https://ziyanglin.netlify.app/img/icon-192.png</url><title>本地部署</title><link>https://ziyanglin.netlify.app/zh/tags/%E6%9C%AC%E5%9C%B0%E9%83%A8%E7%BD%B2/</link></image><item><title>Ollama实用指南：本地部署与管理大型语言模型</title><link>https://ziyanglin.netlify.app/zh/post/ollama-documentation/</link><pubDate>Fri, 27 Jun 2025 02:00:00 +0000</pubDate><guid>https://ziyanglin.netlify.app/zh/post/ollama-documentation/</guid><description>&lt;h2 id="1-">1. 简介&lt;/h2>
&lt;p>Ollama 是一个强大的开源工具，旨在让用户能够轻松地在本地环境下载、运行和管理大型语言模型（LLM）。它的核心优势在于简化了部署和使用复杂模型的流程，使得开发者、研究人员和爱好者无需专业的硬件或复杂的配置，即可在个人计算机上体验和利用 state-of-the-art 的人工智能技术。&lt;/p>
&lt;p>&lt;strong>主要优势:&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>&lt;strong>易于使用:&lt;/strong> 通过简单的命令行指令，即可完成模型的下载、运行和交互。&lt;/li>
&lt;li>&lt;strong>跨平台支持:&lt;/strong> 支持 macOS, Windows, 和 Linux。&lt;/li>
&lt;li>&lt;strong>模型库丰富:&lt;/strong> 支持众多流行的开源模型，如 Llama 3, Mistral, Gemma, Phi-3 等。&lt;/li>
&lt;li>&lt;strong>高度可定制:&lt;/strong> 通过 &lt;code>Modelfile&lt;/code>，用户可以轻松地自定义模型的行为、系统提示和参数。&lt;/li>
&lt;li>&lt;strong>API 驱动:&lt;/strong> 提供 REST API，方便与其他应用程序和服务集成。&lt;/li>
&lt;li>&lt;strong>开源社区:&lt;/strong> 拥有活跃的社区，不断贡献新的模型和功能。&lt;/li>
&lt;/ul>
&lt;p>本篇文档将深入浅出地介绍 Ollama 的各项功能，从基础入门到高级应用，帮助您全面掌握这个强大的工具。&lt;/p>
&lt;hr>
&lt;h2 id="2-">2. 快速入门&lt;/h2>
&lt;p>本节将指导您完成 Ollama 的安装和基本使用。&lt;/p>
&lt;h3 id="21-">2.1 安装&lt;/h3>
&lt;p>访问 &lt;a href="https://ollama.com/">Ollama 官方网站&lt;/a> 下载适用于您操作系统的安装包并进行安装。&lt;/p>
&lt;h3 id="22-">2.2 运行第一个模型&lt;/h3>
&lt;p>安装完成后，打开终端（或命令提示符），使用 &lt;code>ollama run&lt;/code> 命令来下载并运行一个模型。例如，运行 Llama 3 模型：&lt;/p>
&lt;pre>&lt;code class="language-shell">ollama run llama3
&lt;/code>&lt;/pre>
&lt;p>首次运行时，Ollama 会自动从模型库下载所需的模型文件。下载完成后，您就可以直接在终端与模型进行对话。&lt;/p>
&lt;h3 id="23-">2.3 管理本地模型&lt;/h3>
&lt;p>您可以使用以下命令来管理本地已下载的模型：&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;strong>列出本地模型:&lt;/strong>&lt;/p>
&lt;pre>&lt;code class="language-shell">ollama list
&lt;/code>&lt;/pre>
&lt;p>该命令会显示所有已下载模型的名称、ID、大小和修改时间。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>移除本地模型:&lt;/strong>&lt;/p>
&lt;pre>&lt;code class="language-shell">ollama rm &amp;lt;model_name&amp;gt;
&lt;/code>&lt;/pre>
&lt;/li>
&lt;/ul>
&lt;hr>
&lt;h2 id="3-">3. 核心概念&lt;/h2>
&lt;h3 id="31-modelfile">3.1 Modelfile&lt;/h3>
&lt;p>&lt;code>Modelfile&lt;/code> 是 Ollama 的核心功能之一，它是一个类似于 &lt;code>Dockerfile&lt;/code> 的配置文件，允许您定义和创建自定义模型。通过 &lt;code>Modelfile&lt;/code>，您可以：&lt;/p>
&lt;ul>
&lt;li>指定基础模型。&lt;/li>
&lt;li>设置模型参数（如温度、top_p 等）。&lt;/li>
&lt;li>定义模型的系统提示（System Prompt）。&lt;/li>
&lt;li>自定义模型的交互模板。&lt;/li>
&lt;li>应用 LoRA 适配器。&lt;/li>
&lt;/ul>
&lt;p>一个简单的 &lt;code>Modelfile&lt;/code> 示例：&lt;/p>
&lt;pre>&lt;code class="language-Modelfile"># 指定基础模型
FROM llama3
# 设置模型温度
PARAMETER temperature 0.8
# 设置系统提示
SYSTEM &amp;quot;&amp;quot;&amp;quot;
You are a helpful AI assistant. Your name is Roo.
&amp;quot;&amp;quot;&amp;quot;
&lt;/code>&lt;/pre>
&lt;p>使用 &lt;code>ollama create&lt;/code> 命令基于 &lt;code>Modelfile&lt;/code> 创建新模型：&lt;/p>
&lt;pre>&lt;code class="language-shell">ollama create my-custom-model -f ./Modelfile
&lt;/code>&lt;/pre>
&lt;h3 id="32-">3.2 模型导入&lt;/h3>
&lt;p>Ollama 支持从外部文件系统导入模型，特别是从 &lt;code>Safetensors&lt;/code> 格式的权重文件。&lt;/p>
&lt;p>在 &lt;code>Modelfile&lt;/code> 中，使用 &lt;code>FROM&lt;/code> 指令并提供包含 &lt;code>safetensors&lt;/code> 文件的目录路径：&lt;/p>
&lt;pre>&lt;code class="language-Modelfile">FROM /path/to/safetensors/directory
&lt;/code>&lt;/pre>
&lt;p>然后使用 &lt;code>ollama create&lt;/code> 命令创建模型。&lt;/p>
&lt;h3 id="33-">3.3 多模态模型&lt;/h3>
&lt;p>Ollama 支持多模态模型（如 LLaVA），可以同时处理文本和图像输入。&lt;/p>
&lt;pre>&lt;code class="language-shell">ollama run llava &amp;quot;这张图片里有什么? /path/to/image.png&amp;quot;
&lt;/code>&lt;/pre>
&lt;hr>
&lt;h2 id="4-api-">4. API 参考&lt;/h2>
&lt;p>Ollama 提供了一套 REST API，用于以编程方式与模型进行交互。默认服务地址为 &lt;code>http://localhost:11434&lt;/code>。&lt;/p>
&lt;h3 id="41-apigenerate">4.1 &lt;code>/api/generate&lt;/code>&lt;/h3>
&lt;p>生成文本。&lt;/p>
&lt;ul>
&lt;li>&lt;strong>请求 (Streaming):&lt;/strong>
&lt;pre>&lt;code class="language-shell">curl http://localhost:11434/api/generate -d '{
&amp;quot;model&amp;quot;: &amp;quot;llama3&amp;quot;,
&amp;quot;prompt&amp;quot;: &amp;quot;Why is the sky blue?&amp;quot;
}'
&lt;/code>&lt;/pre>
&lt;/li>
&lt;li>&lt;strong>请求 (Non-streaming):&lt;/strong>
&lt;pre>&lt;code class="language-shell">curl http://localhost:11434/api/generate -d '{
&amp;quot;model&amp;quot;: &amp;quot;llama3&amp;quot;,
&amp;quot;prompt&amp;quot;: &amp;quot;Why is the sky blue?&amp;quot;,
&amp;quot;stream&amp;quot;: false
}'
&lt;/code>&lt;/pre>
&lt;/li>
&lt;/ul>
&lt;h3 id="42-apichat">4.2 &lt;code>/api/chat&lt;/code>&lt;/h3>
&lt;p>进行多轮对话。&lt;/p>
&lt;ul>
&lt;li>&lt;strong>请求:&lt;/strong>
&lt;pre>&lt;code class="language-shell">curl http://localhost:11434/api/chat -d '{
&amp;quot;model&amp;quot;: &amp;quot;llama3&amp;quot;,
&amp;quot;messages&amp;quot;: [
{
&amp;quot;role&amp;quot;: &amp;quot;user&amp;quot;,
&amp;quot;content&amp;quot;: &amp;quot;why is the sky blue?&amp;quot;
}
],
&amp;quot;stream&amp;quot;: false
}'
&lt;/code>&lt;/pre>
&lt;/li>
&lt;/ul>
&lt;h3 id="43-apiembed">4.3 &lt;code>/api/embed&lt;/code>&lt;/h3>
&lt;p>生成文本的嵌入向量。&lt;/p>
&lt;ul>
&lt;li>&lt;strong>请求:&lt;/strong>
&lt;pre>&lt;code class="language-shell">curl http://localhost:11434/api/embed -d '{
&amp;quot;model&amp;quot;: &amp;quot;all-minilm&amp;quot;,
&amp;quot;input&amp;quot;: [&amp;quot;Why is the sky blue?&amp;quot;, &amp;quot;Why is the grass green?&amp;quot;]
}'
&lt;/code>&lt;/pre>
&lt;/li>
&lt;/ul>
&lt;h3 id="44-apitags">4.4 &lt;code>/api/tags&lt;/code>&lt;/h3>
&lt;p>列出本地所有可用的模型。&lt;/p>
&lt;ul>
&lt;li>&lt;strong>请求:&lt;/strong>
&lt;pre>&lt;code class="language-shell">curl http://localhost:11434/api/tags
&lt;/code>&lt;/pre>
&lt;/li>
&lt;/ul>
&lt;hr>
&lt;h2 id="5--cli">5. 命令行工具 (CLI)&lt;/h2>
&lt;p>Ollama 提供了一套丰富的命令行工具来管理模型和与服务交互。&lt;/p>
&lt;ul>
&lt;li>&lt;code>ollama run &amp;lt;model&amp;gt;&lt;/code>: 运行一个模型。&lt;/li>
&lt;li>&lt;code>ollama create &amp;lt;model&amp;gt; -f &amp;lt;Modelfile&amp;gt;&lt;/code>: 从 Modelfile 创建一个模型。&lt;/li>
&lt;li>&lt;code>ollama pull &amp;lt;model&amp;gt;&lt;/code>: 从远程库拉取一个模型。&lt;/li>
&lt;li>&lt;code>ollama push &amp;lt;model&amp;gt;&lt;/code>: 将一个模型推送到远程库。&lt;/li>
&lt;li>&lt;code>ollama list&lt;/code>: 列出本地模型。&lt;/li>
&lt;li>&lt;code>ollama cp &amp;lt;source_model&amp;gt; &amp;lt;dest_model&amp;gt;&lt;/code>: 复制一个模型。&lt;/li>
&lt;li>&lt;code>ollama rm &amp;lt;model&amp;gt;&lt;/code>: 删除一个模型。&lt;/li>
&lt;li>&lt;code>ollama ps&lt;/code>: 查看正在运行的模型及其资源占用。&lt;/li>
&lt;li>&lt;code>ollama stop &amp;lt;model&amp;gt;&lt;/code>: 停止一个正在运行的模型并将其从内存中卸载。&lt;/li>
&lt;/ul>
&lt;hr>
&lt;h2 id="6-">6. 高级功能&lt;/h2>
&lt;h3 id="61-openai-api-">6.1 OpenAI API 兼容性&lt;/h3>
&lt;p>Ollama 提供了一个与 OpenAI API 兼容的端点，允许您将现有的 OpenAI 应用无缝迁移到 Ollama。默认地址为 &lt;code>http://localhost:11434/v1&lt;/code>。&lt;/p>
&lt;ul>
&lt;li>&lt;strong>列出模型 (Python):&lt;/strong>
&lt;pre>&lt;code class="language-python">from openai import OpenAI
client = OpenAI(
base_url='http://localhost:11434/v1',
api_key='ollama', # required, but unused
)
response = client.models.list()
print(response)
&lt;/code>&lt;/pre>
&lt;/li>
&lt;/ul>
&lt;h3 id="62-">6.2 结构化输出&lt;/h3>
&lt;p>结合使用 OpenAI 兼容 API 和 Pydantic，可以强制模型输出特定结构的 JSON。&lt;/p>
&lt;pre>&lt;code class="language-python">from pydantic import BaseModel
from openai import OpenAI
client = OpenAI(base_url=&amp;quot;http://localhost:11434/v1&amp;quot;, api_key=&amp;quot;ollama&amp;quot;)
class UserInfo(BaseModel):
name: str
age: int
try:
completion = client.beta.chat.completions.parse(
model=&amp;quot;llama3.1:8b&amp;quot;,
messages=[{&amp;quot;role&amp;quot;: &amp;quot;user&amp;quot;, &amp;quot;content&amp;quot;: &amp;quot;My name is John and I am 30 years old.&amp;quot;}],
response_format=UserInfo,
)
print(completion.choices[0].message.parsed)
except Exception as e:
print(f&amp;quot;Error: {e}&amp;quot;)
&lt;/code>&lt;/pre>
&lt;h3 id="63-">6.3 性能调优&lt;/h3>
&lt;p>您可以通过环境变量来调整 Ollama 的性能和资源管理：&lt;/p>
&lt;ul>
&lt;li>&lt;code>OLLAMA_KEEP_ALIVE&lt;/code>: 设置模型在内存中保持活动状态的时间。例如 &lt;code>10m&lt;/code>, &lt;code>24h&lt;/code>, 或 &lt;code>-1&lt;/code> (永久)。&lt;/li>
&lt;li>&lt;code>OLLAMA_MAX_LOADED_MODELS&lt;/code>: 同时加载到内存中的最大模型数量。&lt;/li>
&lt;li>&lt;code>OLLAMA_NUM_PARALLEL&lt;/code>: 每个模型可以并行处理的请求数量。&lt;/li>
&lt;/ul>
&lt;h3 id="64-lora-">6.4 LoRA 适配器&lt;/h3>
&lt;p>在 &lt;code>Modelfile&lt;/code> 中使用 &lt;code>ADAPTER&lt;/code> 指令来应用一个 LoRA (Low-Rank Adaptation) 适配器，从而在不修改基础模型权重的情况下，改变模型的行为。&lt;/p>
&lt;pre>&lt;code class="language-Modelfile">FROM llama3
ADAPTER /path/to/your-lora-adapter.safetensors
&lt;/code>&lt;/pre>
&lt;hr>
&lt;h2 id="7-">7. 附录&lt;/h2>
&lt;h3 id="71-">7.1 故障排除&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>检查 CPU 特性:&lt;/strong> 在 Linux 上，可以使用以下命令检查 CPU 是否支持 AVX 等指令集，这对于某些模型的性能至关重要。
&lt;pre>&lt;code class="language-shell">cat /proc/cpuinfo | grep flags | head -1
&lt;/code>&lt;/pre>
&lt;/li>
&lt;/ul>
&lt;h3 id="72-">7.2 贡献指南&lt;/h3>
&lt;p>Ollama 是一个开源项目，欢迎社区贡献。在提交代码时，请遵循良好的提交消息格式，例如：&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Good:&lt;/strong> &lt;code>llm/backend/mlx: support the llama architecture&lt;/code>&lt;/li>
&lt;li>&lt;strong>Bad:&lt;/strong> &lt;code>feat: add more emoji&lt;/code>&lt;/li>
&lt;/ul>
&lt;h3 id="73-">7.3 相关链接&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>官方网站:&lt;/strong> &lt;a href="https://ollama.com/">&lt;a href="https://ollama.com/">https://ollama.com/&lt;/a>&lt;/a>&lt;/li>
&lt;li>&lt;strong>GitHub 仓库:&lt;/strong> &lt;a href="https://github.com/ollama/ollama">&lt;a href="https://github.com/ollama/ollama">https://github.com/ollama/ollama&lt;/a>&lt;/a>&lt;/li>
&lt;li>&lt;strong>模型库:&lt;/strong> &lt;a href="https://ollama.com/library">&lt;a href="https://ollama.com/library">https://ollama.com/library&lt;/a>&lt;/a>&lt;/li>
&lt;/ul></description></item><item><title>Llama.cpp 技术详解：轻量级大模型推理引擎</title><link>https://ziyanglin.netlify.app/zh/post/llama-cpp-documentation/</link><pubDate>Thu, 26 Jun 2025 01:06:00 +0000</pubDate><guid>https://ziyanglin.netlify.app/zh/post/llama-cpp-documentation/</guid><description>&lt;h2 id="1-">1. 引言&lt;/h2>
&lt;p>Llama.cpp 是一个用 C/C++ 编写的高性能、轻量级的大型语言模型 (LLM) 推理框架。它专注于在消费级硬件上高效运行 LLM，实现了在普通笔记本电脑甚至手机上进行本地推理的可能。&lt;/p>
&lt;p>&lt;strong>核心优势:&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>&lt;strong>高性能:&lt;/strong> 通过优化的 C/C++ 代码、量化技术和硬件加速支持（如 Apple Metal, CUDA, OpenCL, SYCL），实现了极快的推理速度。&lt;/li>
&lt;li>&lt;strong>轻量级:&lt;/strong> 极低的内存和计算资源消耗，无需昂贵的 GPU 即可运行。&lt;/li>
&lt;li>&lt;strong>跨平台:&lt;/strong> 支持 macOS, Linux, Windows, Docker, Android, 和 iOS 等多种平台。&lt;/li>
&lt;li>&lt;strong>开放生态:&lt;/strong> 拥有活跃的社区和丰富的生态系统，包括 Python 绑定、UI 工具和与 OpenAI 兼容的服务器。&lt;/li>
&lt;li>&lt;strong>持续创新:&lt;/strong> 快速跟进并实现最新的模型架构和推理优化技术。&lt;/li>
&lt;/ul>
&lt;h2 id="2-">2. 核心概念&lt;/h2>
&lt;h3 id="21-gguf-">2.1. GGUF 模型格式&lt;/h3>
&lt;p>GGUF (Georgi Gerganov Universal Format) 是 &lt;code>llama.cpp&lt;/code> 使用的核心模型文件格式，是其前身 GGML 的演进版本。GGUF 是一个专为快速加载和内存映射设计的二进制格式。&lt;/p>
&lt;p>&lt;strong>主要特点:&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>&lt;strong>统一文件:&lt;/strong> 将模型元数据、词汇表和所有张量（权重）打包在单个文件中。&lt;/li>
&lt;li>&lt;strong>可扩展性:&lt;/strong> 允许在不破坏兼容性的情况下添加新的元数据。&lt;/li>
&lt;li>&lt;strong>向后兼容:&lt;/strong> 保证了对旧版本 GGUF 模型的兼容。&lt;/li>
&lt;li>&lt;strong>内存效率:&lt;/strong> 支持内存映射（mmap），允许多个进程共享同一模型权重，从而节省内存。&lt;/li>
&lt;/ul>
&lt;h3 id="22--quantization">2.2. 量化 (Quantization)&lt;/h3>
&lt;p>量化是 &lt;code>llama.cpp&lt;/code> 的核心优势之一。它是一种将模型权重从高精度浮点数（如 32 位或 16 位）转换为低精度整数（如 4 位、5 位或 8 位）的技术。&lt;/p>
&lt;p>&lt;strong>主要优势:&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>&lt;strong>减小模型体积:&lt;/strong> 显著降低模型文件的大小，使其更易于分发和存储。&lt;/li>
&lt;li>&lt;strong>降低内存占用:&lt;/strong> 减少了模型加载到内存中所需的 RAM。&lt;/li>
&lt;li>&lt;strong>加速推理:&lt;/strong> 低精度计算通常比高精度计算更快，尤其是在 CPU 上。&lt;/li>
&lt;/ul>
&lt;p>&lt;code>llama.cpp&lt;/code> 支持多种量化方法，特别是 &lt;strong>k-quants&lt;/strong>，这是一种先进的量化技术，能够在保持较高模型性能的同时实现极高的压缩率。&lt;/p>
&lt;h3 id="23-">2.3. 多模态支持&lt;/h3>
&lt;p>&lt;code>llama.cpp&lt;/code> 不仅仅局限于文本模型，它已经发展成为一个强大的多模态推理引擎，支持同时处理文本、图像甚至音频。&lt;/p>
&lt;ul>
&lt;li>&lt;strong>支持的模型:&lt;/strong> 支持如 LLaVA, MobileVLM, Granite, Qwen2.5 Omni, InternVL, SmolVLM 等多种主流多模态模型。&lt;/li>
&lt;li>&lt;strong>工作原理:&lt;/strong> 通常通过一个视觉编码器（如 CLIP）将图像转换为嵌入向量，然后将这些向量与文本嵌入向量一起输入到 LLM 中。&lt;/li>
&lt;li>&lt;strong>使用工具:&lt;/strong> &lt;code>llama-mtmd-cli&lt;/code> 和 &lt;code>llama-server&lt;/code> 提供了对多模态模型的原生支持。&lt;/li>
&lt;/ul>
&lt;h2 id="3-">3. 使用方法&lt;/h2>
&lt;h3 id="31-">3.1. 编译&lt;/h3>
&lt;p>从源码编译 &lt;code>llama.cpp&lt;/code> 非常简单。&lt;/p>
&lt;pre>&lt;code class="language-bash">git clone https://github.com/ggml-org/llama.cpp.git
cd llama.cpp
make
&lt;/code>&lt;/pre>
&lt;p>对于特定硬件加速（如 CUDA 或 Metal），需要使用相应的编译选项：&lt;/p>
&lt;pre>&lt;code class="language-bash"># For CUDA
make LLAMA_CUDA=1
# For Metal (on macOS)
make LLAMA_METAL=1
&lt;/code>&lt;/pre>
&lt;h3 id="32-">3.2. 基本推理&lt;/h3>
&lt;p>编译后，可以使用 &lt;code>llama-cli&lt;/code> 工具进行推理。&lt;/p>
&lt;pre>&lt;code class="language-bash">./llama-cli -m ./models/7B/ggml-model-q4_0.gguf -p &amp;quot;Building a website can be done in 10 simple steps:&amp;quot; -n 400
&lt;/code>&lt;/pre>
&lt;ul>
&lt;li>&lt;code>-m&lt;/code>: 指定 GGUF 模型文件的路径。&lt;/li>
&lt;li>&lt;code>-p&lt;/code>: 指定提示（prompt）。&lt;/li>
&lt;li>&lt;code>-n&lt;/code>: 指定要生成的最大 token 数量。&lt;/li>
&lt;/ul>
&lt;h3 id="33-openai-">3.3. OpenAI 兼容服务器&lt;/h3>
&lt;p>&lt;code>llama.cpp&lt;/code> 提供了一个内置的 HTTP 服务器，其 API 与 OpenAI 的 API 兼容。这使得它可以轻松地与 LangChain, LlamaIndex 等现有工具集成。&lt;/p>
&lt;p>启动服务器：&lt;/p>
&lt;pre>&lt;code class="language-bash">./llama-server -m models/7B/ggml-model-q4_0.gguf -c 4096
&lt;/code>&lt;/pre>
&lt;p>然后，你可以像调用 OpenAI API 一样向 &lt;code>http://localhost:8080/v1/chat/completions&lt;/code> 发送请求。&lt;/p>
&lt;h2 id="4-">4. 高级功能&lt;/h2>
&lt;h3 id="41--speculative-decoding">4.1. 投机性解码 (Speculative Decoding)&lt;/h3>
&lt;p>这是一种先进的推理优化技术，通过使用一个小的&amp;quot;草稿&amp;quot;模型来预测主模型的输出，从而显著加速生成速度。&lt;/p>
&lt;ul>
&lt;li>&lt;strong>工作原理:&lt;/strong> 草稿模型快速生成一个 token 序列草稿，然后由主模型一次性验证整个序列。如果验证通过，就可以节省逐个生成 token 的时间。&lt;/li>
&lt;li>&lt;strong>使用方法:&lt;/strong> 在 &lt;code>llama-cli&lt;/code> 或 &lt;code>llama-server&lt;/code> 中使用 &lt;code>--draft-model&lt;/code> 参数指定一个小的、快速的草稿模型。&lt;/li>
&lt;/ul>
&lt;h3 id="42-lora-">4.2. LoRA 支持&lt;/h3>
&lt;p>LoRA (Low-Rank Adaptation) 允许在不修改原始模型权重的情况下，通过训练一个小的适配器来微调模型的行为。&lt;code>llama.cpp&lt;/code> 支持在推理时加载一个或多个 LoRA 适配器。&lt;/p>
&lt;pre>&lt;code class="language-bash">./llama-cli -m base-model.gguf --lora lora-adapter.gguf
&lt;/code>&lt;/pre>
&lt;p>甚至可以为不同的 LoRA 适配器设置不同的权重：&lt;/p>
&lt;pre>&lt;code class="language-bash">./llama-cli -m base.gguf --lora-scaled lora_A.gguf 0.5 --lora-scaled lora_B.gguf 0.5
&lt;/code>&lt;/pre>
&lt;h3 id="43--grammars">4.3. 文法约束 (Grammars)&lt;/h3>
&lt;p>文法约束是一个非常强大的功能，它允许你强制模型的输出遵循特定的格式，例如严格的 JSON 模式。&lt;/p>
&lt;ul>
&lt;li>&lt;strong>格式:&lt;/strong> 使用一种名为 GBNF (GGML BNF) 的格式来定义语法规则。&lt;/li>
&lt;li>&lt;strong>应用:&lt;/strong> 在 API 请求中通过 &lt;code>grammar&lt;/code> 参数提供 GBNF 规则，可以确保模型返回格式正确、可直接解析的 JSON 数据，避免了输出格式错误和繁琐的后处理。&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>示例：&lt;/strong> 使用 Pydantic 模型生成 JSON Schema，然后转换为 GBNF，以确保模型输出符合预期的 Python 对象结构。&lt;/p>
&lt;pre>&lt;code class="language-python">import json
from typing import List
from pydantic import BaseModel
class QAPair(BaseModel):
question: str
answer: str
class Summary(BaseModel):
key_facts: List[str]
qa_pairs: List[QAPair]
# 生成 JSON Schema 并打印
schema = Summary.model_json_schema()
print(json.dumps(schema, indent=2))
&lt;/code>&lt;/pre>
&lt;h2 id="5-">5. 生态系统&lt;/h2>
&lt;p>&lt;code>llama.cpp&lt;/code> 的成功催生了一个充满活力的生态系统：&lt;/p>
&lt;ul>
&lt;li>&lt;strong>&lt;a href="https://github.com/abetlen/llama-cpp-python">llama-cpp-python&lt;/a>:&lt;/strong> 最流行的 Python 绑定，提供了与 &lt;code>llama.cpp&lt;/code> 几乎所有功能的接口，并与 LangChain、LlamaIndex 等框架深度集成。&lt;/li>
&lt;li>&lt;strong>&lt;a href="https://ollama.com/">Ollama&lt;/a>:&lt;/strong> 一个将模型打包、分发和运行的工具，底层使用了 &lt;code>llama.cpp&lt;/code>，极大地简化了在本地运行 LLM 的流程。&lt;/li>
&lt;li>&lt;strong>众多 UI 工具:&lt;/strong> 社区开发了大量的图形界面工具，让非技术用户也能轻松地与本地模型进行交互。&lt;/li>
&lt;/ul>
&lt;h2 id="6-">6. 总结&lt;/h2>
&lt;p>&lt;code>llama.cpp&lt;/code> 不仅仅是一个推理引擎，它已经成为推动 LLM 本地化和大众化的关键力量。通过其卓越的性能、对资源的高度优化以及不断扩展的功能集（如多模态、文法约束），&lt;code>llama.cpp&lt;/code> 为开发者和研究人员提供了一个强大而灵活的平台，让他们能够在各种设备上探索和部署 AI 应用，开启了低成本、保护隐私的本地 AI 新时代。&lt;/p></description></item></channel></rss>