-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support delete vectorstore operations (#1830)
* Adds optional id field to document base class to support upsert and delete operations * Rehydrate retrieved Chroma docs with ids * Allow ids or a filter type to be passed into vector store delete method * Remove id from TypeORM Document model * Revert base document class changes and use pinecone precendent to pass delete args * Add deletion tests * Update tests * Change vectorstore add docs methods to accept an options object, update docs * Update VectorStoreRetriever interface
- Loading branch information
1 parent
bbbe315
commit 99b1985
Showing
27 changed files
with
762 additions
and
93 deletions.
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Chroma } from "langchain/vectorstores/chroma"; | ||
import { OpenAIEmbeddings } from "langchain/embeddings/openai"; | ||
|
||
const embeddings = new OpenAIEmbeddings(); | ||
const vectorStore = new Chroma(embeddings, { | ||
collectionName: "godel-escher-bach", | ||
}); | ||
|
||
const documents = [ | ||
{ | ||
pageContent: `Tortoise: Labyrinth? Labyrinth? Could it Are we in the notorious Little | ||
Harmonic Labyrinth of the dreaded Majotaur?`, | ||
metadata: { | ||
speaker: "Tortoise", | ||
}, | ||
}, | ||
{ | ||
pageContent: "Achilles: Yiikes! What is that?", | ||
metadata: { | ||
speaker: "Achilles", | ||
}, | ||
}, | ||
{ | ||
pageContent: `Tortoise: They say-although I person never believed it myself-that an I | ||
Majotaur has created a tiny labyrinth sits in a pit in the middle of | ||
it, waiting innocent victims to get lost in its fears complexity. | ||
Then, when they wander and dazed into the center, he laughs and | ||
laughs at them-so hard, that he laughs them to death!`, | ||
metadata: { | ||
speaker: "Tortoise", | ||
}, | ||
}, | ||
{ | ||
pageContent: "Achilles: Oh, no!", | ||
metadata: { | ||
speaker: "Achilles", | ||
}, | ||
}, | ||
{ | ||
pageContent: "Tortoise: But it's only a myth. Courage, Achilles.", | ||
metadata: { | ||
speaker: "Tortoise", | ||
}, | ||
}, | ||
]; | ||
|
||
const ids = await vectorStore.addDocuments(documents); | ||
|
||
const response = await vectorStore.similaritySearch("scared", 2); | ||
console.log(response); | ||
/* | ||
[ | ||
Document { pageContent: 'Achilles: Oh, no!', metadata: {} }, | ||
Document { | ||
pageContent: 'Achilles: Yiikes! What is that?', | ||
metadata: { id: 1 } | ||
} | ||
] | ||
*/ | ||
|
||
// You can also pass a "filter" parameter instead | ||
await vectorStore.delete({ ids }); | ||
|
||
const response2 = await vectorStore.similaritySearch("scared", 2); | ||
console.log(response2); | ||
|
||
/* | ||
[] | ||
*/ |
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
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { SupabaseVectorStore } from "langchain/vectorstores/supabase"; | ||
import { OpenAIEmbeddings } from "langchain/embeddings/openai"; | ||
import { createClient } from "@supabase/supabase-js"; | ||
|
||
// First, follow set-up instructions at | ||
// https://js.langchain.com/docs/modules/indexes/vector_stores/integrations/supabase | ||
|
||
const privateKey = process.env.SUPABASE_PRIVATE_KEY; | ||
if (!privateKey) throw new Error(`Expected env var SUPABASE_PRIVATE_KEY`); | ||
|
||
const url = process.env.SUPABASE_URL; | ||
if (!url) throw new Error(`Expected env var SUPABASE_URL`); | ||
|
||
export const run = async () => { | ||
const client = createClient(url, privateKey); | ||
|
||
const embeddings = new OpenAIEmbeddings(); | ||
|
||
const store = new SupabaseVectorStore(embeddings, { | ||
client, | ||
tableName: "documents", | ||
}); | ||
|
||
const docs = [ | ||
{ pageContent: "hello", metadata: { b: 1, c: 9, stuff: "right" } }, | ||
{ pageContent: "hello", metadata: { b: 1, c: 9, stuff: "wrong" } }, | ||
]; | ||
|
||
const ids = await store.addDocuments(docs); | ||
|
||
const resultA = await store.similaritySearch("hello", 2); | ||
console.log(resultA); | ||
|
||
/* | ||
[ | ||
Document { pageContent: "hello", metadata: { b: 1, c: 9, stuff: "right" } }, | ||
Document { pageContent: "hello", metadata: { b: 1, c: 9, stuff: "wrong" } }, | ||
] | ||
*/ | ||
|
||
await store.delete({ ids }); | ||
|
||
const resultB = await store.similaritySearch("hello", 2); | ||
console.log(resultB); | ||
|
||
/* | ||
[] | ||
*/ | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import weaviate from "weaviate-ts-client"; | ||
import { WeaviateStore } from "langchain/vectorstores/weaviate"; | ||
import { OpenAIEmbeddings } from "langchain/embeddings/openai"; | ||
|
||
export async function run() { | ||
// Something wrong with the weaviate-ts-client types, so we need to disable | ||
const client = (weaviate as any).client({ | ||
scheme: process.env.WEAVIATE_SCHEME || "https", | ||
host: process.env.WEAVIATE_HOST || "localhost", | ||
apiKey: new (weaviate as any).ApiKey( | ||
process.env.WEAVIATE_API_KEY || "default" | ||
), | ||
}); | ||
|
||
// Create a store for an existing index | ||
const store = await WeaviateStore.fromExistingIndex(new OpenAIEmbeddings(), { | ||
client, | ||
indexName: "Test", | ||
metadataKeys: ["foo"], | ||
}); | ||
|
||
const docs = [{ pageContent: "see ya!", metadata: { foo: "bar" } }]; | ||
|
||
const ids = await store.addDocuments(docs); | ||
|
||
// Search the index without any filters | ||
const results = await store.similaritySearch("see ya!", 1); | ||
console.log(results); | ||
/* | ||
[ Document { pageContent: 'see ya!', metadata: { foo: 'bar' } } ] | ||
*/ | ||
|
||
await store.delete({ ids }); | ||
|
||
const results2 = await store.similaritySearch("see ya!", 1); | ||
console.log(results2); | ||
/* | ||
[] | ||
*/ | ||
} |
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
Oops, something went wrong.
99b1985
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.
Successfully deployed to the following URLs:
langchainjs-docs – ./
langchainjs-docs-git-main-langchain.vercel.app
langchainjs-docs-ruddy.vercel.app
langchainjs-docs-langchain.vercel.app
js.langchain.com