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): Remove required param from LanceDB integration #6706

Merged
merged 22 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
17 changes: 17 additions & 0 deletions examples/src/indexes/vector_stores/lancedb/fromDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ const loader = new TextLoader("src/document_loaders/example_data/example.txt");
const docs = await loader.load();

export const run = async () => {
const vectorStore = await LanceDB.fromDocuments(
docs,
new OpenAIEmbeddings(),
);

const resultOne = await vectorStore.similaritySearch("hello world", 1);
console.log(resultOne);

// [
// Document {
// pageContent: 'Foo\nBar\nBaz\n\n',
// metadata: { source: 'src/document_loaders/example_data/example.txt' }
// }
// ]
};

export const run_with_existing_table = async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "lancedb-"));
const db = await connect(dir);
const table = await db.createTable("vectors", [
Expand Down
12 changes: 12 additions & 0 deletions examples/src/indexes/vector_stores/lancedb/fromTexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ import * as path from "node:path";
import os from "node:os";

export const run = async () => {
const vectorStore = await LanceDB.fromTexts(
["Hello world", "Bye bye", "hello nice world"],
[{ id: 2 }, { id: 1 }, { id: 3 }],
new OpenAIEmbeddings(),
);

const resultOne = await vectorStore.similaritySearch("hello world", 1);
console.log(resultOne);
// [ Document { pageContent: 'hello nice world', metadata: { id: 3 } } ]
};

export const run_with_existing_table = async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "lancedb-"));
const db = await connect(dir);
const table = await db.createTable("vectors", [
Expand Down
2 changes: 1 addition & 1 deletion libs/langchain-community/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
"typescript": "~5.1.6",
"typesense": "^1.5.3",
"usearch": "^1.1.1",
"vectordb": "^0.1.4",
"vectordb": "^0.9.0",
"voy-search": "0.6.2",
"weaviate-ts-client": "^1.4.0",
"web-auth-library": "^1.0.3",
Expand Down
43 changes: 34 additions & 9 deletions libs/langchain-community/src/vectorstores/lancedb.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Table } from "vectordb";
import { connect, Table, Connection, WriteMode } from "vectordb";
import type { EmbeddingsInterface } from "@langchain/core/embeddings";
import { VectorStore } from "@langchain/core/vectorstores";
import { Document } from "@langchain/core/documents";
Expand All @@ -8,8 +8,11 @@ import { Document } from "@langchain/core/documents";
* table and an optional textKey.
*/
export type LanceDBArgs = {
table: Table;
table?: Table;
textKey?: string;
uri?: string;
table_name?: string;
mode?: string;
jacoblee93 marked this conversation as resolved.
Show resolved Hide resolved
};

/**
Expand All @@ -18,15 +21,24 @@ export type LanceDBArgs = {
* embeddings.
*/
export class LanceDB extends VectorStore {
private table: Table;
private table?: Table;

private textKey: string;

constructor(embeddings: EmbeddingsInterface, args: LanceDBArgs) {
super(embeddings, args);
this.table = args.table;
private uri: string;

private table_name: string;

private mode: string;

constructor(embeddings: EmbeddingsInterface, args?: LanceDBArgs) {
super(embeddings, args || {});
this.table = args?.table || undefined;
this.embeddings = embeddings;
jacoblee93 marked this conversation as resolved.
Show resolved Hide resolved
this.textKey = args.textKey || "text";
this.textKey = args?.textKey || "text";
this.uri = args?.uri || "~/lancedb";
this.table_name = args?.table_name || "langchain";
this.mode = args?.mode || WriteMode.Overwrite;
}

/**
Expand Down Expand Up @@ -71,6 +83,14 @@ export class LanceDB extends VectorStore {
});
data.push(record);
}
if (!this.table) {
const db: Connection = await connect(this.uri);
this.table = await db.createTable(this.table_name, data, {
writeMode: this.mode,
});

return;
}
await this.table.add(data);
}

Expand All @@ -85,6 +105,11 @@ export class LanceDB extends VectorStore {
query: number[],
k: number
): Promise<[Document, number][]> {
if (!this.table) {
throw new Error(
"Table not found. Please add vectors to the table first."
);
}
const results = await this.table.search(query).limit(k).execute();

const docsAndScore: [Document, number][] = [];
Expand Down Expand Up @@ -119,7 +144,7 @@ export class LanceDB extends VectorStore {
texts: string[],
metadatas: object[] | object,
embeddings: EmbeddingsInterface,
dbConfig: LanceDBArgs
dbConfig?: LanceDBArgs
): Promise<LanceDB> {
const docs: Document[] = [];
for (let i = 0; i < texts.length; i += 1) {
Expand All @@ -143,7 +168,7 @@ export class LanceDB extends VectorStore {
static async fromDocuments(
docs: Document[],
embeddings: EmbeddingsInterface,
dbConfig: LanceDBArgs
dbConfig?: LanceDBArgs
): Promise<LanceDB> {
const instance = new this(embeddings, dbConfig);
await instance.addDocuments(docs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,27 @@ describe("LanceDB", () => {
expect(resultsTwo.length).toBe(5);
});
});

describe("LanceDB empty schema", () => {
test("Test fromTexts + addDocuments", async () => {
const embeddings = new OpenAIEmbeddings();
const vectorStore = await LanceDB.fromTexts(
["hello bye", "hello world", "bye bye"],
[{ id: 1 }, { id: 2 }, { id: 3 }],
embeddings
);

const results = await vectorStore.similaritySearch("hello bye", 10);
expect(results.length).toBe(3);

await vectorStore.addDocuments([
new Document({
pageContent: "a new world",
metadata: { id: 4 },
}),
]);

const resultsTwo = await vectorStore.similaritySearch("hello bye", 10);
expect(resultsTwo.length).toBe(4);
});
});
Loading