Skip to content

Commit 0916a30

Browse files
authored
fix(community, docs): Update PGVector instructions, fix test (#7465)
1 parent fc14443 commit 0916a30

File tree

4 files changed

+86
-35
lines changed

4 files changed

+86
-35
lines changed

docs/core_docs/docs/integrations/vectorstores/pgvector.ipynb

+3-6
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@
7979
"\n",
8080
"```yaml\n",
8181
"# Run this command to start the database:\n",
82-
"# docker-compose up --build\n",
83-
"version: \"3\"\n",
82+
"# docker compose up\n",
8483
"services:\n",
8584
" db:\n",
8685
" hostname: 127.0.0.1\n",
@@ -92,11 +91,9 @@
9291
" - POSTGRES_DB=api\n",
9392
" - POSTGRES_USER=myuser\n",
9493
" - POSTGRES_PASSWORD=ChangeMe\n",
95-
" volumes:\n",
96-
" - ./init.sql:/docker-entrypoint-initdb.d/init.sql\n",
9794
"```\n",
9895
"\n",
99-
"And then in the same directory, run docker compose up to start the container.\n",
96+
"And then in the same directory, run `docker compose up` to start the container.\n",
10097
"\n",
10198
"You can find more information on how to setup pgvector in the [official repository](https://github.com/pgvector/pgvector/).\n",
10299
"\n",
@@ -626,4 +623,4 @@
626623
},
627624
"nbformat": 4,
628625
"nbformat_minor": 5
629-
}
626+
}

libs/langchain-community/src/vectorstores/pgvector.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,11 @@ export class PGVectorStore extends VectorStore {
947947
k
948948
);
949949

950-
const mmrDocs = mmrIndexes.map((index) => docs[index][0]);
950+
const mmrDocs = mmrIndexes.map((index) => {
951+
const doc = docs[index][0];
952+
delete doc.metadata[this.vectorColumnName];
953+
return docs[index][0];
954+
});
951955
return mmrDocs;
952956
}
953957
}

libs/langchain-community/src/vectorstores/tests/pgvector/docker-compose.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Run this command to start the database:
2-
# docker compose up --build
2+
# docker compose up
33
services:
44
db:
55
hostname: 127.0.0.1
@@ -11,5 +11,3 @@ services:
1111
- POSTGRES_DB=api
1212
- POSTGRES_USER=myuser
1313
- POSTGRES_PASSWORD=ChangeMe
14-
volumes:
15-
- ./init.sql:/docker-entrypoint-initdb.d/init.sql

libs/langchain-community/src/vectorstores/tests/pgvector/pgvector.int.test.ts

+77-25
Original file line numberDiff line numberDiff line change
@@ -74,33 +74,27 @@ describe("PGVectorStore", () => {
7474
expect(results[0].pageContent).toEqual("Cat drinks milk");
7575
});
7676

77-
test.only("Test MMR search", async () => {
77+
test("Test MMR search", async () => {
7878
const documents = [
7979
{
8080
pageContent: "hello",
8181
metadata: { a: 1 },
8282
},
83-
{
84-
pageContent: "Cat drinks milk",
85-
metadata: { a: 2 },
86-
},
8783
{
8884
pageContent: "foo",
8985
metadata: { a: 2 },
9086
},
91-
{ pageContent: "hi", metadata: { a: 1 } },
87+
{ pageContent: "bye", metadata: { a: 1 } },
9288
];
9389
await pgvectorVectorStore.addDocuments(documents);
9490
const results = await pgvectorVectorStore.maxMarginalRelevanceSearch(
95-
"milk",
91+
"hello",
9692
{
97-
k: 2,
93+
k: 4,
9894
}
9995
);
10096

101-
expect(results).toHaveLength(2);
102-
expect(results[0].pageContent).toEqual("Cat drinks milk");
103-
expect(results[1].pageContent).toEqual("foo");
97+
expect(results).toHaveLength(3);
10498
});
10599

106100
test("PGvector can save documents with a list greater than default chunk size", async () => {
@@ -157,16 +151,28 @@ describe("PGVectorStore", () => {
157151

158152
expect(result.length).toEqual(2);
159153
expect(result).toEqual([
160-
{ pageContent: "Lorem Ipsum", metadata: { a: 100 } },
161-
{ pageContent: "Lorem Ipsum", metadata: { a: 300 } },
154+
{
155+
id: expect.any(String),
156+
pageContent: "Lorem Ipsum",
157+
metadata: { a: 100 },
158+
},
159+
{
160+
id: expect.any(String),
161+
pageContent: "Lorem Ipsum",
162+
metadata: { a: 300 },
163+
},
162164
]);
163165

164166
const result2 = await pgvectorVectorStore.similaritySearch("hello", 2, {
165167
a: 200,
166168
});
167169
expect(result2.length).toEqual(1);
168170
expect(result2).toEqual([
169-
{ pageContent: "Lorem Ipsum", metadata: { a: 200 } },
171+
{
172+
id: expect.any(String),
173+
pageContent: "Lorem Ipsum",
174+
metadata: { a: 200 },
175+
},
170176
]);
171177

172178
const result3 = await pgvectorVectorStore.similaritySearch("hello", 3);
@@ -191,8 +197,16 @@ describe("PGVectorStore", () => {
191197

192198
expect(result.length).toEqual(2);
193199
expect(result).toEqual([
194-
{ pageContent: "Lorem Ipsum", metadata: { a: ["tag1", "tag2"] } },
195-
{ pageContent: "Lorem Ipsum", metadata: { a: ["tag1"] } },
200+
{
201+
id: expect.any(String),
202+
pageContent: "Lorem Ipsum",
203+
metadata: { a: ["tag1", "tag2"] },
204+
},
205+
{
206+
id: expect.any(String),
207+
pageContent: "Lorem Ipsum",
208+
metadata: { a: ["tag1"] },
209+
},
196210
]);
197211

198212
const result2 = await pgvectorVectorStore.similaritySearch("hello", 2, {
@@ -202,14 +216,28 @@ describe("PGVectorStore", () => {
202216
});
203217
expect(result2.length).toEqual(2);
204218
expect(result2).toEqual([
205-
{ pageContent: "Lorem Ipsum", metadata: { a: ["tag1", "tag2"] } },
206-
{ pageContent: "Lorem Ipsum", metadata: { a: ["tag2"] } },
219+
{
220+
id: expect.any(String),
221+
pageContent: "Lorem Ipsum",
222+
metadata: { a: ["tag1", "tag2"] },
223+
},
224+
{
225+
id: expect.any(String),
226+
pageContent: "Lorem Ipsum",
227+
metadata: { a: ["tag2"] },
228+
},
207229
]);
208230

209231
const result3 = await pgvectorVectorStore.similaritySearch("hello", 3);
210232

211233
expect(result3.length).toEqual(3);
212-
expect(result3).toEqual(documents);
234+
expect(result3).toEqual(
235+
documents.map((doc) => {
236+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, no-param-reassign
237+
(doc as any).id = expect.any(String);
238+
return doc;
239+
})
240+
);
213241
});
214242

215243
test("PGvector can delete document by id", async () => {
@@ -439,16 +467,28 @@ describe("PGVectorStore with collection", () => {
439467

440468
expect(result.length).toEqual(2);
441469
expect(result).toEqual([
442-
{ pageContent: "Lorem Ipsum", metadata: { a: 100 } },
443-
{ pageContent: "Lorem Ipsum", metadata: { a: 300 } },
470+
{
471+
id: expect.any(String),
472+
pageContent: "Lorem Ipsum",
473+
metadata: { a: 100 },
474+
},
475+
{
476+
id: expect.any(String),
477+
pageContent: "Lorem Ipsum",
478+
metadata: { a: 300 },
479+
},
444480
]);
445481

446482
const result2 = await pgvectorVectorStore.similaritySearch("hello", 2, {
447483
a: 200,
448484
});
449485
expect(result2.length).toEqual(1);
450486
expect(result2).toEqual([
451-
{ pageContent: "Lorem Ipsum", metadata: { a: 200 } },
487+
{
488+
id: expect.any(String),
489+
pageContent: "Lorem Ipsum",
490+
metadata: { a: 200 },
491+
},
452492
]);
453493

454494
const result3 = await pgvectorVectorStore.similaritySearch("hello", 3);
@@ -667,16 +707,28 @@ describe("PGVectorStore with schema", () => {
667707

668708
expect(result.length).toEqual(2);
669709
expect(result).toEqual([
670-
{ pageContent: "Lorem Ipsum", metadata: { a: 100 } },
671-
{ pageContent: "Lorem Ipsum", metadata: { a: 300 } },
710+
{
711+
id: expect.any(String),
712+
pageContent: "Lorem Ipsum",
713+
metadata: { a: 100 },
714+
},
715+
{
716+
id: expect.any(String),
717+
pageContent: "Lorem Ipsum",
718+
metadata: { a: 300 },
719+
},
672720
]);
673721

674722
const result2 = await pgvectorVectorStore.similaritySearch("hello", 2, {
675723
a: 200,
676724
});
677725
expect(result2.length).toEqual(1);
678726
expect(result2).toEqual([
679-
{ pageContent: "Lorem Ipsum", metadata: { a: 200 } },
727+
{
728+
id: expect.any(String),
729+
pageContent: "Lorem Ipsum",
730+
metadata: { a: 200 },
731+
},
680732
]);
681733

682734
const result3 = await pgvectorVectorStore.similaritySearch("hello", 3);

0 commit comments

Comments
 (0)