read runner parameter options from map

- read runner options from map to see what was specified explicitly and overwrite zero values
This commit is contained in:
Bruce MacDonald
2023-08-01 13:36:31 -04:00
parent daa0d1de7a
commit 1c5a8770ee
5 changed files with 102 additions and 38 deletions

View File

@@ -19,7 +19,6 @@ import (
"github.com/jmorganca/ollama/api"
"github.com/jmorganca/ollama/parser"
"github.com/mitchellh/mapstructure"
)
type RegistryOptions struct {
@@ -34,7 +33,7 @@ type Model struct {
Template string
System string
Digest string
Options api.Options
Options map[string]interface{}
}
func (m *Model) Prompt(request api.GenerateRequest) (string, error) {
@@ -178,14 +177,7 @@ func GetModel(name string) (*Model, error) {
defer params.Close()
// parse model options parameters into a map so that we can see which fields have been specified explicitly
// TODO: once there are no modelfiles in the wild that do not have default options populated this can be removed
var opts map[string]interface{}
if err = json.NewDecoder(params).Decode(&opts); err != nil {
return nil, err
}
// update the default options on the model with the options that have been specified
if err := mapstructure.Decode(opts, &model.Options); err != nil {
if err = json.NewDecoder(params).Decode(&model.Options); err != nil {
return nil, err
}
}
@@ -448,11 +440,13 @@ func GetLayerWithBufferFromLayer(layer *Layer) (*LayerReader, error) {
return newLayer, nil
}
// paramsToReader converts specified parameter options to their correct types, and returns a reader for the json
func paramsToReader(params map[string][]string) (io.ReadSeeker, error) {
opts := api.DefaultOptions()
typeOpts := reflect.TypeOf(opts)
opts := api.Options{}
valueOpts := reflect.ValueOf(&opts).Elem() // names of the fields in the options struct
typeOpts := reflect.TypeOf(opts) // types of the fields in the options struct
// build map of json struct tags
// build map of json struct tags to their types
jsonOpts := make(map[string]reflect.StructField)
for _, field := range reflect.VisibleFields(typeOpts) {
jsonTag := strings.Split(field.Tag.Get("json"), ",")[0]
@@ -461,7 +455,7 @@ func paramsToReader(params map[string][]string) (io.ReadSeeker, error) {
}
}
valueOpts := reflect.ValueOf(&opts).Elem()
out := make(map[string]interface{})
// iterate params and set values based on json struct tags
for key, vals := range params {
if opt, ok := jsonOpts[key]; ok {
@@ -474,25 +468,26 @@ func paramsToReader(params map[string][]string) (io.ReadSeeker, error) {
return nil, fmt.Errorf("invalid float value %s", vals)
}
field.SetFloat(floatVal)
out[key] = floatVal
case reflect.Int:
intVal, err := strconv.ParseInt(vals[0], 10, 0)
if err != nil {
return nil, fmt.Errorf("invalid int value %s", vals)
}
field.SetInt(intVal)
out[key] = intVal
case reflect.Bool:
boolVal, err := strconv.ParseBool(vals[0])
if err != nil {
return nil, fmt.Errorf("invalid bool value %s", vals)
}
field.SetBool(boolVal)
out[key] = boolVal
case reflect.String:
field.SetString(vals[0])
out[key] = vals[0]
case reflect.Slice:
field.Set(reflect.ValueOf(vals))
// TODO: only string slices are supported right now
out[key] = vals
default:
return nil, fmt.Errorf("unknown type %s for %s", field.Kind(), key)
}
@@ -500,12 +495,6 @@ func paramsToReader(params map[string][]string) (io.ReadSeeker, error) {
}
}
// convert opts to map so that zero fields are not omitted
out := make(map[string]interface{})
if err := mapstructure.Decode(opts, &out); err != nil {
return nil, err
}
bts, err := json.Marshal(out)
if err != nil {
return nil, err

View File

@@ -15,7 +15,6 @@ import (
"sync"
"time"
"dario.cat/mergo"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
@@ -59,8 +58,14 @@ func GenerateHandler(c *gin.Context) {
loaded.llm = nil
}
opts := model.Options
if err := mergo.Merge(&opts, req.Options, mergo.WithOverride); err != nil {
opts := api.DefaultOptions()
if err := opts.FromMap(model.Options); err != nil {
log.Printf("could not load model options: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if err := opts.FromMap(req.Options); err != nil {
log.Printf("could not merge model options: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}