github.com/weaviate/weaviate@v1.24.6/modules/generative-mistral/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  )
    27  
    28  var availableMistralModels = []string{
    29  	"open-mistral-7b", "mistral-tiny-2312", "mistral-tiny", "open-mixtral-8x7b",
    30  	"mistral-small-2312", "mistral-small", "mistral-small-2402", "mistral-small-latest",
    31  	"mistral-medium-latest", "mistral-medium-2312", "mistral-medium", "mistral-large-latest",
    32  	"mistral-large-2402",
    33  }
    34  
    35  // note it might not like this -- might want int values for e.g. MaxTokens
    36  var (
    37  	DefaultBaseURL            = "https://api.mistral.ai"
    38  	DefaultMistralModel       = "open-mistral-7b"
    39  	DefaultMistralTemperature = 0
    40  	DefaultMistralMaxTokens   = 2048
    41  )
    42  
    43  type classSettings struct {
    44  	cfg                  moduletools.ClassConfig
    45  	propertyValuesHelper basesettings.PropertyValuesHelper
    46  }
    47  
    48  func NewClassSettings(cfg moduletools.ClassConfig) *classSettings {
    49  	return &classSettings{cfg: cfg, propertyValuesHelper: basesettings.NewPropertyValuesHelper("generative-mistral")}
    50  }
    51  
    52  func (ic *classSettings) Validate(class *models.Class) error {
    53  	if ic.cfg == nil {
    54  		// we would receive a nil-config on cross-class requests, such as Explore{}
    55  		return errors.New("empty config")
    56  	}
    57  	model := ic.getStringProperty(modelProperty, DefaultMistralModel)
    58  	if model == nil || !ic.validateModel(*model) {
    59  		return errors.Errorf("wrong Mistral model name, available model names are: %v", availableMistralModels)
    60  	}
    61  
    62  	return nil
    63  }
    64  
    65  func (ic *classSettings) getStringProperty(name, defaultValue string) *string {
    66  	asString := ic.propertyValuesHelper.GetPropertyAsStringWithNotExists(ic.cfg, name, "", defaultValue)
    67  	return &asString
    68  }
    69  
    70  func (ic *classSettings) getIntProperty(name string, defaultValue *int) *int {
    71  	var wrongVal int = -1
    72  	return ic.propertyValuesHelper.GetPropertyAsIntWithNotExists(ic.cfg, name, &wrongVal, defaultValue)
    73  }
    74  
    75  func (ic *classSettings) GetMaxTokensForModel(model string) int {
    76  	return DefaultMistralMaxTokens
    77  }
    78  
    79  func (ic *classSettings) validateModel(model string) bool {
    80  	return contains(availableMistralModels, model)
    81  }
    82  
    83  func (ic *classSettings) BaseURL() string {
    84  	return *ic.getStringProperty(baseURLProperty, DefaultBaseURL)
    85  }
    86  
    87  func (ic *classSettings) Model() string {
    88  	return *ic.getStringProperty(modelProperty, DefaultMistralModel)
    89  }
    90  
    91  func (ic *classSettings) MaxTokens() int {
    92  	return *ic.getIntProperty(maxTokensProperty, &DefaultMistralMaxTokens)
    93  }
    94  
    95  func (ic *classSettings) Temperature() int {
    96  	return *ic.getIntProperty(temperatureProperty, &DefaultMistralTemperature)
    97  }
    98  
    99  func contains[T comparable](s []T, e T) bool {
   100  	for _, v := range s {
   101  		if v == e {
   102  			return true
   103  		}
   104  	}
   105  	return false
   106  }