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): Add delete and allow default row id in libsql #7053

Merged
merged 5 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
66 changes: 44 additions & 22 deletions libs/langchain-community/src/vectorstores/libsql.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Client } from "@libsql/client";
import { VectorStore } from "@langchain/core/vectorstores";
import type { EmbeddingsInterface } from "@langchain/core/embeddings";
import { Document } from "@langchain/core/documents";
import type { EmbeddingsInterface } from "@langchain/core/embeddings";
import { VectorStore } from "@langchain/core/vectorstores";
import type { Client, InStatement } from "@libsql/client";

/**
* Interface for LibSQLVectorStore configuration options.
Expand Down Expand Up @@ -82,23 +82,17 @@

for (let i = 0; i < rows.length; i += batchSize) {
const chunk = rows.slice(i, i + batchSize);
const insertQueries = chunk.map((row) => ({
sql: `INSERT INTO ${this.table} (content, metadata, ${this.column}) VALUES (?, ?, ?) RETURNING id`,
args: [row.content, row.metadata, row.embedding],

const insertQueries: InStatement[] = chunk.map((row) => ({
sql: `INSERT INTO ${this.table} (content, metadata, ${this.column}) VALUES (:content, :metadata, vector(:embedding)) RETURNING ${this.table}.rowid AS id`,
args: row,
}));

const results = await this.db.batch(insertQueries);

for (const result of results) {
if (
result &&
result.rows &&
result.rows.length > 0 &&
result.rows[0].id != null
) {
ids.push(result.rows[0].id.toString());
}
}
ids.push(
...results.flatMap((result) => result.rows.map((row) => String(row.id)))
);
}

return ids;
Expand All @@ -123,11 +117,12 @@

const queryVector = `[${query.join(",")}]`;

const sql = `
SELECT ${this.table}.id, ${this.table}.content, ${this.table}.metadata, vector_distance_cos(${this.table}.${this.column}, vector('${queryVector}')) AS distance
FROM vector_top_k('idx_${this.table}_${this.column}', vector('${queryVector}'), ${k}) AS top_k
JOIN ${this.table} ON top_k.rowid = ${this.table}.id
`;
const sql: InStatement = {
sql: `SELECT ${this.table}.rowid as id, ${this.table}.content, ${this.table}.metadata, vector_distance_cos(${this.table}.${this.column}, vector(:queryVector)) AS distance
FROM vector_top_k('idx_${this.table}_${this.column}', vector(:queryVector), CAST(:k AS INTEGER)) as top_k
JOIN ${this.table} ON top_k.rowid = ${this.table}.rowid`,
args: { queryVector, k },
};

const results = await this.db.execute(sql);

Expand All @@ -136,7 +131,7 @@
const metadata = JSON.parse(row.metadata);

const doc = new Document({
id: row.id,
id: String(row.id),
metadata,
pageContent: row.content,
});
Expand All @@ -145,6 +140,33 @@
});
}

/**
* Deletes vectors from the store.
* @param {Object} params - Delete parameters.
* @param {string[] | number[]} [params.ids] - The ids of the vectors to delete.
* @returns {Promise<void>}
*/
async delete(params: {
ids?: string[] | number[];
deleteAll?: boolean;
}): Promise<void> {
if (params.deleteAll) {
await this.db.execute(`DELETE FROM ${this.table}`);
Copy link
Collaborator

Choose a reason for hiding this comment

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

We usually gate this on a deleteAll flag - will add

return;

Check failure on line 155 in libs/langchain-community/src/vectorstores/libsql.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unnecessary return statement
} else if (params.ids !== undefined) {
await this.db.batch(
params.ids.map((id) => ({
sql: `DELETE FROM ${this.table} WHERE rowid = :id`,
args: { id },
}))
);
} else {
throw new Error(
`You must provide an "ids" parameter or a "deleteAll" parameter.`
);
}
}

/**
* Creates a new LibSQLVectorStore instance from texts.
* @param {string[]} texts - The texts to add to the store.
Expand Down
Loading
Loading