move thinking logic into its own package (#10990)

move thinking logic into its own package
This commit is contained in:
Devon Rifkin
2025-06-06 12:02:20 -07:00
committed by GitHub
parent c6a6d7294d
commit a3b6886b7d
6 changed files with 281 additions and 269 deletions

View File

@@ -37,6 +37,7 @@ import (
"github.com/ollama/ollama/server/internal/client/ollama"
"github.com/ollama/ollama/server/internal/registry"
"github.com/ollama/ollama/template"
"github.com/ollama/ollama/thinking"
"github.com/ollama/ollama/tools"
"github.com/ollama/ollama/types/errtypes"
"github.com/ollama/ollama/types/model"
@@ -282,10 +283,10 @@ func (s *Server) GenerateHandler(c *gin.Context) {
prompt = b.String()
}
var thinkingState *ThinkingParser
openingTag, closingTag := inferThinkingTags(m.Template.Template)
var thinkingState *thinking.Parser
openingTag, closingTag := thinking.InferTags(m.Template.Template)
if req.Think != nil && *req.Think && openingTag != "" && closingTag != "" {
thinkingState = &ThinkingParser{
thinkingState = &thinking.Parser{
OpeningTag: openingTag,
ClosingTag: closingTag,
}
@@ -1522,10 +1523,10 @@ func (s *Server) ChatHandler(c *gin.Context) {
return
}
var thinkingState *ThinkingParser
openingTag, closingTag := inferThinkingTags(m.Template.Template)
var thinkingState *thinking.Parser
openingTag, closingTag := thinking.InferTags(m.Template.Template)
if req.Think != nil && *req.Think && openingTag != "" && closingTag != "" {
thinkingState = &ThinkingParser{
thinkingState = &thinking.Parser{
OpeningTag: openingTag,
ClosingTag: closingTag,
}
@@ -1676,7 +1677,7 @@ func filterThinkTags(msgs []api.Message, m *Model) []api.Message {
// change the user output), we should probably perform this filtering
// for all thinking models (not just qwen3 & deepseek-r1) since it tends
// to save tokens and improve quality.
thinkingState := &ThinkingParser{
thinkingState := &thinking.Parser{
OpeningTag: "<think>",
ClosingTag: "</think>",
}