github.com/weaviate/weaviate@v1.24.6/modules/reranker-cohere/config/class_settings.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package config 13 14 import ( 15 "github.com/pkg/errors" 16 "github.com/weaviate/weaviate/entities/models" 17 "github.com/weaviate/weaviate/entities/moduletools" 18 basesettings "github.com/weaviate/weaviate/usecases/modulecomponents/settings" 19 ) 20 21 const ( 22 modelProperty = "model" 23 ) 24 25 var availableCohereModels = []string{ 26 "rerank-english-v2.0", 27 "rerank-multilingual-v2.0", 28 } 29 30 // note it might not like this -- might want int values for e.g. MaxTokens 31 var ( 32 DefaultCohereModel = "rerank-multilingual-v2.0" 33 ) 34 35 type classSettings struct { 36 cfg moduletools.ClassConfig 37 propertyValuesHelper basesettings.PropertyValuesHelper 38 } 39 40 func NewClassSettings(cfg moduletools.ClassConfig) *classSettings { 41 return &classSettings{cfg: cfg, propertyValuesHelper: basesettings.NewPropertyValuesHelper("reranker-cohere")} 42 } 43 44 func (ic *classSettings) Validate(class *models.Class) error { 45 if ic.cfg == nil { 46 // we would receive a nil-config on cross-class requests, such as Explore{} 47 return errors.New("empty config") 48 } 49 model := ic.getStringProperty(modelProperty, DefaultCohereModel) 50 if model == nil || !ic.validateModel(*model) { 51 return errors.Errorf("wrong Cohere model name, available model names are: %v", availableCohereModels) 52 } 53 54 return nil 55 } 56 57 func (ic *classSettings) getStringProperty(name string, defaultValue string) *string { 58 asString := ic.propertyValuesHelper.GetPropertyAsStringWithNotExists(ic.cfg, name, "", defaultValue) 59 return &asString 60 } 61 62 func (ic *classSettings) validateModel(model string) bool { 63 return contains(availableCohereModels, model) 64 } 65 66 func (ic *classSettings) Model() string { 67 return *ic.getStringProperty(modelProperty, DefaultCohereModel) 68 } 69 70 func contains[T comparable](s []T, e T) bool { 71 for _, v := range s { 72 if v == e { 73 return true 74 } 75 } 76 return false 77 }