github.com/weaviate/weaviate@v1.24.6/modules/text2vec-transformers/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-transformers/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  	VectorizeObject(ctx context.Context, input string,
    37  		cfg ent.VectorizationConfig) (*ent.VectorizationResult, error)
    38  	VectorizeQuery(ctx context.Context, input string,
    39  		cfg 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  	VectorizeClassName() bool
    46  	VectorizePropertyName(propertyName string) bool
    47  	PoolingStrategy() string
    48  }
    49  
    50  func (v *Vectorizer) Object(ctx context.Context, object *models.Object,
    51  	comp moduletools.VectorizablePropsComparator, cfg moduletools.ClassConfig,
    52  ) ([]float32, models.AdditionalProperties, error) {
    53  	vec, err := v.object(ctx, object.Class, comp, cfg)
    54  	return vec, nil, err
    55  }
    56  
    57  func (v *Vectorizer) object(ctx context.Context, className string,
    58  	comp moduletools.VectorizablePropsComparator, cfg moduletools.ClassConfig,
    59  ) ([]float32, error) {
    60  	text, vector := v.objectVectorizer.TextsOrVector(ctx, className, comp, NewClassSettings(cfg), cfg.TargetVector())
    61  	if vector != nil {
    62  		// dont' re-vectorize
    63  		return vector, nil
    64  	}
    65  	// vectorize text
    66  	res, err := v.client.VectorizeObject(ctx, text, v.getVectorizationConfig(cfg))
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  
    71  	return res.Vector, nil
    72  }