Skip to content

Commit e610036

Browse files
authored
Merge pull request #13 from jhrozek/options
Add options to QueryRelevantDocuments, add a new WithScoreThreshold option
2 parents c3a1ff3 + f45d3f6 commit e610036

3 files changed

Lines changed: 39 additions & 9 deletions

File tree

examples/qdrant/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ func main() {
7272
}
7373

7474
// Query the most relevant documents based on a given embedding
75-
retrievedDocs, err := vectorDB.QueryRelevantDocuments(ctx, queryEmbedding, 5, collection_name)
75+
retrievedDocs, err := vectorDB.QueryRelevantDocuments(
76+
ctx, queryEmbedding, collection_name,
77+
db.WithLimit(5), db.WithScoreThreshold(0.7))
7678
if err != nil {
7779
log.Fatalf("Failed to query documents: %v", err)
7880
}

pkg/db/qdrant.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,48 @@ func (qv *QdrantVector) SaveEmbeddings(ctx context.Context, docID string, embedd
8888
return nil
8989
}
9090

91+
// QueryOpt represents an option for a query. This is the type that should
92+
// be returned from query options functions.
93+
type QueryOpt func(*qdrant.QueryPoints)
94+
95+
// WithLimit sets the limit of the number of documents to return in a query.
96+
func WithLimit(limit uint64) QueryOpt {
97+
return func(q *qdrant.QueryPoints) {
98+
q.Limit = &limit
99+
}
100+
}
101+
102+
// WithScoreThreshold sets the score threshold for a query. The higher the threshold, the more relevant the results.
103+
func WithScoreThreshold(threshold float32) QueryOpt {
104+
return func(q *qdrant.QueryPoints) {
105+
q.ScoreThreshold = &threshold
106+
}
107+
}
108+
91109
// QueryRelevantDocuments retrieves the most relevant documents based on a given embedding.
92110
//
93111
// Parameters:
94112
// - ctx: The context for the query.
95113
// - embedding: The query embedding.
96114
// - limit: The number of documents to return.
115+
// - collection: The collection name to query.
97116
//
98117
// Returns:
99118
// - A slice of QDrantDocument structs containing the most relevant documents.
100119
// - An error if the query fails.
101-
func (qv *QdrantVector) QueryRelevantDocuments(ctx context.Context, embedding []float32, limit int, colllection string) ([]Document, error) {
102-
limitUint := uint64(limit) // Convert limit to uint64
120+
func (qv *QdrantVector) QueryRelevantDocuments(
121+
ctx context.Context, embedding []float32, collection string, queryOpts ...QueryOpt,
122+
) ([]Document, error) {
103123
query := &qdrant.QueryPoints{
104-
CollectionName: colllection, // Replace with actual collection name
124+
CollectionName: collection, // Replace with actual collection name
105125
Query: qdrant.NewQuery(embedding...),
106-
Limit: &limitUint,
107126
WithPayload: qdrant.NewWithPayloadInclude("content"),
108127
}
109128

129+
for _, opt := range queryOpts {
130+
opt(query)
131+
}
132+
110133
response, err := qv.client.Query(ctx, query)
111134
if err != nil {
112135
return nil, fmt.Errorf("failed to search points: %w", err)

pkg/db/qdrant_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,19 @@ func (t *testQdrantVector) SaveEmbeddings(ctx context.Context, docID string, emb
8484
return err
8585
}
8686

87-
func (t *testQdrantVector) QueryRelevantDocuments(ctx context.Context, embedding []float32, limit int, collection string) ([]Document, error) {
88-
limitUint := uint64(limit)
87+
func (t *testQdrantVector) QueryRelevantDocuments(
88+
ctx context.Context, embedding []float32, collection string, queryOpts ...QueryOpt,
89+
) ([]Document, error) {
8990
query := &qdrant.QueryPoints{
9091
CollectionName: collection,
9192
Query: qdrant.NewQuery(embedding...),
92-
Limit: &limitUint,
9393
WithPayload: qdrant.NewWithPayloadInclude("content"),
9494
}
9595

96+
for _, opt := range queryOpts {
97+
opt(query)
98+
}
99+
96100
response, err := t.mockClient.Query(ctx, query)
97101
if err != nil {
98102
return nil, err
@@ -186,7 +190,8 @@ func TestQueryRelevantDocuments(t *testing.T) {
186190
})).Return(mockResponse, nil)
187191

188192
// Test the QueryRelevantDocuments function
189-
docs, err := qv.QueryRelevantDocuments(ctx, embedding, limit, collection)
193+
docs, err := qv.QueryRelevantDocuments(ctx, embedding, collection,
194+
WithLimit(5), WithScoreThreshold(0.7))
190195
assert.NoError(t, err)
191196
assert.Len(t, docs, 1)
192197
assert.Equal(t, "test content", docs[0].Metadata["content"])

0 commit comments

Comments
 (0)