-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Changes from all commits
492c84c
756de86
b45c162
54abbc8
d644509
121a2b5
1a0fbaa
4055993
e519300
553f1cd
4f95cd1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
}); | ||
}); |
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.