Skip to content

Extends 6413 #6452

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

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions libs/langchain-community/src/vectorstores/pgvector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,19 @@ export class PGVectorStore extends VectorStore {
* `connect` to return a new instance of `PGVectorStore`.
*
* @param embeddings - Embeddings instance.
* @param fields - `PGVectorStoreArgs` instance.
* @param fields - `PGVectorStoreArgs` instance
* @param fields.dimensions Number of dimensions in your vector data type. For example, use 1536 for OpenAI's `text-embedding-3-small`. If not set, indexes like HNSW might not be used during query time.
* @returns A new instance of `PGVectorStore`.
*/
static async initialize(
embeddings: EmbeddingsInterface,
config: PGVectorStoreArgs
config: PGVectorStoreArgs & { dimensions?: number }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to add the optional dimensions to the PGVectorStoreArgs type directly? 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I can now see how it's not convenient - I can just make the constructor Omit dimensions

): Promise<PGVectorStore> {
const postgresqlVectorStore = new PGVectorStore(embeddings, config);
const { dimensions, ...rest } = config;
const postgresqlVectorStore = new PGVectorStore(embeddings, rest);

await postgresqlVectorStore._initializeClient();
await postgresqlVectorStore.ensureTableInDatabase();
await postgresqlVectorStore.ensureTableInDatabase(dimensions);
if (postgresqlVectorStore.collectionTableName) {
await postgresqlVectorStore.ensureCollectionTableInDatabase();
}
Expand Down Expand Up @@ -552,10 +554,10 @@ export class PGVectorStore extends VectorStore {
/**
* Method to ensure the existence of the table in the database. It creates
* the table if it does not already exist.
*
* @param dimensions Number of dimensions in your vector data type. For example, use 1536 for OpenAI's `text-embedding-3-small`. If not set, indexes like HNSW might not be used during query time.
* @returns Promise that resolves when the table has been ensured.
*/
async ensureTableInDatabase(): Promise<void> {
async ensureTableInDatabase(dimensions?: number): Promise<void> {
const vectorQuery =
this.extensionSchemaName == null
? "CREATE EXTENSION IF NOT EXISTS vector;"
Expand All @@ -568,12 +570,15 @@ export class PGVectorStore extends VectorStore {
this.extensionSchemaName == null
? "vector"
: `"${this.extensionSchemaName}"."vector"`;
const vectorColumnType = dimensions
? `${extensionName}(${dimensions})`
: extensionName;
const tableQuery = `
CREATE TABLE IF NOT EXISTS ${this.computedTableName} (
"${this.idColumnName}" uuid NOT NULL DEFAULT uuid_generate_v4() PRIMARY KEY,
"${this.contentColumnName}" text,
"${this.metadataColumnName}" jsonb,
"${this.vectorColumnName}" ${extensionName}
"${this.vectorColumnName}" ${vectorColumnType}
);
`;
await this.pool.query(vectorQuery);
Expand Down Expand Up @@ -633,7 +638,7 @@ export class PGVectorStore extends VectorStore {
texts: string[],
metadatas: object[] | object,
embeddings: EmbeddingsInterface,
dbConfig: PGVectorStoreArgs
dbConfig: PGVectorStoreArgs & { dimensions?: number }
): Promise<PGVectorStore> {
const docs = [];
for (let i = 0; i < texts.length; i += 1) {
Expand All @@ -660,7 +665,7 @@ export class PGVectorStore extends VectorStore {
static async fromDocuments(
docs: Document[],
embeddings: EmbeddingsInterface,
dbConfig: PGVectorStoreArgs
dbConfig: PGVectorStoreArgs & { dimensions?: number }
): Promise<PGVectorStore> {
const instance = await PGVectorStore.initialize(embeddings, dbConfig);
await instance.addDocuments(docs, { ids: dbConfig.ids });
Expand Down
Loading