Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.
harmony: move harmony parsing into a package (#12016)
Parth Sareen7cce5aac7689
diff --git a/server/harmonyparser.go b/harmony/harmonyparser.gosimilarity index 95%rename from server/harmonyparser.gorename to harmony/harmonyparser.goindex 4405cea44..e72496614 100644--- a/server/harmonyparser.go+++ b/harmony/harmonyparser.go@@ -1,10 +1,9 @@-package server+package harmonyimport ("context""fmt""log/slog"- "slices""strings""unicode"@@ -20,18 +19,6 @@ const (harmonyParserState_ParsingContent)-func shouldUseHarmony(model Model) bool {- if slices.Contains([]string{"gptoss", "gpt-oss"}, model.Config.ModelFamily) {- // heuristic to check whether the template expects to be parsed via harmony:- // search for harmony tags that are nearly always used- if model.Template.Contains("<|start|>") && model.Template.Contains("<|end|>") {- return true- }- }-- return false-}-func (s harmonyParserState) String() string {switch s {// we're looking for the message start tag@@ -277,20 +264,20 @@ const (// This is a higher level interface that maps harmony concepts into ollama conceptstype HarmonyMessageHandler struct {state harmonyMessageState- harmonyParser *HarmonyParser- functionNameMap *FunctionNameMap+ HarmonyParser *HarmonyParser+ FunctionNameMap *FunctionNameMap}// NewHarmonyMessageHandler creates a new message handlerfunc NewHarmonyMessageHandler() *HarmonyMessageHandler {return &HarmonyMessageHandler{state: harmonyMessageState_Normal,- harmonyParser: &HarmonyParser{+ HarmonyParser: &HarmonyParser{MessageStartTag: "<|start|>",MessageEndTag: "<|end|>",HeaderEndTag: "<|message|>",},- functionNameMap: NewFunctionNameMap(),+ FunctionNameMap: NewFunctionNameMap(),}}@@ -301,7 +288,7 @@ func (h *HarmonyMessageHandler) AddContent(content string, toolParser *HarmonyTothinkingSb := strings.Builder{}toolContentSb := strings.Builder{}- events := h.harmonyParser.AddContent(content)+ events := h.HarmonyParser.AddContent(content)for _, event := range events {switch event := event.(type) {case HarmonyEventHeaderComplete:diff --git a/server/harmonyparser_test.go b/harmony/harmonyparser_test.gosimilarity index 99%rename from server/harmonyparser_test.gorename to harmony/harmonyparser_test.goindex 8a22f3404..b988a018f 100644--- a/server/harmonyparser_test.go+++ b/harmony/harmonyparser_test.go@@ -1,4 +1,4 @@-package server+package harmonyimport ("fmt"diff --git a/server/routes.go b/server/routes.goindex 60b7e3e84..cc8913537 100644--- a/server/routes.go+++ b/server/routes.go@@ -32,6 +32,7 @@ import ("github.com/ollama/ollama/envconfig""github.com/ollama/ollama/format""github.com/ollama/ollama/fs/ggml"+ "github.com/ollama/ollama/harmony""github.com/ollama/ollama/llm""github.com/ollama/ollama/logutil""github.com/ollama/ollama/openai"@@ -45,6 +46,18 @@ import ("github.com/ollama/ollama/version")+func shouldUseHarmony(model *Model) bool {+ if slices.Contains([]string{"gptoss", "gpt-oss"}, model.Config.ModelFamily) {+ // heuristic to check whether the template expects to be parsed via harmony:+ // search for harmony tags that are nearly always used+ if model.Template.Contains("<|start|>") && model.Template.Contains("<|end|>") {+ return true+ }+ }++ return false+}+func experimentEnabled(name string) bool {return slices.Contains(strings.Split(os.Getenv("OLLAMA_EXPERIMENT"), ","), name)}@@ -194,12 +207,12 @@ func (s *Server) GenerateHandler(c *gin.Context) {return}- useHarmony := shouldUseHarmony(*m) && !req.Raw- var harmonyMessageHandler *HarmonyMessageHandler- var harmonyToolParser *HarmonyToolCallAccumulator+ useHarmony := shouldUseHarmony(m) && !req.Raw+ var harmonyMessageHandler *harmony.HarmonyMessageHandler+ var harmonyToolParser *harmony.HarmonyToolCallAccumulatorif useHarmony {- harmonyMessageHandler = NewHarmonyMessageHandler()- harmonyMessageHandler.harmonyParser.AddImplicitStart()+ harmonyMessageHandler = harmony.NewHarmonyMessageHandler()+ harmonyMessageHandler.HarmonyParser.AddImplicitStart()harmonyToolParser = harmonyMessageHandler.CreateToolParser()}@@ -1603,19 +1616,19 @@ func (s *Server) ChatHandler(c *gin.Context) {}msgs = filterThinkTags(msgs, m)- var harmonyMessageHandler *HarmonyMessageHandler- var harmonyToolParser *HarmonyToolCallAccumulator+ var harmonyMessageHandler *harmony.HarmonyMessageHandler+ var harmonyToolParser *harmony.HarmonyToolCallAccumulator- useHarmony := shouldUseHarmony(*m)+ useHarmony := shouldUseHarmony(m)processedTools := req.Toolsif useHarmony {- harmonyMessageHandler = NewHarmonyMessageHandler()+ harmonyMessageHandler = harmony.NewHarmonyMessageHandler()var lastMessage *api.Messageif len(msgs) > 0 {lastMessage = &msgs[len(msgs)-1]}- harmonyMessageHandler.harmonyParser.AddImplicitStartOrPrefill(lastMessage)+ harmonyMessageHandler.HarmonyParser.AddImplicitStartOrPrefill(lastMessage)harmonyToolParser = harmonyMessageHandler.CreateToolParser()// make a copy of tools to pass to the chat prompt. Function names may be@@ -1623,7 +1636,7 @@ func (s *Server) ChatHandler(c *gin.Context) {processedTools = make([]api.Tool, len(req.Tools))copy(processedTools, req.Tools)for i, tool := range processedTools {- processedTools[i].Function.Name = harmonyMessageHandler.functionNameMap.ConvertAndAdd(tool.Function.Name)+ processedTools[i].Function.Name = harmonyMessageHandler.FunctionNameMap.ConvertAndAdd(tool.Function.Name)}}@@ -1705,7 +1718,7 @@ func (s *Server) ChatHandler(c *gin.Context) {toolName, toolContent := harmonyToolParser.Drain()if toolName != nil {*toolName = strings.TrimPrefix(*toolName, "functions.")- *toolName = harmonyMessageHandler.functionNameMap.OriginalFromConverted(*toolName)+ *toolName = harmonyMessageHandler.FunctionNameMap.OriginalFromConverted(*toolName)var args api.ToolCallFunctionArgumentsif err := json.Unmarshal([]byte(toolContent), &args); err != nil {errStr := fmt.Sprintf("error parsing tool call: raw='%s', err=%s", toolContent, err.Error())