nemotron3nano_test.go 18.9 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
package parsers

import (
	"testing"

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

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

func TestNemotron3NanoParser(t *testing.T) {
	tests := []struct {
		name             string
		input            string
		thinkValue       *api.ThinkValue
		expectedContent  string
		expectedThinking string
		expectedCalls    []api.ToolCall
	}{
		{
			name:            "simple content - no thinking",
			input:           "Hello, how can I help you?",
			thinkValue:      nil,
			expectedContent: "Hello, how can I help you?",
		},
		{
			name:            "simple content - thinking disabled",
			input:           "Hello, how can I help you?",
			thinkValue:      &api.ThinkValue{Value: false},
			expectedContent: "Hello, how can I help you?",
		},
		{
			name:             "thinking then content",
			input:            "Let me think about this...</think>\nHere is my answer.",
			thinkValue:       &api.ThinkValue{Value: true},
			expectedThinking: "Let me think about this...",
			expectedContent:  "Here is my answer.",
		},
		{
			name:             "thinking with newlines",
			input:            "Step 1: Analyze\nStep 2: Process\nStep 3: Conclude</think>\nThe answer is 42.",
			thinkValue:       &api.ThinkValue{Value: true},
			expectedThinking: "Step 1: Analyze\nStep 2: Process\nStep 3: Conclude",
			expectedContent:  "The answer is 42.",
		},
		{
			name:       "simple tool call",
			input:      "<tool_call>\n<function=get_weather>\n<parameter=city>\nParis\n</parameter>\n</function>\n</tool_call>",
			thinkValue: nil,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "get_weather",
54
						Arguments: testArgs(map[string]any{"city": "Paris"}),
55
56
57
58
59
60
61
62
63
64
65
66
67
					},
				},
			},
		},
		{
			name:            "content then tool call",
			input:           "Let me check the weather.\n<tool_call>\n<function=get_weather>\n<parameter=city>\nNYC\n</parameter>\n</function>\n</tool_call>",
			thinkValue:      nil,
			expectedContent: "Let me check the weather.",
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "get_weather",
68
						Arguments: testArgs(map[string]any{"city": "NYC"}),
69
70
71
72
73
74
75
76
77
78
79
80
					},
				},
			},
		},
		{
			name:       "tool call with multiple parameters",
			input:      "<tool_call>\n<function=book_flight>\n<parameter=from>\nSFO\n</parameter>\n<parameter=to>\nNYC\n</parameter>\n</function>\n</tool_call>",
			thinkValue: nil,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name: "book_flight",
81
						Arguments: testArgs(map[string]any{
82
83
							"from": "SFO",
							"to":   "NYC",
84
						}),
85
86
87
88
89
90
91
92
93
94
95
96
97
					},
				},
			},
		},
		{
			name: "multiple tool calls",
			input: "<tool_call>\n<function=get_weather>\n<parameter=city>\nSan Francisco\n</parameter>\n</function>\n</tool_call>\n" +
				"<tool_call>\n<function=get_weather>\n<parameter=city>\nNew York\n</parameter>\n</function>\n</tool_call>",
			thinkValue: nil,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "get_weather",
98
						Arguments: testArgs(map[string]any{"city": "San Francisco"}),
99
100
101
102
103
					},
				},
				{
					Function: api.ToolCallFunction{
						Name:      "get_weather",
104
						Arguments: testArgs(map[string]any{"city": "New York"}),
105
106
107
108
109
110
111
112
113
114
115
116
117
					},
				},
			},
		},
		{
			name:             "thinking then tool call",
			input:            "I should check the weather...</think>\n<tool_call>\n<function=get_weather>\n<parameter=city>\nParis\n</parameter>\n</function>\n</tool_call>",
			thinkValue:       &api.ThinkValue{Value: true},
			expectedThinking: "I should check the weather...",
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "get_weather",
118
						Arguments: testArgs(map[string]any{"city": "Paris"}),
119
120
121
122
123
124
125
126
127
128
129
130
131
132
					},
				},
			},
		},
		{
			name:             "thinking content then tool call",
			input:            "Let me think...</think>\nI'll check for you.\n<tool_call>\n<function=search>\n<parameter=query>\ntest\n</parameter>\n</function>\n</tool_call>",
			thinkValue:       &api.ThinkValue{Value: true},
			expectedThinking: "Let me think...",
			expectedContent:  "I'll check for you.",
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "search",
133
						Arguments: testArgs(map[string]any{"query": "test"}),
134
135
136
137
138
139
140
141
142
143
144
145
					},
				},
			},
		},
		{
			name:       "tool call with multiline parameter value",
			input:      "<tool_call>\n<function=create_note>\n<parameter=content>\nLine 1\nLine 2\nLine 3\n</parameter>\n</function>\n</tool_call>",
			thinkValue: nil,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "create_note",
146
						Arguments: testArgs(map[string]any{"content": "Line 1\nLine 2\nLine 3"}),
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
					},
				},
			},
		},
		{
			name:             "empty thinking block - immediate close",
			input:            "</think>\nHere is my answer.",
			thinkValue:       &api.ThinkValue{Value: true},
			expectedThinking: "",
			expectedContent:  "Here is my answer.",
		},
		{
			name:            "thinking disabled but model outputs think close anyway",
			input:           "</think>\nSome content after spurious tag.",
			thinkValue:      &api.ThinkValue{Value: false},
			expectedContent: "</think>\nSome content after spurious tag.",
		},
		{
			name:          "tool call with no function name - returns empty tool call",
			input:         "<tool_call>\n<function=>\n</function>\n</tool_call>",
			thinkValue:    nil,
168
			expectedCalls: []api.ToolCall{{Function: api.ToolCallFunction{Name: "", Arguments: api.NewToolCallFunctionArguments()}}},
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
		},
		{
			name:            "content with newlines preserved",
			input:           "Line 1\n\nLine 2\n\n\nLine 3",
			thinkValue:      nil,
			expectedContent: "Line 1\n\nLine 2\n\n\nLine 3",
		},
		{
			name:             "thinking with only whitespace after close tag",
			input:            "My thoughts...</think>   \n\t\n   Content here.",
			thinkValue:       &api.ThinkValue{Value: true},
			expectedThinking: "My thoughts...",
			expectedContent:  "Content here.",
		},
		{
			name:            "unicode content",
			input:           "Hello 世界! 🌍 Ñoño",
			thinkValue:      nil,
			expectedContent: "Hello 世界! 🌍 Ñoño",
		},
		{
			name:       "tool call with numeric parameter",
			input:      "<tool_call>\n<function=set_temp>\n<parameter=value>\n42\n</parameter>\n</function>\n</tool_call>",
			thinkValue: nil,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "set_temp",
197
						Arguments: testArgs(map[string]any{"value": "42"}),
198
199
200
201
202
203
204
205
					},
				},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
206
			p := &Nemotron3NanoParser{}
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
			p.Init(nil, nil, tt.thinkValue)

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

func TestNemotron3NanoParser_Streaming(t *testing.T) {
	tests := []struct {
		name             string
		chunks           []string
		thinkValue       *api.ThinkValue
		expectedContent  string
		expectedThinking string
		expectedCalls    []api.ToolCall
	}{
		{
			name:            "streaming content character by character",
			chunks:          []string{"H", "e", "l", "l", "o", ",", " ", "w", "o", "r", "l", "d", "!"},
			thinkValue:      nil,
			expectedContent: "Hello, world!",
		},
		{
			name:            "streaming content small tokens",
			chunks:          []string{"Hel", "lo", ", ", "how ", "can", " I", " help", " you", " today", "?"},
			thinkValue:      nil,
			expectedContent: "Hello, how can I help you today?",
		},
		{
			name:             "streaming thinking then content - granular",
			chunks:           []string{"Let", " me", " th", "ink", " about", " this", "...", "<", "/", "think", ">", "\n", "Here", " is", " my", " answer", "."},
			thinkValue:       &api.ThinkValue{Value: true},
			expectedThinking: "Let me think about this...",
			expectedContent:  "Here is my answer.",
		},
		{
			name:             "streaming thinking with newlines - granular",
			chunks:           []string{"Step", " 1", ":", " Ana", "lyze\n", "Step", " 2", ":", " Pro", "cess", "</", "thi", "nk>", "\n", "The", " ans", "wer."},
			thinkValue:       &api.ThinkValue{Value: true},
			expectedThinking: "Step 1: Analyze\nStep 2: Process",
			expectedContent:  "The answer.",
		},
		{
			name:       "streaming tool call - highly granular",
			chunks:     []string{"<", "tool", "_", "call", ">", "\n", "<", "func", "tion", "=", "get", "_", "weather", ">", "\n", "<", "param", "eter", "=", "city", ">", "\n", "Par", "is", "\n", "</", "param", "eter", ">", "\n", "</", "func", "tion", ">", "\n", "</", "tool", "_", "call", ">"},
			thinkValue: nil,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "get_weather",
279
						Arguments: testArgs(map[string]any{"city": "Paris"}),
280
281
282
283
284
285
286
287
288
289
290
291
292
					},
				},
			},
		},
		{
			name:            "streaming content then tool call - granular",
			chunks:          []string{"Let", " me", " check", " the", " weather", ".", "\n<", "tool_call", ">", "\n", "<function=", "get_weather", ">", "\n", "<parameter=", "city", ">", "\n", "NYC", "\n", "</parameter>", "\n", "</function>", "\n", "</tool_call>"},
			thinkValue:      nil,
			expectedContent: "Let me check the weather.",
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "get_weather",
293
						Arguments: testArgs(map[string]any{"city": "NYC"}),
294
295
296
297
298
299
300
301
302
303
304
					},
				},
			},
		},
		{
			name:   "tool call tag split character by character",
			chunks: []string{"<", "t", "o", "o", "l", "_", "c", "a", "l", "l", ">", "\n", "<", "f", "u", "n", "c", "t", "i", "o", "n", "=", "t", "e", "s", "t", ">", "\n", "<", "/", "f", "u", "n", "c", "t", "i", "o", "n", ">", "\n", "<", "/", "t", "o", "o", "l", "_", "c", "a", "l", "l", ">"},
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "test",
305
						Arguments: api.NewToolCallFunctionArguments(),
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
					},
				},
			},
		},
		{
			name:             "thinking close tag split character by character",
			chunks:           []string{"I", "'", "m", " ", "t", "h", "i", "n", "k", "i", "n", "g", ".", ".", ".", "<", "/", "t", "h", "i", "n", "k", ">", "\n", "D", "o", "n", "e", "!"},
			thinkValue:       &api.ThinkValue{Value: true},
			expectedThinking: "I'm thinking...",
			expectedContent:  "Done!",
		},
		{
			name:             "multiple whitespace after think tag - separate chunks",
			chunks:           []string{"Thinking...", "</think>", "\n", "\n", " ", "Content here."},
			thinkValue:       &api.ThinkValue{Value: true},
			expectedThinking: "Thinking...",
			expectedContent:  "Content here.",
		},
		{
			name:       "tool call with multiple parameters - streaming",
			chunks:     []string{"<tool_", "call>\n", "<function", "=book_", "flight>", "\n<para", "meter=", "from>\n", "SFO\n", "</param", "eter>", "\n<param", "eter=to", ">\nNYC", "\n</para", "meter>", "\n</func", "tion>\n", "</tool_", "call>"},
			thinkValue: nil,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name: "book_flight",
332
						Arguments: testArgs(map[string]any{
333
334
							"from": "SFO",
							"to":   "NYC",
335
						}),
336
337
338
339
340
341
342
343
344
345
346
347
348
349
					},
				},
			},
		},
		{
			name:             "thinking then content then tool call - streaming",
			chunks:           []string{"Ana", "lyzing", " your", " request", "...", "</", "think", ">\n", "I'll", " check", " that", " for", " you", ".", "\n", "<tool", "_call", ">\n", "<function", "=search", ">\n", "<parameter", "=query", ">\n", "test", " query", "\n</", "parameter", ">\n", "</function", ">\n", "</tool", "_call", ">"},
			thinkValue:       &api.ThinkValue{Value: true},
			expectedThinking: "Analyzing your request...",
			expectedContent:  "I'll check that for you.",
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "search",
350
						Arguments: testArgs(map[string]any{"query": "test query"}),
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
					},
				},
			},
		},
		{
			name: "multiple tool calls - streaming",
			chunks: []string{
				"<tool_call>", "\n", "<function=", "get_weather>", "\n",
				"<parameter=", "city>\n", "San Fran", "cisco\n", "</parameter>", "\n",
				"</function>", "\n", "</tool_call>", "\n",
				"<tool_", "call>\n", "<function", "=get_weather", ">\n",
				"<param", "eter=city", ">\nNew", " York\n", "</parameter>\n",
				"</function>\n", "</tool_call>",
			},
			thinkValue: nil,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "get_weather",
370
						Arguments: testArgs(map[string]any{"city": "San Francisco"}),
371
372
373
374
375
					},
				},
				{
					Function: api.ToolCallFunction{
						Name:      "get_weather",
376
						Arguments: testArgs(map[string]any{"city": "New York"}),
377
378
379
380
381
382
383
384
385
386
387
388
					},
				},
			},
		},
		{
			name:       "tool call with multiline parameter - streaming",
			chunks:     []string{"<tool_call>\n", "<function=", "create_note>\n", "<parameter=", "content>\n", "Line 1", "\nLine", " 2\n", "Line 3", "\n</parameter>\n", "</function>\n", "</tool_call>"},
			thinkValue: nil,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "create_note",
389
						Arguments: testArgs(map[string]any{"content": "Line 1\nLine 2\nLine 3"}),
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
					},
				},
			},
		},
		{
			name:             "empty thinking block",
			chunks:           []string{"</think>", "\n", "Just content."},
			thinkValue:       &api.ThinkValue{Value: true},
			expectedThinking: "",
			expectedContent:  "Just content.",
		},
		{
			name:            "empty input chunks interspersed",
			chunks:          []string{"Hello", "", " ", "", "world", "", "!"},
			thinkValue:      nil,
			expectedContent: "Hello world!",
		},
		{
			name:             "tool call immediately after think close - no content",
			chunks:           []string{"Analyzing...", "</think>", "\n", "<tool_call>", "\n<function=test>\n</function>\n", "</tool_call>"},
			thinkValue:       &api.ThinkValue{Value: true},
			expectedThinking: "Analyzing...",
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "test",
416
						Arguments: api.NewToolCallFunctionArguments(),
417
418
419
420
421
422
423
424
425
426
427
428
					},
				},
			},
		},
		{
			name:       "tool call with empty parameter value",
			chunks:     []string{"<tool_call>\n<function=test>\n<parameter=name>\n", "\n</parameter>\n</function>\n</tool_call>"},
			thinkValue: nil,
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "test",
429
						Arguments: testArgs(map[string]any{"name": ""}),
430
431
432
433
434
435
436
437
438
439
440
441
442
443
					},
				},
			},
		},
		{
			name:            "partial tool call tag at end - buffered",
			chunks:          []string{"Here's some content", "<tool"},
			thinkValue:      nil,
			expectedContent: "Here's some content",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
444
			p := &Nemotron3NanoParser{}
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
			p.Init(nil, nil, tt.thinkValue)

			var allContent string
			var allThinking string
			var allCalls []api.ToolCall

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

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

			if diff := cmp.Diff(allContent, tt.expectedContent); diff != "" {
				t.Errorf("content mismatch (-got +want):\n%s", diff)
			}
			if diff := cmp.Diff(allThinking, tt.expectedThinking); diff != "" {
				t.Errorf("thinking mismatch (-got +want):\n%s", diff)
			}
476
			if diff := cmp.Diff(allCalls, tt.expectedCalls, argsComparer); diff != "" {
477
478
479
480
481
482
483
484
485
486
487
488
489
490
				t.Errorf("calls mismatch (-got +want):\n%s", diff)
			}
		})
	}
}

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

func TestNemotron3NanoParser_HasThinkingSupport(t *testing.T) {
491
492
493
494
	p := &Nemotron3NanoParser{}
	if !p.HasThinkingSupport() {
		t.Error("expected HasThinkingSupport to return true")
	}
495
496
497
498
}

func TestNemotron3NanoParser_Init(t *testing.T) {
	t.Run("starts in thinking state when enabled", func(t *testing.T) {
499
		p := &Nemotron3NanoParser{}
500
501
502
503
504
505
506
		p.Init(nil, nil, &api.ThinkValue{Value: true})
		if p.state != Nemotron3NanoCollectingThinking {
			t.Errorf("expected state Nemotron3NanoCollectingThinking, got %v", p.state)
		}
	})

	t.Run("starts in content state when thinking disabled", func(t *testing.T) {
507
		p := &Nemotron3NanoParser{}
508
509
510
511
512
513
514
		p.Init(nil, nil, &api.ThinkValue{Value: false})
		if p.state != Nemotron3NanoCollectingContent {
			t.Errorf("expected state Nemotron3NanoCollectingContent, got %v", p.state)
		}
	})

	t.Run("starts in content state when nil thinkValue", func(t *testing.T) {
515
		p := &Nemotron3NanoParser{}
516
517
518
519
520
521
522
		p.Init(nil, nil, nil)
		if p.state != Nemotron3NanoCollectingContent {
			t.Errorf("expected state Nemotron3NanoCollectingContent, got %v", p.state)
		}
	})

	t.Run("starts in content state with assistant prefill", func(t *testing.T) {
523
		p := &Nemotron3NanoParser{}
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
		prefill := &api.Message{Role: "assistant", Content: "Starting..."}
		p.Init(nil, prefill, &api.ThinkValue{Value: true})
		if p.state != Nemotron3NanoCollectingContent {
			t.Errorf("expected state Nemotron3NanoCollectingContent, got %v", p.state)
		}
	})
}

func TestNemotron3NanoParser_WithTools(t *testing.T) {
	tools := []api.Tool{
		{
			Type: "function",
			Function: api.ToolFunction{
				Name: "get_weather",
				Parameters: api.ToolFunctionParameters{
					Type: "object",
540
					Properties: testPropsMap(map[string]api.ToolProperty{
541
						"city": {Type: api.PropertyType{"string"}},
542
					}),
543
544
545
546
547
548
549
550
				},
			},
		},
	}

	p := &Nemotron3NanoParser{}
	returnedTools := p.Init(tools, nil, nil)

551
	if diff := cmp.Diff(returnedTools, tools, toolsComparer); diff != "" {
552
553
554
555
556
557
558
559
560
561
562
563
564
565
		t.Errorf("tools mismatch (-got +want):\n%s", diff)
	}

	// Parse a tool call
	input := "<tool_call>\n<function=get_weather>\n<parameter=city>\nParis\n</parameter>\n</function>\n</tool_call>"
	_, _, calls, err := p.Add(input, true)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	expectedCalls := []api.ToolCall{
		{
			Function: api.ToolCallFunction{
				Name:      "get_weather",
566
				Arguments: testArgs(map[string]any{"city": "Paris"}),
567
568
569
570
			},
		},
	}

571
	if diff := cmp.Diff(calls, expectedCalls, argsComparer); diff != "" {
572
573
574
		t.Errorf("calls mismatch (-got +want):\n%s", diff)
	}
}