Skip to content
Closed
Show file tree
Hide file tree
Changes from 10 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
9 changes: 9 additions & 0 deletions docs/core_docs/docs/integrations/vectorstores/neon.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ const vectorStore = await NeonPostgres.initialize(embeddings, {
});
```

### Optional
You can put the vector store table in custom db schema by passing `schemaName` to the config options during initialization.
```typescript
const vectorStore = await NeonPostgres.initialize(embeddings, {
connectionString: NEON_POSTGRES_CONNECTION_STRING,
schemaName: "mycustomschema"
});
```

## Usage

import CodeBlock from "@theme/CodeBlock";
Expand Down
19 changes: 15 additions & 4 deletions libs/langchain-community/src/vectorstores/neon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Metadata = Record<string, string | number | Record<"in", string[]>>;
export interface NeonPostgresArgs {
connectionString: string;
tableName?: string;
schemaName?: string;
columns?: {
idColumnName?: string;
vectorColumnName?: string;
Expand All @@ -35,6 +36,8 @@ export class NeonPostgres extends VectorStore {

tableName: string;

schemaName?: string;

idColumnName: string;

vectorColumnName: string;
Expand All @@ -60,6 +63,7 @@ export class NeonPostgres extends VectorStore {

this.neonConnectionString = config.connectionString;
this.tableName = config.tableName ?? "vectorstore_documents";
this.schemaName = config.schemaName;
this.filter = config.filter;

this.vectorColumnName = config.columns?.vectorColumnName ?? "embedding";
Expand All @@ -68,6 +72,12 @@ export class NeonPostgres extends VectorStore {
this.metadataColumnName = config.columns?.metadataColumnName ?? "metadata";
}

get computedTableName() {
return typeof this.schemaName !== "string"
? `${this.tableName}`
: `"${this.schemaName}"."${this.tableName}"`;
}

/**
* Static method to create a new `NeonPostgres` instance from a
* connection. It creates a table if one does not exist.
Expand Down Expand Up @@ -102,7 +112,7 @@ export class NeonPostgres extends VectorStore {
return `(${row.map((_, j) => `$${base + 1 + j}`)})`;
});
const queryString = `
INSERT INTO ${this.tableName} (
INSERT INTO ${this.computedTableName} (
${useIdColumn ? `${this.idColumnName},` : ""}
${this.contentColumnName},
${this.vectorColumnName},
Expand Down Expand Up @@ -222,7 +232,7 @@ export class NeonPostgres extends VectorStore {
: "";
const queryString = `
SELECT *, ${this.vectorColumnName} <=> $1 as "_distance"
FROM ${this.tableName}
FROM ${this.computedTableName}
${whereClause}
ORDER BY "_distance" ASC
LIMIT $2;`;
Expand Down Expand Up @@ -276,7 +286,7 @@ export class NeonPostgres extends VectorStore {

if (params.ids !== undefined) {
await sql(
`DELETE FROM ${this.tableName}
`DELETE FROM ${this.computedTableName}
WHERE ${this.idColumnName}
IN (${params.ids.map((_, idx) => `$${idx + 1}`)})`,
params.ids
Expand All @@ -297,8 +307,9 @@ export class NeonPostgres extends VectorStore {

await sql(`CREATE EXTENSION IF NOT EXISTS vector;`);
await sql(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`);

await sql(`
CREATE TABLE IF NOT EXISTS ${this.tableName} (
CREATE TABLE IF NOT EXISTS ${this.computedTableName} (
${this.idColumnName} uuid NOT NULL DEFAULT uuid_generate_v4() PRIMARY KEY,
${this.contentColumnName} text,
${this.metadataColumnName} jsonb,
Expand Down
31 changes: 31 additions & 0 deletions libs/langchain-community/src/vectorstores/tests/neon.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { test, expect, describe } from "@jest/globals";
import { FakeEmbeddings } from "@langchain/core/utils/testing";
import { NeonPostgres } from "../neon.js";

describe("NeonPostgres schemaName handling", () => {
const fakeEmbeddings = new FakeEmbeddings();

const baseConfig = {
connectionString: "postgres://user:pass@localhost:5432/db",
tableName: "vector_store",
};

test("uses only tableName when schemaName is not provided", () => {
const store = new NeonPostgres(fakeEmbeddings, {
...baseConfig,
});

expect(store.schemaName).toBeUndefined();
expect(store.computedTableName).toBe("vector_store");
});

test("uses schemaName and tableName when schemaName is provided", () => {
const store = new NeonPostgres(fakeEmbeddings, {
...baseConfig,
schemaName: "custom_schema",
});

expect(store.schemaName).toBe("custom_schema");
expect(store.computedTableName).toBe('"custom_schema"."vector_store"');
});
});
Loading