CloseVector
Compatibility
available on both browser and Node.js
CloseVector is a cross-platform vector database that can run in both the browser and Node.js. For example, you can create your index on Node.js and then load/query it on browser. For more information, please visit CloseVector Docs.
Setup
CloseVector Web
- npm
- Yarn
- pnpm
npm install -S closevector-web
yarn add closevector-web
pnpm add closevector-web
CloseVector Node
- npm
- Yarn
- pnpm
npm install -S closevector-node
yarn add closevector-node
pnpm add closevector-node
- npm
- Yarn
- pnpm
npm install @langchain/openai @langchain/community @langchain/core
yarn add @langchain/openai @langchain/community @langchain/core
pnpm add @langchain/openai @langchain/community @langchain/core
Usage
Create a new index from texts
// 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);
};
API Reference:
- CloseVectorNode from @langchain/community/vectorstores/closevector/node
- OpenAIEmbeddings from @langchain/openai
Create a new index from a loader
// 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);
API Reference:
- CloseVectorNode from @langchain/community/vectorstores/closevector/node
- OpenAIEmbeddings from @langchain/openai
- TextLoader from langchain/document_loaders/fs/text
Save an index to CloseVector CDN and load it again
CloseVector supports saving/loading indexes to/from cloud. To use this feature, you need to create an account on CloseVector. Please read CloseVector Docs and generate your API key first by loging in.
// 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);
API Reference:
- CloseVectorNode from @langchain/community/vectorstores/closevector/node
- CloseVectorWeb from @langchain/community/vectorstores/closevector/web
- OpenAIEmbeddings from @langchain/openai
Save an index to file and load it again
// 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);
API Reference:
- CloseVectorNode from @langchain/community/vectorstores/closevector/node
- OpenAIEmbeddings from @langchain/openai
Related
- Vector store conceptual guide
- Vector store how-to guides