From 184ad8f05795fcbd6c02ae7b4cba4121f64a8720 Mon Sep 17 00:00:00 2001 From: Bruce MacDonald Date: Thu, 27 Jul 2023 17:02:14 -0400 Subject: [PATCH 1/3] allow specifying stop conditions in modelfile --- api/types.go | 2 +- llama/llama.go | 2 +- server/images.go | 9 +++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/api/types.go b/api/types.go index 2e3b0578..0b8603fe 100644 --- a/api/types.go +++ b/api/types.go @@ -178,7 +178,7 @@ type Options struct { MirostatTau float32 `json:"mirostat_tau,omitempty"` MirostatEta float32 `json:"mirostat_eta,omitempty"` PenalizeNewline bool `json:"penalize_newline,omitempty"` - StopConditions []string `json:"stop_conditions,omitempty"` + Stop []string `json:"stop,omitempty"` NumThread int `json:"num_thread,omitempty"` } diff --git a/llama/llama.go b/llama/llama.go index 9032bf5f..3b95cfab 100644 --- a/llama/llama.go +++ b/llama/llama.go @@ -246,7 +246,7 @@ func (llm *LLM) Predict(ctx []int, prompt string, fn func(api.GenerateResponse)) } func (llm *LLM) checkStopConditions(b bytes.Buffer) error { - for _, stopCondition := range llm.StopConditions { + for _, stopCondition := range llm.Stop { if stopCondition == b.String() { return io.EOF } else if strings.HasPrefix(stopCondition, b.String()) { diff --git a/server/images.go b/server/images.go index f6b995a9..933fe26c 100644 --- a/server/images.go +++ b/server/images.go @@ -14,6 +14,7 @@ import ( "path" "path/filepath" "reflect" + "regexp" "strconv" "strings" "text/template" @@ -472,6 +473,14 @@ func paramsToReader(params map[string]string) (io.ReadSeeker, error) { field.SetBool(boolVal) case reflect.String: field.SetString(val) + case reflect.Slice: + re := regexp.MustCompile(`"(.*?)"`) // matches everything enclosed in quotes + vals := re.FindAllStringSubmatch(val, -1) + var sliceVal []string + for _, v := range vals { + sliceVal = append(sliceVal, v[1]) // v[1] is the captured group, v[0] is the entire match + } + field.Set(reflect.ValueOf(sliceVal)) default: return nil, fmt.Errorf("unknown type %s for %s", field.Kind(), key) } From f5cbcb08e65a039f1ad0c5d543cdf154c47496e1 Mon Sep 17 00:00:00 2001 From: Bruce MacDonald Date: Fri, 28 Jul 2023 11:29:00 -0400 Subject: [PATCH 2/3] specify stop params separately --- server/images.go | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/server/images.go b/server/images.go index 933fe26c..6bd7e882 100644 --- a/server/images.go +++ b/server/images.go @@ -14,7 +14,6 @@ import ( "path" "path/filepath" "reflect" - "regexp" "strconv" "strings" "text/template" @@ -203,7 +202,7 @@ func CreateModel(name string, path string, fn func(resp api.ProgressResponse)) e } var layers []*LayerReader - params := make(map[string]string) + params := make(map[string][]string) for _, c := range commands { log.Printf("[%s] - %s\n", c.Name, c.Args) @@ -287,8 +286,8 @@ func CreateModel(name string, path string, fn func(resp api.ProgressResponse)) e layer.MediaType = mediaType layers = append(layers, layer) default: - // runtime parameters - params[c.Name] = c.Args + // runtime parameters, build a list of args for each parameter to allow multiple values to be specified (ex: multiple stop tokens) + params[c.Name] = append(params[c.Name], c.Args) } } @@ -430,7 +429,7 @@ func GetLayerWithBufferFromLayer(layer *Layer) (*LayerReader, error) { return newLayer, nil } -func paramsToReader(params map[string]string) (io.ReadSeeker, error) { +func paramsToReader(params map[string][]string) (io.ReadSeeker, error) { opts := api.DefaultOptions() typeOpts := reflect.TypeOf(opts) @@ -445,42 +444,36 @@ func paramsToReader(params map[string]string) (io.ReadSeeker, error) { valueOpts := reflect.ValueOf(&opts).Elem() // iterate params and set values based on json struct tags - for key, val := range params { + for key, vals := range params { if opt, ok := jsonOpts[key]; ok { field := valueOpts.FieldByName(opt.Name) if field.IsValid() && field.CanSet() { switch field.Kind() { case reflect.Float32: - floatVal, err := strconv.ParseFloat(val, 32) + floatVal, err := strconv.ParseFloat(vals[0], 32) if err != nil { - return nil, fmt.Errorf("invalid float value %s", val) + return nil, fmt.Errorf("invalid float value %s", vals) } field.SetFloat(floatVal) case reflect.Int: - intVal, err := strconv.ParseInt(val, 10, 0) + intVal, err := strconv.ParseInt(vals[0], 10, 0) if err != nil { - return nil, fmt.Errorf("invalid int value %s", val) + return nil, fmt.Errorf("invalid int value %s", vals) } field.SetInt(intVal) case reflect.Bool: - boolVal, err := strconv.ParseBool(val) + boolVal, err := strconv.ParseBool(vals[0]) if err != nil { - return nil, fmt.Errorf("invalid bool value %s", val) + return nil, fmt.Errorf("invalid bool value %s", vals) } field.SetBool(boolVal) case reflect.String: - field.SetString(val) + field.SetString(vals[0]) case reflect.Slice: - re := regexp.MustCompile(`"(.*?)"`) // matches everything enclosed in quotes - vals := re.FindAllStringSubmatch(val, -1) - var sliceVal []string - for _, v := range vals { - sliceVal = append(sliceVal, v[1]) // v[1] is the captured group, v[0] is the entire match - } - field.Set(reflect.ValueOf(sliceVal)) + field.Set(reflect.ValueOf(vals)) default: return nil, fmt.Errorf("unknown type %s for %s", field.Kind(), key) } From 47bda0b86034465127f2654dcba73eaa27905d58 Mon Sep 17 00:00:00 2001 From: Bruce MacDonald Date: Fri, 28 Jul 2023 12:30:27 -0400 Subject: [PATCH 3/3] add stop to docs --- docs/modelfile.md | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/docs/modelfile.md b/docs/modelfile.md index 9c5f79b9..deca62b5 100644 --- a/docs/modelfile.md +++ b/docs/modelfile.md @@ -98,20 +98,21 @@ PARAMETER ### Valid Parameters and Values -| Parameter | Description | Value Type | Example Usage | -| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- | ------------------ | -| num_ctx | Sets the size of the context window used to generate the next token. (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 = num_ctx) | 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 | +| Parameter | Description | Value Type | Example Usage | +| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- | -------------------- | +| mirostat | Enable Mirostat sampling for controlling perplexity. (default: 0, 0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0) | int | mirostat 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 | +| 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 | +| num_ctx | Sets the size of the context window used to generate the next token. (Default: 2048) | int | num_ctx 4096 | +| 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 | +| 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 | +| repeat_last_n | Sets how far back for the model to look back to prevent repetition. (Default: 64, 0 = disabled, -1 = num_ctx) | 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 | +| temperature | The temperature of the model. Increasing the temperature will make the model answer more creatively. (Default: 0.8) | float | temperature 0.7 | +| stop | Sets the stop tokens to use. | string | stop "AI assistant:" | +| 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 | +| 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 | ### TEMPLATE