File tree Expand file tree Collapse file tree 3 files changed +46
-5
lines changed
packages/components/nodes/vectorstores/Postgres Expand file tree Collapse file tree 3 files changed +46
-5
lines changed Original file line number Diff line number Diff line change @@ -136,6 +136,29 @@ class Postgres_VectorStores implements INode {
136
136
additionalParams : true ,
137
137
optional : true
138
138
} ,
139
+ {
140
+ label : 'Distance Strategy' ,
141
+ name : 'distanceStrategy' ,
142
+ description : 'Strategy for calculating distances between vectors' ,
143
+ type : 'option' ,
144
+ options : [
145
+ {
146
+ label : 'Cosine' ,
147
+ name : 'cosine'
148
+ } ,
149
+ {
150
+ label : 'Euclidean' ,
151
+ name : 'euclidean'
152
+ } ,
153
+ {
154
+ label : 'Inner Product' ,
155
+ name : 'innerProduct'
156
+ }
157
+ ] ,
158
+ additionalParams : true ,
159
+ default : 'cosine' ,
160
+ optional : true
161
+ } ,
139
162
{
140
163
label : 'Postgres Metadata Filter' ,
141
164
name : 'pgMetadataFilter' ,
Original file line number Diff line number Diff line change 1
1
import { VectorStoreDriver } from './Base'
2
2
import { getCredentialData , getCredentialParam } from '../../../../src'
3
- import { PGVectorStore , PGVectorStoreArgs } from '@langchain/community/vectorstores/pgvector'
3
+ import { DistanceStrategy , PGVectorStore , PGVectorStoreArgs } from '@langchain/community/vectorstores/pgvector'
4
4
import { Document } from '@langchain/core/documents'
5
5
import { PoolConfig } from 'pg'
6
6
@@ -45,7 +45,8 @@ export class PGVectorDriver extends VectorStoreDriver {
45
45
tableName : this . getTableName ( ) ,
46
46
columns : {
47
47
contentColumnName : ( this . nodeData . inputs ?. contentColumnName || PGVectorDriver . CONTENT_COLUMN_NAME_DEFAULT ) as string
48
- }
48
+ } ,
49
+ distanceStrategy : ( this . nodeData . inputs ?. distanceStrategy || 'cosine' ) as DistanceStrategy
49
50
}
50
51
}
51
52
Original file line number Diff line number Diff line change @@ -80,7 +80,8 @@ export class TypeORMDriver extends VectorStoreDriver {
80
80
k ,
81
81
tableName ,
82
82
await this . getPostgresConnectionOptions ( ) ,
83
- filter ?? metadataFilters
83
+ filter ?? metadataFilters ,
84
+ this . computedOperatorString
84
85
)
85
86
}
86
87
@@ -105,12 +106,28 @@ export class TypeORMDriver extends VectorStoreDriver {
105
106
return instance
106
107
}
107
108
109
+ get computedOperatorString ( ) {
110
+ const { distanceStrategy = 'cosine' } = this . nodeData . inputs || { }
111
+
112
+ switch ( distanceStrategy ) {
113
+ case 'cosine' :
114
+ return '<=>'
115
+ case 'innerProduct' :
116
+ return '<#>'
117
+ case 'euclidean' :
118
+ return '<->'
119
+ default :
120
+ throw new Error ( `Unknown distance strategy: ${ distanceStrategy } ` )
121
+ }
122
+ }
123
+
108
124
static similaritySearchVectorWithScore = async (
109
125
query : number [ ] ,
110
126
k : number ,
111
127
tableName : string ,
112
128
postgresConnectionOptions : ICommonObject ,
113
- filter ?: any
129
+ filter ?: any ,
130
+ distanceOperator : string = '<=>'
114
131
) => {
115
132
const embeddingString = `[${ query . join ( ',' ) } ]`
116
133
let notExists = ''
@@ -127,7 +144,7 @@ export class TypeORMDriver extends VectorStoreDriver {
127
144
}
128
145
129
146
const queryString = `
130
- SELECT *, embedding <=> $1 as "_distance"
147
+ SELECT *, embedding ${ distanceOperator } $1 as "_distance"
131
148
FROM ${ tableName }
132
149
WHERE metadata @> $2
133
150
${ notExists }
You can’t perform that action at this time.
0 commit comments