Skip to content

Commit 2627b76

Browse files
committed
Fix data filtering for chatflow uploaded files
1 parent 597e5f5 commit 2627b76

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

packages/components/nodes/vectorstores/Postgres/driver/PGVector.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ export class PGVectorDriver extends VectorStoreDriver {
7575

7676
// @ts-ignore
7777
instance.pool.query = (queryString: string, parameters: any[]) => {
78-
// Tweak query to handle $notexists
78+
// Match chatflow uploaded file and keep filtering on other files:
79+
// https://github.com/FlowiseAI/Flowise/pull/3367#discussion_r1804229295
7980
if ($notexists) {
80-
const chatClause = `${instance.metadataColumnName}->>'${$notexists}' = $${
81-
parameters.length + 1
82-
} OR NOT (metadata ? '${$notexists}')`
81+
parameters.push({ [$notexists]: chatId })
82+
83+
const chatClause = `metadata @> $${parameters.length} OR NOT (metadata ? '${$notexists}')`
8384
const whereClauseRegex = /WHERE ([^\n]+)/
8485
if (queryString.match(whereClauseRegex)) {
8586
queryString = queryString.replace(whereClauseRegex, `WHERE $1 AND (${chatClause})`)
@@ -94,8 +95,6 @@ export class PGVectorDriver extends VectorStoreDriver {
9495
`
9596
)
9697
}
97-
98-
parameters.push(chatId)
9998
}
10099

101100
// Run base function

packages/components/nodes/vectorstores/Postgres/driver/TypeORM.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,17 @@ export class TypeORMDriver extends VectorStoreDriver {
113113
filter?: any
114114
) => {
115115
const embeddingString = `[${query.join(',')}]`
116-
let _filter = '{}'
117116
let notExists = ''
118-
if (filter && typeof filter === 'object') {
119-
if (filter.$notexists) {
120-
notExists = `OR NOT (metadata ? '${filter.$notexists}')`
121-
delete filter.$notexists
122-
}
123-
_filter = JSON.stringify(filter)
117+
const { $notexists, [$notexists]: chatId, ...restFilters } = filter || {}
118+
119+
const _filter = JSON.stringify(restFilters || {})
120+
const parameters: any[] = [embeddingString, _filter, k]
121+
122+
// Match chatflow uploaded file and keep filtering on other files:
123+
// https://github.com/FlowiseAI/Flowise/pull/3367#discussion_r1804229295
124+
if ($notexists) {
125+
parameters.push({ [$notexists]: chatId })
126+
notExists = `AND (metadata @> $${parameters.length} OR NOT (metadata ? '${$notexists}'))`
124127
}
125128

126129
const queryString = `
@@ -135,7 +138,7 @@ export class TypeORMDriver extends VectorStoreDriver {
135138

136139
const conn = await pool.connect()
137140

138-
const documents = await conn.query(queryString, [embeddingString, _filter, k])
141+
const documents = await conn.query(queryString, parameters)
139142

140143
conn.release()
141144

0 commit comments

Comments
 (0)