docs: remove manual paragraph line wraps (#6131)

* docs: remove manual paragraph line wraps

* docs: preserve GitHub alert marker line breaks
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-10 11:48:50 +08:00
1 parent 36c36a1324
commit 50256940be
58 files changed
+848 -3130

No files matched your search

+19 -50
View File
@@ -1,14 +1,8 @@
# How to Improve Ollama Tool-Calling Prompt Cache Reuse in nanobot
Some Ollama model templates move or remove tool definitions as a conversation
switches between user, assistant, and tool messages. nanobot can send a correct
append-only chat request while the model template still renders a different token
prefix. On slower local hardware, re-evaluating that prefix can add tens of seconds
to an otherwise simple tool-using turn.
Some Ollama model templates move or remove tool definitions as a conversation switches between user, assistant, and tool messages. nanobot can send a correct append-only chat request while the model template still renders a different token prefix. On slower local hardware, re-evaluating that prefix can add tens of seconds to an otherwise simple tool-using turn.
This guide shows how to diagnose that specific pattern and create a derived
`llama3.1:8b` tag with a prefix-stable tool template. It does not modify nanobot or
overwrite the original Ollama model.
This guide shows how to diagnose that specific pattern and create a derived `llama3.1:8b` tag with a prefix-stable tool template. It does not modify nanobot or overwrite the original Ollama model.
## What you will build
@@ -22,18 +16,14 @@ Use this guide when all of the following are true:
- direct Ollama responses are reasonably fast;
- nanobot becomes slow after the model calls a tool;
- Ollama logs show a long main prompt, a much shorter tool follow-up, and low
initial cache reuse on the next main prompt;
- the model is `llama3.1:8b` with a template that renders concrete tools only for
the final user message.
- Ollama logs show a long main prompt, a much shorter tool follow-up, and low initial cache reuse on the next main prompt;
- the model is `llama3.1:8b` with a template that renders concrete tools only for the final user message.
Do not apply this template to another model family without checking that model's
tool-call format first.
Do not apply this template to another model family without checking that model's tool-call format first.
## Diagnose the rendered prompt
Stop any existing Ollama process, then start a single-slot debug server. A single
slot makes the cache sequence easier to read.
Stop any existing Ollama process, then start a single-slot debug server. A single slot makes the cache sequence easier to read.
**macOS or Linux**
@@ -53,8 +43,7 @@ $env:OLLAMA_DEBUG = "1"
ollama serve
```
In another terminal, use a fresh session and explicitly request a tool so both
turns exercise the agent loop:
In another terminal, use a fresh session and explicitly request a tool so both turns exercise the agent loop:
```bash
nanobot agent --session cli:ollama-cache-check \
@@ -63,9 +52,7 @@ nanobot agent --session cli:ollama-cache-check \
--message "Use the exec tool to calculate 4+7, then answer"
```
In the Ollama output, find each `new prompt` line and the first
`cached n_tokens` line that follows it. Later increasing `cached n_tokens` lines
are prompt-evaluation progress, not additional initial cache hits.
In the Ollama output, find each `new prompt` line and the first `cached n_tokens` line that follows it. Later increasing `cached n_tokens` lines are prompt-evaluation progress, not additional initial cache hits.
A cache-unfriendly tool template may produce a pattern like this:
@@ -75,18 +62,13 @@ turn 1 tool follow-up: 3713 / 3758 initially cached
turn 2 main: 3767 / 8519 initially cached
```
The cache is working, but the next main request can reuse only the shorter prompt.
Hardware throughput determines how expensive the remaining evaluation is.
The cache is working, but the next main request can reuse only the shorter prompt. Hardware throughput determines how expensive the remaining evaluation is.
To inspect the API request bodies as well, add
`OLLAMA_DEBUG_LOG_REQUESTS=1` before starting Ollama. These logs can contain system
prompts, workspace context, and user messages. Keep them local and disable request
logging after diagnosis.
To inspect the API request bodies as well, add `OLLAMA_DEBUG_LOG_REQUESTS=1` before starting Ollama. These logs can contain system prompts, workspace context, and user messages. Keep them local and disable request logging after diagnosis.
## Why this happens with the stock template
The tested `llama3.1:8b` template conditionally expands the tool definitions inside
a user message:
The tested `llama3.1:8b` template conditionally expands the tool definitions inside a user message:
```gotemplate
{{- if and $.Tools $last }}
@@ -94,19 +76,13 @@ a user message:
{{- end }}
```
The first request ends with a user message, so the tools are rendered there. After
nanobot appends an assistant tool call and its result, that user message is no
longer last, so the same API request history renders without the concrete tool
block. On the next user turn, the tools reappear at a new position.
The first request ends with a user message, so the tools are rendered there. After nanobot appends an assistant tool call and its result, that user message is no longer last, so the same API request history renders without the concrete tool block. On the next user turn, the tools reappear at a new position.
This is a model-template behavior. At the API boundary, nanobot continues to append
the assistant tool call and tool result and sends the same tool definitions.
This is a model-template behavior. At the API boundary, nanobot continues to append the assistant tool call and tool result and sends the same tool definitions.
## Create a prefix-stable derived model
Create `PrefixStable.Modelfile` with the content below. The template keeps concrete
tool definitions in the system block, where they remain in the same position across
user and tool messages.
Create `PrefixStable.Modelfile` with the content below. The template keeps concrete tool definitions in the system block, where they remain in the same position across user and tool messages.
```dockerfile
FROM llama3.1:8b
@@ -164,8 +140,7 @@ ollama create llama3.1:8b-prefix-stable-v1 -f PrefixStable.Modelfile
ollama list
```
Ollama reuses the existing model layers. The new tag adds a small template and
manifest instead of copying the base weights.
Ollama reuses the existing model layers. The new tag adds a small template and manifest instead of copying the base weights.
## Select the derived model in nanobot
@@ -205,15 +180,11 @@ nanobot agent --session cli:ollama-stable-check \
--message "Use the exec tool to calculate 4+7, then answer"
```
In one controlled test with Ollama 0.32.1, `llama3.1:8b`, and one slot, the second
main request improved from `3767 / 8519` initially cached (44.22%) to
`8505 / 8520` (99.82%). The number of re-evaluated tokens fell from 4752 to 15.
Treat these numbers as a diagnostic example, not a performance guarantee.
In one controlled test with Ollama 0.32.1, `llama3.1:8b`, and one slot, the second main request improved from `3767 / 8519` initially cached (44.22%) to `8505 / 8520` (99.82%). The number of re-evaluated tokens fell from 4752 to 15. Treat these numbers as a diagnostic example, not a performance guarantee.
## Roll back
Switch `agents.defaults.modelPreset` back to the original preset. When no config
uses the derived tag, remove it with:
Switch `agents.defaults.modelPreset` back to the original preset. When no config uses the derived tag, remove it with:
```bash
ollama rm llama3.1:8b-prefix-stable-v1
@@ -225,10 +196,8 @@ Removing the derived tag does not remove `llama3.1:8b`.
- The template above is specific to the tested `llama3.1:8b` tool-call format.
- Ollama or the model publisher may update the stock template in a later release.
- Validate multiple tool calls, tool errors, parallel calls, and long conversations
before using a custom template for unattended workloads.
- A higher cache ratio reduces prompt evaluation, but model generation, tool
execution, process startup, and storage can still dominate end-to-end latency.
- Validate multiple tool calls, tool errors, parallel calls, and long conversations before using a custom template for unattended workloads.
- A higher cache ratio reduces prompt evaluation, but model generation, tool execution, process startup, and storage can still dominate end-to-end latency.
- Multiple Ollama slots change cache scheduling and may produce different results.
## Related nanobot docs