# 保存到 Ollama

请查看下面的指南，了解保存到的完整过程 [Ollama](https://github.com/ollama/ollama):

{% content-ref url="/pages/e36b62f99558f2c1e1e644ea9bc15aa9621d449b" %}
[Tutorial: Finetune Llama-3 and Use In Ollama](/docs/zh/kai-shi-shi-yong/fine-tuning-llms-guide/tutorial-how-to-finetune-llama-3-and-use-in-ollama.md)
{% endcontent-ref %}

### 在 Google Colab 中保存

你可以像下面这样将微调后的模型保存为一个 100MB 的小文件，称为 LoRA 适配器。你也可以选择推送到 Hugging Face hub，如果你想上传你的模型！记得通过以下方式获取 Hugging Face 令牌： <https://huggingface.co/settings/tokens> 并添加你的令牌！

<figure><img src="/files/7c6e34b765ee1f0a0d11209255b9e7a2e5113969" alt=""><figcaption></figcaption></figure>

保存模型后，我们还可以再次使用 Unsloth 来运行模型本身！使用 `FastLanguageModel` 再次调用它进行推理！

<figure><img src="/files/9c38aab1b9ea11738f53059f0eaacd10abf6d812" alt=""><figcaption></figcaption></figure>

### 导出到 Ollama

最后，我们可以将微调后的模型导出到 Ollama 本身！首先，我们必须在 Colab 笔记本中安装 Ollama：

<figure><img src="/files/432481c1f5c6132c190138e8ba1e2aa18914d461" alt=""><figcaption></figcaption></figure>

然后，我们将已有的微调模型导出为 llama.cpp 的 GGUF 格式，如下所示：

<figure><img src="/files/24e6747676d34ff6eda23b05bf9bf44912fb2894" alt=""><figcaption></figcaption></figure>

提醒：将 `False` 改为 `True` ，仅用于 1 行，而不要把每一行都改成 `True`，否则你会等很久！我们通常建议将第一行设为 `True`，这样我们就可以快速将微调模型导出为 `Q8_0` 格式（8 位量化）。我们也允许你导出为一整列量化方法，其中常见的一种是 `q4_k_m`.

前往 <https://github.com/ggml-org/llama.cpp> 了解更多关于 GGUF 的信息。如果你愿意，我们这里也提供了如何手动导出到 GGUF 的说明： <https://github.com/unslothai/unsloth/wiki#manually-saving-to-gguf>

你会看到如下所示的一长串文本——请等待 5 到 10 分钟！！

<figure><img src="/files/d84bb2761ba41da695367ff66d58fe3577e9a1a7" alt=""><figcaption></figcaption></figure>

最后，在最末尾，它会像下面这样：

<figure><img src="/files/8232f8de77c4511fc97b6227faa5f6a7fff12d00" alt=""><figcaption></figcaption></figure>

然后，我们需要在后台运行 Ollama 本身。我们使用 `subprocess` ，因为 Colab 不喜欢异步调用，但通常只需在 `ollama serve` 中运行即可。

<figure><img src="/files/09e001f03f0a7fb496659ad1ddbf7b0891fe5334" alt=""><figcaption></figcaption></figure>

### 自动 `Modelfile` 创建

Unsloth 提供的技巧是，我们会自动创建一个 `Modelfile` ，而这是 Ollama 所必需的！这只是一组设置，并包含我们在微调过程中使用的聊天模板！你也可以像下面这样打印 `Modelfile` 生成的

<figure><img src="/files/56b139991dc0374a500c2d51cfcbc3d30d2e92a7" alt=""><figcaption></figcaption></figure>

然后，我们通过使用 `Modelfile`

<figure><img src="/files/82c1d79d292a346acd7d8886d4f4441db8876f48" alt=""><figcaption></figcaption></figure>

### Ollama 推理

来让 Ollama 创建一个与 Ollama 兼容的模型。现在，如果你愿意，也可以调用模型进行推理，直接调用正在你自己的本地机器上运行 / 在免费的 Colab 笔记本后台运行的 Ollama 服务器。记住，你可以编辑黄色下划线部分。

<figure><img src="/files/3d2ce2e2bc1324f259d074cf927d6f93eaa4f5bc" alt=""><figcaption></figcaption></figure>

### 在 Unsloth 中运行效果很好，但导出并在 Ollama 上运行后，结果很差

你有时可能会遇到这样的问题：模型在 Unsloth 中运行并产生良好结果，但当你在像 Ollama 这样的其他平台上使用它时，结果很差，或者你可能会得到乱码、无休止/无限生成 *或* 重复输&#x51FA;**.**

* 这种错误最常见的原因是使用了 <mark style="background-color:blue;">**错误的聊天模板**</mark>**.** 至关重要的是，在 Unsloth 中训练模型时使用的聊天模板，之后在你将其运行于其他框架（如 llama.cpp 或 Ollama）时，也必须使用同样的模板。从已保存模型进行推理时，应用正确的模板非常关键。
* 你必须使用正确的 `eos 令牌`。否则，在较长的生成中你可能会得到乱码。
* 也可能是因为你的推理引擎添加了一个不必要的“序列开头”令牌（或者相反地缺少它），因此请确保同时检查这两个假设！
* <mark style="background-color:green;">**使用我们的对话式笔记本来强制使用聊天模板——这将修复大多数问题。**</mark>
  * Qwen-3 14B 对话式笔记本 [**在 Colab 中打开**](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Qwen3_\(14B\)-Reasoning-Conversational.ipynb)
  * Gemma-3 4B 对话式笔记本 [**在 Colab 中打开**](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Gemma3_\(4B\).ipynb)
  * Llama-3.2 3B 对话式笔记本 [**在 Colab 中打开**](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Llama3.2_\(1B_and_3B\)-Conversational.ipynb)
  * Phi-4 14B 对话式笔记本 [**在 Colab 中打开**](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Phi_4-Conversational.ipynb)
  * Mistral v0.3 7B 对话式笔记本 [**在 Colab 中打开**](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Mistral_v0.3_\(7B\)-Conversational.ipynb)
  * **更多笔记本请查看我们的** [**笔记本文档**](/docs/zh/kai-shi-shi-yong/unsloth-notebooks.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://unsloth.ai/docs/zh/ji-chu/inference-and-deployment/saving-to-ollama.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
