github.com/weaviate/weaviate@v1.24.6/modules/generative-anyscale/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  )
    26  
    27  var availableAnyscaleModels = []string{
    28  	"meta-llama/Llama-2-70b-chat-hf",
    29  	"meta-llama/Llama-2-13b-chat-hf",
    30  	"meta-llama/Llama-2-7b-chat-hf",
    31  	"codellama/CodeLlama-34b-Instruct-hf",
    32  	"mistralai/Mistral-7B-Instruct-v0.1",
    33  	"mistralai/Mixtral-8x7B-Instruct-v0.1",
    34  }
    35  
    36  // note we might want to separate the baseURL and completions URL in the future. Fine-tuned models also use this URL. 12/3/23
    37  var (
    38  	DefaultBaseURL             = "https://api.endpoints.anyscale.com"
    39  	DefaultAnyscaleModel       = "meta-llama/Llama-2-70b-chat-hf"
    40  	DefaultAnyscaleTemperature = 0
    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-anyscale")}
    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, DefaultAnyscaleModel)
    58  	if model == nil || !ic.validateModel(*model) {
    59  		return errors.Errorf("wrong Anyscale model name, available model names are: %v", availableAnyscaleModels)
    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) validateModel(model string) bool {
    76  	return contains(availableAnyscaleModels, model)
    77  }
    78  
    79  func (ic *classSettings) BaseURL() string {
    80  	return *ic.getStringProperty(baseURLProperty, DefaultBaseURL)
    81  }
    82  
    83  func (ic *classSettings) Model() string {
    84  	return *ic.getStringProperty(modelProperty, DefaultAnyscaleModel)
    85  }
    86  
    87  func (ic *classSettings) Temperature() int {
    88  	return *ic.getIntProperty(temperatureProperty, &DefaultAnyscaleTemperature)
    89  }
    90  
    91  func contains[T comparable](s []T, e T) bool {
    92  	for _, v := range s {
    93  		if v == e {
    94  			return true
    95  		}
    96  	}
    97  	return false
    98  }