Skip to content

Commit a815329

Browse files
authored
Merge pull request #912 from Anush008/qdrant-filter
feat: Qdrant filter
2 parents 3d35536 + 73d7d4e commit a815329

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

packages/components/nodes/vectorstores/Qdrant_Existing/Qdrant_Existing.ts

+30-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { QdrantClient } from '@qdrant/js-client-rest'
33
import { QdrantVectorStore, QdrantLibArgs } from 'langchain/vectorstores/qdrant'
44
import { Embeddings } from 'langchain/embeddings/base'
55
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
6+
import { VectorStoreRetrieverInput } from 'langchain/vectorstores/base'
7+
8+
type RetrieverConfig = Partial<VectorStoreRetrieverInput<QdrantVectorStore>>
69

710
class Qdrant_Existing_VectorStores implements INode {
811
label: string
@@ -53,7 +56,7 @@ class Qdrant_Existing_VectorStores implements INode {
5356
},
5457
{
5558
label: 'Qdrant Collection Cofiguration',
56-
name: 'qdrantCollectionCofiguration',
59+
name: 'qdrantCollectionConfiguration',
5760
type: 'json',
5861
optional: true,
5962
additionalParams: true
@@ -66,6 +69,14 @@ class Qdrant_Existing_VectorStores implements INode {
6669
type: 'number',
6770
additionalParams: true,
6871
optional: true
72+
},
73+
{
74+
label: 'Qdrant Search Filter',
75+
name: 'qdrantFilter',
76+
description: 'Only return points which satisfy the conditions',
77+
type: 'json',
78+
additionalParams: true,
79+
optional: true
6980
}
7081
]
7182
this.outputs = [
@@ -85,10 +96,12 @@ class Qdrant_Existing_VectorStores implements INode {
8596
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
8697
const qdrantServerUrl = nodeData.inputs?.qdrantServerUrl as string
8798
const collectionName = nodeData.inputs?.qdrantCollection as string
88-
let qdrantCollectionCofiguration = nodeData.inputs?.qdrantCollectionCofiguration
99+
let qdrantCollectionConfiguration = nodeData.inputs?.qdrantCollectionConfiguration
89100
const embeddings = nodeData.inputs?.embeddings as Embeddings
90101
const output = nodeData.outputs?.output as string
91102
const topK = nodeData.inputs?.topK as string
103+
let queryFilter = nodeData.inputs?.queryFilter
104+
92105
const k = topK ? parseFloat(topK) : 4
93106

94107
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
@@ -104,16 +117,26 @@ class Qdrant_Existing_VectorStores implements INode {
104117
collectionName
105118
}
106119

107-
if (qdrantCollectionCofiguration) {
108-
qdrantCollectionCofiguration =
109-
typeof qdrantCollectionCofiguration === 'object' ? qdrantCollectionCofiguration : JSON.parse(qdrantCollectionCofiguration)
110-
dbConfig.collectionConfig = qdrantCollectionCofiguration
120+
const retrieverConfig: RetrieverConfig = {
121+
k
122+
}
123+
124+
if (qdrantCollectionConfiguration) {
125+
qdrantCollectionConfiguration =
126+
typeof qdrantCollectionConfiguration === 'object'
127+
? qdrantCollectionConfiguration
128+
: JSON.parse(qdrantCollectionConfiguration)
129+
dbConfig.collectionConfig = qdrantCollectionConfiguration
130+
}
131+
132+
if (queryFilter) {
133+
retrieverConfig.filter = typeof queryFilter === 'object' ? queryFilter : JSON.parse(queryFilter)
111134
}
112135

113136
const vectorStore = await QdrantVectorStore.fromExistingCollection(embeddings, dbConfig)
114137

115138
if (output === 'retriever') {
116-
const retriever = vectorStore.asRetriever(k)
139+
const retriever = vectorStore.asRetriever(retrieverConfig)
117140
return retriever
118141
} else if (output === 'vectorStore') {
119142
;(vectorStore as any).k = k

packages/components/nodes/vectorstores/Qdrant_Upsert/Qdrant_Upsert.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { Embeddings } from 'langchain/embeddings/base'
55
import { Document } from 'langchain/document'
66
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
77
import { flatten } from 'lodash'
8+
import { VectorStoreRetrieverInput } from 'langchain/vectorstores/base'
9+
10+
type RetrieverConfig = Partial<VectorStoreRetrieverInput<QdrantVectorStore>>
811

912
class QdrantUpsert_VectorStores implements INode {
1013
label: string
@@ -67,6 +70,14 @@ class QdrantUpsert_VectorStores implements INode {
6770
type: 'number',
6871
additionalParams: true,
6972
optional: true
73+
},
74+
{
75+
label: 'Qdrant Search Filter',
76+
name: 'qdrantFilter',
77+
description: 'Only return points which satisfy the conditions',
78+
type: 'json',
79+
additionalParams: true,
80+
optional: true
7081
}
7182
]
7283
this.outputs = [
@@ -91,6 +102,7 @@ class QdrantUpsert_VectorStores implements INode {
91102
const output = nodeData.outputs?.output as string
92103
const topK = nodeData.inputs?.topK as string
93104
const k = topK ? parseFloat(topK) : 4
105+
let queryFilter = nodeData.inputs?.qdrantFilter
94106

95107
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
96108
const qdrantApiKey = getCredentialParam('qdrantApiKey', credentialData, nodeData)
@@ -111,10 +123,19 @@ class QdrantUpsert_VectorStores implements INode {
111123
url: qdrantServerUrl,
112124
collectionName
113125
}
126+
127+
const retrieverConfig: RetrieverConfig = {
128+
k
129+
}
130+
131+
if (queryFilter) {
132+
retrieverConfig.filter = typeof queryFilter === 'object' ? queryFilter : JSON.parse(queryFilter)
133+
}
134+
114135
const vectorStore = await QdrantVectorStore.fromDocuments(finalDocs, embeddings, dbConfig)
115136

116137
if (output === 'retriever') {
117-
const retriever = vectorStore.asRetriever(k)
138+
const retriever = vectorStore.asRetriever(retrieverConfig)
118139
return retriever
119140
} else if (output === 'vectorStore') {
120141
;(vectorStore as any).k = k

0 commit comments

Comments
 (0)