diff --git a/go.mod b/go.mod index 6807b9b4..2e0c6614 100644 --- a/go.mod +++ b/go.mod @@ -18,6 +18,7 @@ require ( require ( github.com/agnivade/levenshtein v1.1.1 github.com/d4l3k/go-bfloat16 v0.0.0-20211005043715-690c3bdd05f1 + github.com/google/go-cmp v0.6.0 github.com/mattn/go-runewidth v0.0.14 github.com/nlpodyssey/gopickle v0.3.0 github.com/pdevine/tensor v0.0.0-20240510204454-f88f4562727c @@ -71,7 +72,7 @@ require ( golang.org/x/net v0.25.0 // indirect golang.org/x/sys v0.20.0 golang.org/x/term v0.20.0 - golang.org/x/text v0.15.0 // indirect + golang.org/x/text v0.15.0 google.golang.org/protobuf v1.34.1 gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/server/routes_create_test.go b/server/routes_create_test.go index 34061282..269a0ba1 100644 --- a/server/routes_create_test.go +++ b/server/routes_create_test.go @@ -545,9 +545,9 @@ func TestCreateDetectTemplate(t *testing.T) { } checkFileExists(t, filepath.Join(p, "blobs", "*"), []string{ - filepath.Join(p, "blobs", "sha256-2f8e594e6f34b1b4d36a246628eeb3365ce442303d656f1fcc69e821722acea0"), - filepath.Join(p, "blobs", "sha256-542b217f179c7825eeb5bca3c77d2b75ed05bafbd3451d9188891a60a85337c6"), filepath.Join(p, "blobs", "sha256-553c4a3f747b3d22a4946875f1cc8ed011c2930d83f864a0c7265f9ec0a20413"), + filepath.Join(p, "blobs", "sha256-9512c372dfc7d84d6065b8dd2b601aeed8cc1a78e7a7aa784a42fff37f5524b7"), + filepath.Join(p, "blobs", "sha256-b8b78cb8c6eefd14c06f1af042e6161255bf87bbf2dd14fce57cdac893db8139"), }) }) diff --git a/template/alfred.gotmpl b/template/alfred.gotmpl index cecb9d2c..44284f04 100644 --- a/template/alfred.gotmpl +++ b/template/alfred.gotmpl @@ -1 +1,8 @@ -{{ if .System }}{{ .System }}{{ end }}{{ if .Prompt }}{{ .Prompt }}{{ end }}{{ .Response }} \ No newline at end of file +{{- if .Messages }} +{{- if .System }}{{ .System }} +{{- end }} +{{- range .Messages }}{{ .Content }} +{{- end }} +{{- else }} +{{ if .System }}{{ .System }}{{ end }}{{ if .Prompt }}{{ .Prompt }}{{ end }}{{ .Response }} +{{- end }} \ No newline at end of file diff --git a/template/alpaca.gotmpl b/template/alpaca.gotmpl index 440d0662..c1f69dc9 100644 --- a/template/alpaca.gotmpl +++ b/template/alpaca.gotmpl @@ -1,7 +1,19 @@ +{{- if .Messages }} +{{- if .System }}{{ .System }} +{{- end }} +{{- range .Messages }} +{{- if eq .Role "user" }}### Instruction: +{{- else if eq .Role "assistant" }}### Response: +{{- end }} +{{ .Content }} + +{{ end }}### Response: +{{ else }} {{ if .System }}{{ .System }} {{ end }}{{ if .Prompt }}### Instruction: {{ .Prompt }} {{ end }}### Response: -{{ .Response }} \ No newline at end of file +{{ .Response }} +{{- end }} \ No newline at end of file diff --git a/template/chatml.gotmpl b/template/chatml.gotmpl index dcf17285..d945547c 100644 --- a/template/chatml.gotmpl +++ b/template/chatml.gotmpl @@ -1,6 +1,15 @@ +{{- if .Messages }} +{{- if .System }}<|im_start|>system +{{ .System }}<|im_end|> +{{ end }} +{{- range .Messages }}<|im_start|>{{ .Role }} +{{ .Content }}<|im_end|> +{{ end }}<|im_start|>assistant +{{ else }} {{ if .System }}<|im_start|>system {{ .System }}<|im_end|> {{ end }}{{ if .Prompt }}<|im_start|>user {{ .Prompt }}<|im_end|> {{ end }}<|im_start|>assistant -{{ .Response }}<|im_end|> \ No newline at end of file +{{ .Response }}<|im_end|> +{{- end }} \ No newline at end of file diff --git a/template/chatqa.gotmpl b/template/chatqa.gotmpl index 1ede6227..7022c479 100644 --- a/template/chatqa.gotmpl +++ b/template/chatqa.gotmpl @@ -1,5 +1,17 @@ +{{- if .Messages }} +{{- if .System }}System: {{ .System }} + +{{ end }} +{{- range .Messages }} +{{- if eq .Role "user" }}User: +{{- else if eq .Role "assistant" }}Assistant: +{{- end }} {{ .Content }} + +{{ end }}Assistant: +{{- else }} {{ if .System }}System: {{ .System }} {{ end }}{{ if .Prompt }}User: {{ .Prompt }} -{{ end }}Assistant: <|begin_of_text|>{{ .Response }} \ No newline at end of file +{{ end }}Assistant: <|begin_of_text|>{{ .Response }} +{{- end }} \ No newline at end of file diff --git a/template/codellama-70b-instruct.gotmpl b/template/codellama-70b-instruct.gotmpl index 3196bd6f..392d839e 100644 --- a/template/codellama-70b-instruct.gotmpl +++ b/template/codellama-70b-instruct.gotmpl @@ -1,3 +1,13 @@ +{{- if .Messages }} +{{- if .System }}Source: system + + {{ .System }} {{ end }} +{{- range .Messages }}Source: {{ .Role }} + + {{ .Content }} {{ end }}Source: assistant +Destination: user + +{{ else }} {{ if .System }} Source: system {{ .System }} {{ end }} Source: user @@ -5,4 +15,5 @@ {{ .Prompt }} Source: assistant Destination: user - {{ .Response }} \ No newline at end of file + {{ .Response }} +{{- end }} \ No newline at end of file diff --git a/template/falcon-instruct.gotmpl b/template/falcon-instruct.gotmpl index 2309a1c5..99d67f93 100644 --- a/template/falcon-instruct.gotmpl +++ b/template/falcon-instruct.gotmpl @@ -1,3 +1,13 @@ +{{- if .Messages }} +{{- if .System }}System: {{ .System }} +{{ end }} +{{- range .Messages }} +{{- if eq .Role "user" }}User: +{{ else if eq .Role "assistant" }}Falcon: +{{ end }}{{ .Content }} +{{ end }}Falcon: +{{ else }} {{ if .System }}{{ .System }} {{ end }}{{ if .Prompt }}User: {{ .Prompt }} -{{ end }}Assistant: {{ .Response }} \ No newline at end of file +{{ end }}Assistant: {{ .Response }} +{{- end }} \ No newline at end of file diff --git a/template/gemma-instruct.gotmpl b/template/gemma-instruct.gotmpl index 91b9883a..870a8f2e 100644 --- a/template/gemma-instruct.gotmpl +++ b/template/gemma-instruct.gotmpl @@ -1,4 +1,16 @@ +{{- if .Messages }} +{{- range $index, $_ := .Messages }} +{{- if eq .Role "user" }}user +{{- if and $.System (eq $index 0) }} +{{ $.System }} +{{- end }} +{{- else if eq .Role "assistant" }}model +{{- end }} +{{ .Content }} +{{ end }}model +{{ else }} user {{ if .System }}{{ .System }} {{ end }}{{ .Prompt }} model -{{ .Response }} \ No newline at end of file +{{ .Response }} +{{- end }} \ No newline at end of file diff --git a/template/granite-instruct.gotmpl b/template/granite-instruct.gotmpl index 2ede647f..327ff3ee 100644 --- a/template/granite-instruct.gotmpl +++ b/template/granite-instruct.gotmpl @@ -1,3 +1,16 @@ +{{- if .Messages }} +{{- if .System }}System: +{{ .System }} + +{{ end }} +{{- range .Messages }} +{{- if eq .Role "user" }}Question: +{{- else if eq .Role "assistant" }}Answer: +{{- end }} +{{ .Content }} + +{{ end }}Answer: +{{ else }} {{ if .System }} System: {{ .System }} @@ -6,4 +19,5 @@ System: {{ .Prompt }} {{ end }}Answer: -{{ .Response }} \ No newline at end of file +{{ .Response }} +{{- end }} \ No newline at end of file diff --git a/template/llama2-chat.gotmpl b/template/llama2-chat.gotmpl index a739f690..6327d581 100644 --- a/template/llama2-chat.gotmpl +++ b/template/llama2-chat.gotmpl @@ -1,3 +1,16 @@ +{{- if .Messages }} +{{- range $index, $_ := .Messages }} +{{- if eq .Role "user" }}[INST] {{ if eq $index 0 }}<> +{{- if $.System }} +{{ $.System }} +{{ end }}<> + +{{ end }}{{ .Content }} +{{- else }} [/INST] {{ .Content }} +{{- end }} +{{- end }} [/INST] +{{- else }} [INST] <>{{ .System }}<> -{{ .Prompt }} [/INST] {{ .Response }} \ No newline at end of file +{{ .Prompt }} [/INST] {{ .Response }} +{{- end }} \ No newline at end of file diff --git a/template/llama3-instruct.gotmpl b/template/llama3-instruct.gotmpl index 36d0218b..9c81a953 100644 --- a/template/llama3-instruct.gotmpl +++ b/template/llama3-instruct.gotmpl @@ -1,7 +1,19 @@ +{{- if .Messages }} +{{- if .System }}<|start_header_id|>system<|end_header_id|> + +{{ .System }}<|eot_id|> +{{- end }} +{{- range .Messages }}<|start_header_id|>{{ .Role }}<|end_header_id|> + +{{ .Content }}<|eot_id|> +{{- end }}<|start_header_id|>assistant<|end_header_id|> + +{{ else }} {{ if .System }}<|start_header_id|>system<|end_header_id|> {{ .System }}<|eot_id|>{{ end }}{{ if .Prompt }}<|start_header_id|>user<|end_header_id|> {{ .Prompt }}<|eot_id|>{{ end }}<|start_header_id|>assistant<|end_header_id|> -{{ .Response }}<|eot_id|> \ No newline at end of file +{{ .Response }}<|eot_id|> +{{- end }} \ No newline at end of file diff --git a/template/magicoder.gotmpl b/template/magicoder.gotmpl index 306972ec..73a58127 100644 --- a/template/magicoder.gotmpl +++ b/template/magicoder.gotmpl @@ -1,7 +1,20 @@ +{{- if .Messages }} +{{- if .System }}{{ .System }} + +{{ end }} +{{- range .Messages }} +{{- if eq .Role "user" }}@@ Instruction +{{- else if eq .Role "assistant" }}@@ Response +{{- end }} +{{ .Content }} + +{{ end }}@@ Response +{{ else }} {{ if .System }}{{ .System }} {{ end }}{{ if .Prompt }}@@ Instruction {{ .Prompt }} {{ end }}@@ Response -{{ .Response }} \ No newline at end of file +{{ .Response }} +{{- end }} \ No newline at end of file diff --git a/template/mistral-instruct.gotmpl b/template/mistral-instruct.gotmpl index dcf17285..eb3d5ced 100644 --- a/template/mistral-instruct.gotmpl +++ b/template/mistral-instruct.gotmpl @@ -1,6 +1,9 @@ -{{ if .System }}<|im_start|>system -{{ .System }}<|im_end|> -{{ end }}{{ if .Prompt }}<|im_start|>user -{{ .Prompt }}<|im_end|> -{{ end }}<|im_start|>assistant -{{ .Response }}<|im_end|> \ No newline at end of file +{{- if .Messages }} +{{- range $index, $_ := .Messages }} +{{- if eq .Role "user" }}[INST] {{ if and $.System (eq (len (slice $.Messages $index)) 1) }}{{ $.System }} +{{ end }}{{ .Content }} +{{- else if eq .Role "assistant" }}[/INST] {{ .Content }} +{{- end }} +{{- end }}[/INST] +{{- else }}[INST] {{ if .System }}{{ .System }} {{ end }}{{ .Prompt }} [/INST] {{ .Response }} +{{- end }} \ No newline at end of file diff --git a/template/openchat.gotmpl b/template/openchat.gotmpl index d2ca3868..d5e1cbb0 100644 --- a/template/openchat.gotmpl +++ b/template/openchat.gotmpl @@ -1 +1,11 @@ -{{ .System }}<|end_of_turn|>GPT4 Correct User: {{ .Prompt }}<|end_of_turn|>GPT4 Correct Assistant: {{ .Response }}<|end_of_turn|> \ No newline at end of file +{{- if .Messages }} +{{- if .System }}GPT Correct System: {{ .System }}<|end_of_turn|> +{{- end }} +{{- range .Messages }}GPT Correct +{{- if eq .Role "user" }} User: +{{- else if eq .Role "assistant" }} Assistant: +{{- end }} {{ .Content }}<|end_of_turn|> +{{- end }}GPT Correct Assistant: +{{- else }} +{{ .System }}<|end_of_turn|>GPT4 Correct User: {{ .Prompt }}<|end_of_turn|>GPT4 Correct Assistant: {{ .Response }}<|end_of_turn|> +{{- end }} \ No newline at end of file diff --git a/template/phi-3.gotmpl b/template/phi-3.gotmpl index bf26dcee..a3558d2b 100644 --- a/template/phi-3.gotmpl +++ b/template/phi-3.gotmpl @@ -1,6 +1,15 @@ +{{- if .Messages }} +{{- if .System }}<|system|> +{{ .System }}<|end|> +{{ end }} +{{- range .Messages }}<|{{ .Role }}|> +{{ .Content }}<|end|> +{{ end }}<|assistant|> +{{ else }} {{ if .System }}<|system|> {{ .System }}<|end|> {{ end }}{{ if .Prompt }}<|user|> {{ .Prompt }}<|end|> {{ end }}<|assistant|> -{{ .Response }}<|end|> \ No newline at end of file +{{ .Response }}<|end|> +{{- end }} \ No newline at end of file diff --git a/template/solar-instruct.gotmpl b/template/solar-instruct.gotmpl index c275a26a..caa6e8e7 100644 --- a/template/solar-instruct.gotmpl +++ b/template/solar-instruct.gotmpl @@ -1,3 +1,16 @@ +{{- if .Messages }} +{{- if .System }}### System: +{{ .System }} + +{{ end }} +{{- range .Messages }} +{{- if eq .Role "user" }}### User: +{{ .Content }} +{{ else if eq .Role "assistant" }}### Assistant: +{{ .Content }} +{{ end }} +{{ end }}### Assistant: +{{ else }} {{ if .System }}### System: {{ .System }} @@ -5,4 +18,5 @@ {{ .Prompt }} {{ end }}### Assistant: -{{ .Response }} \ No newline at end of file +{{ .Response }} +{{- end }} \ No newline at end of file diff --git a/template/starcoder2-instruct.gotmpl b/template/starcoder2-instruct.gotmpl index 33357e54..7d7ff932 100644 --- a/template/starcoder2-instruct.gotmpl +++ b/template/starcoder2-instruct.gotmpl @@ -1,3 +1,17 @@ +{{- if .Messages }} +{{- if .System }}{{ .System }} + +{{ end }} +{{- range .Messages }} +{{- if eq .Role "user" }}### Instruction +{{ .Content }} + +{{ else if eq .Role "assistant" }}### Response +{{ .Content }}<|endoftext|> + +{{ end }} +{{- end }}### Response +{{ else }} {{ if .System }}{{ .System }} {{ end }}{{ if .Prompt }}### Instruction @@ -7,3 +21,4 @@ {{ end }}### Response {{ .Response }}<|endoftext|> +{{- end }} \ No newline at end of file diff --git a/template/template_test.go b/template/template_test.go index ac16bd60..428cdc77 100644 --- a/template/template_test.go +++ b/template/template_test.go @@ -8,9 +8,10 @@ import ( "os" "path/filepath" "slices" + "strings" "testing" - "text/template" + "github.com/google/go-cmp/cmp" "github.com/ollama/ollama/api" "github.com/ollama/ollama/llm" ) @@ -47,7 +48,7 @@ func TestNamed(t *testing.T) { t.Fatal(err) } - tmpl, err := template.New(s).Parse(b.String()) + tmpl, err := Parse(b.String()) if err != nil { t.Fatal(err) } @@ -60,6 +61,70 @@ func TestNamed(t *testing.T) { } } +func TestTemplate(t *testing.T) { + cases := make(map[string][]api.Message) + for _, mm := range [][]api.Message{ + { + {Role: "user", Content: "Hello, how are you?"}, + }, + { + {Role: "user", Content: "Hello, how are you?"}, + {Role: "assistant", Content: "I'm doing great. How can I help you today?"}, + {Role: "user", Content: "I'd like to show off how chat templating works!"}, + }, + { + {Role: "system", Content: "You are a helpful assistant."}, + {Role: "user", Content: "Hello, how are you?"}, + {Role: "assistant", Content: "I'm doing great. How can I help you today?"}, + {Role: "user", Content: "I'd like to show off how chat templating works!"}, + }, + } { + var roles []string + for _, m := range mm { + roles = append(roles, m.Role) + } + + cases[strings.Join(roles, "-")] = mm + } + + matches, err := filepath.Glob("*.gotmpl") + if err != nil { + t.Fatal(err) + } + + for _, match := range matches { + t.Run(match, func(t *testing.T) { + bts, err := os.ReadFile(match) + if err != nil { + t.Fatal(err) + } + + tmpl, err := Parse(string(bts)) + if err != nil { + t.Fatal(err) + } + + for n, tt := range cases { + t.Run(n, func(t *testing.T) { + var actual bytes.Buffer + if err := tmpl.Execute(&actual, Values{Messages: tt}); err != nil { + t.Fatal(err) + } + + expect, err := os.ReadFile(filepath.Join("testdata", match, n)) + if err != nil { + t.Fatal(err) + } + + if diff := cmp.Diff(actual.Bytes(), expect); diff != "" { + t.Errorf("mismatch (-got +want):\n%s", diff) + } + }) + } + }) + } +} + func TestParse(t *testing.T) { cases := []struct { template string diff --git a/template/testdata/alfred.gotmpl/system-user-assistant-user b/template/testdata/alfred.gotmpl/system-user-assistant-user new file mode 100644 index 00000000..03e23ea9 --- /dev/null +++ b/template/testdata/alfred.gotmpl/system-user-assistant-user @@ -0,0 +1 @@ +You are a helpful assistant.Hello, how are you?I'm doing great. How can I help you today?I'd like to show off how chat templating works! \ No newline at end of file diff --git a/template/testdata/alfred.gotmpl/user b/template/testdata/alfred.gotmpl/user new file mode 100644 index 00000000..7c884a6f --- /dev/null +++ b/template/testdata/alfred.gotmpl/user @@ -0,0 +1 @@ +Hello, how are you? \ No newline at end of file diff --git a/template/testdata/alfred.gotmpl/user-assistant-user b/template/testdata/alfred.gotmpl/user-assistant-user new file mode 100644 index 00000000..a60701ed --- /dev/null +++ b/template/testdata/alfred.gotmpl/user-assistant-user @@ -0,0 +1 @@ +Hello, how are you?I'm doing great. How can I help you today?I'd like to show off how chat templating works! \ No newline at end of file diff --git a/template/testdata/alpaca.gotmpl/system-user-assistant-user b/template/testdata/alpaca.gotmpl/system-user-assistant-user new file mode 100644 index 00000000..20182d82 --- /dev/null +++ b/template/testdata/alpaca.gotmpl/system-user-assistant-user @@ -0,0 +1,10 @@ +You are a helpful assistant.### Instruction: +Hello, how are you? + +### Response: +I'm doing great. How can I help you today? + +### Instruction: +I'd like to show off how chat templating works! + +### Response: diff --git a/template/testdata/alpaca.gotmpl/user b/template/testdata/alpaca.gotmpl/user new file mode 100644 index 00000000..a0ce5dec --- /dev/null +++ b/template/testdata/alpaca.gotmpl/user @@ -0,0 +1,4 @@ +### Instruction: +Hello, how are you? + +### Response: diff --git a/template/testdata/alpaca.gotmpl/user-assistant-user b/template/testdata/alpaca.gotmpl/user-assistant-user new file mode 100644 index 00000000..6c5e23ff --- /dev/null +++ b/template/testdata/alpaca.gotmpl/user-assistant-user @@ -0,0 +1,10 @@ +### Instruction: +Hello, how are you? + +### Response: +I'm doing great. How can I help you today? + +### Instruction: +I'd like to show off how chat templating works! + +### Response: diff --git a/template/testdata/chatml.gotmpl/system-user-assistant-user b/template/testdata/chatml.gotmpl/system-user-assistant-user new file mode 100644 index 00000000..8b013fcf --- /dev/null +++ b/template/testdata/chatml.gotmpl/system-user-assistant-user @@ -0,0 +1,9 @@ +<|im_start|>system +You are a helpful assistant.<|im_end|> +<|im_start|>user +Hello, how are you?<|im_end|> +<|im_start|>assistant +I'm doing great. How can I help you today?<|im_end|> +<|im_start|>user +I'd like to show off how chat templating works!<|im_end|> +<|im_start|>assistant diff --git a/template/testdata/chatml.gotmpl/user b/template/testdata/chatml.gotmpl/user new file mode 100644 index 00000000..aa9e597a --- /dev/null +++ b/template/testdata/chatml.gotmpl/user @@ -0,0 +1,3 @@ +<|im_start|>user +Hello, how are you?<|im_end|> +<|im_start|>assistant diff --git a/template/testdata/chatml.gotmpl/user-assistant-user b/template/testdata/chatml.gotmpl/user-assistant-user new file mode 100644 index 00000000..a7cba4de --- /dev/null +++ b/template/testdata/chatml.gotmpl/user-assistant-user @@ -0,0 +1,7 @@ +<|im_start|>user +Hello, how are you?<|im_end|> +<|im_start|>assistant +I'm doing great. How can I help you today?<|im_end|> +<|im_start|>user +I'd like to show off how chat templating works!<|im_end|> +<|im_start|>assistant diff --git a/template/testdata/chatqa.gotmpl/system-user-assistant-user b/template/testdata/chatqa.gotmpl/system-user-assistant-user new file mode 100644 index 00000000..98fd59bf --- /dev/null +++ b/template/testdata/chatqa.gotmpl/system-user-assistant-user @@ -0,0 +1,9 @@ +System: You are a helpful assistant. + +User: Hello, how are you? + +Assistant: I'm doing great. How can I help you today? + +User: I'd like to show off how chat templating works! + +Assistant: \ No newline at end of file diff --git a/template/testdata/chatqa.gotmpl/user b/template/testdata/chatqa.gotmpl/user new file mode 100644 index 00000000..9e7cf702 --- /dev/null +++ b/template/testdata/chatqa.gotmpl/user @@ -0,0 +1,3 @@ +User: Hello, how are you? + +Assistant: \ No newline at end of file diff --git a/template/testdata/chatqa.gotmpl/user-assistant-user b/template/testdata/chatqa.gotmpl/user-assistant-user new file mode 100644 index 00000000..405bbe12 --- /dev/null +++ b/template/testdata/chatqa.gotmpl/user-assistant-user @@ -0,0 +1,7 @@ +User: Hello, how are you? + +Assistant: I'm doing great. How can I help you today? + +User: I'd like to show off how chat templating works! + +Assistant: \ No newline at end of file diff --git a/template/testdata/codellama-70b-instruct.gotmpl/system-user-assistant-user b/template/testdata/codellama-70b-instruct.gotmpl/system-user-assistant-user new file mode 100644 index 00000000..fdd0fc8b --- /dev/null +++ b/template/testdata/codellama-70b-instruct.gotmpl/system-user-assistant-user @@ -0,0 +1,11 @@ +Source: system + + You are a helpful assistant. Source: user + + Hello, how are you? Source: assistant + + I'm doing great. How can I help you today? Source: user + + I'd like to show off how chat templating works! Source: assistant +Destination: user + diff --git a/template/testdata/codellama-70b-instruct.gotmpl/user b/template/testdata/codellama-70b-instruct.gotmpl/user new file mode 100644 index 00000000..9e7174a8 --- /dev/null +++ b/template/testdata/codellama-70b-instruct.gotmpl/user @@ -0,0 +1,5 @@ +Source: user + + Hello, how are you? Source: assistant +Destination: user + diff --git a/template/testdata/codellama-70b-instruct.gotmpl/user-assistant-user b/template/testdata/codellama-70b-instruct.gotmpl/user-assistant-user new file mode 100644 index 00000000..b4ba1736 --- /dev/null +++ b/template/testdata/codellama-70b-instruct.gotmpl/user-assistant-user @@ -0,0 +1,9 @@ +Source: user + + Hello, how are you? Source: assistant + + I'm doing great. How can I help you today? Source: user + + I'd like to show off how chat templating works! Source: assistant +Destination: user + diff --git a/template/testdata/falcon-instruct.gotmpl/system-user-assistant-user b/template/testdata/falcon-instruct.gotmpl/system-user-assistant-user new file mode 100644 index 00000000..16e45e5b --- /dev/null +++ b/template/testdata/falcon-instruct.gotmpl/system-user-assistant-user @@ -0,0 +1,8 @@ +System: You are a helpful assistant. +User: +Hello, how are you? +Falcon: +I'm doing great. How can I help you today? +User: +I'd like to show off how chat templating works! +Falcon: diff --git a/template/testdata/falcon-instruct.gotmpl/user b/template/testdata/falcon-instruct.gotmpl/user new file mode 100644 index 00000000..110831a2 --- /dev/null +++ b/template/testdata/falcon-instruct.gotmpl/user @@ -0,0 +1,3 @@ +User: +Hello, how are you? +Falcon: diff --git a/template/testdata/falcon-instruct.gotmpl/user-assistant-user b/template/testdata/falcon-instruct.gotmpl/user-assistant-user new file mode 100644 index 00000000..b49639ea --- /dev/null +++ b/template/testdata/falcon-instruct.gotmpl/user-assistant-user @@ -0,0 +1,7 @@ +User: +Hello, how are you? +Falcon: +I'm doing great. How can I help you today? +User: +I'd like to show off how chat templating works! +Falcon: diff --git a/template/testdata/gemma-instruct.gotmpl/system-user-assistant-user b/template/testdata/gemma-instruct.gotmpl/system-user-assistant-user new file mode 100644 index 00000000..5f6c3732 --- /dev/null +++ b/template/testdata/gemma-instruct.gotmpl/system-user-assistant-user @@ -0,0 +1,8 @@ +user +You are a helpful assistant. +Hello, how are you? +model +I'm doing great. How can I help you today? +user +I'd like to show off how chat templating works! +model diff --git a/template/testdata/gemma-instruct.gotmpl/user b/template/testdata/gemma-instruct.gotmpl/user new file mode 100644 index 00000000..dc8b30b6 --- /dev/null +++ b/template/testdata/gemma-instruct.gotmpl/user @@ -0,0 +1,3 @@ +user +Hello, how are you? +model diff --git a/template/testdata/gemma-instruct.gotmpl/user-assistant-user b/template/testdata/gemma-instruct.gotmpl/user-assistant-user new file mode 100644 index 00000000..1185924b --- /dev/null +++ b/template/testdata/gemma-instruct.gotmpl/user-assistant-user @@ -0,0 +1,7 @@ +user +Hello, how are you? +model +I'm doing great. How can I help you today? +user +I'd like to show off how chat templating works! +model diff --git a/template/testdata/granite-instruct.gotmpl/system-user-assistant-user b/template/testdata/granite-instruct.gotmpl/system-user-assistant-user new file mode 100644 index 00000000..a732a77f --- /dev/null +++ b/template/testdata/granite-instruct.gotmpl/system-user-assistant-user @@ -0,0 +1,13 @@ +System: +You are a helpful assistant. + +Question: +Hello, how are you? + +Answer: +I'm doing great. How can I help you today? + +Question: +I'd like to show off how chat templating works! + +Answer: diff --git a/template/testdata/granite-instruct.gotmpl/user b/template/testdata/granite-instruct.gotmpl/user new file mode 100644 index 00000000..7abd2ea3 --- /dev/null +++ b/template/testdata/granite-instruct.gotmpl/user @@ -0,0 +1,4 @@ +Question: +Hello, how are you? + +Answer: diff --git a/template/testdata/granite-instruct.gotmpl/user-assistant-user b/template/testdata/granite-instruct.gotmpl/user-assistant-user new file mode 100644 index 00000000..da5e43ea --- /dev/null +++ b/template/testdata/granite-instruct.gotmpl/user-assistant-user @@ -0,0 +1,10 @@ +Question: +Hello, how are you? + +Answer: +I'm doing great. How can I help you today? + +Question: +I'd like to show off how chat templating works! + +Answer: diff --git a/template/testdata/llama2-chat.gotmpl/system-user-assistant-user b/template/testdata/llama2-chat.gotmpl/system-user-assistant-user new file mode 100644 index 00000000..fc2679bf --- /dev/null +++ b/template/testdata/llama2-chat.gotmpl/system-user-assistant-user @@ -0,0 +1,5 @@ +[INST] <> +You are a helpful assistant. +<> + +Hello, how are you? [/INST] I'm doing great. How can I help you today?[INST] I'd like to show off how chat templating works! [/INST] \ No newline at end of file diff --git a/template/testdata/llama2-chat.gotmpl/user b/template/testdata/llama2-chat.gotmpl/user new file mode 100644 index 00000000..ceef9bdb --- /dev/null +++ b/template/testdata/llama2-chat.gotmpl/user @@ -0,0 +1,3 @@ +[INST] <><> + +Hello, how are you? [/INST] \ No newline at end of file diff --git a/template/testdata/llama2-chat.gotmpl/user-assistant-user b/template/testdata/llama2-chat.gotmpl/user-assistant-user new file mode 100644 index 00000000..42b4c529 --- /dev/null +++ b/template/testdata/llama2-chat.gotmpl/user-assistant-user @@ -0,0 +1,3 @@ +[INST] <><> + +Hello, how are you? [/INST] I'm doing great. How can I help you today?[INST] I'd like to show off how chat templating works! [/INST] \ No newline at end of file diff --git a/template/testdata/llama3-instruct.gotmpl/system-user-assistant-user b/template/testdata/llama3-instruct.gotmpl/system-user-assistant-user new file mode 100644 index 00000000..6740bcb4 --- /dev/null +++ b/template/testdata/llama3-instruct.gotmpl/system-user-assistant-user @@ -0,0 +1,10 @@ +<|start_header_id|>system<|end_header_id|> + +You are a helpful assistant.<|eot_id|><|start_header_id|>user<|end_header_id|> + +Hello, how are you?<|eot_id|><|start_header_id|>assistant<|end_header_id|> + +I'm doing great. How can I help you today?<|eot_id|><|start_header_id|>user<|end_header_id|> + +I'd like to show off how chat templating works!<|eot_id|><|start_header_id|>assistant<|end_header_id|> + diff --git a/template/testdata/llama3-instruct.gotmpl/user b/template/testdata/llama3-instruct.gotmpl/user new file mode 100644 index 00000000..470aa028 --- /dev/null +++ b/template/testdata/llama3-instruct.gotmpl/user @@ -0,0 +1,4 @@ +<|start_header_id|>user<|end_header_id|> + +Hello, how are you?<|eot_id|><|start_header_id|>assistant<|end_header_id|> + diff --git a/template/testdata/llama3-instruct.gotmpl/user-assistant-user b/template/testdata/llama3-instruct.gotmpl/user-assistant-user new file mode 100644 index 00000000..6dd768af --- /dev/null +++ b/template/testdata/llama3-instruct.gotmpl/user-assistant-user @@ -0,0 +1,8 @@ +<|start_header_id|>user<|end_header_id|> + +Hello, how are you?<|eot_id|><|start_header_id|>assistant<|end_header_id|> + +I'm doing great. How can I help you today?<|eot_id|><|start_header_id|>user<|end_header_id|> + +I'd like to show off how chat templating works!<|eot_id|><|start_header_id|>assistant<|end_header_id|> + diff --git a/template/testdata/magicoder.gotmpl/system-user-assistant-user b/template/testdata/magicoder.gotmpl/system-user-assistant-user new file mode 100644 index 00000000..c966a861 --- /dev/null +++ b/template/testdata/magicoder.gotmpl/system-user-assistant-user @@ -0,0 +1,12 @@ +You are a helpful assistant. + +@@ Instruction +Hello, how are you? + +@@ Response +I'm doing great. How can I help you today? + +@@ Instruction +I'd like to show off how chat templating works! + +@@ Response diff --git a/template/testdata/magicoder.gotmpl/user b/template/testdata/magicoder.gotmpl/user new file mode 100644 index 00000000..ccfb02bd --- /dev/null +++ b/template/testdata/magicoder.gotmpl/user @@ -0,0 +1,4 @@ +@@ Instruction +Hello, how are you? + +@@ Response diff --git a/template/testdata/magicoder.gotmpl/user-assistant-user b/template/testdata/magicoder.gotmpl/user-assistant-user new file mode 100644 index 00000000..3aea6dab --- /dev/null +++ b/template/testdata/magicoder.gotmpl/user-assistant-user @@ -0,0 +1,10 @@ +@@ Instruction +Hello, how are you? + +@@ Response +I'm doing great. How can I help you today? + +@@ Instruction +I'd like to show off how chat templating works! + +@@ Response diff --git a/template/testdata/mistral-instruct.gotmpl/system-user-assistant-user b/template/testdata/mistral-instruct.gotmpl/system-user-assistant-user new file mode 100644 index 00000000..b6b4bf93 --- /dev/null +++ b/template/testdata/mistral-instruct.gotmpl/system-user-assistant-user @@ -0,0 +1,2 @@ +[INST] Hello, how are you?[/INST] I'm doing great. How can I help you today?[INST] You are a helpful assistant. +I'd like to show off how chat templating works![/INST] \ No newline at end of file diff --git a/template/testdata/mistral-instruct.gotmpl/user b/template/testdata/mistral-instruct.gotmpl/user new file mode 100644 index 00000000..b04871e5 --- /dev/null +++ b/template/testdata/mistral-instruct.gotmpl/user @@ -0,0 +1 @@ +[INST] Hello, how are you?[/INST] \ No newline at end of file diff --git a/template/testdata/mistral-instruct.gotmpl/user-assistant-user b/template/testdata/mistral-instruct.gotmpl/user-assistant-user new file mode 100644 index 00000000..b473e0df --- /dev/null +++ b/template/testdata/mistral-instruct.gotmpl/user-assistant-user @@ -0,0 +1 @@ +[INST] Hello, how are you?[/INST] I'm doing great. How can I help you today?[INST] I'd like to show off how chat templating works![/INST] \ No newline at end of file diff --git a/template/testdata/openchat.gotmpl/system-user-assistant-user b/template/testdata/openchat.gotmpl/system-user-assistant-user new file mode 100644 index 00000000..1214c126 --- /dev/null +++ b/template/testdata/openchat.gotmpl/system-user-assistant-user @@ -0,0 +1 @@ +GPT Correct System: You are a helpful assistant.<|end_of_turn|>GPT Correct User: Hello, how are you?<|end_of_turn|>GPT Correct Assistant: I'm doing great. How can I help you today?<|end_of_turn|>GPT Correct User: I'd like to show off how chat templating works!<|end_of_turn|>GPT Correct Assistant: \ No newline at end of file diff --git a/template/testdata/openchat.gotmpl/user b/template/testdata/openchat.gotmpl/user new file mode 100644 index 00000000..611daa83 --- /dev/null +++ b/template/testdata/openchat.gotmpl/user @@ -0,0 +1 @@ +GPT Correct User: Hello, how are you?<|end_of_turn|>GPT Correct Assistant: \ No newline at end of file diff --git a/template/testdata/openchat.gotmpl/user-assistant-user b/template/testdata/openchat.gotmpl/user-assistant-user new file mode 100644 index 00000000..f97b02b9 --- /dev/null +++ b/template/testdata/openchat.gotmpl/user-assistant-user @@ -0,0 +1 @@ +GPT Correct User: Hello, how are you?<|end_of_turn|>GPT Correct Assistant: I'm doing great. How can I help you today?<|end_of_turn|>GPT Correct User: I'd like to show off how chat templating works!<|end_of_turn|>GPT Correct Assistant: \ No newline at end of file diff --git a/template/testdata/phi-3.gotmpl/system-user-assistant-user b/template/testdata/phi-3.gotmpl/system-user-assistant-user new file mode 100644 index 00000000..6109a9a2 --- /dev/null +++ b/template/testdata/phi-3.gotmpl/system-user-assistant-user @@ -0,0 +1,9 @@ +<|system|> +You are a helpful assistant.<|end|> +<|user|> +Hello, how are you?<|end|> +<|assistant|> +I'm doing great. How can I help you today?<|end|> +<|user|> +I'd like to show off how chat templating works!<|end|> +<|assistant|> diff --git a/template/testdata/phi-3.gotmpl/user b/template/testdata/phi-3.gotmpl/user new file mode 100644 index 00000000..feb96e7c --- /dev/null +++ b/template/testdata/phi-3.gotmpl/user @@ -0,0 +1,3 @@ +<|user|> +Hello, how are you?<|end|> +<|assistant|> diff --git a/template/testdata/phi-3.gotmpl/user-assistant-user b/template/testdata/phi-3.gotmpl/user-assistant-user new file mode 100644 index 00000000..db79d01c --- /dev/null +++ b/template/testdata/phi-3.gotmpl/user-assistant-user @@ -0,0 +1,7 @@ +<|user|> +Hello, how are you?<|end|> +<|assistant|> +I'm doing great. How can I help you today?<|end|> +<|user|> +I'd like to show off how chat templating works!<|end|> +<|assistant|> diff --git a/template/testdata/solar-instruct.gotmpl/system-user-assistant-user b/template/testdata/solar-instruct.gotmpl/system-user-assistant-user new file mode 100644 index 00000000..28c1730a --- /dev/null +++ b/template/testdata/solar-instruct.gotmpl/system-user-assistant-user @@ -0,0 +1,13 @@ +### System: +You are a helpful assistant. + +### User: +Hello, how are you? + +### Assistant: +I'm doing great. How can I help you today? + +### User: +I'd like to show off how chat templating works! + +### Assistant: diff --git a/template/testdata/solar-instruct.gotmpl/user b/template/testdata/solar-instruct.gotmpl/user new file mode 100644 index 00000000..3a43382a --- /dev/null +++ b/template/testdata/solar-instruct.gotmpl/user @@ -0,0 +1,4 @@ +### User: +Hello, how are you? + +### Assistant: diff --git a/template/testdata/solar-instruct.gotmpl/user-assistant-user b/template/testdata/solar-instruct.gotmpl/user-assistant-user new file mode 100644 index 00000000..8553e73b --- /dev/null +++ b/template/testdata/solar-instruct.gotmpl/user-assistant-user @@ -0,0 +1,10 @@ +### User: +Hello, how are you? + +### Assistant: +I'm doing great. How can I help you today? + +### User: +I'd like to show off how chat templating works! + +### Assistant: diff --git a/template/testdata/starcoder2-instruct.gotmpl/system-user-assistant-user b/template/testdata/starcoder2-instruct.gotmpl/system-user-assistant-user new file mode 100644 index 00000000..5b718b3e --- /dev/null +++ b/template/testdata/starcoder2-instruct.gotmpl/system-user-assistant-user @@ -0,0 +1,12 @@ +You are a helpful assistant. + +### Instruction +Hello, how are you? + +### Response +I'm doing great. How can I help you today?<|endoftext|> + +### Instruction +I'd like to show off how chat templating works! + +### Response diff --git a/template/testdata/starcoder2-instruct.gotmpl/user b/template/testdata/starcoder2-instruct.gotmpl/user new file mode 100644 index 00000000..11b0be1f --- /dev/null +++ b/template/testdata/starcoder2-instruct.gotmpl/user @@ -0,0 +1,4 @@ +### Instruction +Hello, how are you? + +### Response diff --git a/template/testdata/starcoder2-instruct.gotmpl/user-assistant-user b/template/testdata/starcoder2-instruct.gotmpl/user-assistant-user new file mode 100644 index 00000000..d99feabb --- /dev/null +++ b/template/testdata/starcoder2-instruct.gotmpl/user-assistant-user @@ -0,0 +1,10 @@ +### Instruction +Hello, how are you? + +### Response +I'm doing great. How can I help you today?<|endoftext|> + +### Instruction +I'd like to show off how chat templating works! + +### Response diff --git a/template/testdata/vicuna.gotmpl/system-user-assistant-user b/template/testdata/vicuna.gotmpl/system-user-assistant-user new file mode 100644 index 00000000..50d2f92c --- /dev/null +++ b/template/testdata/vicuna.gotmpl/system-user-assistant-user @@ -0,0 +1,6 @@ +You are a helpful assistant. + +USER: Hello, how are you? +ASSISTANT: I'm doing great. How can I help you today? +USER: I'd like to show off how chat templating works! +ASSISTANT: \ No newline at end of file diff --git a/template/testdata/vicuna.gotmpl/user b/template/testdata/vicuna.gotmpl/user new file mode 100644 index 00000000..cbe5ef70 --- /dev/null +++ b/template/testdata/vicuna.gotmpl/user @@ -0,0 +1,2 @@ +USER: Hello, how are you? +ASSISTANT: \ No newline at end of file diff --git a/template/testdata/vicuna.gotmpl/user-assistant-user b/template/testdata/vicuna.gotmpl/user-assistant-user new file mode 100644 index 00000000..9172547e --- /dev/null +++ b/template/testdata/vicuna.gotmpl/user-assistant-user @@ -0,0 +1,4 @@ +USER: Hello, how are you? +ASSISTANT: I'm doing great. How can I help you today? +USER: I'd like to show off how chat templating works! +ASSISTANT: \ No newline at end of file diff --git a/template/testdata/zephyr.gotmpl/system-user-assistant-user b/template/testdata/zephyr.gotmpl/system-user-assistant-user new file mode 100644 index 00000000..03d43fc3 --- /dev/null +++ b/template/testdata/zephyr.gotmpl/system-user-assistant-user @@ -0,0 +1,9 @@ +<|system|> +You are a helpful assistant. +<|user|> +Hello, how are you? +<|assistant|> +I'm doing great. How can I help you today? +<|user|> +I'd like to show off how chat templating works! +<|assistant|> diff --git a/template/testdata/zephyr.gotmpl/user b/template/testdata/zephyr.gotmpl/user new file mode 100644 index 00000000..6cefdaa0 --- /dev/null +++ b/template/testdata/zephyr.gotmpl/user @@ -0,0 +1,3 @@ +<|user|> +Hello, how are you? +<|assistant|> diff --git a/template/testdata/zephyr.gotmpl/user-assistant-user b/template/testdata/zephyr.gotmpl/user-assistant-user new file mode 100644 index 00000000..3937b006 --- /dev/null +++ b/template/testdata/zephyr.gotmpl/user-assistant-user @@ -0,0 +1,7 @@ +<|user|> +Hello, how are you? +<|assistant|> +I'm doing great. How can I help you today? +<|user|> +I'd like to show off how chat templating works! +<|assistant|> diff --git a/template/vicuna.gotmpl b/template/vicuna.gotmpl index 174c1a35..2e13e990 100644 --- a/template/vicuna.gotmpl +++ b/template/vicuna.gotmpl @@ -1,3 +1,14 @@ +{{- if .Messages }} +{{- if .System }}{{ .System }} + +{{ end }} +{{- range .Messages }} +{{- if eq .Role "user" }}USER: {{ .Content }} +{{ else if eq .Role "assistant" }}ASSISTANT: {{ .Content }} +{{ end }} +{{- end }}ASSISTANT: +{{- else }} {{ if .System }}{{ .System }} {{ end }}{{ if .Prompt }}USER: {{ .Prompt }} -{{ end }}ASSISTANT: {{ .Response }} \ No newline at end of file +{{ end }}ASSISTANT: {{ .Response }} +{{- end }} \ No newline at end of file diff --git a/template/zephyr.gotmpl b/template/zephyr.gotmpl index aac0c7a1..e6668848 100644 --- a/template/zephyr.gotmpl +++ b/template/zephyr.gotmpl @@ -1,6 +1,15 @@ +{{- if .Messages }} +{{- if .System }}<|system|> +{{ .System }} +{{ end }} +{{- range .Messages }}<|{{ .Role }}|> +{{ .Content }} +{{ end }}<|assistant|> +{{ else }} {{ if .System }}<|system|> {{ .System }} {{ end }}{{ if .Prompt }}<|user|> {{ .Prompt }} {{ end }}<|assistant|> -{{ .Response }} \ No newline at end of file +{{ .Response }} +{{- end }} \ No newline at end of file