<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>API | 林子杨的个人网站</title><link>https://ziyanglin.netlify.app/zh/tags/api/</link><atom:link href="https://ziyanglin.netlify.app/zh/tags/api/index.xml" rel="self" type="application/rss+xml"/><description>API</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>API</title><link>https://ziyanglin.netlify.app/zh/tags/api/</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></channel></rss>