File size: 3,539 Bytes
e89cd08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2025 Weaviate B.V. All rights reserved.
//
//  CONTACT: hello@weaviate.io
//

package modules

import (
	"context"

	"github.com/pkg/errors"
	"github.com/weaviate/weaviate/entities/dto"
	"github.com/weaviate/weaviate/entities/models"
	"github.com/weaviate/weaviate/entities/modulecapabilities"
	"github.com/weaviate/weaviate/usecases/config"
)

func vectorFromSearchParam[T dto.Embedding](
	ctx context.Context,
	class *models.Class,
	mod modulecapabilities.Module,
	targetModule, targetVector, tenant, param string,
	params interface{},
	findVectorFn modulecapabilities.FindVectorFn[T],
	isModuleNameEqualFn func(module modulecapabilities.Module, targetModule string) bool,
	dbConfig *config.Config,
) (bool, T, error) {
	var moduleName string
	var vectorSearches map[string]modulecapabilities.VectorForParams[T]

	if searcher, ok := mod.(modulecapabilities.Searcher[T]); ok {
		if isModuleNameEqualFn(mod, targetModule) {
			moduleName = mod.Name()
			vectorSearches = searcher.VectorSearches()
		}
	} else if searchers, ok := mod.(modulecapabilities.DependencySearcher[T]); ok {
		if dependencySearchers := searchers.VectorSearches(); dependencySearchers != nil {
			moduleName = targetModule
			vectorSearches = dependencySearchers[targetModule]
		}
	}
	if vectorSearches != nil {
		if searchVectorFn := vectorSearches[param]; searchVectorFn != nil {
			cfg := NewClassBasedModuleConfig(class, moduleName, tenant, targetVector, dbConfig)
			vector, err := searchVectorFn.VectorForParams(ctx, params, class.Class, findVectorFn, cfg)
			if err != nil {
				return true, nil, errors.Errorf("vectorize params: %v", err)
			}
			return true, vector, nil
		}
	}

	return false, nil, nil
}

func crossClassVectorFromSearchParam[T dto.Embedding](
	ctx context.Context,
	mod modulecapabilities.Module,
	param string,
	params interface{},
	findVectorFn modulecapabilities.FindVectorFn[T],
	getTargetVectorFn func(class *models.Class, params interface{}) ([]string, error),
	dbConfig config.Config,
) (bool, T, string, error) {
	if searcher, ok := mod.(modulecapabilities.Searcher[T]); ok {
		if vectorSearches := searcher.VectorSearches(); vectorSearches != nil {
			if searchVectorFn := vectorSearches[param]; searchVectorFn != nil {
				cfg := NewCrossClassModuleConfig()
				vector, err := searchVectorFn.VectorForParams(ctx, params, "", findVectorFn, cfg)
				if err != nil {
					return true, nil, "", errors.Errorf("vectorize params: %v", err)
				}
				targetVector, err := getTargetVectorFn(nil, params)
				if err != nil {
					return true, nil, "", errors.Errorf("get target vector: %v", err)
				}
				if len(targetVector) > 0 {
					return true, vector, targetVector[0], nil
				}
				return true, vector, "", nil
			}
		}
	}

	return false, nil, "", nil
}

func vectorFromInput[T dto.Embedding](
	ctx context.Context,
	mod modulecapabilities.Module,
	class *models.Class,
	input, targetVector string,
	dbConfig *config.Config,
) (bool, T, error) {
	if vectorizer, ok := mod.(modulecapabilities.InputVectorizer[T]); ok {
		// does not access any objects, therefore tenant is irrelevant
		cfg := NewClassBasedModuleConfig(class, mod.Name(), "", targetVector, dbConfig)
		vector, err := vectorizer.VectorizeInput(ctx, input, cfg)
		return true, vector, err
	}
	return false, nil, nil
}