-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathopensearch.ts
65 lines (58 loc) · 1.65 KB
/
opensearch.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { Client } from "@opensearch-project/opensearch";
import { OpenAIEmbeddings } from "@langchain/openai";
import { OpenSearchVectorStore } from "@langchain/community/vectorstores/opensearch";
import * as uuid from "uuid";
import { Document } from "@langchain/core/documents";
export async function run() {
const client = new Client({
nodes: [process.env.OPENSEARCH_URL ?? "http://127.0.0.1:9200"],
});
const embeddings = new OpenAIEmbeddings();
const vectorStore = await OpenSearchVectorStore.fromTexts(
["Hello world", "Bye bye", "What's this?"],
[{ id: 2 }, { id: 1 }, { id: 3 }],
embeddings,
{
client,
indexName: "documents",
}
);
const resultOne = await vectorStore.similaritySearch("Hello world", 1);
console.log(resultOne);
const vectorStore2 = new OpenSearchVectorStore(embeddings, {
client,
indexName: "custom",
});
const documents = [
new Document({
pageContent: "Do I dare to eat an apple?",
metadata: {
foo: "baz",
},
}),
new Document({
pageContent: "There is no better place than the hotel lobby",
metadata: {
foo: "bar",
},
}),
new Document({
pageContent: "OpenSearch is a powerful vector db",
metadata: {
foo: "bat",
},
}),
];
const vectors = Array.from({ length: documents.length }, (_, i) => [
i,
i + 1,
i + 2,
]);
const ids = Array.from({ length: documents.length }, () => uuid.v4());
await vectorStore2.addVectors(vectors, documents, { ids });
const resultTwo = await vectorStore2.similaritySearchVectorWithScore(
vectors[0],
3
);
console.log(resultTwo);
}