readModelConfigsFromFile reads a config file that may contain either a single ModelConfig or an array of ModelConfigs. It tries to unmarshal as an array first, then falls back to a single config if that fails.
(file string, opts ...ConfigLoaderOption)
| 81 | // ModelConfig or an array of ModelConfigs. It tries to unmarshal as an array first, |
| 82 | // then falls back to a single config if that fails. |
| 83 | func readModelConfigsFromFile(file string, opts ...ConfigLoaderOption) ([]*ModelConfig, error) { |
| 84 | f, err := os.ReadFile(file) |
| 85 | if err != nil { |
| 86 | return nil, fmt.Errorf("readModelConfigsFromFile cannot read config file %q: %w", file, err) |
| 87 | } |
| 88 | |
| 89 | // Try to unmarshal as array first |
| 90 | var configs []*ModelConfig |
| 91 | if err := yaml.Unmarshal(f, &configs); err == nil && len(configs) > 0 { |
| 92 | for _, cc := range configs { |
| 93 | cc.modelConfigFile = file |
| 94 | cc.SetDefaults(opts...) |
| 95 | cc.syncKnownUsecasesFromString() |
| 96 | } |
| 97 | return configs, nil |
| 98 | } |
| 99 | |
| 100 | // Fall back to single config |
| 101 | c := &ModelConfig{} |
| 102 | if err := yaml.Unmarshal(f, c); err != nil { |
| 103 | return nil, fmt.Errorf("readModelConfigsFromFile cannot unmarshal config file %q: %w", file, err) |
| 104 | } |
| 105 | |
| 106 | c.modelConfigFile = file |
| 107 | c.syncKnownUsecasesFromString() |
| 108 | c.SetDefaults(opts...) |
| 109 | |
| 110 | return []*ModelConfig{c}, nil |
| 111 | } |
| 112 | |
| 113 | // Load a config file for a model |
| 114 | func (bcl *ModelConfigLoader) LoadModelConfigFileByName(modelName, modelPath string, opts ...ConfigLoaderOption) (*ModelConfig, error) { |
no test coverage detected