Spaces:
Running
Running
File size: 4,924 Bytes
b110593 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: [email protected]
//
package config
import (
"encoding/json"
"github.com/pkg/errors"
"github.com/weaviate/weaviate/entities/models"
"github.com/weaviate/weaviate/entities/moduletools"
)
const (
baseURLProperty = "baseURL"
modelProperty = "model"
temperatureProperty = "temperature"
maxTokensProperty = "maxTokens"
kProperty = "k"
stopSequencesProperty = "stopSequences"
returnLikelihoodsProperty = "returnLikelihoods"
)
var availableCohereModels = []string{
"command-xlarge-beta",
"command-xlarge", "command-medium", "command-xlarge-nightly", "command-medium-nightly", "xlarge", "medium",
"command", "command-light", "command-nightly", "command-light-nightly", "base", "base-light",
}
// note it might not like this -- might want int values for e.g. MaxTokens
var (
DefaultBaseURL = "https://api.cohere.ai"
DefaultCohereModel = "command-nightly"
DefaultCohereTemperature = 0
DefaultCohereMaxTokens = 2048
DefaultCohereK = 0
DefaultCohereStopSequences = []string{}
DefaultCohereReturnLikelihoods = "NONE"
)
type classSettings struct {
cfg moduletools.ClassConfig
}
func NewClassSettings(cfg moduletools.ClassConfig) *classSettings {
return &classSettings{cfg: cfg}
}
func (ic *classSettings) Validate(class *models.Class) error {
if ic.cfg == nil {
// we would receive a nil-config on cross-class requests, such as Explore{}
return errors.New("empty config")
}
model := ic.getStringProperty(modelProperty, DefaultCohereModel)
if model == nil || !ic.validateModel(*model) {
return errors.Errorf("wrong Cohere model name, available model names are: %v", availableCohereModels)
}
return nil
}
func (ic *classSettings) getStringProperty(name, defaultValue string) *string {
if ic.cfg == nil {
// we would receive a nil-config on cross-class requests, such as Explore{}
return &defaultValue
}
model, ok := ic.cfg.ClassByModuleName("generative-cohere")[name]
if ok {
asString, ok := model.(string)
if ok {
return &asString
}
var empty string
return &empty
}
return &defaultValue
}
func (ic *classSettings) getIntProperty(name string, defaultValue *int) *int {
if ic.cfg == nil {
// we would receive a nil-config on cross-class requests, such as Explore{}
return defaultValue
}
val, ok := ic.cfg.ClassByModuleName("generative-cohere")[name]
if ok {
asInt, ok := val.(int)
if ok {
return &asInt
}
asFloat, ok := val.(float64)
if ok {
asInt := int(asFloat)
return &asInt
}
asNumber, ok := val.(json.Number)
if ok {
asFloat, _ := asNumber.Float64()
asInt := int(asFloat)
return &asInt
}
var wrongVal int = -1
return &wrongVal
}
if defaultValue != nil {
return defaultValue
}
return nil
}
func (ic *classSettings) getListOfStringsProperty(name string, defaultValue []string) *[]string {
if ic.cfg == nil {
// we would receive a nil-config on cross-class requests, such as Explore{}
return &defaultValue
}
model, ok := ic.cfg.ClassByModuleName("generative-cohere")[name]
if ok {
asStringList, ok := model.([]string)
if ok {
return &asStringList
}
var empty []string
return &empty
}
return &defaultValue
}
func (ic *classSettings) GetMaxTokensForModel(model string) int {
return DefaultCohereMaxTokens
}
func (ic *classSettings) validateModel(model string) bool {
return contains(availableCohereModels, model)
}
func (ic *classSettings) BaseURL() string {
return *ic.getStringProperty(baseURLProperty, DefaultBaseURL)
}
func (ic *classSettings) Model() string {
return *ic.getStringProperty(modelProperty, DefaultCohereModel)
}
func (ic *classSettings) MaxTokens() int {
return *ic.getIntProperty(maxTokensProperty, &DefaultCohereMaxTokens)
}
func (ic *classSettings) Temperature() int {
return *ic.getIntProperty(temperatureProperty, &DefaultCohereTemperature)
}
func (ic *classSettings) K() int {
return *ic.getIntProperty(kProperty, &DefaultCohereK)
}
func (ic *classSettings) StopSequences() []string {
return *ic.getListOfStringsProperty(stopSequencesProperty, DefaultCohereStopSequences)
}
func (ic *classSettings) ReturnLikelihoods() string {
return *ic.getStringProperty(returnLikelihoodsProperty, DefaultCohereReturnLikelihoods)
}
func contains[T comparable](s []T, e T) bool {
for _, v := range s {
if v == e {
return true
}
}
return false
}
|