From df146c41e27e6e15c0ab51af9070c87c82aa4fc7 Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Mon, 17 Jul 2023 14:21:27 -0700 Subject: [PATCH 1/9] separate prompt into template and system --- parser/parser.go | 101 +++++++++++++++++++++++++++-------------------- server/images.go | 85 ++++++++++++++++++++++++--------------- server/routes.go | 12 +----- 3 files changed, 113 insertions(+), 85 deletions(-) diff --git a/parser/parser.go b/parser/parser.go index 5ba87c84..635e7276 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -2,76 +2,91 @@ package parser import ( "bufio" + "bytes" + "errors" "fmt" "io" - "strings" ) type Command struct { Name string - Arg string + Args string +} + +func (c *Command) Reset() { + c.Name = "" + c.Args = "" } func Parse(reader io.Reader) ([]Command, error) { var commands []Command - var foundModel bool + + var command, modelCommand Command scanner := bufio.NewScanner(reader) - multiline := false - var multilineCommand *Command + scanner.Split(scanModelfile) for scanner.Scan() { - line := scanner.Text() - if multiline { - // If we're in a multiline string and the line is """, end the multiline string. - if strings.TrimSpace(line) == `"""` { - multiline = false - commands = append(commands, *multilineCommand) - } else { - // Otherwise, append the line to the multiline string. - multilineCommand.Arg += "\n" + line - } - continue - } - fields := strings.Fields(line) + line := scanner.Bytes() + + fields := bytes.SplitN(line, []byte(" "), 2) if len(fields) == 0 { continue } - command := Command{} - switch strings.ToUpper(fields[0]) { + switch string(bytes.ToUpper(fields[0])) { case "FROM": command.Name = "model" - command.Arg = fields[1] - if command.Arg == "" { - return nil, fmt.Errorf("no model specified in FROM line") - } - foundModel = true - case "PROMPT", "LICENSE": - command.Name = strings.ToLower(fields[0]) - if fields[1] == `"""` { - multiline = true - multilineCommand = &command - multilineCommand.Arg = "" - } else { - command.Arg = strings.Join(fields[1:], " ") - } + command.Args = string(fields[1]) + // copy command for validation + modelCommand = command + case "LICENSE", "TEMPLATE", "SYSTEM": + command.Name = string(bytes.ToLower(fields[0])) + command.Args = string(fields[1]) case "PARAMETER": - command.Name = fields[1] - command.Arg = strings.Join(fields[2:], " ") + fields = bytes.SplitN(fields[1], []byte(" "), 2) + command.Name = string(fields[0]) + command.Args = string(fields[1]) default: continue } - if !multiline { - commands = append(commands, command) - } + + commands = append(commands, command) + command.Reset() } - if !foundModel { + if modelCommand.Args == "" { return nil, fmt.Errorf("no FROM line for the model was specified") } - if multiline { - return nil, fmt.Errorf("unclosed multiline string") - } return commands, scanner.Err() } + +func scanModelfile(data []byte, atEOF bool) (advance int, token []byte, err error) { + if atEOF || len(data) == 0 { + return 0, nil, nil + } + + newline := bytes.IndexByte(data, '\n') + + if start := bytes.Index(data, []byte(`"""`)); start >= 0 && start < newline { + end := bytes.Index(data[start+3:], []byte(`"""`)) + if end < 0 { + return 0, nil, errors.New(`unterminated multiline string: """`) + } + + n := start + 3 + end + 3 + return n, bytes.Replace(data[:n], []byte(`"""`), []byte(""), 2), nil + } + + if start := bytes.Index(data, []byte(`'''`)); start >= 0 && start < newline { + end := bytes.Index(data[start+3:], []byte(`'''`)) + if end < 0 { + return 0, nil, errors.New("unterminated multiline string: '''") + } + + n := start + 3 + end + 3 + return n, bytes.Replace(data[:n], []byte("'''"), []byte(""), 2), nil + } + + return bufio.ScanLines(data, atEOF) +} diff --git a/server/images.go b/server/images.go index 4140283a..16b32f83 100644 --- a/server/images.go +++ b/server/images.go @@ -16,6 +16,7 @@ import ( "reflect" "strconv" "strings" + "text/template" "github.com/jmorganca/ollama/api" "github.com/jmorganca/ollama/parser" @@ -24,10 +25,33 @@ import ( type Model struct { Name string `json:"name"` ModelPath string - Prompt string + Template string + System string Options api.Options } +func (m *Model) Prompt(request api.GenerateRequest) (string, error) { + tmpl, err := template.New("").Parse(m.Template) + if err != nil { + return "", err + } + + var vars struct { + System string + Prompt string + } + + vars.System = m.System + vars.Prompt = request.Prompt + + var sb strings.Builder + if err := tmpl.Execute(&sb, vars); err != nil { + return "", err + } + + return sb.String(), nil +} + type ManifestV2 struct { SchemaVersion int `json:"schemaVersion"` MediaType string `json:"mediaType"` @@ -71,20 +95,19 @@ func GetManifest(mp ModelPath) (*ManifestV2, error) { if err != nil { return nil, err } + if _, err = os.Stat(fp); err != nil && !errors.Is(err, os.ErrNotExist) { return nil, fmt.Errorf("couldn't find model '%s'", mp.GetShortTagname()) } var manifest *ManifestV2 - f, err := os.Open(fp) + bts, err := os.ReadFile(fp) if err != nil { return nil, fmt.Errorf("couldn't open file '%s'", fp) } - decoder := json.NewDecoder(f) - err = decoder.Decode(&manifest) - if err != nil { + if err := json.Unmarshal(bts, &manifest); err != nil { return nil, err } @@ -112,12 +135,20 @@ func GetModel(name string) (*Model, error) { switch layer.MediaType { case "application/vnd.ollama.image.model": model.ModelPath = filename - case "application/vnd.ollama.image.prompt": - data, err := os.ReadFile(filename) + case "application/vnd.ollama.image.template": + bts, err := os.ReadFile(filename) if err != nil { return nil, err } - model.Prompt = string(data) + + model.Template = string(bts) + case "application/vnd.ollama.image.system": + bts, err := os.ReadFile(filename) + if err != nil { + return nil, err + } + + model.System = string(bts) case "application/vnd.ollama.image.params": params, err := os.Open(filename) if err != nil { @@ -156,13 +187,13 @@ func CreateModel(name string, path string, fn func(status string)) error { params := make(map[string]string) for _, c := range commands { - log.Printf("[%s] - %s\n", c.Name, c.Arg) + log.Printf("[%s] - %s\n", c.Name, c.Args) switch c.Name { case "model": fn("looking for model") - mf, err := GetManifest(ParseModelPath(c.Arg)) + mf, err := GetManifest(ParseModelPath(c.Args)) if err != nil { - fp := c.Arg + fp := c.Args // If filePath starts with ~/, replace it with the user's home directory. if strings.HasPrefix(fp, "~/") { @@ -183,7 +214,7 @@ func CreateModel(name string, path string, fn func(status string)) error { fn("creating model layer") file, err := os.Open(fp) if err != nil { - fn(fmt.Sprintf("couldn't find model '%s'", c.Arg)) + fn(fmt.Sprintf("couldn't find model '%s'", c.Args)) return fmt.Errorf("failed to open file: %v", err) } defer file.Close() @@ -206,31 +237,21 @@ func CreateModel(name string, path string, fn func(status string)) error { layers = append(layers, newLayer) } } - case "prompt": - fn("creating prompt layer") + case "license", "template", "system": + fn(fmt.Sprintf("creating %s layer", c.Name)) // remove the prompt layer if one exists - layers = removeLayerFromLayers(layers, "application/vnd.ollama.image.prompt") + mediaType := fmt.Sprintf("application/vnd.ollama.image.%s", c.Name) + layers = removeLayerFromLayers(layers, mediaType) - prompt := strings.NewReader(c.Arg) - l, err := CreateLayer(prompt) + layer, err := CreateLayer(strings.NewReader(c.Args)) if err != nil { - fn(fmt.Sprintf("couldn't create prompt layer: %v", err)) - return fmt.Errorf("failed to create layer: %v", err) + return err } - l.MediaType = "application/vnd.ollama.image.prompt" - layers = append(layers, l) - case "license": - fn("creating license layer") - license := strings.NewReader(c.Arg) - l, err := CreateLayer(license) - if err != nil { - fn(fmt.Sprintf("couldn't create license layer: %v", err)) - return fmt.Errorf("failed to create layer: %v", err) - } - l.MediaType = "application/vnd.ollama.image.license" - layers = append(layers, l) + + layer.MediaType = mediaType + layers = append(layers, layer) default: - params[c.Name] = c.Arg + params[c.Name] = c.Args } } diff --git a/server/routes.go b/server/routes.go index 46e05989..f97ab120 100644 --- a/server/routes.go +++ b/server/routes.go @@ -9,7 +9,6 @@ import ( "os" "path/filepath" "strings" - "text/template" "time" "dario.cat/mergo" @@ -54,19 +53,12 @@ func generate(c *gin.Context) { return } - templ, err := template.New("").Parse(model.Prompt) + prompt, err := model.Prompt(req) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } - var sb strings.Builder - if err = templ.Execute(&sb, req); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - return - } - req.Prompt = sb.String() - llm, err := llama.New(model.ModelPath, opts) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) @@ -77,7 +69,7 @@ func generate(c *gin.Context) { ch := make(chan any) go func() { defer close(ch) - llm.Predict(req.Context, req.Prompt, func(r api.GenerateResponse) { + llm.Predict(req.Context, prompt, func(r api.GenerateResponse) { r.Model = req.Model r.CreatedAt = time.Now().UTC() if r.Done { From ca210ba48059c8d7f274fe5b7c77e603b371fdf2 Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Wed, 19 Jul 2023 19:43:00 -0700 Subject: [PATCH 2/9] handle vnd.ollama.image.prompt for compat --- server/images.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/server/images.go b/server/images.go index 16b32f83..9be2c68b 100644 --- a/server/images.go +++ b/server/images.go @@ -39,6 +39,9 @@ func (m *Model) Prompt(request api.GenerateRequest) (string, error) { var vars struct { System string Prompt string + + // deprecated: versions <= 0.0.7 used this to omit the system prompt + Context []int } vars.System = m.System @@ -149,6 +152,14 @@ func GetModel(name string) (*Model, error) { } model.System = string(bts) + case "application/vnd.ollama.image.prompt": + log.Printf("PROMPT is deprecated. Please use TEMPLATE and SYSTEM instead.") + bts, err := os.ReadFile(filename) + if err != nil { + return nil, err + } + + model.Template = string(bts) case "application/vnd.ollama.image.params": params, err := os.Open(filename) if err != nil { From 1c72e46e09328d0836a7487c2d6c9ab7a6bdcf41 Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Wed, 19 Jul 2023 19:51:14 -0700 Subject: [PATCH 3/9] update modelfile.md --- docs/modelfile.md | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/docs/modelfile.md b/docs/modelfile.md index ab1d6f62..fc5b36ea 100644 --- a/docs/modelfile.md +++ b/docs/modelfile.md @@ -11,12 +11,13 @@ The format of the Modelfile: INSTRUCTION arguments ``` -| Instruction | Description | -|------------------------- |--------------------------------------------------------- | -| FROM
(required) | Defines the base model to be used when creating a model | -| PARAMETER
(optional) | Sets the parameters for how the model will be run | -| PROMPT
(optional) | Sets the prompt to use when the model will be run | -| LICENSE
(optional) | Specify the license of the model. It is additive, and | +| Instruction | Description | +|-------------------------- |--------------------------------------------------------- | +| `FROM`
(required) | Defines the base model to be used when creating a model | +| `PARAMETER`
(optional) | Sets the parameters for how the model will be run | +| `TEMPLATE`
(optional) | Sets the prompt template to use when the model will be run | +| `SYSTEM`
(optional) | // todo | +| `LICENSE`
(optional) | Specify the license of the model. It is additive, and | ## Examples @@ -25,11 +26,13 @@ An example of a model file creating a mario blueprint: ``` FROM llama2 PARAMETER temperature 1 -PROMPT """ -System: You are Mario from super mario bros, acting as an assistant. +TEMPLATE """ +System: {{ .System }} User: {{ .Prompt }} Assistant: """ + +SYSTEM You are Mario from super mario bros, acting as an assistant. ``` To use this: @@ -64,7 +67,7 @@ FROM ./ollama-model.bin ## PARAMETER (Optional) -The PARAMETER instruction defines a parameter that can be set when the model is run. +The `PARAMETER` instruction defines a parameter that can be set when the model is run. ``` PARAMETER @@ -87,24 +90,31 @@ PARAMETER | MirostatEta | Influences how quickly the algorithm responds to feedback from the generated text. A lower learning rate will result in slower adjustments, while a higher learning rate will make the algorithm more responsive. (Default: 0.1) | float | MirostatEta 0.1 | | NumThread | Sets the number of threads to use during computation. By default, Ollama will detect this for optimal performance. It is recommended to set this value to the number of physical CPU cores your system has (as opposed to the logical number of cores). | int | NumThread 8 | -## PROMPT +## TEMPLATE -Prompt is a set of instructions to an LLM to cause the model to return desired response(s). Typically there are 3-4 components to a prompt: System, context, user, and response. +`TEMPLATE` is a set of instructions to an LLM to cause the model to return desired response(s). Typically there are 3-4 components to a prompt: system, input, and response. ```modelfile -PROMPT """ -{{- if not .Context }} +TEMPLATE """ ### System: -You are a content marketer who needs to come up with a short but succinct tweet. Make sure to include the appropriate hashtags and links. Sometimes when appropriate, describe a meme that can be includes as well. All answers should be in the form of a tweet which has a max size of 280 characters. Every instruction will be the topic to create a tweet about. -{{- end }} +{{ .System }} + ### Instruction: {{ .Prompt }} ### Response: """ +SYSTEM """ +You are a content marketer who needs to come up with a short but succinct tweet. Make sure to include the appropriate hashtags and links. Sometimes when appropriate, describe a meme that can be includes as well. All answers should be in the form of a tweet which has a max size of 280 characters. Every instruction will be the topic to create a tweet about. +""" + ``` +## SYSTEM + +// todo + ## Notes - the **modelfile is not case sensitive**. In the examples, we use uppercase for instructions to make it easier to distinguish it from arguments. From 5c5948b4e72fccbc68ad7f9b40bbaabb35fb9090 Mon Sep 17 00:00:00 2001 From: Michael Chiang Date: Wed, 19 Jul 2023 22:43:33 -0700 Subject: [PATCH 4/9] clean up my previous empty sentences --- docs/modelfile.md | 92 ++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 50 deletions(-) diff --git a/docs/modelfile.md b/docs/modelfile.md index fc5b36ea..5a30e896 100644 --- a/docs/modelfile.md +++ b/docs/modelfile.md @@ -11,13 +11,13 @@ The format of the Modelfile: INSTRUCTION arguments ``` -| Instruction | Description | -|-------------------------- |--------------------------------------------------------- | -| `FROM`
(required) | Defines the base model to be used when creating a model | -| `PARAMETER`
(optional) | Sets the parameters for how the model will be run | -| `TEMPLATE`
(optional) | Sets the prompt template to use when the model will be run | -| `SYSTEM`
(optional) | // todo | -| `LICENSE`
(optional) | Specify the license of the model. It is additive, and | +| Instruction | Description | +| ------------------------- | ----------------------------------------------------- | +| `FROM`
(required) | Defines the base model to use | +| `PARAMETER`
(optional) | Sets the parameters for how Ollama will run the model | +| `SYSTEM`
(optional) | Specifies the system prompt that will set the context | +| `TEMPLATE`
(optional) | The full prompt template to be sent to the model | +| `LICENSE`
(optional) | Specifies the legal license | ## Examples @@ -25,14 +25,23 @@ An example of a model file creating a mario blueprint: ``` FROM llama2 +# sets the temperature to 1 [higher is more creative, lower is more coherent] +# sets the context size to 4096 PARAMETER temperature 1 -TEMPLATE """ -System: {{ .System }} -User: {{ .Prompt }} -Assistant: -""" +PARAMETER num_ctx 4096 -SYSTEM You are Mario from super mario bros, acting as an assistant. +# Check for first system message, so the model output won't repeat itself. +# <> and [INST] are special tags used by the Llama2 model. + +PROMPT """ +{{- if .First }} +<> +You are Mario from super mario bros, acting as an assistant. +<> + +{{- end }} +[INST] {{ .Prompt }} [/INST] +""" ``` To use this: @@ -44,7 +53,7 @@ To use this: ## FROM (Required) -The FROM instruction defines the base model to be used when creating a model. +The FROM instruction Defines the base model to use when creating a model. ``` FROM : @@ -62,7 +71,7 @@ A list of available base models: ### Build from a bin file ``` -FROM ./ollama-model.bin +FROM ./ollama-model.bin ``` ## PARAMETER (Optional) @@ -75,45 +84,28 @@ PARAMETER ### Valid Parameters and Values -| Parameter | Description | Value Type | Example Usage | -|---------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------|-------------------| -| NumCtx | Sets the size of the prompt context size length model. (Default: 2048) | int | Numctx 4096 | -| temperature | The temperature of the model. Increasing the temperature will make the model answer more creatively. (Default: 0.8) | float | Temperature 0.7 | -| TopK | Reduces the probability of generating nonsense. A higher value (e.g. 100) will give more diverse answers, while a lower value (e.g. 10) will be more conservative. (Default: 40) | int | TopK 40 | -| TopP | Works together with top-k. A higher value (e.g., 0.95) will lead to more diverse text, while a lower value (e.g., 0.5) will generate more focused and conservative text. (Default: 0.9) | float | TopP 0.9 | -| NumGPU | The number of GPUs to use. On macOS it defaults to 1 to enable metal support, 0 to disable. | int | numGPU 1 | -| RepeatLastN | Sets how far back for the model to look back to prevent repetition. (Default: 64, 0 = disabled, -1 = ctx-size) | int | RepeatLastN 64 | -| RepeatPenalty | Sets how strongly to penalize repetitions. A higher value (e.g., 1.5) will penalize repetitions more strongly, while a lower value (e.g., 0.9) will be more lenient. (Default: 1.1) | float | RepeatPenalty 1.1 | -| TFSZ | Tail free sampling is used to reduce the impact of less probable tokens from the output. A higher value (e.g., 2.0) will reduce the impact more, while a value of 1.0 disables this setting. (default: 1) | float | TFSZ 1 | -| Mirostat | Enable Mirostat sampling for controlling perplexity. (default: 0, 0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0) | int | Mirostat 0 | -| MirostatTau | Controls the balance between coherence and diversity of the output. A lower value will result in more focused and coherent text. (Default: 5.0) | float | MirostatTau 5.0 | -| MirostatEta | Influences how quickly the algorithm responds to feedback from the generated text. A lower learning rate will result in slower adjustments, while a higher learning rate will make the algorithm more responsive. (Default: 0.1) | float | MirostatEta 0.1 | -| NumThread | Sets the number of threads to use during computation. By default, Ollama will detect this for optimal performance. It is recommended to set this value to the number of physical CPU cores your system has (as opposed to the logical number of cores). | int | NumThread 8 | +| Parameter | Description | Value Type | Example Usage | +| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- | ------------------ | +| num_ctx | Sets the size of the prompt context size length model. (Default: 2048) | int | num_ctx 4096 | +| temperature | The temperature of the model. Increasing the temperature will make the model answer more creatively. (Default: 0.8) | float | temperature 0.7 | +| top_k | Reduces the probability of generating nonsense. A higher value (e.g. 100) will give more diverse answers, while a lower value (e.g. 10) will be more conservative. (Default: 40) | int | top_k 40 | +| top_p | Works together with top-k. A higher value (e.g., 0.95) will lead to more diverse text, while a lower value (e.g., 0.5) will generate more focused and conservative text. (Default: 0.9) | float | top_p 0.9 | +| num_gpu | The number of GPUs to use. On macOS it defaults to 1 to enable metal support, 0 to disable. | int | num_gpu 1 | +| repeat_last_n | Sets how far back for the model to look back to prevent repetition. (Default: 64, 0 = disabled, -1 = ctx-size) | int | repeat_last_n 64 | +| repeat_penalty | Sets how strongly to penalize repetitions. A higher value (e.g., 1.5) will penalize repetitions more strongly, while a lower value (e.g., 0.9) will be more lenient. (Default: 1.1) | float | repeat_penalty 1.1 | +| tfs_z | Tail free sampling is used to reduce the impact of less probable tokens from the output. A higher value (e.g., 2.0) will reduce the impact more, while a value of 1.0 disables this setting. (default: 1) | float | tfs_z 1 | +| mirostat | Enable Mirostat sampling for controlling perplexity. (default: 0, 0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0) | int | mirostat 0 | +| mirostat_tau | Controls the balance between coherence and diversity of the output. A lower value will result in more focused and coherent text. (Default: 5.0) | float | mirostat_tau 5.0 | +| mirostat_eta | Influences how quickly the algorithm responds to feedback from the generated text. A lower learning rate will result in slower adjustments, while a higher learning rate will make the algorithm more responsive. (Default: 0.1) | float | mirostat_eta 0.1 | +| num_thread | Sets the number of threads to use during computation. By default, Ollama will detect this for optimal performance. It is recommended to set this value to the number of physical CPU cores your system has (as opposed to the logical number of cores). | int | num_thread 8 | -## TEMPLATE +## Prompt -`TEMPLATE` is a set of instructions to an LLM to cause the model to return desired response(s). Typically there are 3-4 components to a prompt: system, input, and response. +When building on top of the base models supplied by Ollama, it comes with the prompt template predefined. To override the supplied system prompt, simply add `SYSTEM: insert system prompt` to change the systme prompt. -```modelfile -TEMPLATE """ -### System: -{{ .System }} +### Prompt Template -### Instruction: -{{ .Prompt }} - -### Response: -""" - -SYSTEM """ -You are a content marketer who needs to come up with a short but succinct tweet. Make sure to include the appropriate hashtags and links. Sometimes when appropriate, describe a meme that can be includes as well. All answers should be in the form of a tweet which has a max size of 280 characters. Every instruction will be the topic to create a tweet about. -""" - -``` - -## SYSTEM - -// todo +`TEMPLATE` the full prompt template to be passed into the model. It may include (optionally) a system prompt, user prompt, and assistant prompt. This is used to create a full custom prompt, and syntax may be model specific. ## Notes From df100ce5409fa52a113b1bfbb69f933021d8dd02 Mon Sep 17 00:00:00 2001 From: Michael Chiang Date: Wed, 19 Jul 2023 22:58:15 -0700 Subject: [PATCH 5/9] Update docs/modelfile.md Co-authored-by: Michael Yang --- docs/modelfile.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/modelfile.md b/docs/modelfile.md index 5a30e896..5918479d 100644 --- a/docs/modelfile.md +++ b/docs/modelfile.md @@ -53,7 +53,7 @@ To use this: ## FROM (Required) -The FROM instruction Defines the base model to use when creating a model. +The FROM instruction defines the base model to use when creating a model. ``` FROM : From c47786c1b0138fd3e58323876030c5b1a7378789 Mon Sep 17 00:00:00 2001 From: Michael Chiang Date: Wed, 19 Jul 2023 22:58:24 -0700 Subject: [PATCH 6/9] Update docs/modelfile.md Co-authored-by: Michael Yang --- docs/modelfile.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/modelfile.md b/docs/modelfile.md index 5918479d..b3c4bfab 100644 --- a/docs/modelfile.md +++ b/docs/modelfile.md @@ -101,7 +101,7 @@ PARAMETER ## Prompt -When building on top of the base models supplied by Ollama, it comes with the prompt template predefined. To override the supplied system prompt, simply add `SYSTEM: insert system prompt` to change the systme prompt. +When building on top of the base models supplied by Ollama, it comes with the prompt template predefined. To override the supplied system prompt, simply add `SYSTEM insert system prompt` to change the system prompt. ### Prompt Template From c161aef5f970bc759c6241ec3d34e85324416190 Mon Sep 17 00:00:00 2001 From: Michael Chiang Date: Wed, 19 Jul 2023 23:01:03 -0700 Subject: [PATCH 7/9] update example --- docs/modelfile.md | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/docs/modelfile.md b/docs/modelfile.md index b3c4bfab..91dee508 100644 --- a/docs/modelfile.md +++ b/docs/modelfile.md @@ -30,17 +30,9 @@ FROM llama2 PARAMETER temperature 1 PARAMETER num_ctx 4096 -# Check for first system message, so the model output won't repeat itself. -# <> and [INST] are special tags used by the Llama2 model. +# Overriding the system prompt +SYSTEM You are Mario from super mario bros, acting as an assistant. -PROMPT """ -{{- if .First }} -<> -You are Mario from super mario bros, acting as an assistant. -<> - -{{- end }} -[INST] {{ .Prompt }} [/INST] """ ``` From 7c6ea2a966bd7702922c81b05c7ea05bbcee67ba Mon Sep 17 00:00:00 2001 From: Michael Chiang Date: Wed, 19 Jul 2023 23:10:32 -0700 Subject: [PATCH 8/9] fix dangling """ --- docs/modelfile.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/modelfile.md b/docs/modelfile.md index 91dee508..9009ce58 100644 --- a/docs/modelfile.md +++ b/docs/modelfile.md @@ -32,8 +32,6 @@ PARAMETER num_ctx 4096 # Overriding the system prompt SYSTEM You are Mario from super mario bros, acting as an assistant. - -""" ``` To use this: From 60b4db63890d84e1f6711e037455f76b2228fc54 Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Wed, 19 Jul 2023 23:22:19 -0700 Subject: [PATCH 9/9] add .First --- server/images.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/server/images.go b/server/images.go index 9be2c68b..1ac5b74b 100644 --- a/server/images.go +++ b/server/images.go @@ -37,6 +37,7 @@ func (m *Model) Prompt(request api.GenerateRequest) (string, error) { } var vars struct { + First bool System string Prompt string @@ -44,8 +45,10 @@ func (m *Model) Prompt(request api.GenerateRequest) (string, error) { Context []int } + vars.First = len(vars.Context) == 0 vars.System = m.System vars.Prompt = request.Prompt + vars.Context = request.Context var sb strings.Builder if err := tmpl.Execute(&sb, vars); err != nil {