github.com/weaviate/weaviate@v1.24.6/modules/text2vec-huggingface/vectorizer/objects.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 vectorizer
    13  
    14  import (
    15  	"context"
    16  
    17  	"github.com/weaviate/weaviate/entities/models"
    18  	"github.com/weaviate/weaviate/entities/moduletools"
    19  	"github.com/weaviate/weaviate/modules/text2vec-huggingface/ent"
    20  	objectsvectorizer "github.com/weaviate/weaviate/usecases/modulecomponents/vectorizer"
    21  )
    22  
    23  type Vectorizer struct {
    24  	client           Client
    25  	objectVectorizer *objectsvectorizer.ObjectVectorizer
    26  }
    27  
    28  func New(client Client) *Vectorizer {
    29  	return &Vectorizer{
    30  		client:           client,
    31  		objectVectorizer: objectsvectorizer.New(),
    32  	}
    33  }
    34  
    35  type Client interface {
    36  	Vectorize(ctx context.Context, input string,
    37  		config ent.VectorizationConfig) (*ent.VectorizationResult, error)
    38  	VectorizeQuery(ctx context.Context, input string,
    39  		config ent.VectorizationConfig) (*ent.VectorizationResult, error)
    40  }
    41  
    42  // IndexCheck returns whether a property of a class should be indexed
    43  type ClassSettings interface {
    44  	PropertyIndexed(property string) bool
    45  	VectorizePropertyName(propertyName string) bool
    46  	VectorizeClassName() bool
    47  	EndpointURL() string
    48  	PassageModel() string
    49  	QueryModel() string
    50  	OptionWaitForModel() bool
    51  	OptionUseGPU() bool
    52  	OptionUseCache() bool
    53  }
    54  
    55  func (v *Vectorizer) Object(ctx context.Context, object *models.Object,
    56  	comp moduletools.VectorizablePropsComparator, cfg moduletools.ClassConfig,
    57  ) ([]float32, models.AdditionalProperties, error) {
    58  	vec, err := v.object(ctx, object.Class, comp, cfg)
    59  	return vec, nil, err
    60  }
    61  
    62  func (v *Vectorizer) object(ctx context.Context, className string,
    63  	comp moduletools.VectorizablePropsComparator, cfg moduletools.ClassConfig,
    64  ) ([]float32, error) {
    65  	text, vector := v.objectVectorizer.TextsOrVector(ctx, className, comp, NewClassSettings(cfg), cfg.TargetVector())
    66  	if vector != nil {
    67  		// dont' re-vectorize
    68  		return vector, nil
    69  	}
    70  	// vectorize text
    71  	icheck := NewClassSettings(cfg)
    72  	res, err := v.client.Vectorize(ctx, text, ent.VectorizationConfig{
    73  		EndpointURL:  icheck.EndpointURL(),
    74  		Model:        icheck.PassageModel(),
    75  		WaitForModel: icheck.OptionWaitForModel(),
    76  		UseGPU:       icheck.OptionUseGPU(),
    77  		UseCache:     icheck.OptionUseCache(),
    78  	})
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	return res.Vector, nil
    84  }