Skip to content

Commit

Permalink
feat(pinecone): add support for delete by metadata (#3193)
Browse files Browse the repository at this point in the history
  • Loading branch information
omikader committed Nov 8, 2023
1 parent 12e9811 commit 7ed956e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 5 additions & 2 deletions langchain/src/vectorstores/pinecone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ export interface PineconeLibArgs extends AsyncCallerParams {

/**
* Type that defines the parameters for the delete operation in the
* PineconeStore class. It includes ids, deleteAll flag, and namespace.
* PineconeStore class. It includes ids, filter, deleteAll flag, and namespace.
*/
export type PineconeDeleteParams = {
ids?: string[];
deleteAll?: boolean;
filter?: object;
namespace?: string;
};

Expand Down Expand Up @@ -161,7 +162,7 @@ export class PineconeStore extends VectorStore {
* @returns Promise that resolves when the delete operation is complete.
*/
async delete(params: PineconeDeleteParams): Promise<void> {
const { deleteAll, ids } = params;
const { deleteAll, ids, filter } = params;
const namespace = this.pineconeIndex.namespace(this.namespace ?? "");

if (deleteAll) {
Expand All @@ -172,6 +173,8 @@ export class PineconeStore extends VectorStore {
const batchIds = ids.slice(i, i + batchSize);
await namespace.deleteMany(batchIds);
}
} else if (filter) {
await namespace.deleteMany(filter);
} else {
throw new Error("Either ids or delete_all must be provided.");
}
Expand Down
18 changes: 18 additions & 0 deletions langchain/src/vectorstores/tests/pinecone.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,24 @@ describe("PineconeStore", () => {
expect(results2.length).toEqual(1);
});

test("delete by metadata filter", async () => {
const pageContent = faker.lorem.sentence(5);

await pineconeStore.addDocuments([
{ pageContent, metadata: { foo: "bar" } },
{ pageContent, metadata: { foo: "baz" } },
]);

const results = await pineconeStore.similaritySearch(pageContent, 2);
expect(results.length).toEqual(2);

await pineconeStore.delete({ filter: { foo: "bar" } });

const results2 = await pineconeStore.similaritySearch(pageContent, 2);
expect(results2.length).toEqual(1);
expect(results2[0].metadata).toMatchObject({ foo: "baz" });
});

test("delete all", async () => {
const pageContent = faker.lorem.sentence(5);
const id = uuid.v4();
Expand Down

0 comments on commit 7ed956e

Please sign in to comment.