| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| 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 { |
| |
| cfg := NewClassBasedModuleConfig(class, mod.Name(), "", targetVector, dbConfig) |
| vector, err := vectorizer.VectorizeInput(ctx, input, cfg) |
| return true, vector, err |
| } |
| return false, nil, nil |
| } |
|
|