Commit 900f64e6 authored by Jesse Gross's avatar Jesse Gross Committed by Jesse Gross
Browse files

prompt: Don't trim whitespace from prompts

New lines can be an important part of a user's prompt and trimming
it can alter the results. We previously only trimmed prompts with
images but refactoring brought this behavior to all prompts, where
it became more noticable.

The /generate endpoint adds less whitespace and therefore doesn't
need to trim it out - this brings the same behavior to /chat.

Thanks to @gabe-l-hart for spotting the issue!

Fixes #7795
parent da09488f
...@@ -114,7 +114,6 @@ func chatPrompt(ctx context.Context, m *Model, tokenize tokenizeFunc, opts *api. ...@@ -114,7 +114,6 @@ func chatPrompt(ctx context.Context, m *Model, tokenize tokenizeFunc, opts *api.
ID: len(images), ID: len(images),
Data: i, Data: i,
} }
imgPrompt = " "
} }
imgTag := fmt.Sprintf("[img-%d]", imgData.ID) imgTag := fmt.Sprintf("[img-%d]", imgData.ID)
...@@ -126,7 +125,7 @@ func chatPrompt(ctx context.Context, m *Model, tokenize tokenizeFunc, opts *api. ...@@ -126,7 +125,7 @@ func chatPrompt(ctx context.Context, m *Model, tokenize tokenizeFunc, opts *api.
images = append(images, imgData) images = append(images, imgData)
} }
msgs[currMsgIdx+cnt].Content = strings.TrimSpace(prefix + imgPrompt + prompt) msgs[currMsgIdx+cnt].Content = prefix + imgPrompt + prompt
} }
// truncate any messages that do not fit into the context window // truncate any messages that do not fit into the context window
......
...@@ -95,7 +95,7 @@ func TestChatPrompt(t *testing.T) { ...@@ -95,7 +95,7 @@ func TestChatPrompt(t *testing.T) {
{Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{[]byte("something")}}, {Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{[]byte("something")}},
}, },
expect: expect{ expect: expect{
prompt: "[img-0] A test. And a thumping good one at that, I'd wager. ", prompt: "[img-0]A test. And a thumping good one at that, I'd wager. ",
images: [][]byte{ images: [][]byte{
[]byte("something"), []byte("something"),
}, },
...@@ -111,7 +111,7 @@ func TestChatPrompt(t *testing.T) { ...@@ -111,7 +111,7 @@ func TestChatPrompt(t *testing.T) {
{Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{[]byte("somethingelse")}}, {Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{[]byte("somethingelse")}},
}, },
expect: expect{ expect: expect{
prompt: "[img-0] A test. And a thumping good one at that, I'd wager. ", prompt: "[img-0]A test. And a thumping good one at that, I'd wager. ",
images: [][]byte{ images: [][]byte{
[]byte("somethingelse"), []byte("somethingelse"),
}, },
...@@ -127,7 +127,7 @@ func TestChatPrompt(t *testing.T) { ...@@ -127,7 +127,7 @@ func TestChatPrompt(t *testing.T) {
{Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{[]byte("somethingelse")}}, {Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{[]byte("somethingelse")}},
}, },
expect: expect{ 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. ", 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{ images: [][]byte{
[]byte("something"), []byte("something"),
[]byte("somethingelse"), []byte("somethingelse"),
...@@ -144,7 +144,7 @@ func TestChatPrompt(t *testing.T) { ...@@ -144,7 +144,7 @@ func TestChatPrompt(t *testing.T) {
{Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{[]byte("somethingelse")}}, {Role: "user", Content: "A test. And a thumping good one at that, I'd wager.", Images: []api.ImageData{[]byte("somethingelse")}},
}, },
expect: expect{ 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. ", 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{ images: [][]byte{
[]byte("something"), []byte("something"),
[]byte("somethingelse"), []byte("somethingelse"),
...@@ -224,7 +224,7 @@ func TestChatPrompt(t *testing.T) { ...@@ -224,7 +224,7 @@ func TestChatPrompt(t *testing.T) {
{Role: "user", Content: "Compare these two pictures of hotdogs", Images: []api.ImageData{[]byte("one hotdog"), []byte("two hotdogs")}}, {Role: "user", Content: "Compare these two pictures of hotdogs", Images: []api.ImageData{[]byte("one hotdog"), []byte("two hotdogs")}},
}, },
expect: expect{ expect: expect{
prompt: "[img-0][img-1] Compare these two pictures of hotdogs ", prompt: "[img-0][img-1]Compare these two pictures of hotdogs ",
images: [][]byte{[]byte("one hotdog"), []byte("two hotdogs")}, images: [][]byte{[]byte("one hotdog"), []byte("two hotdogs")},
}, },
}, },
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment