[PR #8511] [CLOSED] Added golang template string manipulation functions, contains, hasPrefix, hasSuffix, split. #11456

Closed
opened 2025-11-12 16:14:37 -06:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/8511
Author: @tbiehn
Created: 1/21/2025
Status: Closed

Base: mainHead: TemplateFunctions


📝 Commits (1)

  • d52dc5a Added string manipulation functions, contains, hasPrefix, hasSuffix, and split to templates. Uncovered and corrected a bug where the Template.Execute block drops custom registered functions. Extended test suite to cover these new template functions. Supports template logic like found in DeepSeek R1 blocks.

📊 Changes

2 files changed (+73 additions, -1 deletions)

View changed files

📝 template/template.go (+6 -1)
📝 template/template_test.go (+67 -0)

📄 Description

Uncovered and corrected a bug where the Template.Execute block drops custom registered functions. Extended test suite to cover these new template functions.
These have been introduced to support DeepSeek R1 models, their training templates elide old <think> blocks as the conversation progresses.
Supporting this use case;

  {{- else if eq $msg.Role "assistant" }}
    {{- if $is_tool }}
<|tool▁outputs▁end|>{{ $msg.Content }}<|end▁of▁sentence|>
      {{- $is_tool = false -}}
    {{- else }}
      {{- $content := $msg.Content -}}
      {{- /* Only strip chain-of-thought if we detect `</think>` via `contains` */ -}}
      {{- if (contains $content "</think>") }}
        {{- $parts := split $content "</think>" -}}
        {{- if gt (len $parts) 1 }}
          {{- $content = index $parts 1 -}}
        {{- end }}
      {{- end }}
<|Assistant|>{{ $content }}<|end▁of▁sentence|>
    {{- end }}

Duplicating this template (spacing mine):

{% set content = message['content'] %}
  {% if '</think>' in content %}
    {% set content = content.split('</think>')[-1] %}
  {% endif %}
  {{'<|Assistant|>' + content + '<|end▁of▁sentence|>'}}
{%- endif %}

Addresses: #8502


🔄 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/8511 **Author:** [@tbiehn](https://github.com/tbiehn) **Created:** 1/21/2025 **Status:** ❌ Closed **Base:** `main` ← **Head:** `TemplateFunctions` --- ### 📝 Commits (1) - [`d52dc5a`](https://github.com/ollama/ollama/commit/d52dc5acd9d461948a145e6e020cda1a6eff8c60) Added string manipulation functions, contains, hasPrefix, hasSuffix, and split to templates. Uncovered and corrected a bug where the Template.Execute block drops custom registered functions. Extended test suite to cover these new template functions. Supports template logic like found in DeepSeek R1 <think/> blocks. ### 📊 Changes **2 files changed** (+73 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `template/template.go` (+6 -1) 📝 `template/template_test.go` (+67 -0) </details> ### 📄 Description Uncovered and corrected a bug where the Template.Execute block drops custom registered functions. Extended test suite to cover these new template functions. These have been introduced to support DeepSeek R1 models, their training templates elide old `<think>` blocks as the conversation progresses. Supporting this use case; ``` {{- else if eq $msg.Role "assistant" }} {{- if $is_tool }} <|tool▁outputs▁end|>{{ $msg.Content }}<|end▁of▁sentence|> {{- $is_tool = false -}} {{- else }} {{- $content := $msg.Content -}} {{- /* Only strip chain-of-thought if we detect `</think>` via `contains` */ -}} {{- if (contains $content "</think>") }} {{- $parts := split $content "</think>" -}} {{- if gt (len $parts) 1 }} {{- $content = index $parts 1 -}} {{- end }} {{- end }} <|Assistant|>{{ $content }}<|end▁of▁sentence|> {{- end }} ``` Duplicating this template (spacing mine): ``` {% set content = message['content'] %} {% if '</think>' in content %} {% set content = content.split('</think>')[-1] %} {% endif %} {{'<|Assistant|>' + content + '<|end▁of▁sentence|>'}} {%- endif %} ``` Addresses: #8502 --- <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 2025-11-12 16:14:37 -06:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama-ollama#11456