> For the complete documentation index, see [llms.txt](https://unsloth.ai/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://unsloth.ai/docs/zh/ji-chu-zhi-shi/inference-and-deployment/saving-to-ollama.md).

# 将模型保存到 Ollama

请参阅下面的指南，了解如何将模型保存到 [Ollama](https://github.com/ollama/ollama):

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

### 在 Google Colab 上保存

您可以将微调后的模型保存为一个小型的 100MB 文件，称为 LoRA adapter，如下所示。如果您愿意上传自己的模型，也可以将其推送到 Hugging Face hub！别忘了通过以下链接获取 Hugging Face token： <https://huggingface.co/settings/tokens> 并添加您的 token！

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-8c577103f7c4fe883cabaf35c8437307c6501686%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-1a1be852ca551240bdce47cf99e6ccd7d31c1326%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

### 导出到 Ollama

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

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-24f9429ed4a8b3a630dc8f68dcf81555da0a80ee%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-56991ea7e2685bb9905af9baf2f3f685123dcdd8%2Fimage%20(52).png?alt=media" alt=""><figcaption></figcaption></figure>

提醒转换 `假` 改为 `真` 为 1 行，而不是把每一行都改成 `真`，否则您会等待很长时间！我们通常建议将第一行设为 `真`，这样我们就能快速将微调后的模型导出为 `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="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-271b392fdafd0e7d01c525d7a11a97ee5c34b713%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-a554bd388fd0394dd8cdef85fd9d208bfd7feee7%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

然后，我们必须在后台运行 Ollama 本身。我们使用 `subprocess` 因为 Colab 不喜欢异步调用，但通常只需在 `ollama serve` 终端 / 命令提示符中运行即可。

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-e431609dfc5c742f0b5ab2388dbbd0d8e15c7670%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

### 自动 `Modelfile` 创建

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

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-6945ba10a2e25cfc198848c0e863001375c32c4c%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

然后我们使用 `Modelfile`

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-d431a64613b39d913d1780c22cde37edc6564272%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

### Ollama 推理

来让 Ollama 创建一个兼容的模型，如果您想直接调用正在您本地机器上 / 免费 Colab 笔记本后台运行的 Ollama 服务器，现在就可以这样做。记得您可以编辑黄色下划线部分。

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-49b93efa192fdd741f3ac8484cef8c3fd7415283%2FInference.png?alt=media" alt=""><figcaption></figcaption></figure>

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

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

* 这种错误最常见的原因是使用了 <mark style="background-color:blue;">**不正确的聊天模板**</mark>**.** 至关重要的是，要使用与在 Unsloth 中训练模型时相同的聊天模板，并在之后于其他框架中运行时也保持一致，例如 llama.cpp 或 Ollama。从已保存的模型进行推理时，应用正确的模板非常关键。
* 您必须使用正确的 `eos token`。否则，长文本生成时可能会出现乱码。
* 也可能是因为您的推理引擎添加了一个不必要的“序列开始”token（反之亦然，即缺少它），因此请确保检查这两种可能性！
* <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/kuai-su-kai-shi/unsloth-notebooks.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
