npm install -S closevector-web
npm install -S closevector-node
npm install @langchain/openai @langchain/community @langchain/core
// If you want to import the browser version, use the following line instead:
// import { CloseVectorWeb } from "@langchain/community/vectorstores/closevector/web";
import { CloseVectorNode } from "@langchain/community/vectorstores/closevector/node";
import { OpenAIEmbeddings } from "@langchain/openai";
export const run = async () => {
// If you want to import the browser version, use the following line instead:
// const vectorStore = await CloseVectorWeb.fromTexts(
const vectorStore = await CloseVectorNode.fromTexts(
["Hello world", "Bye bye", "hello nice world"],
[{ id: 2 }, { id: 1 }, { id: 3 }],
new OpenAIEmbeddings()
);
const resultOne = await vectorStore.similaritySearch("hello world", 1);
console.log(resultOne);
};
// If you want to import the browser version, use the following line instead:
// import { CloseVectorWeb } from "@langchain/community/vectorstores/closevector/web";
import { CloseVectorNode } from "@langchain/community/vectorstores/closevector/node";
import { OpenAIEmbeddings } from "@langchain/openai";
import { TextLoader } from "langchain/document_loaders/fs/text";
// Create docs with a loader
const loader = new TextLoader("src/document_loaders/example_data/example.txt");
const docs = await loader.load();
// Load the docs into the vector store
// If you want to import the browser version, use the following line instead:
// const vectorStore = await CloseVectorWeb.fromDocuments(
const vectorStore = await CloseVectorNode.fromDocuments(
docs,
new OpenAIEmbeddings()
);
// Search for the most similar document
const resultOne = await vectorStore.similaritySearch("hello world", 1);
console.log(resultOne);
// If you want to import the browser version, use the following line instead:
// import { CloseVectorWeb } from "@langchain/community/vectorstores/closevector/web";
import { CloseVectorNode } from "@langchain/community/vectorstores/closevector/node";
import { CloseVectorWeb } from "@langchain/community/vectorstores/closevector/web";
import { OpenAIEmbeddings } from "@langchain/openai";
// Create a vector store through any method, here from texts as an example
// If you want to import the browser version, use the following line instead:
// const vectorStore = await CloseVectorWeb.fromTexts(
const vectorStore = await CloseVectorNode.fromTexts(
["Hello world", "Bye bye", "hello nice world"],
[{ id: 2 }, { id: 1 }, { id: 3 }],
new OpenAIEmbeddings(),
undefined,
{
key: "your access key",
secret: "your secret",
}
);
// Save the vector store to cloud
await vectorStore.saveToCloud({
description: "example",
public: true,
});
const { uuid } = vectorStore.instance;
// Load the vector store from cloud
// const loadedVectorStore = await CloseVectorWeb.load(
const loadedVectorStore = await CloseVectorNode.loadFromCloud({
uuid,
embeddings: new OpenAIEmbeddings(),
credentials: {
key: "your access key",
secret: "your secret",
},
});
// If you want to import the node version, use the following lines instead:
// const loadedVectorStoreOnNode = await CloseVectorNode.loadFromCloud({
// uuid,
// embeddings: new OpenAIEmbeddings(),
// credentials: {
// key: "your access key",
// secret: "your secret"
// }
// });
const loadedVectorStoreOnBrowser = await CloseVectorWeb.loadFromCloud({
uuid,
embeddings: new OpenAIEmbeddings(),
credentials: {
key: "your access key",
secret: "your secret",
},
});
// vectorStore and loadedVectorStore are identical
const result = await loadedVectorStore.similaritySearch("hello world", 1);
console.log(result);
// or
const resultOnBrowser = await loadedVectorStoreOnBrowser.similaritySearch(
"hello world",
1
);
console.log(resultOnBrowser);
// If you want to import the browser version, use the following line instead:
// import { CloseVectorWeb } from "@langchain/community/vectorstores/closevector/web";
import { CloseVectorNode } from "@langchain/community/vectorstores/closevector/node";
import { OpenAIEmbeddings } from "@langchain/openai";
// Create a vector store through any method, here from texts as an example
// If you want to import the browser version, use the following line instead:
// const vectorStore = await CloseVectorWeb.fromTexts(
const vectorStore = await CloseVectorNode.fromTexts(
["Hello world", "Bye bye", "hello nice world"],
[{ id: 2 }, { id: 1 }, { id: 3 }],
new OpenAIEmbeddings()
);
// Save the vector store to a directory
const directory = "your/directory/here";
await vectorStore.save(directory);
// Load the vector store from the same directory
// If you want to import the browser version, use the following line instead:
// const loadedVectorStore = await CloseVectorWeb.load(
const loadedVectorStore = await CloseVectorNode.load(
directory,
new OpenAIEmbeddings()
);
// vectorStore and loadedVectorStore are identical
const result = await loadedVectorStore.similaritySearch("hello world", 1);
console.log(result);
Was this page helpful?