2024-02-12 23:06:57 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-06-17 17:38:55 +00:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2024-02-12 23:06:57 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2024-03-26 20:04:17 +00:00
|
|
|
"github.com/ollama/ollama/api"
|
2024-06-10 21:54:42 +00:00
|
|
|
"github.com/ollama/ollama/template"
|
2024-02-12 23:06:57 +00:00
|
|
|
)
|
|
|
|
|
2024-06-20 18:00:08 +00:00
|
|
|
func tokenize(_ context.Context, s string) (tokens []int, err error) {
|
2024-06-17 17:38:55 +00:00
|
|
|
for range strings.Fields(s) {
|
|
|
|
tokens = append(tokens, len(tokens))
|
2024-02-12 23:06:57 +00:00
|
|
|
}
|
2024-06-17 17:38:55 +00:00
|
|
|
|
|
|
|
return
|
2024-02-12 23:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestChatPrompt(t *testing.T) {
|
2024-06-17 17:38:55 +00:00
|
|
|
type expect struct {
|
|
|
|
prompt string
|
|
|
|
images [][]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
limit int
|
|
|
|
msgs []api.Message
|
|
|
|
expect
|
2024-02-12 23:06:57 +00:00
|
|
|
}{
|
|
|
|
{
|
2024-06-17 17:38:55 +00:00
|
|
|
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. ",
|
2024-02-12 23:06:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-06-20 18:00:08 +00:00
|
|
|
name: "truncate messages",
|
2024-06-17 17:38:55 +00:00
|
|
|
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. ",
|
|
|
|
},
|
2024-02-12 23:06:57 +00:00
|
|
|
},
|
|
|
|
{
|
2024-06-20 18:00:08 +00:00
|
|
|
name: "truncate messages with image",
|
2024-06-17 17:38:55 +00:00
|
|
|
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"),
|
|
|
|
},
|
|
|
|
},
|
2024-02-12 23:06:57 +00:00
|
|
|
},
|
|
|
|
{
|
2024-06-20 18:00:08 +00:00
|
|
|
name: "truncate messages with images",
|
2024-06-17 17:38:55 +00:00
|
|
|
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"),
|
|
|
|
},
|
|
|
|
},
|
2024-02-12 23:06:57 +00:00
|
|
|
},
|
|
|
|
{
|
2024-06-20 18:00:08 +00:00
|
|
|
name: "messages with images",
|
2024-06-17 17:38:55 +00:00
|
|
|
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"),
|
|
|
|
},
|
|
|
|
},
|
2024-02-12 23:06:57 +00:00
|
|
|
},
|
|
|
|
{
|
2024-06-20 18:00:08 +00:00
|
|
|
name: "message with image tag",
|
2024-06-17 17:38:55 +00:00
|
|
|
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"),
|
|
|
|
},
|
|
|
|
},
|
2024-02-12 23:06:57 +00:00
|
|
|
},
|
|
|
|
{
|
2024-06-20 18:00:08 +00:00
|
|
|
name: "messages with interleaved images",
|
2024-06-17 17:38:55 +00:00
|
|
|
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"),
|
|
|
|
},
|
|
|
|
},
|
2024-02-12 23:06:57 +00:00
|
|
|
},
|
|
|
|
{
|
2024-06-20 18:00:08 +00:00
|
|
|
name: "truncate message with interleaved images",
|
2024-06-17 17:38:55 +00:00
|
|
|
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"),
|
|
|
|
},
|
|
|
|
},
|
2024-02-12 23:06:57 +00:00
|
|
|
},
|
|
|
|
{
|
2024-06-20 18:00:08 +00:00
|
|
|
name: "message with system prompt",
|
2024-06-17 17:38:55 +00:00
|
|
|
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{
|
2024-07-10 18:00:07 +00:00
|
|
|
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. ",
|
2024-02-12 23:06:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-06-17 17:38:55 +00:00
|
|
|
tmpl, err := template.Parse(`
|
|
|
|
{{- if .System }}{{ .System }} {{ end }}
|
|
|
|
{{- if .Prompt }}{{ .Prompt }} {{ end }}
|
|
|
|
{{- if .Response }}{{ .Response }} {{ end }}`)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2024-02-12 23:06:57 +00:00
|
|
|
}
|
|
|
|
|
2024-06-17 17:38:55 +00:00
|
|
|
for _, tt := range cases {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-06-20 18:00:08 +00:00
|
|
|
model := Model{Template: tmpl, ProjectorPaths: []string{"vision"}}
|
|
|
|
opts := api.Options{Runner: api.Runner{NumCtx: tt.limit}}
|
|
|
|
prompt, images, err := chatPrompt(context.TODO(), &model, tokenize, &opts, tt.msgs)
|
2024-06-10 21:54:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-06-17 17:38:55 +00:00
|
|
|
if tt.prompt != prompt {
|
|
|
|
t.Errorf("expected %q, got %q", tt.prompt, prompt)
|
2024-02-12 23:06:57 +00:00
|
|
|
}
|
|
|
|
|
2024-06-17 17:38:55 +00:00
|
|
|
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])
|
|
|
|
}
|
2024-02-12 23:06:57 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|