Spaces:
Running
Running
File size: 8,405 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 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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: [email protected]
//
package traverser
import (
"context"
"fmt"
"strings"
"github.com/go-openapi/strfmt"
"github.com/pkg/errors"
"github.com/weaviate/weaviate/entities/additional"
"github.com/weaviate/weaviate/entities/modulecapabilities"
"github.com/weaviate/weaviate/entities/schema/crossref"
"github.com/weaviate/weaviate/entities/search"
"github.com/weaviate/weaviate/entities/searchparams"
libvectorizer "github.com/weaviate/weaviate/usecases/vectorizer"
)
type nearParamsVector struct {
modulesProvider ModulesProvider
search nearParamsSearcher
}
type nearParamsSearcher interface {
Object(ctx context.Context, className string, id strfmt.UUID,
props search.SelectProperties, additional additional.Properties,
repl *additional.ReplicationProperties, tenant string) (*search.Result, error)
ObjectsByID(ctx context.Context, id strfmt.UUID, props search.SelectProperties,
additional additional.Properties, tenant string) (search.Results, error)
}
func newNearParamsVector(modulesProvider ModulesProvider, search nearParamsSearcher) *nearParamsVector {
return &nearParamsVector{modulesProvider, search}
}
func (v *nearParamsVector) vectorFromParams(ctx context.Context,
nearVector *searchparams.NearVector, nearObject *searchparams.NearObject,
moduleParams map[string]interface{}, className string, tenant string,
) ([]float32, error) {
err := v.validateNearParams(nearVector, nearObject, moduleParams, className)
if err != nil {
return nil, err
}
if len(moduleParams) == 1 {
for name, value := range moduleParams {
return v.vectorFromModules(ctx, className, name, value, tenant)
}
}
if nearVector != nil {
return nearVector.Vector, nil
}
if nearObject != nil {
vector, err := v.vectorFromNearObjectParams(ctx, className, nearObject, tenant)
if err != nil {
return nil, errors.Errorf("nearObject params: %v", err)
}
return vector, nil
}
// either nearObject or nearVector or module search param has to be set,
// so if we land here, something has gone very wrong
panic("vectorFromParams was called without any known params present")
}
func (v *nearParamsVector) validateNearParams(nearVector *searchparams.NearVector,
nearObject *searchparams.NearObject,
moduleParams map[string]interface{}, className ...string,
) error {
if len(moduleParams) == 1 && nearVector != nil && nearObject != nil {
return errors.Errorf("found 'nearText' and 'nearVector' and 'nearObject' parameters " +
"which are conflicting, choose one instead")
}
if len(moduleParams) == 1 && nearVector != nil {
return errors.Errorf("found both 'nearText' and 'nearVector' parameters " +
"which are conflicting, choose one instead")
}
if len(moduleParams) == 1 && nearObject != nil {
return errors.Errorf("found both 'nearText' and 'nearObject' parameters " +
"which are conflicting, choose one instead")
}
if nearVector != nil && nearObject != nil {
return errors.Errorf("found both 'nearVector' and 'nearObject' parameters " +
"which are conflicting, choose one instead")
}
if v.modulesProvider != nil {
if len(moduleParams) > 1 {
params := []string{}
for p := range moduleParams {
params = append(params, fmt.Sprintf("'%s'", p))
}
return errors.Errorf("found more then one module param: %s which are conflicting "+
"choose one instead", strings.Join(params, ", "))
}
for name, value := range moduleParams {
if len(className) == 1 {
err := v.modulesProvider.ValidateSearchParam(name, value, className[0])
if err != nil {
return err
}
} else {
err := v.modulesProvider.CrossClassValidateSearchParam(name, value)
if err != nil {
return err
}
}
}
}
if nearVector != nil {
if nearVector.Certainty != 0 && nearVector.Distance != 0 {
return errors.Errorf("found 'certainty' and 'distance' set in nearVector " +
"which are conflicting, choose one instead")
}
}
if nearObject != nil {
if nearObject.Certainty != 0 && nearObject.Distance != 0 {
return errors.Errorf("found 'certainty' and 'distance' set in nearObject " +
"which are conflicting, choose one instead")
}
}
return nil
}
func (v *nearParamsVector) vectorFromModules(ctx context.Context,
className, paramName string, paramValue interface{}, tenant string,
) ([]float32, error) {
if v.modulesProvider != nil {
vector, err := v.modulesProvider.VectorFromSearchParam(ctx,
className, paramName, paramValue, v.findVector, tenant,
)
if err != nil {
return nil, errors.Errorf("vectorize params: %v", err)
}
return vector, nil
}
return nil, errors.New("no modules defined")
}
func (v *nearParamsVector) findVector(ctx context.Context, className string, id strfmt.UUID, tenant string) ([]float32, error) {
switch className {
case "":
// Explore cross class searches where we don't have class context
return v.crossClassFindVector(ctx, id)
default:
return v.classFindVector(ctx, className, id, tenant)
}
}
func (v *nearParamsVector) classFindVector(ctx context.Context, className string,
id strfmt.UUID, tenant string,
) ([]float32, error) {
res, err := v.search.Object(ctx, className, id, search.SelectProperties{}, additional.Properties{}, nil, tenant)
if err != nil {
return nil, err
}
if res == nil {
return nil, errors.New("vector not found")
}
return res.Vector, nil
}
func (v *nearParamsVector) crossClassFindVector(ctx context.Context, id strfmt.UUID) ([]float32, error) {
res, err := v.search.ObjectsByID(ctx, id, search.SelectProperties{}, additional.Properties{}, "")
if err != nil {
return nil, errors.Wrap(err, "find objects")
}
switch len(res) {
case 0:
return nil, errors.New("vector not found")
case 1:
return res[0].Vector, nil
default:
vectors := make([][]float32, len(res))
for i := range res {
vectors[i] = res[i].Vector
}
return libvectorizer.CombineVectors(vectors), nil
}
}
func (v *nearParamsVector) crossClassVectorFromNearObjectParams(ctx context.Context,
params *searchparams.NearObject,
) ([]float32, error) {
return v.vectorFromNearObjectParams(ctx, "", params, "")
}
func (v *nearParamsVector) vectorFromNearObjectParams(ctx context.Context,
className string, params *searchparams.NearObject, tenant string,
) ([]float32, error) {
if len(params.ID) == 0 && len(params.Beacon) == 0 {
return nil, errors.New("empty id and beacon")
}
var id strfmt.UUID
targetClassName := className
if len(params.ID) > 0 {
id = strfmt.UUID(params.ID)
} else {
ref, err := crossref.Parse(params.Beacon)
if err != nil {
return nil, err
}
id = ref.TargetID
if ref.Class != "" {
targetClassName = ref.Class
}
}
return v.findVector(ctx, targetClassName, id, tenant)
}
func (v *nearParamsVector) extractCertaintyFromParams(nearVector *searchparams.NearVector,
nearObject *searchparams.NearObject, moduleParams map[string]interface{},
) float64 {
if nearVector != nil {
if nearVector.Certainty != 0 {
return nearVector.Certainty
} else if nearVector.WithDistance {
return additional.DistToCertainty(nearVector.Distance)
}
}
if nearObject != nil {
if nearObject.Certainty != 0 {
return nearObject.Certainty
} else if nearObject.WithDistance {
return additional.DistToCertainty(nearObject.Distance)
}
}
if len(moduleParams) == 1 {
return v.extractCertaintyFromModuleParams(moduleParams)
}
return 0
}
func (v *nearParamsVector) extractCertaintyFromModuleParams(moduleParams map[string]interface{}) float64 {
for _, param := range moduleParams {
if nearParam, ok := param.(modulecapabilities.NearParam); ok {
if nearParam.SimilarityMetricProvided() {
if certainty := nearParam.GetCertainty(); certainty != 0 {
return certainty
} else {
return additional.DistToCertainty(nearParam.GetDistance())
}
}
}
}
return 0
}
|