olmo3.go 10.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package parsers

import (
	"context"
	"fmt"
	"log/slog"
	"regexp"
	"strconv"
	"strings"

	"github.com/ollama/ollama/api"
	"github.com/ollama/ollama/logutil"
)

type olmo3ParserState int

const (
	olmo3StateContent olmo3ParserState = iota
	olmo3StateToolCalls
	olmo3StateToolCallsDone
)

const (
	olmo3FuncCallsOpenTag  = "<function_calls>"
	olmo3FuncCallsCloseTag = "</function_calls>"
)

type Olmo3Parser struct {
	state  olmo3ParserState
	buffer strings.Builder
}

func (p *Olmo3Parser) HasToolSupport() bool {
	return true
}

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

func (p *Olmo3Parser) Init(tools []api.Tool, lastMessage *api.Message, thinkValue *api.ThinkValue) []api.Tool {
	p.state = olmo3StateContent
	return tools
}

type olmo3ParserEvent interface {
	isOlmo3ParserEvent()
}

type olmo3ParserEventContent struct {
	content string
}

type olmo3ParserEventToolCalls struct {
	calls []api.ToolCall
}

func (olmo3ParserEventContent) isOlmo3ParserEvent()   {}
func (olmo3ParserEventToolCalls) isOlmo3ParserEvent() {}

func (p *Olmo3Parser) Add(s string, done bool) (content string, thinking string, calls []api.ToolCall, err error) {
	p.buffer.WriteString(s)

	if done {
		// Drain any remaining content
		bufStr := p.buffer.String()
		p.buffer.Reset()
		if p.state == olmo3StateContent && len(bufStr) > 0 {
			return bufStr, "", nil, nil
		}
		return "", "", nil, nil
	}

	events := p.parseEvents()

	var contentSb strings.Builder
	var allCalls []api.ToolCall
	for _, event := range events {
		switch event := event.(type) {
		case olmo3ParserEventContent:
			contentSb.WriteString(event.content)
		case olmo3ParserEventToolCalls:
			allCalls = append(allCalls, event.calls...)
		}
	}

	return contentSb.String(), "", allCalls, nil
}

func (p *Olmo3Parser) parseEvents() []olmo3ParserEvent {
	var all []olmo3ParserEvent

	keepLooping := true
	for keepLooping {
		var events []olmo3ParserEvent
		events, keepLooping = p.eat()
		if len(events) > 0 {
			all = append(all, events...)
		}
	}

	if len(all) > 0 {
		slog.Log(context.TODO(), logutil.LevelTrace, "olmo3 events parsed", "events", all, "state", p.state, "buffer", p.buffer.String())
	}

	return all
}

func (p *Olmo3Parser) eat() ([]olmo3ParserEvent, bool) {
	var events []olmo3ParserEvent
	bufStr := p.buffer.String()
	if bufStr == "" {
		return events, false
	}

	switch p.state {
	case olmo3StateContent:
		if strings.Contains(bufStr, olmo3FuncCallsOpenTag) {
			// Found <function_calls> tag
			split := strings.SplitN(bufStr, olmo3FuncCallsOpenTag, 2)
			content := split[0]
			remaining := split[1]

			p.buffer.Reset()
			p.buffer.WriteString(remaining)
			p.state = olmo3StateToolCalls

			if len(content) > 0 {
				events = append(events, olmo3ParserEventContent{content: content})
			}
			return events, true
		} else if overlapLen := overlap(bufStr, olmo3FuncCallsOpenTag); overlapLen > 0 {
			// Partial <function_calls> tag - withhold ambiguous content
			unambiguous := bufStr[:len(bufStr)-overlapLen]
			ambiguous := bufStr[len(bufStr)-overlapLen:]
			p.buffer.Reset()
			p.buffer.WriteString(ambiguous)
			if len(unambiguous) > 0 {
				events = append(events, olmo3ParserEventContent{content: unambiguous})
			}
			return events, false
		} else {
			// Regular content - emit all
			p.buffer.Reset()
			if len(bufStr) > 0 {
				events = append(events, olmo3ParserEventContent{content: bufStr})
			}
			return events, false
		}

	case olmo3StateToolCalls:
		if strings.Contains(bufStr, olmo3FuncCallsCloseTag) {
			// Found </function_calls> tag
			split := strings.SplitN(bufStr, olmo3FuncCallsCloseTag, 2)
			toolCallsStr := split[0]
			remaining := split[1]

			p.buffer.Reset()
			p.buffer.WriteString(remaining)
			p.state = olmo3StateToolCallsDone

			// Parse the function calls
			calls, err := parseOlmo3FunctionCalls(toolCallsStr)
			if err != nil {
				slog.Log(context.TODO(), logutil.LevelTrace, "failed to parse olmo3 function calls", "error", err, "content", toolCallsStr)
			} else if len(calls) > 0 {
				events = append(events, olmo3ParserEventToolCalls{calls: calls})
			}
			return events, true
		} else if overlapLen := overlap(bufStr, olmo3FuncCallsCloseTag); overlapLen > 0 {
			// Partial </function_calls> tag - wait for more
			return events, false
		}
		// Still collecting tool calls, wait for close tag
		return events, false

	case olmo3StateToolCallsDone:
		// After tool calls, emit remaining content
		p.buffer.Reset()
		p.state = olmo3StateContent
		if len(bufStr) > 0 {
			events = append(events, olmo3ParserEventContent{content: bufStr})
		}
		return events, false
	}

	return events, false
}

// parseOlmo3FunctionCalls parses function calls in Python-esque format:
// func_name(arg1="value1", arg2=123)
// Multiple calls are separated by newlines
func parseOlmo3FunctionCalls(s string) ([]api.ToolCall, error) {
	var calls []api.ToolCall
	s = strings.TrimSpace(s)
	if s == "" {
		return calls, nil
	}

	// Split by newlines for multiple function calls
	lines := strings.Split(s, "\n")
	for _, line := range lines {
		line = strings.TrimSpace(line)
		if line == "" {
			continue
		}

		call, err := parseOlmo3SingleFunctionCall(line)
		if err != nil {
			return nil, fmt.Errorf("failed to parse function call %q: %w", line, err)
		}
		calls = append(calls, call)
	}

	return calls, nil
}

// Regex to match function call: func_name(args)
var funcCallRegex = regexp.MustCompile(`^(\w+)\((.*)\)$`)

func parseOlmo3SingleFunctionCall(s string) (api.ToolCall, error) {
	matches := funcCallRegex.FindStringSubmatch(s)
	if matches == nil {
		return api.ToolCall{}, fmt.Errorf("invalid function call format")
	}

	funcName := matches[1]
	argsStr := matches[2]

	args, err := parseOlmo3Arguments(argsStr)
	if err != nil {
		return api.ToolCall{}, fmt.Errorf("failed to parse arguments: %w", err)
	}

	return api.ToolCall{
		Function: api.ToolCallFunction{
			Name:      funcName,
			Arguments: args,
		},
	}, nil
}

// parseOlmo3Arguments parses comma-separated key=value pairs
// Handles nested parentheses, brackets, braces, and quoted strings
func parseOlmo3Arguments(s string) (map[string]any, error) {
	args := make(map[string]any)
	s = strings.TrimSpace(s)
	if s == "" {
		return args, nil
	}

	// Split by commas, but respect nested structures and quotes
	parts := splitArguments(s)

	for _, part := range parts {
		part = strings.TrimSpace(part)
		if part == "" {
			continue
		}

		// Find the first = sign
		eqIdx := strings.Index(part, "=")
		if eqIdx == -1 {
			return nil, fmt.Errorf("invalid argument format: %s", part)
		}

		key := strings.TrimSpace(part[:eqIdx])
		valueStr := strings.TrimSpace(part[eqIdx+1:])

		value, err := parseOlmo3Value(valueStr)
		if err != nil {
			return nil, fmt.Errorf("failed to parse value for %s: %w", key, err)
		}

		args[key] = value
	}

	return args, nil
}

// splitArguments splits arguments by commas, respecting quotes and nested structures
func splitArguments(s string) []string {
	var parts []string
	var current strings.Builder
	depth := 0
	inString := false
	stringChar := byte(0)
	escaped := false

	for i := range s {
		c := s[i]

		if escaped {
			current.WriteByte(c)
			escaped = false
			continue
		}

		if c == '\\' && inString {
			current.WriteByte(c)
			escaped = true
			continue
		}

		if (c == '"' || c == '\'') && !inString {
			inString = true
			stringChar = c
			current.WriteByte(c)
			continue
		}

		if c == stringChar && inString {
			inString = false
			stringChar = 0
			current.WriteByte(c)
			continue
		}

		if !inString {
			switch c {
			case '(', '[', '{':
				depth++
				current.WriteByte(c)
			case ')', ']', '}':
				depth--
				current.WriteByte(c)
			case ',':
				if depth == 0 {
					parts = append(parts, current.String())
					current.Reset()
					continue
				}
				current.WriteByte(c)
			default:
				current.WriteByte(c)
			}
		} else {
			current.WriteByte(c)
		}
	}

	if current.Len() > 0 {
		parts = append(parts, current.String())
	}

	return parts
}

// parseOlmo3Value parses a value which can be a string, number, boolean, null, array, or object
func parseOlmo3Value(s string) (any, error) {
	s = strings.TrimSpace(s)

	// Check for quoted string
	if (strings.HasPrefix(s, `"`) && strings.HasSuffix(s, `"`)) ||
		(strings.HasPrefix(s, `'`) && strings.HasSuffix(s, `'`)) {
		// Remove quotes and unescape
		inner := s[1 : len(s)-1]
		return unescapeString(inner), nil
	}

	// Check for boolean
	if s == "true" || s == "True" {
		return true, nil
	}
	if s == "false" || s == "False" {
		return false, nil
	}

	// Check for null/None
	if s == "null" || s == "None" || s == "nil" {
		return nil, nil
	}

	// Check for number
	if i, err := strconv.ParseInt(s, 10, 64); err == nil {
		return i, nil
	}
	if f, err := strconv.ParseFloat(s, 64); err == nil {
		return f, nil
	}

	// Check for array [...]
	if strings.HasPrefix(s, "[") && strings.HasSuffix(s, "]") {
		return parseOlmo3Array(s[1 : len(s)-1])
	}

	// Check for object {...}
	if strings.HasPrefix(s, "{") && strings.HasSuffix(s, "}") {
		return parseOlmo3Object(s[1 : len(s)-1])
	}

	// Default to string without quotes
	return s, nil
}

func parseOlmo3Array(s string) ([]any, error) {
	s = strings.TrimSpace(s)
	if s == "" {
		return []any{}, nil
	}

	parts := splitArguments(s)
	var arr []any
	for _, part := range parts {
		val, err := parseOlmo3Value(part)
		if err != nil {
			return nil, err
		}
		arr = append(arr, val)
	}
	return arr, nil
}

func parseOlmo3Object(s string) (map[string]any, error) {
	s = strings.TrimSpace(s)
	if s == "" {
		return map[string]any{}, nil
	}

	// Objects use key: value or "key": value format
	obj := make(map[string]any)
	parts := splitArguments(s)
	for _, part := range parts {
		part = strings.TrimSpace(part)
		if part == "" {
			continue
		}

		// Find colon separator
		colonIdx := strings.Index(part, ":")
		if colonIdx == -1 {
			return nil, fmt.Errorf("invalid object entry: %s", part)
		}

		keyStr := strings.TrimSpace(part[:colonIdx])
		valueStr := strings.TrimSpace(part[colonIdx+1:])

		// Remove quotes from key if present
		if (strings.HasPrefix(keyStr, `"`) && strings.HasSuffix(keyStr, `"`)) ||
			(strings.HasPrefix(keyStr, `'`) && strings.HasSuffix(keyStr, `'`)) {
			keyStr = keyStr[1 : len(keyStr)-1]
		}

		val, err := parseOlmo3Value(valueStr)
		if err != nil {
			return nil, fmt.Errorf("failed to parse value for key %s: %w", keyStr, err)
		}

		obj[keyStr] = val
	}

	return obj, nil
}

func unescapeString(s string) string {
	// Handle common escape sequences
	s = strings.ReplaceAll(s, `\\`, "\x00") // Placeholder for backslash
	s = strings.ReplaceAll(s, `\"`, `"`)
	s = strings.ReplaceAll(s, `\'`, `'`)
	s = strings.ReplaceAll(s, `\n`, "\n")
	s = strings.ReplaceAll(s, `\t`, "\t")
	s = strings.ReplaceAll(s, `\r`, "\r")
	s = strings.ReplaceAll(s, "\x00", `\`) // Restore backslash
	return s
}