prompt_test.go 10.9 KB
Newer Older
mashun1's avatar
v1  
mashun1 committed
1
2
3
package server

import (
xuxzh1's avatar
init  
xuxzh1 committed
4
5
	"bytes"
	"context"
xuxzh1's avatar
update  
xuxzh1 committed
6
7
	"image"
	"image/png"
mashun1's avatar
v1  
mashun1 committed
8
9
	"testing"

xuxzh1's avatar
init  
xuxzh1 committed
10
11
	"github.com/google/go-cmp/cmp"

mashun1's avatar
v1  
mashun1 committed
12
	"github.com/ollama/ollama/api"
xuxzh1's avatar
init  
xuxzh1 committed
13
	"github.com/ollama/ollama/template"
mashun1's avatar
v1  
mashun1 committed
14
15
)

xuxzh1's avatar
init  
xuxzh1 committed
16
17
func TestChatPrompt(t *testing.T) {
	type expect struct {
xuxzh1's avatar
update  
xuxzh1 committed
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
		prompt        string
		images        [][]byte
		aspectRatioID int
		error         error
	}

	tmpl, err := template.Parse(`
{{- if .System }}{{ .System }} {{ end }}
{{- if .Prompt }}{{ .Prompt }} {{ end }}
{{- if .Response }}{{ .Response }} {{ end }}`)
	if err != nil {
		t.Fatal(err)
	}
	visionModel := Model{Template: tmpl, ProjectorPaths: []string{"vision"}}
	mllamaModel := Model{Template: tmpl, ProjectorPaths: []string{"vision"}, Config: ConfigV2{ModelFamilies: []string{"mllama"}}}

	createImg := func(width, height int) ([]byte, error) {
		img := image.NewRGBA(image.Rect(0, 0, width, height))
		var buf bytes.Buffer

		if err := png.Encode(&buf, img); err != nil {
			return nil, err
		}

		return buf.Bytes(), nil
	}

	imgBuf, err := createImg(5, 5)
	if err != nil {
		t.Fatal(err)
	}

	imgBuf2, err := createImg(6, 6)
	if err != nil {
		t.Fatal(err)
mashun1's avatar
v1  
mashun1 committed
53
54
	}

xuxzh1's avatar
init  
xuxzh1 committed
55
56
	cases := []struct {
		name  string
xuxzh1's avatar
update  
xuxzh1 committed
57
		model Model
xuxzh1's avatar
init  
xuxzh1 committed
58
59
60
		limit int
		msgs  []api.Message
		expect
mashun1's avatar
v1  
mashun1 committed
61
62
	}{
		{
xuxzh1's avatar
init  
xuxzh1 committed
63
			name:  "messages",
xuxzh1's avatar
update  
xuxzh1 committed
64
			model: visionModel,
xuxzh1's avatar
init  
xuxzh1 committed
65
66
67
68
69
70
71
72
			limit: 64,
			msgs: []api.Message{
				{Role: "user", Content: "You're a test, Harry!"},
				{Role: "assistant", Content: "I-I'm a what?"},
				{Role: "user", Content: "A test. And a thumping good one at that, I'd wager."},
			},
			expect: expect{
				prompt: "You're a test, Harry! I-I'm a what? A test. And a thumping good one at that, I'd wager. ",
mashun1's avatar
v1  
mashun1 committed
73
74
75
			},
		},
		{
xuxzh1's avatar
init  
xuxzh1 committed
76
			name:  "truncate messages",
xuxzh1's avatar
update  
xuxzh1 committed
77
			model: visionModel,
xuxzh1's avatar
init  
xuxzh1 committed
78
79
80
81
82
83
84
85
86
			limit: 1,
			msgs: []api.Message{
				{Role: "user", Content: "You're a test, Harry!"},
				{Role: "assistant", Content: "I-I'm a what?"},
				{Role: "user", Content: "A test. And a thumping good one at that, I'd wager."},
			},
			expect: expect{
				prompt: "A test. And a thumping good one at that, I'd wager. ",
			},
mashun1's avatar
v1  
mashun1 committed
87
88
		},
		{
xuxzh1's avatar
init  
xuxzh1 committed
89
			name:  "truncate messages with image",
xuxzh1's avatar
update  
xuxzh1 committed
90
			model: visionModel,
xuxzh1's avatar
init  
xuxzh1 committed
91
92
93
94
95
96
97
98
99
100
101
102
			limit: 64,
			msgs: []api.Message{
				{Role: "user", Content: "You're a test, Harry!"},
				{Role: "assistant", Content: "I-I'm a what?"},
				{Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{[]byte("something")}},
			},
			expect: expect{
				prompt: "[img-0] A test. And a thumping good one at that, I'd wager. ",
				images: [][]byte{
					[]byte("something"),
				},
			},
mashun1's avatar
v1  
mashun1 committed
103
104
		},
		{
xuxzh1's avatar
init  
xuxzh1 committed
105
			name:  "truncate messages with images",
xuxzh1's avatar
update  
xuxzh1 committed
106
			model: visionModel,
xuxzh1's avatar
init  
xuxzh1 committed
107
108
109
110
111
112
113
114
115
116
117
118
			limit: 64,
			msgs: []api.Message{
				{Role: "user", Content: "You're a test, Harry!", Images: []api.ImageData{[]byte("something")}},
				{Role: "assistant", Content: "I-I'm a what?"},
				{Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{[]byte("somethingelse")}},
			},
			expect: expect{
				prompt: "[img-0] A test. And a thumping good one at that, I'd wager. ",
				images: [][]byte{
					[]byte("somethingelse"),
				},
			},
mashun1's avatar
v1  
mashun1 committed
119
120
		},
		{
xuxzh1's avatar
init  
xuxzh1 committed
121
			name:  "messages with images",
xuxzh1's avatar
update  
xuxzh1 committed
122
			model: visionModel,
xuxzh1's avatar
init  
xuxzh1 committed
123
124
125
126
127
128
129
130
131
132
133
134
135
			limit: 2048,
			msgs: []api.Message{
				{Role: "user", Content: "You're a test, Harry!", Images: []api.ImageData{[]byte("something")}},
				{Role: "assistant", Content: "I-I'm a what?"},
				{Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{[]byte("somethingelse")}},
			},
			expect: expect{
				prompt: "[img-0] You're a test, Harry! I-I'm a what? [img-1] A test. And a thumping good one at that, I'd wager. ",
				images: [][]byte{
					[]byte("something"),
					[]byte("somethingelse"),
				},
			},
mashun1's avatar
v1  
mashun1 committed
136
137
		},
		{
xuxzh1's avatar
init  
xuxzh1 committed
138
			name:  "message with image tag",
xuxzh1's avatar
update  
xuxzh1 committed
139
			model: visionModel,
xuxzh1's avatar
init  
xuxzh1 committed
140
141
142
143
144
145
146
147
148
149
150
151
152
			limit: 2048,
			msgs: []api.Message{
				{Role: "user", Content: "You're a test, Harry! [img]", Images: []api.ImageData{[]byte("something")}},
				{Role: "assistant", Content: "I-I'm a what?"},
				{Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{[]byte("somethingelse")}},
			},
			expect: expect{
				prompt: "You're a test, Harry! [img-0] I-I'm a what? [img-1] A test. And a thumping good one at that, I'd wager. ",
				images: [][]byte{
					[]byte("something"),
					[]byte("somethingelse"),
				},
			},
mashun1's avatar
v1  
mashun1 committed
153
154
		},
		{
xuxzh1's avatar
init  
xuxzh1 committed
155
			name:  "messages with interleaved images",
xuxzh1's avatar
update  
xuxzh1 committed
156
			model: visionModel,
xuxzh1's avatar
init  
xuxzh1 committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
			limit: 2048,
			msgs: []api.Message{
				{Role: "user", Content: "You're a test, Harry!"},
				{Role: "user", Images: []api.ImageData{[]byte("something")}},
				{Role: "user", Images: []api.ImageData{[]byte("somethingelse")}},
				{Role: "assistant", Content: "I-I'm a what?"},
				{Role: "user", Content: "A test. And a thumping good one at that, I'd wager."},
			},
			expect: expect{
				prompt: "You're a test, Harry!\n\n[img-0]\n\n[img-1] I-I'm a what? A test. And a thumping good one at that, I'd wager. ",
				images: [][]byte{
					[]byte("something"),
					[]byte("somethingelse"),
				},
			},
mashun1's avatar
v1  
mashun1 committed
172
173
		},
		{
xuxzh1's avatar
init  
xuxzh1 committed
174
			name:  "truncate message with interleaved images",
xuxzh1's avatar
update  
xuxzh1 committed
175
			model: visionModel,
xuxzh1's avatar
init  
xuxzh1 committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
			limit: 1024,
			msgs: []api.Message{
				{Role: "user", Content: "You're a test, Harry!"},
				{Role: "user", Images: []api.ImageData{[]byte("something")}},
				{Role: "user", Images: []api.ImageData{[]byte("somethingelse")}},
				{Role: "assistant", Content: "I-I'm a what?"},
				{Role: "user", Content: "A test. And a thumping good one at that, I'd wager."},
			},
			expect: expect{
				prompt: "[img-0] I-I'm a what? A test. And a thumping good one at that, I'd wager. ",
				images: [][]byte{
					[]byte("somethingelse"),
				},
			},
mashun1's avatar
v1  
mashun1 committed
190
191
		},
		{
xuxzh1's avatar
init  
xuxzh1 committed
192
			name:  "message with system prompt",
xuxzh1's avatar
update  
xuxzh1 committed
193
			model: visionModel,
xuxzh1's avatar
init  
xuxzh1 committed
194
195
196
197
198
199
200
201
202
203
			limit: 2048,
			msgs: []api.Message{
				{Role: "system", Content: "You are the Test Who Lived."},
				{Role: "user", Content: "You're a test, Harry!"},
				{Role: "assistant", Content: "I-I'm a what?"},
				{Role: "user", Content: "A test. And a thumping good one at that, I'd wager."},
			},
			expect: expect{
				prompt: "You are the Test Who Lived. You're a test, Harry! I-I'm a what? A test. And a thumping good one at that, I'd wager. ",
			},
mashun1's avatar
v1  
mashun1 committed
204
205
		},
		{
xuxzh1's avatar
init  
xuxzh1 committed
206
			name:  "out of order system",
xuxzh1's avatar
update  
xuxzh1 committed
207
			model: visionModel,
xuxzh1's avatar
init  
xuxzh1 committed
208
209
210
211
212
213
214
215
216
			limit: 2048,
			msgs: []api.Message{
				{Role: "user", Content: "You're a test, Harry!"},
				{Role: "assistant", Content: "I-I'm a what?"},
				{Role: "system", Content: "You are the Test Who Lived."},
				{Role: "user", Content: "A test. And a thumping good one at that, I'd wager."},
			},
			expect: expect{
				prompt: "You're a test, Harry! I-I'm a what? You are the Test Who Lived. A test. And a thumping good one at that, I'd wager. ",
mashun1's avatar
v1  
mashun1 committed
217
218
			},
		},
xuxzh1's avatar
update  
xuxzh1 committed
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
		{
			name:  "multiple images same prompt",
			model: visionModel,
			limit: 2048,
			msgs: []api.Message{
				{Role: "user", Content: "Compare these two pictures of hotdogs", Images: []api.ImageData{[]byte("one hotdog"), []byte("two hotdogs")}},
			},
			expect: expect{
				prompt: "[img-0][img-1] Compare these two pictures of hotdogs ",
				images: [][]byte{[]byte("one hotdog"), []byte("two hotdogs")},
			},
		},
		{
			name:  "messages with mllama (no images)",
			model: mllamaModel,
			limit: 2048,
			msgs: []api.Message{
				{Role: "user", Content: "You're a test, Harry!"},
				{Role: "assistant", Content: "I-I'm a what?"},
				{Role: "user", Content: "A test. And a thumping good one at that, I'd wager."},
			},
			expect: expect{
				prompt: "You're a test, Harry! I-I'm a what? A test. And a thumping good one at that, I'd wager. ",
			},
		},
		{
			name:  "messages with mllama single prompt",
			model: mllamaModel,
			limit: 2048,
			msgs: []api.Message{
				{Role: "user", Content: "How many hotdogs are in this image?", Images: []api.ImageData{imgBuf}},
			},
			expect: expect{
				prompt:        "[img-0]<|image|>How many hotdogs are in this image? ",
				images:        [][]byte{imgBuf},
				aspectRatioID: 1,
			},
		},
		{
			name:  "messages with mllama",
			model: mllamaModel,
			limit: 2048,
			msgs: []api.Message{
				{Role: "user", Content: "You're a test, Harry!"},
				{Role: "assistant", Content: "I-I'm a what?"},
				{Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{imgBuf}},
			},
			expect: expect{
				prompt:        "You're a test, Harry! I-I'm a what? [img-0]<|image|>A test. And a thumping good one at that, I'd wager. ",
				images:        [][]byte{imgBuf},
				aspectRatioID: 1,
			},
		},
		{
			name:  "multiple messages with mllama",
			model: mllamaModel,
			limit: 2048,
			msgs: []api.Message{
				{Role: "user", Content: "You're a test, Harry!", Images: []api.ImageData{imgBuf}},
				{Role: "assistant", Content: "I-I'm a what?"},
				{Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{imgBuf2}},
			},
			expect: expect{
				prompt:        "[img-0]<|image|>You're a test, Harry! I-I'm a what? [img-1]<|image|>A test. And a thumping good one at that, I'd wager. ",
				images:        [][]byte{imgBuf, imgBuf2},
				aspectRatioID: 1,
			},
		},
		{
			name:  "earlier image with mllama",
			model: mllamaModel,
			limit: 2048,
			msgs: []api.Message{
				{Role: "user", Content: "How many hotdogs are in this image?", Images: []api.ImageData{imgBuf}},
				{Role: "assistant", Content: "There are four hotdogs."},
				{Role: "user", Content: "Which ones have mustard?"},
			},
			expect: expect{
				prompt:        "[img-0]<|image|>How many hotdogs are in this image? There are four hotdogs. Which ones have mustard? ",
				images:        [][]byte{imgBuf},
				aspectRatioID: 1,
			},
		},
		{
			name:  "too many images with mllama",
			model: mllamaModel,
			limit: 2048,
			msgs: []api.Message{
				{Role: "user", Content: "You're a test, Harry!"},
				{Role: "assistant", Content: "I-I'm a what?"},
				{Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{imgBuf, imgBuf}},
			},
			expect: expect{
				error: errTooManyImages,
			},
		},
mashun1's avatar
v1  
mashun1 committed
315
316
	}

xuxzh1's avatar
init  
xuxzh1 committed
317
318
	for _, tt := range cases {
		t.Run(tt.name, func(t *testing.T) {
xuxzh1's avatar
update  
xuxzh1 committed
319
			model := tt.model
xuxzh1's avatar
init  
xuxzh1 committed
320
321
			opts := api.Options{Runner: api.Runner{NumCtx: tt.limit}}
			prompt, images, err := chatPrompt(context.TODO(), &model, mockRunner{}.Tokenize, &opts, tt.msgs, nil)
xuxzh1's avatar
update  
xuxzh1 committed
322
			if tt.error == nil && err != nil {
xuxzh1's avatar
init  
xuxzh1 committed
323
				t.Fatal(err)
xuxzh1's avatar
update  
xuxzh1 committed
324
325
			} else if tt.error != nil && err != tt.error {
				t.Fatalf("expected err '%q', got '%q'", tt.error, err)
mashun1's avatar
v1  
mashun1 committed
326
327
			}

xuxzh1's avatar
init  
xuxzh1 committed
328
329
330
331
332
333
334
335
336
337
338
339
340
			if diff := cmp.Diff(prompt, tt.prompt); diff != "" {
				t.Errorf("mismatch (-got +want):\n%s", diff)
			}

			if len(images) != len(tt.images) {
				t.Fatalf("expected %d images, got %d", len(tt.images), len(images))
			}

			for i := range images {
				if images[i].ID != i {
					t.Errorf("expected ID %d, got %d", i, images[i].ID)
				}

xuxzh1's avatar
update  
xuxzh1 committed
341
342
343
344
345
346
347
348
				if len(model.Config.ModelFamilies) == 0 {
					if !bytes.Equal(images[i].Data, tt.images[i]) {
						t.Errorf("expected %q, got %q", tt.images[i], images[i].Data)
					}
				} else {
					if images[i].AspectRatioID != tt.aspectRatioID {
						t.Errorf("expected aspect ratio %d, got %d", tt.aspectRatioID, images[i].AspectRatioID)
					}
xuxzh1's avatar
init  
xuxzh1 committed
349
				}
mashun1's avatar
v1  
mashun1 committed
350
351
352
353
			}
		})
	}
}