File size: 6,841 Bytes
5ed9edf | 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | // _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2025 Weaviate B.V. All rights reserved.
//
// CONTACT: hello@weaviate.io
//
package modules
import (
"context"
"fmt"
"github.com/weaviate/weaviate/entities/additional"
"github.com/weaviate/weaviate/entities/dto"
"github.com/weaviate/weaviate/entities/models"
"github.com/weaviate/weaviate/entities/modulecapabilities"
"github.com/weaviate/weaviate/entities/moduletools"
"github.com/weaviate/weaviate/entities/schema"
"github.com/weaviate/weaviate/entities/search"
)
func reVectorize(ctx context.Context,
cfg moduletools.ClassConfig,
mod modulecapabilities.Vectorizer[[]float32],
object *models.Object,
class *models.Class,
sourceProperties []string,
targetVector string,
findObjectFn modulecapabilities.FindObjectFn,
reVectorizeDisabled bool,
) (bool, models.AdditionalProperties, []float32, error) {
if reVectorizeDisabled {
return true, nil, nil, nil
}
shouldReVectorize, oldObject := reVectorizeEmbeddings(ctx, cfg, mod, object, class, sourceProperties, findObjectFn)
if shouldReVectorize {
return shouldReVectorize, nil, nil, nil
}
if targetVector == "" {
return false, oldObject.AdditionalProperties, oldObject.Vector, nil
} else {
vector, err := getVector(oldObject.Vectors[targetVector])
if err != nil {
return false, nil, nil, fmt.Errorf("get vector: %w", err)
}
return false, oldObject.AdditionalProperties, vector, nil
}
}
func getVector(v models.Vector) ([]float32, error) {
switch vector := v.(type) {
case nil:
return nil, nil
case []float32:
return vector, nil
default:
return nil, fmt.Errorf("unrecognized vector type: %T", v)
}
}
func reVectorizeMulti(ctx context.Context,
cfg moduletools.ClassConfig,
mod modulecapabilities.Vectorizer[[][]float32],
object *models.Object,
class *models.Class,
sourceProperties []string,
targetVector string,
findObjectFn modulecapabilities.FindObjectFn,
reVectorizeDisabled bool,
) (bool, models.AdditionalProperties, [][]float32, error) {
if reVectorizeDisabled {
return true, nil, nil, nil
}
shouldReVectorize, oldObject := reVectorizeEmbeddings(ctx, cfg, mod, object, class, sourceProperties, findObjectFn)
if shouldReVectorize {
return shouldReVectorize, nil, nil, nil
}
if targetVector == "" {
return false, oldObject.AdditionalProperties, nil, nil
} else {
multiVector, err := getMultiVector(oldObject.Vectors[targetVector])
if err != nil {
return false, nil, nil, fmt.Errorf("get multi vector: %w", err)
}
return false, oldObject.AdditionalProperties, multiVector, nil
}
}
func getMultiVector(v models.Vector) ([][]float32, error) {
switch vector := v.(type) {
case nil:
return nil, nil
case [][]float32:
return vector, nil
default:
return nil, fmt.Errorf("unrecognized multi vector type: %T", v)
}
}
func reVectorizeEmbeddings[T dto.Embedding](ctx context.Context,
cfg moduletools.ClassConfig,
mod modulecapabilities.Vectorizer[T],
object *models.Object,
class *models.Class,
sourceProperties []string,
findObjectFn modulecapabilities.FindObjectFn,
) (bool, *search.Result) {
textProps, mediaProps, err := mod.VectorizableProperties(cfg)
if err != nil {
return true, nil
}
type compareProps struct {
Name string
IsArray bool
}
propsToCompare := make([]compareProps, 0)
var sourcePropsSet map[string]struct{} = nil
if len(sourceProperties) > 0 {
sourcePropsSet = make(map[string]struct{}, len(sourceProperties))
for _, sourceProp := range sourceProperties {
sourcePropsSet[sourceProp] = struct{}{}
}
}
mediaPropsSet := make(map[string]struct{}, len(mediaProps))
for _, mediaProp := range mediaProps {
mediaPropsSet[mediaProp] = struct{}{}
}
for _, prop := range class.Properties {
if len(prop.DataType) > 1 {
continue // multi cref
}
// for named vectors with explicit source properties, skip if not in the list
if sourcePropsSet != nil {
if _, ok := sourcePropsSet[prop.Name]; !ok {
continue
}
}
if prop.ModuleConfig != nil {
if modConfig, ok := prop.ModuleConfig.(map[string]interface{})[class.Vectorizer]; ok {
if skip, ok2 := modConfig.(map[string]interface{})["skip"]; ok2 && skip == true {
continue
}
}
}
if prop.DataType[0] == schema.DataTypeText.String() && textProps {
propsToCompare = append(propsToCompare, compareProps{Name: prop.Name, IsArray: false})
continue
}
if prop.DataType[0] == schema.DataTypeTextArray.String() && textProps {
propsToCompare = append(propsToCompare, compareProps{Name: prop.Name, IsArray: true})
continue
}
if _, ok := mediaPropsSet[prop.Name]; ok {
propsToCompare = append(propsToCompare, compareProps{Name: prop.Name, IsArray: schema.IsArrayDataType(prop.DataType)})
continue
}
}
// if no properties to compare, we can skip the comparison. Return vectors of old object if present
if len(propsToCompare) == 0 {
oldObject, err := findObjectFn(ctx, class.Class, object.ID, nil, additional.Properties{}, object.Tenant)
if err != nil || oldObject == nil {
return true, nil
}
return false, oldObject
}
returnProps := make(search.SelectProperties, 0, len(propsToCompare))
for _, prop := range propsToCompare {
returnProps = append(returnProps, search.SelectProperty{Name: prop.Name, IsPrimitive: true, IsObject: false})
}
oldObject, err := findObjectFn(ctx, class.Class, object.ID, returnProps, additional.Properties{}, object.Tenant)
if err != nil || oldObject == nil {
return true, nil
}
oldProps := oldObject.Schema.(map[string]interface{})
var newProps map[string]interface{}
if object.Properties == nil {
newProps = make(map[string]interface{})
} else {
newProps = object.Properties.(map[string]interface{})
}
for _, propStruct := range propsToCompare {
valNew, isPresentNew := newProps[propStruct.Name]
valOld, isPresentOld := oldProps[propStruct.Name]
if isPresentNew != isPresentOld {
return true, nil
}
if !isPresentNew {
continue
}
if propStruct.IsArray {
// empty strings do not have type information saved with them - the new value can also come from disk if
// an update happens
if _, ok := valOld.([]interface{}); ok && len(valOld.([]interface{})) == 0 {
valOld = []string{}
}
if _, ok := valNew.([]interface{}); ok && len(valNew.([]interface{})) == 0 {
valNew = []string{}
}
if len(valOld.([]string)) != len(valNew.([]string)) {
return true, nil
}
for i, val := range valOld.([]string) {
if val != valNew.([]string)[i] {
return true, nil
}
}
} else {
if valOld != valNew {
return true, nil
}
}
}
return false, oldObject
}
|