olmo3_test.go 12.3 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
package parsers

import (
	"testing"

	"github.com/google/go-cmp/cmp"

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

func TestOlmo3Parser(t *testing.T) {
	tests := []struct {
		name             string
		input            string
		expectedContent  string
		expectedThinking string
		expectedCalls    []api.ToolCall
	}{
		{
			name:            "simple content",
			input:           "Hello, how can I help you?",
			expectedContent: "Hello, how can I help you?",
		},
		{
			name:  "simple tool call",
			input: `<function_calls>get_weather(location="San Francisco")</function_calls>`,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "get_weather",
31
						Arguments: testArgs(map[string]any{"location": "San Francisco"}),
32
33
34
35
36
37
38
39
40
41
42
43
					},
				},
			},
		},
		{
			name:            "content then tool call",
			input:           `Let me check the weather.<function_calls>get_weather(location="NYC")</function_calls>`,
			expectedContent: "Let me check the weather.",
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "get_weather",
44
						Arguments: testArgs(map[string]any{"location": "NYC"}),
45
46
47
48
49
50
51
52
53
54
55
					},
				},
			},
		},
		{
			name:  "tool call with multiple arguments",
			input: `<function_calls>book_flight(from="SFO", to="NYC", date="2024-01-15")</function_calls>`,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name: "book_flight",
56
						Arguments: testArgs(map[string]any{
57
58
59
							"from": "SFO",
							"to":   "NYC",
							"date": "2024-01-15",
60
						}),
61
62
63
64
65
66
67
68
69
70
71
72
					},
				},
			},
		},
		{
			name: "multiple tool calls",
			input: `<function_calls>get_weather(location="San Francisco")
get_weather(location="New York")</function_calls>`,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "get_weather",
73
						Arguments: testArgs(map[string]any{"location": "San Francisco"}),
74
75
76
77
78
					},
				},
				{
					Function: api.ToolCallFunction{
						Name:      "get_weather",
79
						Arguments: testArgs(map[string]any{"location": "New York"}),
80
81
82
83
84
85
86
87
88
89
90
					},
				},
			},
		},
		{
			name:  "tool call with numeric argument",
			input: `<function_calls>set_temperature(value=72)</function_calls>`,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "set_temperature",
91
						Arguments: testArgs(map[string]any{"value": int64(72)}),
92
93
94
95
96
97
98
99
100
101
102
					},
				},
			},
		},
		{
			name:  "tool call with float argument",
			input: `<function_calls>set_price(amount=19.99)</function_calls>`,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "set_price",
103
						Arguments: testArgs(map[string]any{"amount": 19.99}),
104
105
106
107
108
109
110
111
112
113
114
					},
				},
			},
		},
		{
			name:  "tool call with boolean argument",
			input: `<function_calls>toggle_setting(enabled=true)</function_calls>`,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "toggle_setting",
115
						Arguments: testArgs(map[string]any{"enabled": true}),
116
117
118
119
120
121
122
123
124
125
126
					},
				},
			},
		},
		{
			name:  "tool call with null argument",
			input: `<function_calls>clear_value(field=null)</function_calls>`,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "clear_value",
127
						Arguments: testArgs(map[string]any{"field": nil}),
128
129
130
131
132
133
134
135
136
137
138
					},
				},
			},
		},
		{
			name:  "tool call with array argument",
			input: `<function_calls>process_items(items=["apple", "banana", "cherry"])</function_calls>`,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "process_items",
139
						Arguments: testArgs(map[string]any{"items": []any{"apple", "banana", "cherry"}}),
140
141
142
143
144
145
146
147
148
149
150
					},
				},
			},
		},
		{
			name:  "tool call with dict argument",
			input: `<function_calls>update_config(settings={"theme": "dark", "fontSize": 14})</function_calls>`,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name: "update_config",
151
						Arguments: testArgs(map[string]any{
152
153
154
155
							"settings": map[string]any{
								"theme":    "dark",
								"fontSize": int64(14),
							},
156
						}),
157
158
159
160
161
162
163
164
165
166
167
					},
				},
			},
		},
		{
			name:  "tool call with nested dict",
			input: `<function_calls>create_request(data={"user": {"name": "John", "age": 30}, "active": true})</function_calls>`,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name: "create_request",
168
						Arguments: testArgs(map[string]any{
169
170
171
172
173
174
175
							"data": map[string]any{
								"user": map[string]any{
									"name": "John",
									"age":  int64(30),
								},
								"active": true,
							},
176
						}),
177
178
179
180
181
182
183
184
185
186
187
					},
				},
			},
		},
		{
			name:  "tool call with no arguments",
			input: `<function_calls>get_current_time()</function_calls>`,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "get_current_time",
188
						Arguments: testArgs(map[string]any{}),
189
190
191
192
193
194
195
196
197
198
199
					},
				},
			},
		},
		{
			name:  "tool call with single quotes",
			input: `<function_calls>search(query='hello world')</function_calls>`,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "search",
200
						Arguments: testArgs(map[string]any{"query": "hello world"}),
201
202
203
204
205
206
207
208
209
210
211
					},
				},
			},
		},
		{
			name:  "tool call with escaped quotes",
			input: `<function_calls>search(query="say \"hello\"")</function_calls>`,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "search",
212
						Arguments: testArgs(map[string]any{"query": `say "hello"`}),
213
214
215
216
217
218
219
220
221
222
223
					},
				},
			},
		},
		{
			name:  "tool call with mixed argument types",
			input: `<function_calls>create_user(name="John", age=30, active=true)</function_calls>`,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name: "create_user",
224
						Arguments: testArgs(map[string]any{
225
226
227
							"name":   "John",
							"age":    int64(30),
							"active": true,
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
					},
				},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			p := &Olmo3Parser{}
			p.Init(nil, nil, nil)

			content, thinking, calls, err := p.Add(tt.input, false)
			if err != nil {
				t.Fatalf("unexpected error: %v", err)
			}

			// Drain remaining content
			finalContent, finalThinking, finalCalls, err := p.Add("", true)
			if err != nil {
				t.Fatalf("unexpected error on done: %v", err)
			}
			content += finalContent
			thinking += finalThinking
			calls = append(calls, finalCalls...)

			if diff := cmp.Diff(content, tt.expectedContent); diff != "" {
				t.Errorf("content mismatch (-got +want):\n%s", diff)
			}
			if diff := cmp.Diff(thinking, tt.expectedThinking); diff != "" {
				t.Errorf("thinking mismatch (-got +want):\n%s", diff)
			}
260
			if diff := cmp.Diff(calls, tt.expectedCalls, argsComparer); diff != "" {
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
				t.Errorf("calls mismatch (-got +want):\n%s", diff)
			}
		})
	}
}

func TestOlmo3Parser_Streaming(t *testing.T) {
	tests := []struct {
		name            string
		chunks          []string
		expectedContent string
		expectedCalls   []api.ToolCall
	}{
		{
			name:            "streaming content",
			chunks:          []string{"Hello, ", "how ", "can I help?"},
			expectedContent: "Hello, how can I help?",
		},
		{
			name:   "streaming tool call",
			chunks: []string{"<function_", "calls>get_weather", "(location=\"SF\")", "</function_calls>"},
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "get_weather",
286
						Arguments: testArgs(map[string]any{"location": "SF"}),
287
288
289
290
291
292
293
294
295
296
297
298
					},
				},
			},
		},
		{
			name:            "streaming content then tool call",
			chunks:          []string{"Let me check.", "<function_calls>", "get_weather(location=\"NYC\")", "</function_calls>"},
			expectedContent: "Let me check.",
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "get_weather",
299
						Arguments: testArgs(map[string]any{"location": "NYC"}),
300
301
302
303
304
305
306
307
308
309
310
					},
				},
			},
		},
		{
			name:   "tool call tag split across chunks",
			chunks: []string{"<func", "tion_calls>test()</function_calls>"},
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "test",
311
						Arguments: testArgs(map[string]any{}),
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
					},
				},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			p := &Olmo3Parser{}
			p.Init(nil, nil, nil)

			var allContent string
			var allCalls []api.ToolCall

			for _, chunk := range tt.chunks {
				content, _, calls, err := p.Add(chunk, false)
				if err != nil {
					t.Fatalf("unexpected error: %v", err)
				}
				allContent += content
				allCalls = append(allCalls, calls...)
			}

			// Drain
			content, _, calls, err := p.Add("", true)
			if err != nil {
				t.Fatalf("unexpected error on done: %v", err)
			}
			allContent += content
			allCalls = append(allCalls, calls...)

			if diff := cmp.Diff(allContent, tt.expectedContent); diff != "" {
				t.Errorf("content mismatch (-got +want):\n%s", diff)
			}
346
			if diff := cmp.Diff(allCalls, tt.expectedCalls, argsComparer); diff != "" {
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
				t.Errorf("calls mismatch (-got +want):\n%s", diff)
			}
		})
	}
}

func TestOlmo3Parser_HasToolSupport(t *testing.T) {
	p := &Olmo3Parser{}
	if !p.HasToolSupport() {
		t.Error("expected HasToolSupport to return true")
	}
}

func TestOlmo3Parser_HasThinkingSupport(t *testing.T) {
	p := &Olmo3Parser{}
	if p.HasThinkingSupport() {
		t.Error("expected HasThinkingSupport to return false")
	}
}

func TestParseOlmo3FunctionCalls(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		expected []api.ToolCall
		wantErr  bool
	}{
		{
			name:  "simple call",
			input: `get_weather(location="SF")`,
			expected: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "get_weather",
381
						Arguments: testArgs(map[string]any{"location": "SF"}),
382
383
384
385
386
387
388
389
390
391
392
					},
				},
			},
		},
		{
			name:  "multiple args",
			input: `send_email(to="user@example.com", subject="Hello", body="Test message")`,
			expected: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name: "send_email",
393
						Arguments: testArgs(map[string]any{
394
395
396
							"to":      "user@example.com",
							"subject": "Hello",
							"body":    "Test message",
397
						}),
398
399
400
401
402
403
404
405
406
407
408
409
					},
				},
			},
		},
		{
			name: "multiple calls with newlines",
			input: `get_weather(location="SF")
get_time(timezone="PST")`,
			expected: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "get_weather",
410
						Arguments: testArgs(map[string]any{"location": "SF"}),
411
412
413
414
415
					},
				},
				{
					Function: api.ToolCallFunction{
						Name:      "get_time",
416
						Arguments: testArgs(map[string]any{"timezone": "PST"}),
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
					},
				},
			},
		},
		{
			name:     "empty input",
			input:    "",
			expected: nil,
		},
		{
			name:     "whitespace only",
			input:    "   \n   ",
			expected: nil,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			calls, err := parseOlmo3FunctionCalls(tt.input)
			if (err != nil) != tt.wantErr {
				t.Errorf("parseOlmo3FunctionCalls() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
440
			if diff := cmp.Diff(calls, tt.expected, argsComparer); diff != "" {
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
				t.Errorf("calls mismatch (-got +want):\n%s", diff)
			}
		})
	}
}

func TestParseOlmo3Value(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		expected any
	}{
		{"string double quotes", `"hello"`, "hello"},
		{"string single quotes", `'hello'`, "hello"},
		{"integer", "42", int64(42)},
		{"negative integer", "-10", int64(-10)},
		{"float", "3.14", 3.14},
		{"boolean true", "true", true},
		{"boolean True", "True", true},
		{"boolean false", "false", false},
		{"null", "null", nil},
		{"None", "None", nil},
		{"empty array", "[]", []any{}},
		{"array with strings", `["a", "b"]`, []any{"a", "b"}},
		{"array with numbers", "[1, 2, 3]", []any{int64(1), int64(2), int64(3)}},
		{"empty object", "{}", map[string]any{}},
		{"simple object", `{"name": "John"}`, map[string]any{"name": "John"}},
		{"object with number", `{"age": 30}`, map[string]any{"age": int64(30)}},
		{"object with multiple keys", `{"a": 1, "b": 2}`, map[string]any{"a": int64(1), "b": int64(2)}},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result, err := parseOlmo3Value(tt.input)
			if err != nil {
				t.Fatalf("unexpected error: %v", err)
			}
			if diff := cmp.Diff(result, tt.expected); diff != "" {
				t.Errorf("value mismatch (-got +want):\n%s", diff)
			}
		})
	}
}