Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
import process from "node:process";
import { getModelBuilder } from "https://esm.town/v/webup/getModelBuilder";
export async function getVectorStoreBuilder(docs: object[], spec: {
type: "memory" | "baas";
provider?: "pinecone" | "milvus";
} = { type: "memory" }, embed: "openai" | "huggingface" = "openai") {
const { cond, matches } = await import("npm:lodash-es");
const builder = await getModelBuilder({
type: "embedding",
provider: embed,
});
const model = await builder();
const setup = cond([
[
matches({ type: "memory" }),
async () => {
const { MemoryVectorStore } = await import(
"npm:langchain/vectorstores/memory"
);
return await MemoryVectorStore.fromDocuments(docs, model);
},
],
[
matches({ type: "baas", provider: "pinecone" }),
async () => {
await import("npm:dotenv");
const { PineconeClient } = await import(
"npm:@pinecone-database/pinecone"
);
const client = new PineconeClient();
await client.init({
apiKey: process.env.PINECONE,
environment: "asia-southeast1-gcp-free",
});
const pineconeIndex = client.Index("langchain-valtown");
const { PineconeStore } = await import(
"npm:langchain/vectorstores/pinecone"
);
// PineconeStore.fromDocuments() gives out PineClient upsert error
return await PineconeStore.fromExistingIndex(model, {
pineconeIndex,
});
},
],
[
matches({ type: "baas", provider: "milvus" }),
async () => {
await import("npm:@zilliz/milvus2-sdk-node");
const { Milvus } = await import("npm:langchain/vectorstores/milvus");
// PermissionDenied: Requires read access to <exec_path>, run again with the --allow-read flag
return await Milvus.fromDocuments(docs, model, {
url: {
address: process.env.ZILIZ_URL,
token: process.env.ZILIZ,
},
collectionName: "langchain_valtown",
});
},
],
]);
return () => setup(spec);
}
October 23, 2023