Skip to content

Commit a38282f

Browse files
dudanogueirahntrl
authored andcommitted
chore(weaviate/v1): patch changes
* #8974 * #8971 * #8717 * #8702 * #8665
1 parent 57b1f48 commit a38282f

File tree

5 files changed

+159
-53
lines changed

5 files changed

+159
-53
lines changed

libs/providers/langchain-weaviate/src/tests/generate.int.test.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,24 @@ const collectionName = "GenerateCollection";
1717
beforeAll(async () => {
1818
expect(process.env.WEAVIATE_URL).toBeDefined();
1919
expect(process.env.WEAVIATE_URL!.length).toBeGreaterThan(0);
20-
client = await weaviate.connectToWeaviateCloud(process.env.WEAVIATE_URL!, {
21-
authCredentials: new weaviate.ApiKey(process.env.WEAVIATE_API_KEY || ""),
22-
headers: {
23-
"X-OpenAI-Api-Key": process.env.OPENAI_API_KEY || "",
24-
"X-Cohere-Api-Key": process.env.COHERE_API_KEY || "",
25-
},
26-
});
20+
if (process.env.WEAVIATE_URL === "local") {
21+
client = await weaviate.connectToLocal({
22+
headers: {
23+
"X-OpenAI-Api-Key": process.env.OPENAI_API_KEY || "",
24+
"X-Cohere-Api-Key": process.env.COHERE_API_KEY || "",
25+
},
26+
});
27+
} else {
28+
client = await weaviate.connectToWeaviateCloud(process.env.WEAVIATE_URL!, {
29+
authCredentials: new weaviate.ApiKey(process.env.WEAVIATE_API_KEY || ""),
30+
headers: {
31+
"X-OpenAI-Api-Key": process.env.OPENAI_API_KEY || "",
32+
"X-Cohere-Api-Key": process.env.COHERE_API_KEY || "",
33+
},
34+
});
35+
}
36+
console.log("Connecting to Weaviate at", process.env.WEAVIATE_URL);
37+
console.log("Ready?", await client.isReady());
2738
});
2839

2940
test("Generate with limit", async () => {

libs/providers/langchain-weaviate/src/tests/hybridsearch.int.test.ts

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,25 @@ const collectionName = "MyCollection";
1919
beforeAll(async () => {
2020
expect(process.env.WEAVIATE_URL).toBeDefined();
2121
expect(process.env.WEAVIATE_URL!.length).toBeGreaterThan(0);
22-
client = await weaviate.connectToWeaviateCloud(process.env.WEAVIATE_URL!, {
23-
authCredentials: new weaviate.ApiKey(process.env.WEAVIATE_API_KEY || ""),
24-
headers: {
25-
"X-OpenAI-Api-Key": process.env.OPENAI_API_KEY || "",
26-
"X-Cohere-Api-Key": process.env.COHERE_API_KEY || "",
27-
},
28-
});
22+
if (process.env.WEAVIATE_URL === "local") {
23+
client = await weaviate.connectToLocal({
24+
headers: {
25+
"X-OpenAI-Api-Key": process.env.OPENAI_API_KEY || "",
26+
"X-Cohere-Api-Key": process.env.COHERE_API_KEY || "",
27+
},
28+
});
29+
} else {
30+
client = await weaviate.connectToWeaviateCloud(process.env.WEAVIATE_URL!, {
31+
authCredentials: new weaviate.ApiKey(process.env.WEAVIATE_API_KEY || ""),
32+
headers: {
33+
"X-OpenAI-Api-Key": process.env.OPENAI_API_KEY || "",
34+
"X-Cohere-Api-Key": process.env.COHERE_API_KEY || "",
35+
},
36+
});
37+
}
38+
await client.collections.delete("MyCollection");
39+
console.log("Connecting to Weaviate at", process.env.WEAVIATE_URL);
40+
console.log("Ready?", await client.isReady());
2941
});
3042

3143
test("Hybridsearch with limit", async () => {
@@ -48,6 +60,7 @@ test("Hybridsearch with limit", async () => {
4860
const weaviateArgs = {
4961
client,
5062
schema,
63+
indexName: "HybridSearchWithLimit",
5164
};
5265
try {
5366
const store = await WeaviateStore.fromTexts(
@@ -70,15 +83,16 @@ test("Hybridsearch with limit", async () => {
7083
new Document({
7184
id: expect.any(String) as unknown as string,
7285
pageContent: "hello world",
73-
metadata: { foo: "bar", title: undefined },
86+
metadata: { foo: "bar", title: undefined, score: expect.any(Number) },
7487
}),
7588
]);
7689
} finally {
77-
await client.collections.delete(weaviateArgs.schema.name);
90+
await client.collections.delete(weaviateArgs.indexName);
7891
}
7992
});
8093

8194
test("Hybridsearch with named vectors", async () => {
95+
await client.collections.delete(collectionName);
8296
const embeddings = new OpenAIEmbeddings();
8397
const schemaWithNamedVectors = {
8498
name: collectionName,
@@ -120,7 +134,8 @@ test("Hybridsearch with named vectors", async () => {
120134
weaviateArgs
121135
);
122136

123-
const results = await store.hybridSearch("title", {
137+
// how are you in portuguese
138+
const results = await store.hybridSearch("como você está?", {
124139
limit: 1,
125140
targetVector: ["title"],
126141
});
@@ -129,7 +144,11 @@ test("Hybridsearch with named vectors", async () => {
129144
new Document({
130145
id: expect.any(String) as unknown as string,
131146
pageContent: "how are you",
132-
metadata: { foo: "qux", title: "another title" },
147+
metadata: {
148+
foo: "qux",
149+
title: "another title",
150+
score: expect.any(Number),
151+
},
133152
}),
134153
]);
135154
} finally {
@@ -184,14 +203,57 @@ test("Hybridsearch with rerank", async () => {
184203
new Document({
185204
id: expect.any(String) as unknown as string,
186205
pageContent: "hello world",
187-
metadata: { foo: "bar", title: "hello world" },
206+
metadata: {
207+
foo: "bar",
208+
title: "hello world",
209+
rerankScore: expect.any(Number),
210+
score: expect.any(Number),
211+
},
188212
}),
189213
]);
190214
} finally {
191215
await client.collections.delete(weaviateArgs.schema.name);
192216
}
193217
});
194218

219+
test("HybridSearch providing vector and return metadata", async () => {
220+
const embeddings = new OpenAIEmbeddings();
221+
const weaviateArgs = {
222+
client,
223+
indexName: "HybridSearchProvidingVector",
224+
};
225+
try {
226+
const store = await WeaviateStore.fromTexts(
227+
["a dog can bark", "what more?", "How many is enough?"],
228+
[{ foo: "bar" }],
229+
embeddings,
230+
weaviateArgs
231+
);
232+
233+
const results = await store.hybridSearch("domestic dog", {
234+
alpha: 0.75,
235+
autoLimit: 1,
236+
vector: await embeddings.embedQuery("domestic dog"),
237+
returnMetadata: ["explainScore", "creationTime"],
238+
});
239+
240+
expect(results).toEqual([
241+
new Document({
242+
id: expect.any(String) as unknown as string,
243+
pageContent: "a dog can bark",
244+
metadata: {
245+
score: 1,
246+
foo: "bar",
247+
explainScore: expect.any(String),
248+
creationTime: expect.any(Date),
249+
},
250+
}),
251+
]);
252+
} finally {
253+
await client.collections.delete(weaviateArgs.indexName);
254+
}
255+
});
256+
195257
afterAll(async () => {
196258
await client.close();
197259
});

libs/providers/langchain-weaviate/src/tests/translator.int.test.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,24 @@ const indexName = "TestTranslate";
1515
beforeAll(async () => {
1616
expect(process.env.WEAVIATE_URL).toBeDefined();
1717
expect(process.env.WEAVIATE_URL!.length).toBeGreaterThan(0);
18-
19-
client = await weaviate.connectToWeaviateCloud(process.env.WEAVIATE_URL!, {
20-
authCredentials: new weaviate.ApiKey(process.env.WEAVIATE_API_KEY || ""),
21-
headers: {
22-
"X-OpenAI-Api-Key": process.env.OPENAI_API_KEY || "",
23-
"X-Azure-Api-Key": process.env.AZURE_OPENAI_API_KEY || "",
24-
},
25-
});
18+
if (process.env.WEAVIATE_URL === "local") {
19+
client = await weaviate.connectToLocal({
20+
headers: {
21+
"X-OpenAI-Api-Key": process.env.OPENAI_API_KEY || "",
22+
"X-Cohere-Api-Key": process.env.COHERE_API_KEY || "",
23+
},
24+
});
25+
} else {
26+
client = await weaviate.connectToWeaviateCloud(process.env.WEAVIATE_URL!, {
27+
authCredentials: new weaviate.ApiKey(process.env.WEAVIATE_API_KEY || ""),
28+
headers: {
29+
"X-OpenAI-Api-Key": process.env.OPENAI_API_KEY || "",
30+
"X-Cohere-Api-Key": process.env.COHERE_API_KEY || "",
31+
},
32+
});
33+
}
34+
console.log("Connecting to Weaviate at", process.env.WEAVIATE_URL);
35+
console.log("Ready?", await client.isReady());
2636
});
2737

2838
test("Weaviate Self Query Retriever Test", async () => {
@@ -376,19 +386,7 @@ test("Weaviate Vector Store Self Query Retriever Test With Default Filter And Me
376386
const llm = new ChatOpenAI({
377387
model: "gpt-3.5-turbo",
378388
});
379-
expect(process.env.WEAVIATE_URL).toBeDefined();
380-
expect(process.env.WEAVIATE_URL!.length).toBeGreaterThan(0);
381389

382-
const client = await weaviate.connectToWeaviateCloud(
383-
process.env.WEAVIATE_URL!,
384-
{
385-
authCredentials: new weaviate.ApiKey(process.env.WEAVIATE_API_KEY || ""),
386-
headers: {
387-
"X-OpenAI-Api-Key": process.env.OPENAI_API_KEY || "",
388-
"X-Azure-Api-Key": process.env.AZURE_OPENAI_API_KEY || "",
389-
},
390-
}
391-
);
392390
const weaviateArgs = {
393391
client,
394392
indexName,

libs/providers/langchain-weaviate/src/tests/vectorstores.int.test.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import dotenv from "dotenv";
44

55
import { OpenAIEmbeddings } from "@langchain/openai";
66
import { Document } from "@langchain/core/documents";
7-
7+
import { FakeEmbeddings } from "@langchain/core/utils/testing";
88
import { WeaviateStore } from "../vectorstores.js";
99

1010
dotenv.config();
@@ -13,13 +13,24 @@ let client: WeaviateClient;
1313
beforeAll(async () => {
1414
expect(process.env.WEAVIATE_URL).toBeDefined();
1515
expect(process.env.WEAVIATE_URL!.length).toBeGreaterThan(0);
16-
client = await weaviate.connectToWeaviateCloud(process.env.WEAVIATE_URL!, {
17-
authCredentials: new weaviate.ApiKey(process.env.WEAVIATE_API_KEY || ""),
18-
headers: {
19-
"X-OpenAI-Api-Key": process.env.OPENAI_API_KEY || "",
20-
"X-Azure-Api-Key": process.env.AZURE_OPENAI_API_KEY || "",
21-
},
22-
});
16+
if (process.env.WEAVIATE_URL === "local") {
17+
client = await weaviate.connectToLocal({
18+
headers: {
19+
"X-OpenAI-Api-Key": process.env.OPENAI_API_KEY || "",
20+
"X-Cohere-Api-Key": process.env.COHERE_API_KEY || "",
21+
},
22+
});
23+
} else {
24+
client = await weaviate.connectToWeaviateCloud(process.env.WEAVIATE_URL!, {
25+
authCredentials: new weaviate.ApiKey(process.env.WEAVIATE_API_KEY || ""),
26+
headers: {
27+
"X-OpenAI-Api-Key": process.env.OPENAI_API_KEY || "",
28+
"X-Cohere-Api-Key": process.env.COHERE_API_KEY || "",
29+
},
30+
});
31+
}
32+
console.log("Connecting to Weaviate at", process.env.WEAVIATE_URL);
33+
console.log("Ready?", await client.isReady());
2334
});
2435

2536
test("WeaviateStore", async () => {
@@ -31,6 +42,8 @@ test("WeaviateStore", async () => {
3142
metadataKeys: ["foo"],
3243
};
3344
try {
45+
// delete indexName first
46+
await client.collections.delete(weaviateArgs.indexName);
3447
const store = await WeaviateStore.fromTexts(
3548
["hello world", "hi there", "how are you", "bye now"],
3649
[{ foo: "bar" }, { foo: "baz" }, { foo: "qux" }, { foo: "bar" }],

libs/providers/langchain-weaviate/src/vectorstores.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import * as uuid from "uuid";
22
import {
3-
BaseHybridOptions,
3+
HybridOptions,
44
CollectionConfigCreate,
55
configure,
66
type DataObject,
77
type FilterValue,
88
GenerateOptions,
99
GenerativeConfigRuntime,
10-
HybridOptions,
1110
Metadata,
1211
Vectors,
1312
WeaviateClient,
1413
type WeaviateField,
14+
BaseHybridOptions,
15+
MetadataKeys,
1516
} from "weaviate-client";
1617
import {
1718
type MaxMarginalRelevanceSearchOptions,
@@ -294,26 +295,41 @@ export class WeaviateStore extends VectorStore {
294295
options?: HybridOptions<undefined>
295296
): Promise<Document[]> {
296297
const collection = this.client.collections.get(this.indexName);
298+
let query_vector: number[] | undefined;
299+
if (!options?.vector) {
300+
query_vector = await this.embeddings.embedQuery(query);
301+
}
302+
303+
const options_with_vector = {
304+
...options,
305+
vector: options?.vector || query_vector,
306+
returnMetadata: [
307+
"score",
308+
...((options?.returnMetadata as MetadataKeys) || []),
309+
] as MetadataKeys,
310+
};
297311
let result;
298312
if (this.tenant) {
299313
result = await collection.withTenant(this.tenant).query.hybrid(query, {
300-
...(options || {}),
314+
...options_with_vector,
301315
});
302316
} else {
303317
result = await collection.query.hybrid(query, {
304-
...(options || {}),
318+
...options_with_vector,
305319
});
306320
}
307-
const documents = [];
321+
const documents: Document[] = [];
322+
308323
for (const data of result.objects) {
309-
const { properties = {} } = data ?? {};
324+
const { properties = {}, metadata = {} } = data ?? {};
310325
const { [this.textKey]: text, ...rest } = properties;
311326

312327
documents.push(
313328
new Document({
314329
pageContent: String(text ?? ""),
315330
metadata: {
316331
...rest,
332+
...metadata,
317333
},
318334
id: data.uuid,
319335
})
@@ -413,6 +429,10 @@ export class WeaviateStore extends VectorStore {
413429
): Promise<[Document, number, number, number[]][]> {
414430
try {
415431
const collection = this.client.collections.get(this.indexName);
432+
// define query attributes to return
433+
// if no queryAttrs, show all properties
434+
const queryAttrs =
435+
this.queryAttrs.length > 1 ? this.queryAttrs : undefined;
416436
let result;
417437
if (this.tenant) {
418438
result = await collection
@@ -421,13 +441,15 @@ export class WeaviateStore extends VectorStore {
421441
filters: filter,
422442
limit: k,
423443
returnMetadata: ["distance", "score"],
444+
returnProperties: queryAttrs,
424445
});
425446
} else {
426447
result = await collection.query.nearVector(query, {
427448
filters: filter,
428449
limit: k,
429450
includeVector: true,
430451
returnMetadata: ["distance", "score"],
452+
returnProperties: queryAttrs,
431453
});
432454
}
433455

0 commit comments

Comments
 (0)