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"
);
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");
return await Milvus.fromDocuments(docs, model, {
url: {
address: process.env.ZILIZ_URL,
token: process.env.ZILIZ,
},
collectionName: "langchain_valtown",
});
},
],
]);
return () => setup(spec);
}