parsers.go 1.95 KB
Newer Older
Devon Rifkin's avatar
Devon Rifkin committed
1
2
3
4
package parsers

import (
	"github.com/ollama/ollama/api"
5
	"github.com/ollama/ollama/harmony"
Devon Rifkin's avatar
Devon Rifkin committed
6
7
)

Devon Rifkin's avatar
Devon Rifkin committed
8
type Parser interface {
9
10
11
12
13
14
	// Init initializes the parser with tools and optional last message for chat prefill
	// Returns processed tools if the parser needs to modify them (e.g., harmony renames them)
	Init(tools []api.Tool, lastMessage *api.Message) []api.Tool
	// Add processes streamed content and returns parsed content, thinking, and tool calls
	// The done flag indicates if this is the last chunk (used for draining accumulators)
	Add(s string, done bool) (content string, thinking string, calls []api.ToolCall, err error)
Devon Rifkin's avatar
Devon Rifkin committed
15
16
17
18
	HasToolSupport() bool
	HasThinkingSupport() bool
}

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
type ParserConstructor func() Parser

type ParserRegistry struct {
	constructors map[string]ParserConstructor
}

func (r *ParserRegistry) Register(name string, constructor ParserConstructor) {
	r.constructors[name] = constructor
}

var registry = ParserRegistry{
	constructors: make(map[string]ParserConstructor),
}

func Register(name string, constructor ParserConstructor) {
	registry.Register(name, constructor)
}

Devon Rifkin's avatar
Devon Rifkin committed
37
func ParserForName(name string) Parser {
38
39
40
	if parser, ok := registry.constructors[name]; ok {
		return parser()
	}
Devon Rifkin's avatar
Devon Rifkin committed
41
42
43
44
	switch name {
	case "qwen3-coder":
		parser := &Qwen3CoderParser{}
		return parser
45
46
47
	case "qwen3-vl-instruct":
		parser := &Qwen3VLParser{hasThinkingSupport: false}
		return parser
Devon Rifkin's avatar
Devon Rifkin committed
48
49
	case "passthrough":
		return &PassthroughParser{}
50
51
	case "harmony":
		return harmony.NewHarmonyMessageHandler()
Devon Rifkin's avatar
Devon Rifkin committed
52
53
54
55
56
57
58
	default:
		return nil
	}
}

type PassthroughParser struct{}

59
60
61
62
63
func (p *PassthroughParser) Init(tools []api.Tool, lastMessage *api.Message) []api.Tool {
	return tools // passthrough doesn't modify tools
}

func (p *PassthroughParser) Add(s string, done bool) (content string, thinking string, calls []api.ToolCall, err error) {
Devon Rifkin's avatar
Devon Rifkin committed
64
65
66
67
68
69
70
71
72
73
	return s, "", nil, nil
}

func (p *PassthroughParser) HasToolSupport() bool {
	return false
}

func (p *PassthroughParser) HasThinkingSupport() bool {
	return false
}