prompt_test.go 6.48 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"
mashun1's avatar
v1  
mashun1 committed
6
7
	"testing"

xuxzh1's avatar
init  
xuxzh1 committed
8
9
	"github.com/google/go-cmp/cmp"

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

xuxzh1's avatar
init  
xuxzh1 committed
14
15
16
17
func TestChatPrompt(t *testing.T) {
	type expect struct {
		prompt string
		images [][]byte
mashun1's avatar
v1  
mashun1 committed
18
19
	}

xuxzh1's avatar
init  
xuxzh1 committed
20
21
22
23
24
	cases := []struct {
		name  string
		limit int
		msgs  []api.Message
		expect
mashun1's avatar
v1  
mashun1 committed
25
26
	}{
		{
xuxzh1's avatar
init  
xuxzh1 committed
27
28
29
30
31
32
33
34
35
			name:  "messages",
			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
36
37
38
			},
		},
		{
xuxzh1's avatar
init  
xuxzh1 committed
39
40
41
42
43
44
45
46
47
48
			name:  "truncate messages",
			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
49
50
		},
		{
xuxzh1's avatar
init  
xuxzh1 committed
51
52
53
54
55
56
57
58
59
60
61
62
63
			name:  "truncate messages with image",
			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
64
65
		},
		{
xuxzh1's avatar
init  
xuxzh1 committed
66
67
68
69
70
71
72
73
74
75
76
77
78
			name:  "truncate messages with images",
			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
79
80
		},
		{
xuxzh1's avatar
init  
xuxzh1 committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
			name:  "messages with images",
			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
95
96
		},
		{
xuxzh1's avatar
init  
xuxzh1 committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
			name:  "message with image tag",
			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
111
112
		},
		{
xuxzh1's avatar
init  
xuxzh1 committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
			name:  "messages with interleaved images",
			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
129
130
		},
		{
xuxzh1's avatar
init  
xuxzh1 committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
			name:  "truncate message with interleaved images",
			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
146
147
		},
		{
xuxzh1's avatar
init  
xuxzh1 committed
148
149
150
151
152
153
154
155
156
157
158
			name:  "message with system prompt",
			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
159
160
		},
		{
xuxzh1's avatar
init  
xuxzh1 committed
161
162
163
164
165
166
167
168
169
170
			name:  "out of order system",
			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
171
172
173
174
			},
		},
	}

xuxzh1's avatar
init  
xuxzh1 committed
175
176
177
178
179
180
	tmpl, err := template.Parse(`
{{- if .System }}{{ .System }} {{ end }}
{{- if .Prompt }}{{ .Prompt }} {{ end }}
{{- if .Response }}{{ .Response }} {{ end }}`)
	if err != nil {
		t.Fatal(err)
mashun1's avatar
v1  
mashun1 committed
181
182
	}

xuxzh1's avatar
init  
xuxzh1 committed
183
184
185
186
187
	for _, tt := range cases {
		t.Run(tt.name, func(t *testing.T) {
			model := Model{Template: tmpl, ProjectorPaths: []string{"vision"}}
			opts := api.Options{Runner: api.Runner{NumCtx: tt.limit}}
			prompt, images, err := chatPrompt(context.TODO(), &model, mockRunner{}.Tokenize, &opts, tt.msgs, nil)
mashun1's avatar
v1  
mashun1 committed
188
			if err != nil {
xuxzh1's avatar
init  
xuxzh1 committed
189
				t.Fatal(err)
mashun1's avatar
v1  
mashun1 committed
190
191
			}

xuxzh1's avatar
init  
xuxzh1 committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
			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)
				}

				if !bytes.Equal(images[i].Data, tt.images[i]) {
					t.Errorf("expected %q, got %q", tt.images[i], images[i])
				}
mashun1's avatar
v1  
mashun1 committed
208
209
210
211
			}
		})
	}
}