"object_detection/CONTRIBUTING.md" did not exist on "60c3ed2e34efbe428ae04ab99ccebd795187c12f"
template_test.go 9.24 KB
Newer Older
Michael Yang's avatar
Michael Yang committed
1
2
3
4
5
6
7
8
9
10
package template

import (
	"bufio"
	"bytes"
	"encoding/json"
	"io"
	"os"
	"path/filepath"
	"slices"
Michael Yang's avatar
Michael Yang committed
11
	"strings"
Michael Yang's avatar
Michael Yang committed
12
13
	"testing"

Michael Yang's avatar
Michael Yang committed
14
	"github.com/google/go-cmp/cmp"
Michael Yang's avatar
Michael Yang committed
15
	"github.com/ollama/ollama/api"
Michael Yang's avatar
Michael Yang committed
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
	"github.com/ollama/ollama/llm"
)

func TestNamed(t *testing.T) {
	f, err := os.Open(filepath.Join("testdata", "templates.jsonl"))
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()

	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		var ss map[string]string
		if err := json.Unmarshal(scanner.Bytes(), &ss); err != nil {
			t.Fatal(err)
		}

		for k, v := range ss {
			t.Run(k, func(t *testing.T) {
				kv := llm.KV{"tokenizer.chat_template": v}
				s := kv.ChatTemplate()
				r, err := Named(s)
				if err != nil {
					t.Fatal(err)
				}

				if r.Name != k {
					t.Errorf("expected %q, got %q", k, r.Name)
				}

				var b bytes.Buffer
				if _, err := io.Copy(&b, r.Reader()); err != nil {
					t.Fatal(err)
				}

Michael Yang's avatar
Michael Yang committed
51
				tmpl, err := Parse(b.String())
Michael Yang's avatar
Michael Yang committed
52
53
54
55
56
57
58
59
60
61
62
63
				if err != nil {
					t.Fatal(err)
				}

				if tmpl.Tree.Root.String() == "" {
					t.Errorf("empty %s template", k)
				}
			})
		}
	}
}

Michael Yang's avatar
Michael Yang committed
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
func TestTemplate(t *testing.T) {
	cases := make(map[string][]api.Message)
	for _, mm := range [][]api.Message{
		{
			{Role: "user", Content: "Hello, how are you?"},
		},
		{
			{Role: "user", Content: "Hello, how are you?"},
			{Role: "assistant", Content: "I'm doing great. How can I help you today?"},
			{Role: "user", Content: "I'd like to show off how chat templating works!"},
		},
		{
			{Role: "system", Content: "You are a helpful assistant."},
			{Role: "user", Content: "Hello, how are you?"},
			{Role: "assistant", Content: "I'm doing great. How can I help you today?"},
			{Role: "user", Content: "I'd like to show off how chat templating works!"},
		},
	} {
		var roles []string
		for _, m := range mm {
			roles = append(roles, m.Role)
		}

		cases[strings.Join(roles, "-")] = mm
	}

	matches, err := filepath.Glob("*.gotmpl")
	if err != nil {
		t.Fatal(err)
	}

	for _, match := range matches {
		t.Run(match, func(t *testing.T) {
			bts, err := os.ReadFile(match)
			if err != nil {
				t.Fatal(err)
			}

			tmpl, err := Parse(string(bts))
			if err != nil {
				t.Fatal(err)
			}

			for n, tt := range cases {
108
				var actual bytes.Buffer
Michael Yang's avatar
Michael Yang committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
				t.Run(n, func(t *testing.T) {
					if err := tmpl.Execute(&actual, Values{Messages: tt}); err != nil {
						t.Fatal(err)
					}

					expect, err := os.ReadFile(filepath.Join("testdata", match, n))
					if err != nil {
						t.Fatal(err)
					}

					if diff := cmp.Diff(actual.Bytes(), expect); diff != "" {
						t.Errorf("mismatch (-got +want):\n%s", diff)
					}
				})
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

				t.Run("legacy", func(t *testing.T) {
					var legacy bytes.Buffer
					if err := tmpl.Execute(&legacy, Values{Messages: tt, forceLegacy: true}); err != nil {
						t.Fatal(err)
					}

					legacyBytes := legacy.Bytes()
					if slices.Contains([]string{"chatqa.gotmpl", "openchat.gotmpl", "vicuna.gotmpl"}, match) && legacyBytes[len(legacyBytes)-1] == ' ' {
						t.Log("removing trailing space from legacy output")
						legacyBytes = legacyBytes[:len(legacyBytes)-1]
					} else if slices.Contains([]string{"codellama-70b-instruct.gotmpl", "llama2-chat.gotmpl", "mistral-instruct.gotmpl"}, match) {
						t.Skip("legacy outputs cannot be compared to messages outputs")
					}

					if diff := cmp.Diff(legacyBytes, actual.Bytes()); diff != "" {
						t.Errorf("mismatch (-got +want):\n%s", diff)
					}
				})
Michael Yang's avatar
Michael Yang committed
142
143
144
145
146
			}
		})
	}
}

Michael Yang's avatar
Michael Yang committed
147
148
func TestParse(t *testing.T) {
	cases := []struct {
Michael Yang's avatar
Michael Yang committed
149
150
		template string
		vars     []string
Michael Yang's avatar
Michael Yang committed
151
	}{
Michael Yang's avatar
Michael Yang committed
152
153
		{"{{ .Prompt }}", []string{"prompt", "response"}},
		{"{{ .System }} {{ .Prompt }}", []string{"prompt", "response", "system"}},
Michael Yang's avatar
Michael Yang committed
154
		{"{{ .System }} {{ .Prompt }} {{ .Response }}", []string{"prompt", "response", "system"}},
Michael Yang's avatar
Michael Yang committed
155
		{"{{ with .Tools }}{{ . }}{{ end }} {{ .System }} {{ .Prompt }}", []string{"prompt", "response", "system", "tools"}},
Michael Yang's avatar
Michael Yang committed
156
157
		{"{{ range .Messages }}{{ .Role }} {{ .Content }}{{ end }}", []string{"content", "messages", "role"}},
		{"{{ range .Messages }}{{ if eq .Role \"system\" }}SYSTEM: {{ .Content }}{{ else if eq .Role \"user\" }}USER: {{ .Content }}{{ else if eq .Role \"assistant\" }}ASSISTANT: {{ .Content }}{{ end }}{{ end }}", []string{"content", "messages", "role"}},
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
		{`{{- if .Messages }}
{{- if .System }}<|im_start|>system
{{ .System }}<|im_end|>
{{ end }}
{{- range .Messages }}<|im_start|>{{ .Role }}
{{ .Content }}<|im_end|>
{{ end }}<|im_start|>assistant
{{ else -}}
{{ if .System }}<|im_start|>system
{{ .System }}<|im_end|>
{{ end }}{{ if .Prompt }}<|im_start|>user
{{ .Prompt }}<|im_end|>
{{ end }}<|im_start|>assistant
{{ .Response }}<|im_end|>
{{- end -}}`, []string{"content", "messages", "prompt", "response", "role", "system"}},
Michael Yang's avatar
Michael Yang committed
173
174
175
176
177
178
179
180
181
	}

	for _, tt := range cases {
		t.Run("", func(t *testing.T) {
			tmpl, err := Parse(tt.template)
			if err != nil {
				t.Fatal(err)
			}

182
183
			if diff := cmp.Diff(tmpl.Vars(), tt.vars); diff != "" {
				t.Errorf("mismatch (-got +want):\n%s", diff)
Michael Yang's avatar
Michael Yang committed
184
185
186
187
			}
		})
	}
}
Michael Yang's avatar
Michael Yang committed
188
189

func TestExecuteWithMessages(t *testing.T) {
Michael Yang's avatar
Michael Yang committed
190
191
192
193
	type template struct {
		name     string
		template string
	}
Michael Yang's avatar
Michael Yang committed
194
	cases := []struct {
Michael Yang's avatar
Michael Yang committed
195
196
		name      string
		templates []template
Michael Yang's avatar
Michael Yang committed
197
198
199
200
		values    Values
		expected  string
	}{
		{
Michael Yang's avatar
Michael Yang committed
201
202
203
204
			"mistral",
			[]template{
				{"no response", `[INST] {{ if .System }}{{ .System }}{{ "\n\n" }}{{ end }}{{ .Prompt }}[/INST] `},
				{"response", `[INST] {{ if .System }}{{ .System }}{{ "\n\n" }}{{ end }}{{ .Prompt }}[/INST] {{ .Response }}`},
Michael Yang's avatar
Michael Yang committed
205
				{"messages", `{{- range $index, $_ := .Messages }}
206
{{- if eq .Role "user" }}[INST] {{ if and (eq $index 0) $.System }}{{ $.System }}{{ "\n\n" }}
Michael Yang's avatar
Michael Yang committed
207
208
{{- end }}{{ .Content }}[/INST] {{ else if eq .Role "assistant" }}{{ .Content }}
{{- end }}
Michael Yang's avatar
Michael Yang committed
209
{{- end }}`},
Michael Yang's avatar
Michael Yang committed
210
211
212
213
214
			},
			Values{
				Messages: []api.Message{
					{Role: "user", Content: "Hello friend!"},
					{Role: "assistant", Content: "Hello human!"},
Michael Yang's avatar
Michael Yang committed
215
					{Role: "user", Content: "What is your name?"},
Michael Yang's avatar
Michael Yang committed
216
217
				},
			},
Michael Yang's avatar
Michael Yang committed
218
			`[INST] Hello friend![/INST] Hello human![INST] What is your name?[/INST] `,
Michael Yang's avatar
Michael Yang committed
219
220
		},
		{
Michael Yang's avatar
Michael Yang committed
221
222
223
224
225
			"mistral system",
			[]template{
				{"no response", `[INST] {{ if .System }}{{ .System }}{{ "\n\n" }}{{ end }}{{ .Prompt }}[/INST] `},
				{"response", `[INST] {{ if .System }}{{ .System }}{{ "\n\n" }}{{ end }}{{ .Prompt }}[/INST] {{ .Response }}`},
				{"messages", `
Michael Yang's avatar
Michael Yang committed
226
{{- range $index, $_ := .Messages }}
227
{{- if eq .Role "user" }}[INST] {{ if and (eq $index 0) $.System }}{{ $.System }}{{ "\n\n" }}
Michael Yang's avatar
Michael Yang committed
228
229
{{- end }}{{ .Content }}[/INST] {{ else if eq .Role "assistant" }}{{ .Content }}
{{- end }}
Michael Yang's avatar
Michael Yang committed
230
{{- end }}`},
Michael Yang's avatar
Michael Yang committed
231
232
233
234
235
236
			},
			Values{
				Messages: []api.Message{
					{Role: "system", Content: "You are a helpful assistant!"},
					{Role: "user", Content: "Hello friend!"},
					{Role: "assistant", Content: "Hello human!"},
Michael Yang's avatar
Michael Yang committed
237
					{Role: "user", Content: "What is your name?"},
Michael Yang's avatar
Michael Yang committed
238
239
				},
			},
240
			`[INST] You are a helpful assistant!
Michael Yang's avatar
Michael Yang committed
241

242
Hello friend![/INST] Hello human![INST] What is your name?[/INST] `,
Michael Yang's avatar
Michael Yang committed
243
244
		},
		{
Michael Yang's avatar
Michael Yang committed
245
246
247
248
			"chatml",
			[]template{
				// this does not have a "no response" test because it's impossible to render the same output
				{"response", `{{ if .System }}<|im_start|>system
Michael Yang's avatar
Michael Yang committed
249
250
251
252
253
{{ .System }}<|im_end|>
{{ end }}{{ if .Prompt }}<|im_start|>user
{{ .Prompt }}<|im_end|>
{{ end }}<|im_start|>assistant
{{ .Response }}<|im_end|>
Michael Yang's avatar
Michael Yang committed
254
255
`},
				{"messages", `
Michael Yang's avatar
Michael Yang committed
256
{{- range $index, $_ := .Messages }}
257
{{- if and (eq .Role "user") (eq $index 0) $.System }}<|im_start|>system
Michael Yang's avatar
Michael Yang committed
258
{{ $.System }}<|im_end|>{{ "\n" }}
Michael Yang's avatar
Michael Yang committed
259
{{- end }}<|im_start|>{{ .Role }}
Michael Yang's avatar
Michael Yang committed
260
{{ .Content }}<|im_end|>{{ "\n" }}
Michael Yang's avatar
Michael Yang committed
261
{{- end }}<|im_start|>assistant
Michael Yang's avatar
Michael Yang committed
262
`},
Michael Yang's avatar
Michael Yang committed
263
264
265
266
267
268
			},
			Values{
				Messages: []api.Message{
					{Role: "system", Content: "You are a helpful assistant!"},
					{Role: "user", Content: "Hello friend!"},
					{Role: "assistant", Content: "Hello human!"},
Michael Yang's avatar
Michael Yang committed
269
					{Role: "user", Content: "What is your name?"},
Michael Yang's avatar
Michael Yang committed
270
271
				},
			},
272
273
274
			`<|im_start|>system
You are a helpful assistant!<|im_end|>
<|im_start|>user
Michael Yang's avatar
Michael Yang committed
275
276
277
278
Hello friend!<|im_end|>
<|im_start|>assistant
Hello human!<|im_end|>
<|im_start|>user
Michael Yang's avatar
Michael Yang committed
279
What is your name?<|im_end|>
Michael Yang's avatar
Michael Yang committed
280
281
282
283
<|im_start|>assistant
`,
		},
		{
Michael Yang's avatar
Michael Yang committed
284
285
286
287
			"moondream",
			[]template{
				// this does not have a "no response" test because it's impossible to render the same output
				{"response", `{{ if .Prompt }}Question: {{ .Prompt }}
Michael Yang's avatar
Michael Yang committed
288
289
290

{{ end }}Answer: {{ .Response }}

Michael Yang's avatar
Michael Yang committed
291
292
`},
				{"messages", `
Michael Yang's avatar
Michael Yang committed
293
{{- range .Messages }}
Michael Yang's avatar
Michael Yang committed
294
295
{{- if eq .Role "user" }}Question: {{ .Content }}{{ "\n\n" }}
{{- else if eq .Role "assistant" }}Answer: {{ .Content }}{{ "\n\n" }}
Michael Yang's avatar
Michael Yang committed
296
{{- end }}
Michael Yang's avatar
Michael Yang committed
297
{{- end }}Answer: `},
Michael Yang's avatar
Michael Yang committed
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
			},
			Values{
				Messages: []api.Message{
					{Role: "user", Content: "What's in this image?", Images: []api.ImageData{[]byte("")}},
					{Role: "assistant", Content: "It's a hot dog."},
					{Role: "user", Content: "What's in _this_ image?"},
					{Role: "user", Images: []api.ImageData{[]byte("")}},
					{Role: "user", Content: "Is it a hot dog?"},
				},
			},
			`Question: [img-0] What's in this image?

Answer: It's a hot dog.

Question: What's in _this_ image?

[img-1]

Is it a hot dog?

Answer: `,
		},
	}

	for _, tt := range cases {
Michael Yang's avatar
Michael Yang committed
323
324
325
326
		t.Run(tt.name, func(t *testing.T) {
			for _, ttt := range tt.templates {
				t.Run(ttt.name, func(t *testing.T) {
					tmpl, err := Parse(ttt.template)
Michael Yang's avatar
Michael Yang committed
327
328
329
330
331
332
333
334
335
					if err != nil {
						t.Fatal(err)
					}

					var b bytes.Buffer
					if err := tmpl.Execute(&b, tt.values); err != nil {
						t.Fatal(err)
					}

336
337
					if diff := cmp.Diff(b.String(), tt.expected); diff != "" {
						t.Errorf("mismatch (-got +want):\n%s", diff)
Michael Yang's avatar
Michael Yang committed
338
339
340
341
342
343
					}
				})
			}
		})
	}
}