github.com/weaviate/weaviate@v1.24.6/modules/text2vec-palm/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 "fmt" 17 18 "github.com/weaviate/weaviate/entities/models" 19 "github.com/weaviate/weaviate/entities/moduletools" 20 "github.com/weaviate/weaviate/modules/text2vec-palm/ent" 21 objectsvectorizer "github.com/weaviate/weaviate/usecases/modulecomponents/vectorizer" 22 libvectorizer "github.com/weaviate/weaviate/usecases/vectorizer" 23 ) 24 25 type Vectorizer struct { 26 client Client 27 objectVectorizer *objectsvectorizer.ObjectVectorizer 28 } 29 30 func New(client Client) *Vectorizer { 31 return &Vectorizer{ 32 client: client, 33 objectVectorizer: objectsvectorizer.New(), 34 } 35 } 36 37 type Client interface { 38 Vectorize(ctx context.Context, input []string, 39 config ent.VectorizationConfig, titlePropertyValue string) (*ent.VectorizationResult, error) 40 VectorizeQuery(ctx context.Context, input []string, 41 config ent.VectorizationConfig) (*ent.VectorizationResult, error) 42 } 43 44 // IndexCheck returns whether a property of a class should be indexed 45 type ClassSettings interface { 46 PropertyIndexed(property string) bool 47 VectorizePropertyName(propertyName string) bool 48 VectorizeClassName() bool 49 ApiEndpoint() string 50 ProjectID() string 51 ModelID() string 52 TitleProperty() string 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 icheck := NewClassSettings(cfg) 66 67 corpi, titlePropertyValue, vector := v.objectVectorizer.TextsOrVectorWithTitleProperty(ctx, 68 className, comp, icheck, icheck.TitleProperty(), cfg.TargetVector()) 69 if vector != nil { 70 // dont' re-vectorize 71 return vector, nil 72 } 73 // vectorize text 74 res, err := v.client.Vectorize(ctx, []string{corpi}, ent.VectorizationConfig{ 75 ApiEndpoint: icheck.ApiEndpoint(), 76 ProjectID: icheck.ProjectID(), 77 Model: icheck.ModelID(), 78 }, titlePropertyValue) 79 if err != nil { 80 return nil, err 81 } 82 if len(res.Vectors) == 0 { 83 return nil, fmt.Errorf("no vectors generated") 84 } 85 86 if len(res.Vectors) > 1 { 87 return libvectorizer.CombineVectors(res.Vectors), nil 88 } 89 return res.Vectors[0], nil 90 }