# 保存到 Ollama

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

{% content-ref url="../../kai-shi-shi-yong/fine-tuning-llms-guide/tutorial-how-to-finetune-llama-3-and-use-in-ollama" %}
[tutorial-how-to-finetune-llama-3-and-use-in-ollama](https://unsloth.ai/docs/zh/kai-shi-shi-yong/fine-tuning-llms-guide/tutorial-how-to-finetune-llama-3-and-use-in-ollama)
{% endcontent-ref %}

### 在 Google Colab 上保存

您可以将微调后的模型保存为一个小约100MB的文件，称为 LoRA 适配器，如下所示。如果想上传模型，也可以将其推送到 Hugging Face hub！别忘了通过以下方式获取 Hugging Face 令牌： <https://huggingface.co/settings/tokens> 并添加您的令牌！

<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>

提醒将 `False` 转换为 `True` 仅为第 1 行，而不要将每一行都改为 `True`，否则你将要等待很长时间！我们通常建议将第一行设置为 `True`，这样我们可以快速将微调模型导出为 `Q8_0` 格式（8 位量化）。我们还允许您导出到一整列量化方法，其中一个流行的为 `q4_k_m`.

前往 <https://github.com/ggerganov/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>

### 自动 `模型文件` 创建

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

<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>

然后我们要求 Ollama 创建一个与 Ollama 兼容的模型，使用 `模型文件`

<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 推理

如果您想调用在您本地机器/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 令牌`。否则，在较长的生成中您可能会得到乱码。
* 也可能是因为您的推理引擎添加了不必要的“序列开始”令牌（或相反地缺少该令牌），因此请确保检查这两种假设！
* <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)
  * **在我们的更多笔记本中** [**笔记本文档**](https://unsloth.ai/docs/zh/kai-shi-shi-yong/unsloth-notebooks)


---

# 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.
