Skip to content

Commit

Permalink
Add support for weaviate multi tenancy (#2231)
Browse files Browse the repository at this point in the history
* Adding support for weaviate multi-tenancy

* removed husky post install script

* Reverting changes to package.json

* Added clarity around the check for optional tenant when constructing a document

* Run lint and format

---------

Co-authored-by: jacoblee93 <jacoblee93@gmail.com>
  • Loading branch information
mbennett-talentnet and jacoblee93 authored Aug 11, 2023
1 parent b76ebb8 commit 0a90278
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
4 changes: 2 additions & 2 deletions langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@
"typesense": "^1.5.3",
"usearch": "^1.1.1",
"vectordb": "^0.1.4",
"weaviate-ts-client": "^1.0.0"
"weaviate-ts-client": "^1.4.0"
},
"peerDependencies": {
"@aws-sdk/client-dynamodb": "^3.310.0",
Expand Down Expand Up @@ -716,7 +716,7 @@
"typesense": "^1.5.3",
"usearch": "^1.1.1",
"vectordb": "^0.1.4",
"weaviate-ts-client": "^1.0.0"
"weaviate-ts-client": "^1.4.0"
},
"peerDependenciesMeta": {
"@aws-sdk/client-dynamodb": {
Expand Down
6 changes: 3 additions & 3 deletions langchain/src/vectorstores/tests/weaviate.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { WeaviateStore } from "../weaviate.js";
import { OpenAIEmbeddings } from "../../embeddings/openai.js";
import { Document } from "../../document.js";

test.skip("WeaviateStore", async () => {
test("WeaviateStore", async () => {
// Something wrong with the weaviate-ts-client types, so we need to disable
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const client = (weaviate as any).client({
Expand Down Expand Up @@ -90,7 +90,7 @@ test.skip("WeaviateStore", async () => {
]);
});

test.skip("WeaviateStore upsert + delete", async () => {
test("WeaviateStore upsert + delete", async () => {
// Something wrong with the weaviate-ts-client types, so we need to disable
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const client = (weaviate as any).client({
Expand Down Expand Up @@ -200,7 +200,7 @@ test.skip("WeaviateStore upsert + delete", async () => {
]);
});

test.skip("WeaviateStore delete with filter", async () => {
test("WeaviateStore delete with filter", async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const client = (weaviate as any).client({
scheme:
Expand Down
31 changes: 25 additions & 6 deletions langchain/src/vectorstores/weaviate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface WeaviateLibArgs {
indexName: string;
textKey?: string;
metadataKeys?: string[];
tenant?: string;
}

interface ResultRow {
Expand All @@ -80,6 +81,8 @@ export class WeaviateStore extends VectorStore {

private queryAttrs: string[];

private tenant?: string;

_vectorstoreType(): string {
return "weaviate";
}
Expand All @@ -91,6 +94,7 @@ export class WeaviateStore extends VectorStore {
this.indexName = args.indexName;
this.textKey = args.textKey || "text";
this.queryAttrs = [this.textKey];
this.tenant = args.tenant;

if (args.metadataKeys) {
this.queryAttrs = [
Expand Down Expand Up @@ -126,6 +130,7 @@ export class WeaviateStore extends VectorStore {

const flattenedMetadata = flattenObjectForWeaviate(document.metadata);
return {
...(this.tenant ? { tenant: this.tenant } : {}),
class: this.indexName,
id: documentIds[index],
vector: vectors[index],
Expand Down Expand Up @@ -163,18 +168,28 @@ export class WeaviateStore extends VectorStore {

if (ids && ids.length > 0) {
for (const id of ids) {
await this.client.data
let deleter = this.client.data
.deleter()
.withClassName(this.indexName)
.withId(id)
.do();
.withId(id);

if (this.tenant) {
deleter = deleter.withTenant(this.tenant);
}

await deleter.do();
}
} else if (filter) {
await this.client.batch
let batchDeleter = this.client.batch
.objectsBatchDeleter()
.withClassName(this.indexName)
.withWhere(filter.where)
.do();
.withWhere(filter.where);

if (this.tenant) {
batchDeleter = batchDeleter.withTenant(this.tenant);
}

await batchDeleter.do();
} else {
throw new Error(
`This method requires either "ids" or "filter" to be set in the input object`
Expand All @@ -198,6 +213,10 @@ export class WeaviateStore extends VectorStore {
})
.withLimit(k);

if (this.tenant) {
builder = builder.withTenant(this.tenant);
}

if (filter?.where) {
builder = builder.withWhere(filter.where);
}
Expand Down
15 changes: 13 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13231,7 +13231,7 @@ __metadata:
usearch: ^1.1.1
uuid: ^9.0.0
vectordb: ^0.1.4
weaviate-ts-client: ^1.0.0
weaviate-ts-client: ^1.4.0
yaml: ^2.2.1
zod: ^3.21.4
zod-to-json-schema: ^3.20.4
Expand Down Expand Up @@ -13298,7 +13298,7 @@ __metadata:
typesense: ^1.5.3
usearch: ^1.1.1
vectordb: ^0.1.4
weaviate-ts-client: ^1.0.0
weaviate-ts-client: ^1.4.0
peerDependenciesMeta:
"@aws-sdk/client-dynamodb":
optional: true
Expand Down Expand Up @@ -18992,6 +18992,17 @@ __metadata:
languageName: node
linkType: hard

"weaviate-ts-client@npm:^1.4.0":
version: 1.4.0
resolution: "weaviate-ts-client@npm:1.4.0"
dependencies:
graphql-request: ^5.1.0
isomorphic-fetch: ^3.0.0
uuid: ^9.0.0
checksum: aef68e7d77ac8b3d42e1a3dd3c4dcb7a0c74b96c1a3d7b11e52313c7dfa9a9b3fc9ba5f3048628d67193ca6c84bb8a7eca641dacfdc459cfe26ac2fe20b07f11
languageName: node
linkType: hard

"web-streams-polyfill@npm:4.0.0-beta.3":
version: 4.0.0-beta.3
resolution: "web-streams-polyfill@npm:4.0.0-beta.3"
Expand Down

1 comment on commit 0a90278

@vercel
Copy link

@vercel vercel bot commented on 0a90278 Aug 11, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.