github.com/weaviate/weaviate@v1.24.6/usecases/modulecomponents/arguments/nearImage/searcher.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package nearImage 13 14 import ( 15 "context" 16 "fmt" 17 "time" 18 19 "github.com/pkg/errors" 20 "github.com/weaviate/weaviate/entities/modulecapabilities" 21 "github.com/weaviate/weaviate/entities/moduletools" 22 ) 23 24 type Searcher struct { 25 vectorizer imgVectorizer 26 } 27 28 func NewSearcher(vectorizer imgVectorizer) *Searcher { 29 return &Searcher{vectorizer} 30 } 31 32 type imgVectorizer interface { 33 VectorizeImage(ctx context.Context, 34 id, image string, cfg moduletools.ClassConfig) ([]float32, error) 35 } 36 37 func (s *Searcher) VectorSearches() map[string]modulecapabilities.VectorForParams { 38 vectorSearches := map[string]modulecapabilities.VectorForParams{} 39 vectorSearches["nearImage"] = s.vectorForNearImageParam 40 return vectorSearches 41 } 42 43 func (s *Searcher) vectorForNearImageParam(ctx context.Context, params interface{}, 44 className string, 45 findVectorFn modulecapabilities.FindVectorFn, 46 cfg moduletools.ClassConfig, 47 ) ([]float32, error) { 48 return s.vectorFromNearImageParam(ctx, params.(*NearImageParams), className, findVectorFn, cfg) 49 } 50 51 func (s *Searcher) vectorFromNearImageParam(ctx context.Context, 52 params *NearImageParams, className string, findVectorFn modulecapabilities.FindVectorFn, 53 cfg moduletools.ClassConfig, 54 ) ([]float32, error) { 55 // find vector for given search query 56 searchID := fmt.Sprintf("search_%v", time.Now().UnixNano()) 57 vector, err := s.vectorizer.VectorizeImage(ctx, searchID, params.Image, cfg) 58 if err != nil { 59 return nil, errors.Errorf("vectorize image: %v", err) 60 } 61 62 return vector, nil 63 }