prompt_test.go 7.38 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
30

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

Michael Yang's avatar
Michael Yang committed
207
208
	for _, tt := range cases {
		t.Run(tt.name, func(t *testing.T) {
209
			model := tt.model
Michael Yang's avatar
Michael Yang committed
210
			opts := api.Options{Runner: api.Runner{NumCtx: tt.limit}}
211
			think := false
Devon Rifkin's avatar
Devon Rifkin committed
212
			prompt, images, err := chatPrompt(t.Context(), &model, mockRunner{}.Tokenize, &opts, tt.msgs, nil, &api.ThinkValue{Value: think})
213
			if tt.error == nil && err != nil {
Michael Yang's avatar
Michael Yang committed
214
				t.Fatal(err)
215
216
			} 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
217
218
			}

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

Michael Yang's avatar
Michael Yang committed
223
224
225
226
227
228
229
230
231
			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)
				}

232
233
234
235
				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
236
				}
237
238
239
240
			}
		})
	}
}