"llm/vscode:/vscode.git/clone" did not exist on "35934b2e05cd598a6de0a1ed1ef62c11fb078f36"
prompt_test.go 6.72 KB
Newer Older
1
2
3
package server

import (
Michael Yang's avatar
Michael Yang committed
4
5
	"bytes"
	"context"
6
7
8
	"strings"
	"testing"

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

Michael Yang's avatar
Michael Yang committed
14
func tokenize(_ context.Context, s string) (tokens []int, err error) {
Michael Yang's avatar
Michael Yang committed
15
16
	for range strings.Fields(s) {
		tokens = append(tokens, len(tokens))
17
	}
Michael Yang's avatar
Michael Yang committed
18
19

	return
20
21
22
}

func TestChatPrompt(t *testing.T) {
Michael Yang's avatar
Michael Yang committed
23
24
25
26
27
28
29
30
31
32
	type expect struct {
		prompt string
		images [][]byte
	}

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

Michael Yang's avatar
Michael Yang committed
183
184
185
186
187
188
	tmpl, err := template.Parse(`
{{- if .System }}{{ .System }} {{ end }}
{{- if .Prompt }}{{ .Prompt }} {{ end }}
{{- if .Response }}{{ .Response }} {{ end }}`)
	if err != nil {
		t.Fatal(err)
189
190
	}

Michael Yang's avatar
Michael Yang committed
191
192
	for _, tt := range cases {
		t.Run(tt.name, func(t *testing.T) {
Michael Yang's avatar
Michael Yang committed
193
194
			model := Model{Template: tmpl, ProjectorPaths: []string{"vision"}}
			opts := api.Options{Runner: api.Runner{NumCtx: tt.limit}}
Michael Yang's avatar
tools  
Michael Yang committed
195
			prompt, images, err := chatPrompt(context.TODO(), &model, tokenize, &opts, tt.msgs, nil)
Michael Yang's avatar
Michael Yang committed
196
197
198
199
			if err != nil {
				t.Fatal(err)
			}

Michael Yang's avatar
Michael Yang committed
200
201
			if tt.prompt != prompt {
				t.Errorf("expected %q, got %q", tt.prompt, prompt)
202
203
			}

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

Michael Yang's avatar
Michael Yang committed
208
209
210
211
212
213
214
215
216
217
218
219
			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])
				}
220
221
222
223
			}
		})
	}
}