-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce ChatML prompt format
Models are taugh with specially formatted prompts. Using the same format for inference gives the best results.
- Loading branch information
Showing
7 changed files
with
148 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (C) 2023 Maciej Żok | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package llama | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// supported prompt formats | ||
var promptFormats = map[string]func(Prompt) string{ | ||
"": func(p Prompt) string { | ||
return fmt.Sprintf("%s\n%s", p.System, strings.Join(p.userPrompt, "\n")) | ||
}, | ||
"chatml": func(p Prompt) string { | ||
systemPrompt := fmt.Sprintf("<|im_start|>system\n%s<|im_end|>\n", p.System) | ||
userPrompt := "" | ||
for i := range p.userPrompt { | ||
userPrompt += fmt.Sprintf("<|im_start|>user\n%s<|im_end|>\n", p.userPrompt[i]) | ||
} | ||
if userPrompt == "" { | ||
userPrompt = "<|im_start|>user\n<|im_end|>\n" | ||
} | ||
|
||
return fmt.Sprintf("%s%s<|im_start|>assistant", systemPrompt, userPrompt) | ||
}, | ||
} | ||
|
||
// Prompt represents prompt for the LLM. | ||
type Prompt struct { | ||
Format string | ||
System string | ||
userPrompt []string | ||
} | ||
|
||
// String returns prompt string in format specified by Format. | ||
// If Format is not specified, returns prompt in default format. | ||
func (p *Prompt) String() string { | ||
formatFunc, ok := promptFormats[strings.ToLower(p.Format)] | ||
if !ok { | ||
formatFunc = promptFormats[""] | ||
} | ||
|
||
return formatFunc(*p) | ||
} | ||
|
||
// Add adds user prompt to the prompt. | ||
func (p *Prompt) Add(userPrompt string) { | ||
p.userPrompt = append(p.userPrompt, userPrompt) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (C) 2023 Maciej Żok | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package llama | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestPromptString(t *testing.T) { | ||
testcases := []struct { | ||
prompt Prompt | ||
want string | ||
}{ | ||
{Prompt{Format: "chatml"}, "<|im_start|>system\n<|im_end|>\n<|im_start|>user\n<|im_end|>\n<|im_start|>assistant"}, | ||
{Prompt{Format: "ChatML", System: "You are a helpful assistant."}, "<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<|im_end|>\n<|im_start|>assistant"}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
tc := tc | ||
t.Run(tc.prompt.String(), func(t *testing.T) { | ||
t.Parallel() | ||
got := tc.prompt.String() | ||
if got != tc.want { | ||
t.Fatalf("Prompt.String() = %v, want %v", got, tc.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestPromptAdd(t *testing.T) { | ||
testcases := []struct { | ||
userPrompts []string | ||
want string | ||
}{ | ||
{[]string{}, "<|im_start|>system\n<|im_end|>\n<|im_start|>user\n<|im_end|>\n<|im_start|>assistant"}, | ||
{[]string{"How are you?"}, "<|im_start|>system\n<|im_end|>\n<|im_start|>user\nHow are you?<|im_end|>\n<|im_start|>assistant"}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
tc := tc | ||
t.Run(strings.Join(tc.userPrompts, "; "), func(t *testing.T) { | ||
t.Parallel() | ||
prompt := Prompt{ | ||
Format: "ChatML", | ||
} | ||
for i := range tc.userPrompts { | ||
prompt.Add(tc.userPrompts[i]) | ||
} | ||
got := prompt.String() | ||
if got != tc.want { | ||
t.Fatalf("Prompt.String() = %v, want %v", got, tc.want) | ||
} | ||
}) | ||
} | ||
} |