Feature/Add filter to Postgres VectorStore (#2674)

* add filter to pgvs

* fix types
pull/2676/head
Henry Heng 2024-06-19 13:44:20 +01:00 committed by GitHub
parent 371e632a2c
commit b5ead0745b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 20 additions and 3 deletions

View File

@ -25,7 +25,7 @@ class Postgres_VectorStores implements INode {
constructor() {
this.label = 'Postgres'
this.name = 'postgres'
this.version = 4.0
this.version = 5.0
this.type = 'Postgres'
this.icon = 'postgres.svg'
this.category = 'Vector Stores'
@ -98,6 +98,13 @@ class Postgres_VectorStores implements INode {
type: 'number',
additionalParams: true,
optional: true
},
{
label: 'Postgres Metadata Filter',
name: 'pgMetadataFilter',
type: 'json',
additionalParams: true,
optional: true
}
]
this.outputs = [
@ -209,6 +216,12 @@ class Postgres_VectorStores implements INode {
const output = nodeData.outputs?.output as string
const topK = nodeData.inputs?.topK as string
const k = topK ? parseFloat(topK) : 4
const _pgMetadataFilter = nodeData.inputs?.pgMetadataFilter
let pgMetadataFilter: any
if (_pgMetadataFilter) {
pgMetadataFilter = typeof _pgMetadataFilter === 'object' ? _pgMetadataFilter : JSON.parse(_pgMetadataFilter)
}
let additionalConfiguration = {}
if (additionalConfig) {
@ -244,7 +257,7 @@ class Postgres_VectorStores implements INode {
[ERROR]: uncaughtException: Illegal invocation TypeError: Illegal invocation at Socket.ref (node:net:1524:18) at Connection.ref (.../node_modules/pg/lib/connection.js:183:17) at Client.ref (.../node_modules/pg/lib/client.js:591:21) at BoundPool._pulseQueue (/node_modules/pg-pool/index.js:148:28) at .../node_modules/pg-pool/index.js:184:37 at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
*/
vectorStore.similaritySearchVectorWithScore = async (query: number[], k: number, filter?: any) => {
return await similaritySearchVectorWithScore(query, k, tableName, postgresConnectionOptions, filter)
return await similaritySearchVectorWithScore(query, k, tableName, postgresConnectionOptions, filter ?? pgMetadataFilter)
}
if (output === 'retriever') {
@ -252,6 +265,9 @@ class Postgres_VectorStores implements INode {
return retriever
} else if (output === 'vectorStore') {
;(vectorStore as any).k = k
if (pgMetadataFilter) {
;(vectorStore as any).filter = pgMetadataFilter
}
return vectorStore
}
return vectorStore
@ -266,7 +282,8 @@ const similaritySearchVectorWithScore = async (
filter?: any
) => {
const embeddingString = `[${query.join(',')}]`
const _filter = filter ?? '{}'
let _filter = '{}'
if (filter && typeof filter === 'object') _filter = JSON.stringify(filter)
const queryString = `
SELECT *, embedding <=> $1 as "_distance"