github.com/weaviate/weaviate@v1.24.6/modules/text2vec-cohere/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-cohere/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) (*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 Model() string 50 Truncate() string 51 BaseURL() string 52 } 53 54 func (v *Vectorizer) Object(ctx context.Context, object *models.Object, 55 comp moduletools.VectorizablePropsComparator, cfg moduletools.ClassConfig, 56 ) ([]float32, models.AdditionalProperties, error) { 57 vec, err := v.object(ctx, object.Class, comp, cfg) 58 return vec, nil, err 59 } 60 61 func (v *Vectorizer) object(ctx context.Context, className string, 62 comp moduletools.VectorizablePropsComparator, cfg moduletools.ClassConfig, 63 ) ([]float32, error) { 64 text, vector := v.objectVectorizer.TextsOrVector(ctx, className, comp, NewClassSettings(cfg), cfg.TargetVector()) 65 if vector != nil { 66 // dont' re-vectorize 67 return vector, nil 68 } 69 // vectorize text 70 icheck := NewClassSettings(cfg) 71 res, err := v.client.Vectorize(ctx, []string{text}, ent.VectorizationConfig{ 72 Model: icheck.Model(), 73 BaseURL: icheck.BaseURL(), 74 }) 75 if err != nil { 76 return nil, err 77 } 78 if len(res.Vectors) == 0 { 79 return nil, fmt.Errorf("no vectors generated") 80 } 81 82 if len(res.Vectors) > 1 { 83 return libvectorizer.CombineVectors(res.Vectors), nil 84 } 85 return res.Vectors[0], nil 86 }