github.com/weaviate/weaviate@v1.24.6/modules/generative-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 baseURLProperty = "baseURL" 23 modelProperty = "model" 24 temperatureProperty = "temperature" 25 maxTokensProperty = "maxTokens" 26 kProperty = "k" 27 stopSequencesProperty = "stopSequences" 28 returnLikelihoodsProperty = "returnLikelihoods" 29 ) 30 31 var availableCohereModels = []string{ 32 "command-xlarge-beta", 33 "command-xlarge", "command-medium", "command-xlarge-nightly", "command-medium-nightly", "xlarge", "medium", 34 "command", "command-light", "command-nightly", "command-light-nightly", "base", "base-light", 35 } 36 37 // note it might not like this -- might want int values for e.g. MaxTokens 38 var ( 39 DefaultBaseURL = "https://api.cohere.ai" 40 DefaultCohereModel = "command-nightly" 41 DefaultCohereTemperature = 0 42 DefaultCohereMaxTokens = 2048 43 DefaultCohereK = 0 44 DefaultCohereStopSequences = []string{} 45 DefaultCohereReturnLikelihoods = "NONE" 46 ) 47 48 type classSettings struct { 49 cfg moduletools.ClassConfig 50 propertyValuesHelper basesettings.PropertyValuesHelper 51 } 52 53 func NewClassSettings(cfg moduletools.ClassConfig) *classSettings { 54 return &classSettings{cfg: cfg, propertyValuesHelper: basesettings.NewPropertyValuesHelper("generative-cohere")} 55 } 56 57 func (ic *classSettings) Validate(class *models.Class) error { 58 if ic.cfg == nil { 59 // we would receive a nil-config on cross-class requests, such as Explore{} 60 return errors.New("empty config") 61 } 62 model := ic.getStringProperty(modelProperty, DefaultCohereModel) 63 if model == nil || !ic.validateModel(*model) { 64 return errors.Errorf("wrong Cohere model name, available model names are: %v", availableCohereModels) 65 } 66 67 return nil 68 } 69 70 func (ic *classSettings) getStringProperty(name, defaultValue string) *string { 71 asString := ic.propertyValuesHelper.GetPropertyAsStringWithNotExists(ic.cfg, name, "", defaultValue) 72 return &asString 73 } 74 75 func (ic *classSettings) getIntProperty(name string, defaultValue *int) *int { 76 var wrongVal int = -1 77 return ic.propertyValuesHelper.GetPropertyAsIntWithNotExists(ic.cfg, name, &wrongVal, defaultValue) 78 } 79 80 func (ic *classSettings) getListOfStringsProperty(name string, defaultValue []string) *[]string { 81 if ic.cfg == nil { 82 // we would receive a nil-config on cross-class requests, such as Explore{} 83 return &defaultValue 84 } 85 86 model, ok := ic.cfg.ClassByModuleName("generative-cohere")[name] 87 if ok { 88 asStringList, ok := model.([]string) 89 if ok { 90 return &asStringList 91 } 92 var empty []string 93 return &empty 94 } 95 return &defaultValue 96 } 97 98 func (ic *classSettings) GetMaxTokensForModel(model string) int { 99 return DefaultCohereMaxTokens 100 } 101 102 func (ic *classSettings) validateModel(model string) bool { 103 return contains(availableCohereModels, model) 104 } 105 106 func (ic *classSettings) BaseURL() string { 107 return *ic.getStringProperty(baseURLProperty, DefaultBaseURL) 108 } 109 110 func (ic *classSettings) Model() string { 111 return *ic.getStringProperty(modelProperty, DefaultCohereModel) 112 } 113 114 func (ic *classSettings) MaxTokens() int { 115 return *ic.getIntProperty(maxTokensProperty, &DefaultCohereMaxTokens) 116 } 117 118 func (ic *classSettings) Temperature() int { 119 return *ic.getIntProperty(temperatureProperty, &DefaultCohereTemperature) 120 } 121 122 func (ic *classSettings) K() int { 123 return *ic.getIntProperty(kProperty, &DefaultCohereK) 124 } 125 126 func (ic *classSettings) StopSequences() []string { 127 return *ic.getListOfStringsProperty(stopSequencesProperty, DefaultCohereStopSequences) 128 } 129 130 func (ic *classSettings) ReturnLikelihoods() string { 131 return *ic.getStringProperty(returnLikelihoodsProperty, DefaultCohereReturnLikelihoods) 132 } 133 134 func contains[T comparable](s []T, e T) bool { 135 for _, v := range s { 136 if v == e { 137 return true 138 } 139 } 140 return false 141 }