olmo3_think_test.go 10.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
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
package parsers

import (
	"strings"
	"testing"

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

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

func TestOlmo3ThinkParser(t *testing.T) {
	tests := []struct {
		name             string
		input            string
		expectedContent  string
		expectedThinking string
		lastMessage      *api.Message
	}{
		{
			name:             "thinking_only",
			input:            "I need to think about this.</think>Here is my response.",
			expectedContent:  "Here is my response.",
			expectedThinking: "I need to think about this.",
		},
		{
			name:             "thinking_with_newlines",
			input:            "Let me think step by step.\n\n1. First point\n2. Second point</think>The answer is 42.",
			expectedContent:  "The answer is 42.",
			expectedThinking: "Let me think step by step.\n\n1. First point\n2. Second point",
		},
		{
			name:             "thinking_then_content",
			input:            "Deep thinking here.</think>Here is my detailed response with multiple sentences. I have thought carefully.",
			expectedContent:  "Here is my detailed response with multiple sentences. I have thought carefully.",
			expectedThinking: "Deep thinking here.",
		},
		{
			name:             "empty_thinking",
			input:            "</think>Just content here.",
			expectedContent:  "Just content here.",
			expectedThinking: "",
		},
		{
			name:            "prefill_skips_thinking",
			input:           "Continuing from previous content.",
			expectedContent: "Continuing from previous content.",
			lastMessage: &api.Message{
				Role:    "assistant",
				Content: "Previous content",
			},
		},
		{
			name:             "thinking_with_whitespace",
			input:            "  Some thinking  </think>  Content here  ",
			expectedContent:  "Content here  ",
			expectedThinking: "  Some thinking",
		},
		{
			name:             "real_model_output_with_newlines",
			input:            "Yes, that should work. Let me go with that response.\n\n</think>\n\nHi! I'm all set and ready to assist. How about you? How are you today? 😊",
			expectedThinking: "Yes, that should work. Let me go with that response.",
			expectedContent:  "Hi! I'm all set and ready to assist. How about you? How are you today? 😊",
		},
		// Edge cases
		{
			name:             "nested_think_tags_in_thinking",
			input:            "I'm thinking <think>nested</think> more thinking</think>Final content.",
			expectedContent:  "more thinking</think>Final content.",
			expectedThinking: "I'm thinking <think>nested",
		},
		{
			name:             "multiple_think_close_tags",
			input:            "First thinking</think>Content</think>More content.",
			expectedContent:  "Content</think>More content.",
			expectedThinking: "First thinking",
		},
	}

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

			content, thinking, toolCalls, err := parser.Add(tt.input, true)
			if err != nil {
				t.Fatalf("Add() error = %v", err)
			}

			if diff := cmp.Diff(tt.expectedContent, content); diff != "" {
				t.Errorf("content mismatch (-want +got):\n%s", diff)
			}

			if diff := cmp.Diff(tt.expectedThinking, thinking); diff != "" {
				t.Errorf("thinking mismatch (-want +got):\n%s", diff)
			}

			// No tool calls expected
			if len(toolCalls) > 0 {
				t.Errorf("expected no tool calls, got %d", len(toolCalls))
			}
		})
	}
}

func TestOlmo3ThinkParser_Streaming(t *testing.T) {
	parser := &Olmo3ThinkParser{}
	parser.Init(nil, nil, nil)

	chunks := []string{
		"I am ",
		"thinking about",
		" this.</think>Here ",
		"is the response.",
	}

	var finalContent, finalThinking strings.Builder

	for i, chunk := range chunks {
		done := i == len(chunks)-1
		content, thinking, _, err := parser.Add(chunk, done)
		if err != nil {
			t.Fatalf("Add() error on chunk %d: %v", i, err)
		}

		finalContent.WriteString(content)
		finalThinking.WriteString(thinking)
	}

	expectedContent := "Here is the response."
	expectedThinking := "I am thinking about this."

	if finalContent.String() != expectedContent {
		t.Errorf("expected content %q, got %q", expectedContent, finalContent.String())
	}

	if finalThinking.String() != expectedThinking {
		t.Errorf("expected thinking %q, got %q", expectedThinking, finalThinking.String())
	}
}

func TestOlmo3ThinkParser_StreamingEdgeCases(t *testing.T) {
	tests := []struct {
		name             string
		chunks           []string
		expectedContent  string
		expectedThinking string
	}{
		{
			name: "thinking_tag_split_across_chunks",
			chunks: []string{
				"This is thinking content",
				"</think>",
				"This is content.",
			},
			expectedContent:  "This is content.",
			expectedThinking: "This is thinking content",
		},
		{
			name: "thinking_tag_split_mid_token",
			chunks: []string{
				"Thinking?</",
				"think>",
				"Content here.",
			},
			expectedContent:  "Content here.",
			expectedThinking: "Thinking?",
		},
		{
			name: "thinking_tag_split_at_angle_bracket",
			chunks: []string{
				"Thinking<",
				"/think>",
				"Content.",
			},
			expectedContent:  "Content.",
			expectedThinking: "Thinking",
		},
	}

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

			var finalContent, finalThinking strings.Builder

			for i, chunk := range tt.chunks {
				done := i == len(tt.chunks)-1
				content, thinking, _, err := parser.Add(chunk, done)
				if err != nil {
					t.Fatalf("Add() error on chunk %d: %v", i, err)
				}

				finalContent.WriteString(content)
				finalThinking.WriteString(thinking)
			}

			if finalContent.String() != tt.expectedContent {
				t.Errorf("expected content %q, got %q", tt.expectedContent, finalContent.String())
			}

			if finalThinking.String() != tt.expectedThinking {
				t.Errorf("expected thinking %q, got %q", tt.expectedThinking, finalThinking.String())
			}
		})
	}
}

// TestOlmo3ThinkParser_ThinkBoundary tests streaming thinking content
// where thinking chunks come in succession before the </think> tag
func TestOlmo3ThinkParser_ThinkBoundary(t *testing.T) {
	tests := []struct {
		name             string
		chunks           []string
		expectedThinking string
		expectedContent  string
	}{
		{
			name: "multiple_thinking_chunks",
			chunks: []string{
				"First part of thinking. ",
				"Second part of thinking. ",
				"Third part.</think>",
				"Content here.",
			},
			expectedThinking: "First part of thinking. Second part of thinking. Third part.",
			expectedContent:  "Content here.",
		},
		{
			name: "thinking_chunks_with_newlines",
			chunks: []string{
				"Step 1: Analyze the problem.\n",
				"Step 2: Consider options.\n",
				"Step 3: Make decision.</think>",
				"Here is my answer.",
			},
			expectedThinking: "Step 1: Analyze the problem.\nStep 2: Consider options.\nStep 3: Make decision.",
			expectedContent:  "Here is my answer.",
		},
		{
			name: "single_char_thinking_chunks",
			chunks: []string{
				"H", "e", "l", "l", "o", "</think>", "World",
			},
			expectedThinking: "Hello",
			expectedContent:  "World",
		},
		{
			name: "thinking_with_special_chars",
			chunks: []string{
				"Let me think... ",
				"Option A: $100 ",
				"Option B: €200</think>",
				"I recommend Option A.",
			},
			expectedThinking: "Let me think... Option A: $100 Option B: €200",
			expectedContent:  "I recommend Option A.",
		},
		{
			name: "long_thinking_multiple_chunks",
			chunks: []string{
				"This is a very long thinking process. ",
				"I need to consider many factors. ",
				"First, let me look at the data. ",
				"The numbers show interesting patterns. ",
				"Based on my analysis, ",
				"I can conclude that...</think>",
				"The answer is 42.",
			},
			expectedThinking: "This is a very long thinking process. I need to consider many factors. First, let me look at the data. The numbers show interesting patterns. Based on my analysis, I can conclude that...",
			expectedContent:  "The answer is 42.",
		},
		{
			name: "thinking_ends_exactly_at_chunk_boundary",
			chunks: []string{
				"Thinking content",
				"</think>",
				"Content",
			},
			expectedThinking: "Thinking content",
			expectedContent:  "Content",
		},
		{
			name: "empty_chunks_between_thinking",
			chunks: []string{
				"Start thinking",
				"",
				" middle ",
				"",
				"end</think>",
				"Content",
			},
			expectedThinking: "Start thinking middle end",
			expectedContent:  "Content",
		},
	}

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

			var finalContent, finalThinking strings.Builder

			for i, chunk := range tt.chunks {
				done := i == len(tt.chunks)-1
				content, thinking, _, err := parser.Add(chunk, done)
				if err != nil {
					t.Fatalf("Add() error on chunk %d: %v", i, err)
				}

				finalContent.WriteString(content)
				finalThinking.WriteString(thinking)
			}

			if finalThinking.String() != tt.expectedThinking {
				t.Errorf("thinking mismatch:\nexpected: %q\ngot:      %q", tt.expectedThinking, finalThinking.String())
			}

			if finalContent.String() != tt.expectedContent {
				t.Errorf("content mismatch:\nexpected: %q\ngot:      %q", tt.expectedContent, finalContent.String())
			}
		})
	}
}

// TestOlmo3ThinkParser_StateTransitions tests that state transitions work correctly
func TestOlmo3ThinkParser_StateTransitions(t *testing.T) {
	t.Run("thinking_to_content", func(t *testing.T) {
		parser := &Olmo3ThinkParser{}
		parser.Init(nil, nil, nil)

		if parser.state != olmo3CollectingThink {
			t.Errorf("initial state should be olmo3CollectingThink, got %v", parser.state)
		}

		parser.Add("thinking</think>content", true)

		if parser.state != olmo3CollectingContent {
			t.Errorf("state after </think> should be olmo3CollectingContent, got %v", parser.state)
		}
	})
}

func TestOlmo3ThinkParser_HasToolSupport(t *testing.T) {
	parser := &Olmo3ThinkParser{}
	if parser.HasToolSupport() {
		t.Error("Olmo3ThinkParser should NOT support tools")
	}
}

func TestOlmo3ThinkParser_HasThinkingSupport(t *testing.T) {
	parser := &Olmo3ThinkParser{}
	if !parser.HasThinkingSupport() {
		t.Error("Olmo3ThinkParser should support thinking")
	}
}

func TestOlmo3ThinkParser_Init(t *testing.T) {
	parser := &Olmo3ThinkParser{}

	tools := []api.Tool{
		{Function: api.ToolFunction{Name: "test_tool"}},
	}

	lastMessage := &api.Message{Role: "assistant", Content: "previous"}

	returnedTools := parser.Init(tools, lastMessage, nil)

	if len(returnedTools) != len(tools) {
		t.Errorf("expected %d tools returned, got %d", len(tools), len(returnedTools))
	}

	// Should be in content collection mode due to prefill
	if parser.state != olmo3CollectingContent {
		t.Errorf("expected state olmo3CollectingContent, got %v", parser.state)
	}
}

func TestOlmo3ThinkParser_InitWithoutPrefill(t *testing.T) {
	parser := &Olmo3ThinkParser{}

	parser.Init(nil, nil, nil)

	// Should be in thinking collection mode (model always thinks first)
	if parser.state != olmo3CollectingThink {
		t.Errorf("expected state olmo3CollectingThink, got %v", parser.state)
	}
}