-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
community[minor]: Improve Azure Cosmos DB vector store support #5197
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
492c84c
feat: add delete by filter
sinedied 756de86
feat: return added document ids
sinedied b45c162
fix: delete by id
sinedied 54abbc8
test: update integration tests
sinedied d644509
feat: add automatic index creation
sinedied 121a2b5
Update azure_cosmosdb.ts
jacoblee93 1a0fbaa
refactor: separate ids and filter params for delete()
sinedied 4055993
refactor: use a single param for delete
sinedied e519300
test: fix unit tests
sinedied 553f1cd
Merge branch 'main' of https://github.com/hwchase17/langchainjs into …
jacoblee93 4f95cd1
Address feedback
jacoblee93 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,13 +48,18 @@ describe.skip("AzureCosmosDBVectorStore", () => { | |
process.env.AZURE_COSMOSDB_CONNECTION_STRING! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey there! I've reviewed the code and noticed that the recent changes explicitly access an environment variable using |
||
); | ||
await client.connect(); | ||
const collection = client.db(DATABASE_NAME).collection(COLLECTION_NAME); | ||
const db = client.db(DATABASE_NAME); | ||
const collection = await db.createCollection(COLLECTION_NAME); | ||
|
||
// Make sure the database is empty | ||
await collection.deleteMany({}); | ||
|
||
// Delete any existing index | ||
await collection.dropIndex(INDEX_NAME); | ||
try { | ||
await collection.dropIndex(INDEX_NAME); | ||
} catch { | ||
// Ignore error if the index does not exist | ||
} | ||
|
||
await client.close(); | ||
}); | ||
|
@@ -64,6 +69,9 @@ describe.skip("AzureCosmosDBVectorStore", () => { | |
databaseName: DATABASE_NAME, | ||
collectionName: COLLECTION_NAME, | ||
indexName: INDEX_NAME, | ||
indexOptions: { | ||
numLists: 1, | ||
}, | ||
}); | ||
|
||
expect(vectorStore).toBeDefined(); | ||
|
@@ -75,9 +83,6 @@ describe.skip("AzureCosmosDBVectorStore", () => { | |
{ pageContent: "The house is open", metadata: { d: 1, e: 2 } }, | ||
]); | ||
|
||
// Make sure the index is created | ||
await vectorStore.createIndex(1); | ||
|
||
const results: Document[] = await vectorStore.similaritySearch( | ||
"sandwich", | ||
1 | ||
|
@@ -110,12 +115,12 @@ describe.skip("AzureCosmosDBVectorStore", () => { | |
databaseName: DATABASE_NAME, | ||
collectionName: COLLECTION_NAME, | ||
indexName: INDEX_NAME, | ||
indexOptions: { | ||
numLists: 1, | ||
}, | ||
} | ||
); | ||
|
||
// Make sure the index is created | ||
await vectorStore.createIndex(1); | ||
|
||
const output = await vectorStore.maxMarginalRelevanceSearch("foo", { | ||
k: 10, | ||
fetchK: 20, | ||
|
@@ -160,4 +165,90 @@ describe.skip("AzureCosmosDBVectorStore", () => { | |
|
||
await vectorStore.close(); | ||
}); | ||
|
||
test("deletes documents by id", async () => { | ||
const vectorStore = new AzureCosmosDBVectorStore(new OpenAIEmbeddings(), { | ||
databaseName: DATABASE_NAME, | ||
collectionName: COLLECTION_NAME, | ||
indexName: INDEX_NAME, | ||
indexOptions: { | ||
numLists: 1, | ||
}, | ||
}); | ||
|
||
const ids = await vectorStore.addDocuments([ | ||
{ pageContent: "This book is about politics", metadata: { a: 1 } }, | ||
{ | ||
pageContent: "The is the house of parliament", | ||
metadata: { d: 1, e: 2 }, | ||
}, | ||
]); | ||
|
||
// Delete document matching specified ids | ||
await vectorStore.delete({ ids: ids.slice(0, 1) }); | ||
|
||
const results = await vectorStore.similaritySearch("politics", 10); | ||
|
||
expect(results.length).toEqual(1); | ||
expect(results[0].pageContent).toEqual("The is the house of parliament"); | ||
|
||
await vectorStore.close(); | ||
}); | ||
|
||
test("deletes documents by filter", async () => { | ||
const vectorStore = new AzureCosmosDBVectorStore(new OpenAIEmbeddings(), { | ||
databaseName: DATABASE_NAME, | ||
collectionName: COLLECTION_NAME, | ||
indexName: INDEX_NAME, | ||
indexOptions: { | ||
numLists: 1, | ||
}, | ||
}); | ||
|
||
await vectorStore.addDocuments([ | ||
{ pageContent: "This book is about politics", metadata: { a: 1 } }, | ||
{ | ||
pageContent: "The is the house of parliament", | ||
metadata: { d: 1, e: 2 }, | ||
}, | ||
]); | ||
|
||
// Delete document matching the filter | ||
await vectorStore.delete({ filter: { a: 1 } }); | ||
|
||
const results = await vectorStore.similaritySearch("politics", 10); | ||
|
||
expect(results.length).toEqual(1); | ||
expect(results[0].pageContent).toEqual("The is the house of parliament"); | ||
|
||
await vectorStore.close(); | ||
}); | ||
|
||
test("deletes all documents", async () => { | ||
const vectorStore = new AzureCosmosDBVectorStore(new OpenAIEmbeddings(), { | ||
databaseName: DATABASE_NAME, | ||
collectionName: COLLECTION_NAME, | ||
indexName: INDEX_NAME, | ||
indexOptions: { | ||
numLists: 1, | ||
}, | ||
}); | ||
|
||
await vectorStore.addDocuments([ | ||
{ pageContent: "This book is about politics", metadata: { a: 1 } }, | ||
{ | ||
pageContent: "The is the house of parliament", | ||
metadata: { d: 1, e: 2 }, | ||
}, | ||
]); | ||
|
||
// Delete all documents | ||
await vectorStore.delete(); | ||
|
||
const results = await vectorStore.similaritySearch("politics", 10); | ||
|
||
expect(results.length).toEqual(0); | ||
|
||
await vectorStore.close(); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If this is hypothetically called before
connectPromise
is initialized, could we throw instead?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.
Ah never mind, I see below. Could we always just rely on
this.initPromise
instead?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.
Since
init
callscreateIndex
we need 2 different promise there otherwise there's an interlocking: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.
For the throw question, the issue is that the promise is created in the constructor and there's no way for the user to wait for it or know when the connect task is done.