(data string, logo string)
| 89 | } |
| 90 | |
| 91 | func NewModel(data string, logo string) (IModel, error) { |
| 92 | var cfg entity.AIModel |
| 93 | err := yaml.Unmarshal([]byte(data), &cfg) |
| 94 | if err != nil { |
| 95 | return nil, err |
| 96 | } |
| 97 | params := make(ParamValidator, 0, len(cfg.ParameterRules)) |
| 98 | defaultConfig := make(map[string]interface{}) |
| 99 | for _, p := range cfg.ParameterRules { |
| 100 | if (p.Default == nil || p.Default == "" || p.Default == 0 || p.Default == false) && !p.Required { |
| 101 | defaultConfig[p.Name] = nil |
| 102 | continue |
| 103 | } |
| 104 | t := p.Type |
| 105 | if t == "" { |
| 106 | t = ParameterTypeStr |
| 107 | } |
| 108 | switch t { |
| 109 | case ParameterTypeInt: |
| 110 | switch t := p.Default.(type) { |
| 111 | case int: |
| 112 | defaultConfig[p.Name] = t |
| 113 | case float64: |
| 114 | defaultConfig[p.Name] = int(t) |
| 115 | default: |
| 116 | defaultConfig[p.Name] = 0 |
| 117 | } |
| 118 | case ParameterTypeStr: |
| 119 | switch t := p.Default.(type) { |
| 120 | case int: |
| 121 | defaultConfig[p.Name] = t |
| 122 | case float64: |
| 123 | defaultConfig[p.Name] = int(t) |
| 124 | case string: |
| 125 | defaultConfig[p.Name] = t |
| 126 | case bool: |
| 127 | defaultConfig[p.Name] = strconv.FormatBool(t) |
| 128 | default: |
| 129 | defaultConfig[p.Name] = "" |
| 130 | } |
| 131 | case ParameterTypeFloat: |
| 132 | switch t := p.Default.(type) { |
| 133 | case int: |
| 134 | defaultConfig[p.Name] = float64(t) |
| 135 | case float64: |
| 136 | defaultConfig[p.Name] = t |
| 137 | default: |
| 138 | defaultConfig[p.Name] = float64(0) |
| 139 | } |
| 140 | case ParameterTypeBool: |
| 141 | switch t := p.Default.(type) { |
| 142 | case bool: |
| 143 | defaultConfig[p.Name] = t |
| 144 | case string: |
| 145 | defaultConfig[p.Name] = t == "true" |
| 146 | default: |
| 147 | defaultConfig[p.Name] = false |
| 148 | } |
no test coverage detected