Skip to content
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

feat(community): enabling pass-through of InsertOptions to Astra #7343

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions libs/langchain-community/src/vectorstores/astradb.ts
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import {
CreateCollectionOptions,
Db,
InsertManyError,
InsertManyOptions,
} from "@datastax/astra-db-ts";

import {
@@ -135,19 +136,22 @@ export class AstraDBVectorStore extends VectorStore {
*
* @param vectors Vectors to save.
* @param documents The documents associated with the vectors.
* @param options Optional configuration for saving vectors:
* - `ids`: An array of unique identifiers for the documents. If not provided, IDs will be auto-generated.
* - `insertOptions`: Additional options to customize the `insertMany` operation (e.g., `ordered`, `bypassDocumentValidation`).
* @returns Promise that resolves when the vectors have been added.
*/
async addVectors(
vectors: number[][],
documents: Document[],
options?: string[]
options?: { ids?: string[]; insertOptions?: InsertManyOptions }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flagging that this is technically breaking

) {
if (!this.collection) {
throw new Error("Must connect to a collection before adding vectors");
}

const docs = vectors.map((embedding, idx) => ({
[this.idKey]: options?.[idx] ?? uuid.v4(),
[this.idKey]: options?.ids?.[idx] ?? uuid.v4(),
[this.contentKey]: documents[idx].pageContent,
$vector: embedding,
...documents[idx].metadata,
@@ -161,6 +165,7 @@ export class AstraDBVectorStore extends VectorStore {
try {
insertResults = await this.collection.insertMany(docs, {
ordered: false,
...options?.insertOptions,
});
} catch (error) {
if (isInsertManyError(error)) {
@@ -192,10 +197,15 @@ export class AstraDBVectorStore extends VectorStore {
* Method that adds documents to AstraDB.
*
* @param documents Array of documents to add to AstraDB.
* @param options Optional ids for the documents.
* @param options Optional configuration for saving documents:
* - `ids`: An array of unique identifiers for the documents. If not provided, IDs will be auto-generated.
* - `insertOptions`: Additional options to customize the `insertMany` operation (e.g., `ordered`, `bypassDocumentValidation`).
* @returns Promise that resolves the documents have been added.
*/
async addDocuments(documents: Document[], options?: string[]) {
async addDocuments(
documents: Document[],
options?: { ids?: string[]; insertOptions?: InsertManyOptions }
) {
if (!this.collection) {
throw new Error("Must connect to a collection before adding vectors");
}
Original file line number Diff line number Diff line change
@@ -158,8 +158,8 @@ describe.skip("AstraDBVectorStore", () => {
fail("Should have thrown error");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
expect(e.message).toContain(
"already exists with different collection options"
expect(e.message).toMatch(
/Collection already exists.*different settings/
);
}
}, 60000);
@@ -213,11 +213,40 @@ describe.skip("AstraDBVectorStore", () => {

await store.addDocuments([
{ pageContent: "upserted", metadata: { a: 1, _id: "123456789" } },
{ pageContent: "upserted", metadata: { a: 2 } },
]);

const collection = await db.collection(astraConfig.collection);
const doc = await collection.findOne({ _id: "123456789" });

expect(doc?.text).toEqual("upserted");
});

test("addDocuments with insertOptions (timeout)", async () => {
const store = new AstraDBVectorStore(new FakeEmbeddings(), {
...astraConfig,
collectionOptions: {
vector: {
dimension: 4,
metric: "cosine",
},
},
});
await store.initialize();

const documents = [
new Document({ pageContent: "Test document 1", metadata: { key: "value1" } }),
new Document({ pageContent: "Test document 2", metadata: { key: "value2" } }),
];

try {
// Setting maxTimeMS to 1 to trigger a timeout
await store.addDocuments(documents, {
insertOptions: { maxTimeMS: 1 },
});
fail("Should have thrown timeout error");
} catch (e: any) {
expect(e.message).toContain("Command timed out");
}
});
});
Loading