Spaces:
Running
Running
File size: 4,272 Bytes
b110593 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: [email protected]
//
package vectorizer
import (
"context"
"github.com/pkg/errors"
"github.com/go-openapi/strfmt"
"github.com/weaviate/weaviate/entities/models"
"github.com/weaviate/weaviate/entities/moduletools"
"github.com/weaviate/weaviate/modules/multi2vec-clip/ent"
libvectorizer "github.com/weaviate/weaviate/usecases/vectorizer"
)
type Vectorizer struct {
client Client
}
func New(client Client) *Vectorizer {
return &Vectorizer{
client: client,
}
}
type Client interface {
Vectorize(ctx context.Context,
texts, images []string) (*ent.VectorizationResult, error)
}
type ClassSettings interface {
ImageField(property string) bool
ImageFieldsWeights() ([]float32, error)
TextField(property string) bool
TextFieldsWeights() ([]float32, error)
}
func (v *Vectorizer) Object(ctx context.Context, object *models.Object,
objDiff *moduletools.ObjectDiff, settings ClassSettings,
) error {
vec, err := v.object(ctx, object.ID, object.Properties, objDiff, settings)
if err != nil {
return err
}
object.Vector = vec
return nil
}
func (v *Vectorizer) VectorizeImage(ctx context.Context, image string) ([]float32, error) {
res, err := v.client.Vectorize(ctx, []string{}, []string{image})
if err != nil {
return nil, err
}
if len(res.ImageVectors) != 1 {
return nil, errors.New("empty vector")
}
return res.ImageVectors[0], nil
}
func (v *Vectorizer) object(ctx context.Context, id strfmt.UUID,
schema interface{}, objDiff *moduletools.ObjectDiff, ichek ClassSettings,
) ([]float32, error) {
vectorize := objDiff == nil || objDiff.GetVec() == nil
// vectorize image and text
texts := []string{}
images := []string{}
if schema != nil {
for prop, value := range schema.(map[string]interface{}) {
if ichek.ImageField(prop) {
valueString, ok := value.(string)
if ok {
images = append(images, valueString)
vectorize = vectorize || (objDiff != nil && objDiff.IsChangedProp(prop))
}
}
if ichek.TextField(prop) {
valueString, ok := value.(string)
if ok {
texts = append(texts, valueString)
vectorize = vectorize || (objDiff != nil && objDiff.IsChangedProp(prop))
}
}
valueArr, ok := value.([]interface{})
if ok {
for _, value := range valueArr {
valueString, ok := value.(string)
if ok {
texts = append(texts, valueString)
vectorize = vectorize || (objDiff != nil && objDiff.IsChangedProp(prop))
}
}
}
}
}
// no property was changed, old vector can be used
if !vectorize {
return objDiff.GetVec(), nil
}
vectors := [][]float32{}
if len(texts) > 0 || len(images) > 0 {
res, err := v.client.Vectorize(ctx, texts, images)
if err != nil {
return nil, err
}
vectors = append(vectors, res.TextVectors...)
vectors = append(vectors, res.ImageVectors...)
}
weights, err := v.getWeights(ichek)
if err != nil {
return nil, err
}
return libvectorizer.CombineVectorsWithWeights(vectors, weights), nil
}
func (v *Vectorizer) getWeights(ichek ClassSettings) ([]float32, error) {
weights := []float32{}
textFieldsWeights, err := ichek.TextFieldsWeights()
if err != nil {
return nil, err
}
imageFieldsWeights, err := ichek.ImageFieldsWeights()
if err != nil {
return nil, err
}
weights = append(weights, textFieldsWeights...)
weights = append(weights, imageFieldsWeights...)
normalizedWeights := v.normalizeWeights(weights)
return normalizedWeights, nil
}
func (v *Vectorizer) normalizeWeights(weights []float32) []float32 {
if len(weights) > 0 {
var denominator float32
for i := range weights {
denominator += weights[i]
}
normalizer := 1 / denominator
normalized := make([]float32, len(weights))
for i := range weights {
normalized[i] = weights[i] * normalizer
}
return normalized
}
return nil
}
|