-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
weaviate_search.ts
42 lines (38 loc) · 1.41 KB
/
weaviate_search.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* eslint-disable @typescript-eslint/no-explicit-any */
import weaviate, { ApiKey } from "weaviate-ts-client";
import { WeaviateStore } from "@langchain/weaviate";
import { OpenAIEmbeddings } from "@langchain/openai";
export async function run() {
// Something wrong with the weaviate-ts-client types, so we need to disable
const client = (weaviate as any).client({
scheme: process.env.WEAVIATE_SCHEME || "https",
host: process.env.WEAVIATE_HOST || "localhost",
apiKey: new ApiKey(process.env.WEAVIATE_API_KEY || "default"),
});
// Create a store for an existing index
const store = await WeaviateStore.fromExistingIndex(new OpenAIEmbeddings(), {
client,
indexName: "Test",
metadataKeys: ["foo"],
});
// Search the index without any filters
const results = await store.similaritySearch("hello world", 1);
console.log(results);
/*
[ Document { pageContent: 'hello world', metadata: { foo: 'bar' } } ]
*/
// Search the index with a filter, in this case, only return results where
// the "foo" metadata key is equal to "baz", see the Weaviate docs for more
// https://weaviate.io/developers/weaviate/api/graphql/filters
const results2 = await store.similaritySearch("hello world", 1, {
where: {
operator: "Equal",
path: ["foo"],
valueText: "baz",
},
});
console.log(results2);
/*
[ Document { pageContent: 'hi there', metadata: { foo: 'baz' } } ]
*/
}