# llama-server 与 OpenAI 端点部署指南

我们正在部署 Devstral-2 —— 参见 [devstral-2](https://unsloth.ai/docs/zh/mo-xing/tutorials/devstral-2 "mention") 以获取有关该模型的更多详细信息。&#x20;

获取最新的 `llama.cpp` 在 [此处的 GitHub](https://github.com/ggml-org/llama.cpp)。您也可以按照下面的构建说明进行。若 `-DGGML_CUDA=ON` 更改为 `-DGGML_CUDA=OFF` 如果您没有 GPU 或仅想要在 CPU 上进行推理。 **对于 Apple Mac / Metal 设备**，设置 `-DGGML_CUDA=OFF` 然后照常继续 - Metal 支持默认启用。

{% code overflow="wrap" %}

```bash
apt-get update
apt-get install pciutils build-essential cmake curl libcurl4-openssl-dev -y
git clone https://github.com/ggml-org/llama.cpp
cmake llama.cpp -B llama.cpp/build \
    -DBUILD_SHARED_LIBS=OFF -DGGML_CUDA=ON -DLLAMA_CURL=ON
cmake --build llama.cpp/build --config Release -j --clean-first --target llama-cli llama-mtmd-cli llama-server llama-gguf-split
cp llama.cpp/build/bin/llama-* llama.cpp
```

{% endcode %}

{% hint style="info" %}
在使用 `--jinja` 时，llama-server 会追加以下系统消息（如果支持工具）： `以 JSON 格式响应，要么使用 tool_call（请求调用工具），要么使用 response 回应用户的请求` 。这有时会对微调造成问题！请参见 [llama.cpp 仓库](https://github.com/ggml-org/llama.cpp/blob/12ee1763a6f6130ce820a366d220bbadff54b818/common/chat.cpp#L849) 以获取更多详细信息。
{% endhint %}

首先下载 Devstral 2：

{% code overflow="wrap" %}

```python
# !pip install huggingface_hub hf_transfer
import os
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
from huggingface_hub import snapshot_download
snapshot_download(
    repo_id = "unsloth/Devstral-2-123B-Instruct-2512-GGUF",
    local_dir = "Devstral-2-123B-Instruct-2512-GGUF",
    allow_patterns = ["*UD-Q2_K_XL*", "*mmproj-F16*"],
)
```

{% endcode %}

要将 Devstral 2 部署到生产环境，我们使用 `llama-server` 在新终端（例如通过 tmux）中，通过以下命令部署模型：

{% code overflow="wrap" %}

```bash
./llama.cpp/llama-server \
    --model Devstral-Small-2-24B-Instruct-2512-GGUF/Devstral-Small-2-24B-Instruct-2512-UD-Q4_K_XL.gguf \
    --mmproj Devstral-Small-2-24B-Instruct-2512-GGUF/mmproj-F16.gguf \
    --alias "unsloth/Devstral-Small-2-24B-Instruct-2512" \
    --threads -1 \
    --n-gpu-layers 999 \
    --prio 3 \
    --min_p 0.01 \
    --ctx-size 16384 \
    --port 8001 \
    --jinja
```

{% endcode %}

运行上述命令后，您将获得：

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FJSTkWDCcHk5DI6otb72X%2Fimage.png?alt=media&#x26;token=b685008e-e9ad-4dea-8f1d-af3fdade8e3b" alt=""><figcaption></figcaption></figure>

然后在新终端中，执行 `pip install openai`，然后执行：

{% code overflow="wrap" %}

```python
from openai import OpenAI
import json
openai_client = OpenAI(
    base_url = "http://127.0.0.1:8001/v1",
    api_key = "sk-no-key-required",
)
completion = openai_client.chat.completions.create(
    model = "unsloth/Devstral-Small-2-24B-Instruct-2512",
    messages = [{"role": "user", "content": "What is 2+2?"},],
)
print(completion.choices[0].message.content)
```

{% endcode %}

这将仅打印 4。\
\
你可以返回到 llama-server 屏幕，可能会看到一些有趣的统计信息：

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2F6msFgOWJEWXEEgXvLRnr%2Fimage.png?alt=media&#x26;token=25fa1784-f671-4e0d-9bad-a4534525afb6" alt=""><figcaption></figcaption></figure>

关于诸如使用推测解码等参数，请参见 <https://github.com/ggml-org/llama.cpp/blob/master/tools/server/README.md>

## :grey\_question:Llama-server 的怪异行为

* 在使用 `--jinja` 时，llama-server 会追加以下系统消息（如果支持工具）： `以 JSON 格式响应，要么使用 tool_call（请求调用工具），要么使用 response 回应用户的请求` 。这有时会对微调造成问题！请参见 [llama.cpp 仓库](https://github.com/ggml-org/llama.cpp/blob/12ee1763a6f6130ce820a366d220bbadff54b818/common/chat.cpp#L849) 以获取更多详情。\
  \
  你可以通过使用来停止此行为 `--no-jinja` 但这样一来， `工具` 将不再受支持。\
  \
  例如 FunctionGemma 默认使用：

  <pre class="language-notebook-python" data-overflow="wrap"><code class="lang-notebook-python">您是一个可以进行函数调用的模型，具有以下函数
  </code></pre>

  但由于 llama-server 追加了一条额外消息，我们得到：

  <pre class="language-notebook-python" data-overflow="wrap"><code class="lang-notebook-python">你是一个可以进行函数调用的模型，具有以下函数\n\n以 JSON 格式响应，要么使用 `tool_call`（请求调用工具），要么使用 `response` 回应用户的请求
  </code></pre>

  我们已将该问题报告给 <https://github.com/ggml-org/llama.cpp/issues/18323> llama.cpp 的开发者正在努力修复此问题！\
  \
  在此期间，对于所有微调，请特别添加用于工具调用的提示词！

## :toolbox:使用 llama-server 的工具调用

GLM 4.7 的工具调用 [tool-calling-guide-for-local-llms](https://unsloth.ai/docs/zh/ji-chu/tool-calling-guide-for-local-llms "mention") 有关如何进行工具调用的说明！


---

# 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/llama-server-and-openai-endpoint.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.
