[PR #9552] Add PREPEND Modelfile directive to prepend text to model responses #12998

Open
opened 2026-04-13 00:15:01 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/9552
Author: @ehartford
Created: 3/6/2025
Status: 🔄 Open

Base: mainHead: add-prepend-directive


📝 Commits (1)

  • 1faaa12 Add PREPEND Modelfile directive to prepend text to model responses

📊 Changes

8 files changed (+129 additions, -4 deletions)

View changed files

📝 api/types.go (+2 -0)
📝 llm/server.go (+6 -1)
📝 llm/server_test.go (+74 -0)
📝 parser/parser.go (+5 -3)
📝 parser/parser_test.go (+4 -0)
📝 server/create.go (+20 -0)
📝 server/images.go (+15 -0)
📝 server/routes.go (+3 -0)

📄 Description

I would like to have a PREPEND directive in the modelfile, to support prepending arbitrary text to all responses from a model.

In particular - I would like to add a token like this:

TEMPLATE """
{{- if or .System .Tools }}<|im_start|>system
{{- if .System }}
{{ .System }}
{{- end }}
{{- if .Tools }}

# Tools

You may call one or more functions to assist with the user query.

You are provided with function signatures within <tools></tools> XML tags:
<tools>
{{- range .Tools }}
{"type": "function", "function": {{ .Function }}}
{{- end }}
</tools>

For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
<tool_call>
{"name": <function-name>, "arguments": <args-json-object>}
</tool_call>
{{- end }}<|im_end|>
{{ end }}
{{- range $i, $_ := .Messages }}
{{- $last := eq (len (slice $.Messages $i)) 1 -}}
{{- if eq .Role "user" }}<|im_start|>user
{{ .Content }}<|im_end|>
{{ else if eq .Role "assistant" }}<|im_start|>assistant
{{ if .Content }}{{ .Content }}
{{- else if .ToolCalls }}<tool_call>
{{ range .ToolCalls }}{"name": "{{ .Function.Name }}", "arguments": {{ .Function.Arguments }}}
{{ end }}</tool_call>
{{- end }}{{ if not $last }}<|im_end|>
{{ end }}
{{- else if eq .Role "tool" }}<|im_start|>user
<tool_response>
{{ .Content }}
</tool_response><|im_end|>
{{ end }}
{{- if and (ne .Role "assistant") $last }}<|im_start|>assistant
<think>   // Added a <think> token in the template like they do in the official huggingface template - but that causes the model not to return a <think>
{{ end }}
{{- end }}
"""

PREPEND "<think>" // This fixes clients which expect to see a <think> token up front

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ollama/ollama/pull/9552 **Author:** [@ehartford](https://github.com/ehartford) **Created:** 3/6/2025 **Status:** 🔄 Open **Base:** `main` ← **Head:** `add-prepend-directive` --- ### 📝 Commits (1) - [`1faaa12`](https://github.com/ollama/ollama/commit/1faaa129e7a8019e60693331346e1283c2ee255d) Add PREPEND Modelfile directive to prepend text to model responses ### 📊 Changes **8 files changed** (+129 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `api/types.go` (+2 -0) 📝 `llm/server.go` (+6 -1) 📝 `llm/server_test.go` (+74 -0) 📝 `parser/parser.go` (+5 -3) 📝 `parser/parser_test.go` (+4 -0) 📝 `server/create.go` (+20 -0) 📝 `server/images.go` (+15 -0) 📝 `server/routes.go` (+3 -0) </details> ### 📄 Description I would like to have a PREPEND directive in the modelfile, to support prepending arbitrary text to all responses from a model. In particular - I would like to add a <think> token like this: ``` TEMPLATE """ {{- if or .System .Tools }}<|im_start|>system {{- if .System }} {{ .System }} {{- end }} {{- if .Tools }} # Tools You may call one or more functions to assist with the user query. You are provided with function signatures within <tools></tools> XML tags: <tools> {{- range .Tools }} {"type": "function", "function": {{ .Function }}} {{- end }} </tools> For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags: <tool_call> {"name": <function-name>, "arguments": <args-json-object>} </tool_call> {{- end }}<|im_end|> {{ end }} {{- range $i, $_ := .Messages }} {{- $last := eq (len (slice $.Messages $i)) 1 -}} {{- if eq .Role "user" }}<|im_start|>user {{ .Content }}<|im_end|> {{ else if eq .Role "assistant" }}<|im_start|>assistant {{ if .Content }}{{ .Content }} {{- else if .ToolCalls }}<tool_call> {{ range .ToolCalls }}{"name": "{{ .Function.Name }}", "arguments": {{ .Function.Arguments }}} {{ end }}</tool_call> {{- end }}{{ if not $last }}<|im_end|> {{ end }} {{- else if eq .Role "tool" }}<|im_start|>user <tool_response> {{ .Content }} </tool_response><|im_end|> {{ end }} {{- if and (ne .Role "assistant") $last }}<|im_start|>assistant <think> // Added a <think> token in the template like they do in the official huggingface template - but that causes the model not to return a <think> {{ end }} {{- end }} """ PREPEND "<think>" // This fixes clients which expect to see a <think> token up front ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-13 00:15:01 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#12998