Skip to content

Commit

Permalink
Merge pull request #46 from grafana/vectorservice-query-fix
Browse files Browse the repository at this point in the history
vectorservice query fix
  • Loading branch information
yoziru authored Sep 14, 2023
2 parents 2936f84 + 8dd1622 commit 67d7394
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
10 changes: 5 additions & 5 deletions pkg/plugin/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ func newOpenAIProxy() http.Handler {
}

type vectorSearchRequest struct {
Text string `json:"text"`
Query string `json:"query"`
Collection string `json:"collection"`
Limit uint64 `json:"limit"`
TopK uint64 `json:"topK"`
}

type vectorSearchResponse struct {
Expand All @@ -87,10 +87,10 @@ func (app *App) handleVectorSearch(w http.ResponseWriter, req *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if body.Limit == 0 {
body.Limit = 10
if body.TopK == 0 {
body.TopK = 10
}
results, err := app.vectorService.Search(req.Context(), body.Collection, body.Text, body.Limit)
results, err := app.vectorService.Search(req.Context(), body.Collection, body.Query, body.TopK)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down
9 changes: 6 additions & 3 deletions pkg/plugin/vector/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

type Service interface {
Search(ctx context.Context, collection string, query string, limit uint64) ([]store.SearchResult, error)
Search(ctx context.Context, collection string, query string, topK uint64) ([]store.SearchResult, error)
Cancel()
}

Expand Down Expand Up @@ -57,7 +57,10 @@ func NewService(s VectorSettings, secrets map[string]string) (Service, error) {
}, nil
}

func (v *vectorService) Search(ctx context.Context, collection string, query string, limit uint64) ([]store.SearchResult, error) {
func (v *vectorService) Search(ctx context.Context, collection string, query string, topK uint64) ([]store.SearchResult, error) {
if query == "" {
return nil, fmt.Errorf("query cannot be empty")
}
exists, err := v.store.CollectionExists(ctx, collection)
if err != nil {
return nil, fmt.Errorf("vector store collections: %w", err)
Expand All @@ -75,7 +78,7 @@ func (v *vectorService) Search(ctx context.Context, collection string, query str

log.DefaultLogger.Info("Searching", "collection", collection, "query", query)
// Search the vector store for similar vectors.
results, err := v.store.Search(ctx, collection, e, limit)
results, err := v.store.Search(ctx, collection, e, topK)
if err != nil {
return nil, fmt.Errorf("vector store search: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugin/vector/store/qdrant.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ func (q *qdrantStore) CollectionExists(ctx context.Context, collection string) (
return true, nil
}

func (q *qdrantStore) Search(ctx context.Context, collection string, vector []float32, limit uint64) ([]SearchResult, error) {
func (q *qdrantStore) Search(ctx context.Context, collection string, vector []float32, topK uint64) ([]SearchResult, error) {
if q.md != nil {
ctx = metadata.NewOutgoingContext(ctx, *q.md)
}
result, err := q.pointsClient.Search(ctx, &qdrant.SearchPoints{
CollectionName: collection,
Vector: vector,
Limit: limit,
Limit: topK,
// Include all payloads in the search result
WithVectors: &qdrant.WithVectorsSelector{SelectorOptions: &qdrant.WithVectorsSelector_Enable{Enable: false}},
WithPayload: &qdrant.WithPayloadSelector{SelectorOptions: &qdrant.WithPayloadSelector_Enable{Enable: true}},
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugin/vector/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type SearchResult struct {

type ReadVectorStore interface {
CollectionExists(ctx context.Context, collection string) (bool, error)
Search(ctx context.Context, collection string, vector []float32, limit uint64) ([]SearchResult, error)
Search(ctx context.Context, collection string, vector []float32, topK uint64) ([]SearchResult, error)
}

type WriteVectorStore interface {
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugin/vector/store/vectorapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ func (g *grafanaVectorAPI) CollectionExists(ctx context.Context, collection stri
return true, nil
}

func (g *grafanaVectorAPI) Search(ctx context.Context, collection string, vector []float32, limit uint64) ([]SearchResult, error) {
func (g *grafanaVectorAPI) Search(ctx context.Context, collection string, vector []float32, topK uint64) ([]SearchResult, error) {
type queryPointsRequest struct {
Query []float32 `json:"query"`
TopK uint64 `json:"top_k"`
}
reqBody := queryPointsRequest{
Query: vector,
TopK: limit,
TopK: topK,
}
reqJSON, err := json.Marshal(reqBody)
if err != nil {
Expand Down

0 comments on commit 67d7394

Please sign in to comment.