prompt_test.go 8.18 KB
Newer Older
1
2
3
package server

import (
Michael Yang's avatar
Michael Yang committed
4
	"bytes"
5
6
	"testing"

Michael Yang's avatar
Michael Yang committed
7
	"github.com/google/go-cmp/cmp"
Michael Yang's avatar
lint  
Michael Yang committed
8

9
	"github.com/ollama/ollama/api"
Michael Yang's avatar
Michael Yang committed
10
	"github.com/ollama/ollama/template"
11
12
13
)

func TestChatPrompt(t *testing.T) {
Michael Yang's avatar
Michael Yang committed
14
	type expect struct {
15
16
17
		prompt string
		images [][]byte
		error  error
18
19
20
21
22
23
24
25
26
27
	}

	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"}}
Michael Yang's avatar
Michael Yang committed
28
29

	cases := []struct {
30
31
32
33
34
		name     string
		model    Model
		limit    int
		truncate bool
		msgs     []api.Message
Michael Yang's avatar
Michael Yang committed
35
		expect
36
37
	}{
		{
38
39
40
41
			name:     "messages",
			model:    visionModel,
			limit:    64,
			truncate: true,
Michael Yang's avatar
Michael Yang committed
42
43
44
45
46
47
48
			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. ",
49
50
51
			},
		},
		{
52
53
54
55
			name:     "truncate messages",
			model:    visionModel,
			limit:    1,
			truncate: true,
Michael Yang's avatar
Michael Yang committed
56
57
58
59
60
61
62
63
			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. ",
			},
64
65
		},
		{
66
67
68
69
			name:     "truncate messages with image",
			model:    visionModel,
			limit:    64,
			truncate: true,
Michael Yang's avatar
Michael Yang committed
70
71
72
73
74
75
			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{
76
				prompt: "[img-0]A test. And a thumping good one at that, I'd wager. ",
Michael Yang's avatar
Michael Yang committed
77
78
79
80
				images: [][]byte{
					[]byte("something"),
				},
			},
81
82
		},
		{
83
84
85
86
			name:     "truncate messages with images",
			model:    visionModel,
			limit:    64,
			truncate: true,
Michael Yang's avatar
Michael Yang committed
87
88
89
90
91
92
			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{
93
				prompt: "[img-0]A test. And a thumping good one at that, I'd wager. ",
Michael Yang's avatar
Michael Yang committed
94
95
96
97
				images: [][]byte{
					[]byte("somethingelse"),
				},
			},
98
99
		},
		{
100
101
102
103
			name:     "messages with images",
			model:    visionModel,
			limit:    2048,
			truncate: true,
Michael Yang's avatar
Michael Yang committed
104
105
106
107
108
109
			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{
110
				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. ",
Michael Yang's avatar
Michael Yang committed
111
112
113
114
115
				images: [][]byte{
					[]byte("something"),
					[]byte("somethingelse"),
				},
			},
116
117
		},
		{
118
119
120
121
			name:     "message with image tag",
			model:    visionModel,
			limit:    2048,
			truncate: true,
Michael Yang's avatar
Michael Yang committed
122
123
124
125
126
127
			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{
128
				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. ",
Michael Yang's avatar
Michael Yang committed
129
130
131
132
133
				images: [][]byte{
					[]byte("something"),
					[]byte("somethingelse"),
				},
			},
134
135
		},
		{
136
137
138
139
			name:     "messages with interleaved images",
			model:    visionModel,
			limit:    2048,
			truncate: true,
Michael Yang's avatar
Michael Yang committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
			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"),
				},
			},
154
155
		},
		{
156
157
158
159
			name:     "truncate message with interleaved images",
			model:    visionModel,
			limit:    1024,
			truncate: true,
Michael Yang's avatar
Michael Yang committed
160
161
162
163
164
165
166
167
168
169
170
171
172
			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"),
				},
			},
173
174
		},
		{
175
176
177
178
			name:     "message with system prompt",
			model:    visionModel,
			limit:    2048,
			truncate: true,
Michael Yang's avatar
Michael Yang committed
179
180
181
182
183
184
185
			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{
186
				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. ",
187
188
			},
		},
Michael Yang's avatar
Michael Yang committed
189
		{
190
191
192
193
			name:     "out of order system",
			model:    visionModel,
			limit:    2048,
			truncate: true,
Michael Yang's avatar
Michael Yang committed
194
195
196
197
198
199
200
201
202
203
			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. ",
			},
		},
204
		{
205
206
207
208
			name:     "multiple images same prompt",
			model:    visionModel,
			limit:    2048,
			truncate: true,
209
210
211
212
			msgs: []api.Message{
				{Role: "user", Content: "Compare these two pictures of hotdogs", Images: []api.ImageData{[]byte("one hotdog"), []byte("two hotdogs")}},
			},
			expect: expect{
213
				prompt: "[img-0][img-1]Compare these two pictures of hotdogs ",
214
215
216
				images: [][]byte{[]byte("one hotdog"), []byte("two hotdogs")},
			},
		},
217
218
219
220
221
222
223
224
225
226
227
228
229
230
		{
			name:     "no truncate with limit exceeded",
			model:    visionModel,
			limit:    10,
			truncate: false,
			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. ",
			},
		},
231
232
	}

Michael Yang's avatar
Michael Yang committed
233
234
	for _, tt := range cases {
		t.Run(tt.name, func(t *testing.T) {
235
			model := tt.model
Michael Yang's avatar
Michael Yang committed
236
			opts := api.Options{Runner: api.Runner{NumCtx: tt.limit}}
237
			think := false
238
			prompt, images, err := chatPrompt(t.Context(), &model, mockRunner{}.Tokenize, &opts, tt.msgs, nil, &api.ThinkValue{Value: think}, tt.truncate)
239
			if tt.error == nil && err != nil {
Michael Yang's avatar
Michael Yang committed
240
				t.Fatal(err)
241
242
			} else if tt.error != nil && err != tt.error {
				t.Fatalf("expected err '%q', got '%q'", tt.error, err)
Michael Yang's avatar
Michael Yang committed
243
244
			}

Michael Yang's avatar
Michael Yang committed
245
246
247
248
			if diff := cmp.Diff(prompt, tt.prompt); diff != "" {
				t.Errorf("mismatch (-got +want):\n%s", diff)
			}

Michael Yang's avatar
Michael Yang committed
249
250
251
252
253
254
255
256
257
			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)
				}

258
259
260
261
				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)
					}
Michael Yang's avatar
Michael Yang committed
262
				}
263
264
265
266
			}
		})
	}
}