-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
community[patch]: Adds support for passing schemaName to pgvector #4543
Merged
Merged
Changes from 5 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
68d7290
Adds support for passing schemaName to pgvector
ZuesYousif 74d40b6
Updates refrences to tableName to use schemaName where missing
ZuesYousif 4c3c6ff
Adds missing var to pool call
ZuesYousif 7a8ebba
Adds support for nesting extensions in schema
ZuesYousif d1054a3
Adds support for nesting extensions under schemas
ZuesYousif 95923a3
Adds wrapping to handle capital letters
ZuesYousif 0d651b0
fixes formatting
ZuesYousif 5f5cff9
Removes duplicate queries
ZuesYousif 5b347cd
Removes extra string wrapping
ZuesYousif 67a02e2
Fixes syntax error
ZuesYousif aa7e17d
Removes string wrapper around tableName where schemaName isnt present
ZuesYousif 9e46910
Removes additional quote wrapping
ZuesYousif 43d5a0f
Adds computedTableName to class
ZuesYousif 0d7f49e
Uses tableName for constraint identifier
ZuesYousif f5822bd
yarn format
ZuesYousif 62f2132
Fixes edgecase where operator was out of scope due to order of operat…
ZuesYousif 48b3b92
Adds check to handle unused extensionSchemaName
ZuesYousif 213cf3a
Refactors updating search_path to use operator function instead
ZuesYousif 8b2b3f5
Change to getter method, revert inadvertent change
jacoblee93 ad17a20
Revert
jacoblee93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ export interface PGVectorStoreArgs { | |
collectionTableName?: string; | ||
collectionName?: string; | ||
collectionMetadata?: Metadata | null; | ||
schemaName?: string | null; | ||
extensionSchemaName?: String | null; | ||
columns?: { | ||
idColumnName?: string; | ||
vectorColumnName?: string; | ||
|
@@ -51,12 +53,16 @@ export class PGVectorStore extends VectorStore { | |
|
||
collectionMetadata: Metadata | null; | ||
|
||
schemaName: string | null; | ||
|
||
idColumnName: string; | ||
|
||
vectorColumnName: string; | ||
|
||
contentColumnName: string; | ||
|
||
extensionSchemaName: string | null; | ||
|
||
metadataColumnName: string; | ||
|
||
filter?: Metadata; | ||
|
@@ -82,6 +88,9 @@ export class PGVectorStore extends VectorStore { | |
this.collectionTableName = config.collectionTableName; | ||
this.collectionName = config.collectionName ?? "langchain"; | ||
this.collectionMetadata = config.collectionMetadata ?? null; | ||
this.schemaName = config.schemaName ?? null; | ||
this.extensionSchemaName = config.extensionSchemaName ?? null; | ||
|
||
this.filter = config.filter; | ||
|
||
this.vectorColumnName = config.columns?.vectorColumnName ?? "embedding"; | ||
|
@@ -237,11 +246,16 @@ export class PGVectorStore extends VectorStore { | |
.map((_, j) => this.generatePlaceholderForRowAt(j, columns.length)) | ||
.join(", "); | ||
|
||
const text = ` | ||
const text = this.schemaName == null ? ` | ||
INSERT INTO ${this.tableName}( | ||
${columns.map((column) => `"${column}"`).join(", ")} | ||
) | ||
VALUES ${valuesPlaceholders} | ||
` : ` | ||
INSERT INTO ${this.schemaName}.${this.tableName}( | ||
${columns.map((column) => `"${column}"`).join(", ")} | ||
) | ||
VALUES ${valuesPlaceholders} | ||
`; | ||
return text; | ||
} | ||
|
@@ -322,11 +336,16 @@ export class PGVectorStore extends VectorStore { | |
// Set parameters of dynamically generated query | ||
const params = collectionId ? [ids, collectionId] : [ids]; | ||
|
||
const queryString = ` | ||
const queryString = this.schemaName == null ? ` | ||
DELETE FROM ${this.tableName} | ||
WHERE ${collectionId ? "collection_id = $2 AND " : ""}${ | ||
this.idColumnName | ||
} = ANY($1::uuid[]) | ||
` : ` | ||
DELETE FROM ${this.schemaName}.${this.tableName} | ||
WHERE ${collectionId ? "collection_id = $2 AND " : ""}${ | ||
this.idColumnName | ||
} = ANY($1::uuid[]) | ||
`; | ||
await this.pool.query(queryString, params); | ||
} | ||
|
@@ -347,11 +366,16 @@ export class PGVectorStore extends VectorStore { | |
// Set parameters of dynamically generated query | ||
const params = collectionId ? [filter, collectionId] : [filter]; | ||
|
||
const queryString = ` | ||
const queryString = this.schemaName == null ? ` | ||
DELETE FROM ${this.tableName} | ||
WHERE ${collectionId ? "collection_id = $2 AND " : ""}${ | ||
this.metadataColumnName | ||
}::jsonb @> $1 | ||
` : ` | ||
DELETE FROM ${this.schemaName}.${this.tableName} | ||
WHERE ${collectionId ? "collection_id = $2 AND " : ""}${ | ||
this.metadataColumnName | ||
}::jsonb @> $1 | ||
`; | ||
return await this.pool.query(queryString, params); | ||
} | ||
|
@@ -454,13 +478,20 @@ export class PGVectorStore extends VectorStore { | |
? `WHERE ${whereClauses.join(" AND ")}` | ||
: ""; | ||
|
||
const queryString = ` | ||
const queryString = this.schemaName == null ? ` | ||
SELECT *, ${this.vectorColumnName} <=> $1 as "_distance" | ||
FROM ${this.tableName} | ||
${whereClause} | ||
${collectionId ? "AND collection_id = $3" : ""} | ||
ORDER BY "_distance" ASC | ||
LIMIT $2; | ||
` : ` | ||
SELECT *, ${this.vectorColumnName} <=> $1 as "_distance" | ||
FROM ${this.schemaName}.${this.tableName} | ||
${whereClause} | ||
${collectionId ? "AND collection_id = $3" : ""} | ||
ORDER BY "_distance" ASC | ||
LIMIT $2; | ||
`; | ||
|
||
const documents = (await this.pool.query(queryString, parameters)).rows; | ||
|
@@ -485,17 +516,26 @@ export class PGVectorStore extends VectorStore { | |
* @returns Promise that resolves when the table has been ensured. | ||
*/ | ||
async ensureTableInDatabase(): Promise<void> { | ||
await this.pool.query("CREATE EXTENSION IF NOT EXISTS vector;"); | ||
await this.pool.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";'); | ||
|
||
await this.pool.query(` | ||
const vectorQuery = this.extensionSchemaName == null ? "CREATE EXTENSION IF NOT EXISTS vector;" : `CREATE EXTENSION IF NOT EXISTS vector WITH SCHEMA ${this.extensionSchemaName};` | ||
const uuidQuery = this.extensionSchemaName == null ? 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp";' : `CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA ${this.extensionSchemaName};` | ||
const tableQuery = this.schemaName == null ? ` | ||
CREATE TABLE IF NOT EXISTS ${this.tableName} ( | ||
"${this.idColumnName}" uuid NOT NULL DEFAULT uuid_generate_v4() PRIMARY KEY, | ||
"${this.contentColumnName}" text, | ||
"${this.metadataColumnName}" jsonb, | ||
"${this.vectorColumnName}" vector | ||
); | ||
`); | ||
` : ` | ||
CREATE TABLE IF NOT EXISTS ${this.schemaName}.${this.tableName} ( | ||
"${this.idColumnName}" uuid NOT NULL DEFAULT uuid_generate_v4() PRIMARY KEY, | ||
"${this.contentColumnName}" text, | ||
"${this.metadataColumnName}" jsonb, | ||
"${this.vectorColumnName}" ${this.schemaName}.vector | ||
); | ||
` | ||
await this.pool.query(vectorQuery); | ||
await this.pool.query(uuidQuery); | ||
await this.pool.query(tableQuery); | ||
} | ||
|
||
/** | ||
|
@@ -506,7 +546,7 @@ export class PGVectorStore extends VectorStore { | |
*/ | ||
async ensureCollectionTableInDatabase(): Promise<void> { | ||
try { | ||
await this.pool.query(` | ||
const queryString = this.schemaName == null ? ` | ||
CREATE TABLE IF NOT EXISTS ${this.collectionTableName} ( | ||
uuid uuid NOT NULL DEFAULT uuid_generate_v4() PRIMARY KEY, | ||
name character varying, | ||
|
@@ -521,7 +561,23 @@ export class PGVectorStore extends VectorStore { | |
FOREIGN KEY (collection_id) | ||
REFERENCES ${this.collectionTableName}(uuid) | ||
ON DELETE CASCADE; | ||
`); | ||
` : ` | ||
CREATE TABLE IF NOT EXISTS ${this.schemaName}.${this.collectionTableName} ( | ||
uuid uuid NOT NULL DEFAULT uuid_generate_v4() PRIMARY KEY, | ||
name character varying, | ||
cmetadata jsonb | ||
); | ||
|
||
ALTER TABLE ${this.schemaName}.${this.tableName} | ||
ADD COLUMN collection_id uuid; | ||
|
||
ALTER TABLE ${this.schemaName}.${this.tableName} | ||
ADD CONSTRAINT ${this.schemaName}.${this.tableName}_collection_id_fkey | ||
FOREIGN KEY (collection_id) | ||
REFERENCES ${this.collectionTableName}(uuid) | ||
ON DELETE CASCADE; | ||
` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't forget to run |
||
await this.pool.query(queryString); | ||
} catch (e) { | ||
if (!(e as Error).message.includes("already exists")) { | ||
console.error(e); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these be in quotes to account for capitalization?