Faiss
Compatibility
Only available on Node.js.
Faiss is a library for efficient similarity search and clustering of dense vectors.
Langchainjs supports using Faiss as a vectorstore that can be saved to file. It also provides the ability to read the saved file from Python's implementation.
Setup
Install the faiss-node, which is a Node.js bindings for Faiss.
- npm
- Yarn
- pnpm
npm install -S faiss-node
yarn add faiss-node
pnpm add faiss-node
To enable the ability to read the saved file from Python's implementation, the pickleparser also needs to install.
- npm
- Yarn
- pnpm
npm install -S pickleparser
yarn add pickleparser
pnpm add pickleparser
Usage
- npm
- Yarn
- pnpm
npm install @langchain/openai @langchain/community
yarn add @langchain/openai @langchain/community
pnpm add @langchain/openai @langchain/community
Create a new index from texts
import { FaissStore } from "@langchain/community/vectorstores/faiss";
import { OpenAIEmbeddings } from "@langchain/openai";
export const run = async () => {
  const vectorStore = await FaissStore.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:
- FaissStore from @langchain/community/vectorstores/faiss
- OpenAIEmbeddings from @langchain/openai
Create a new index from a loader
import { FaissStore } from "@langchain/community/vectorstores/faiss";
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
const vectorStore = await FaissStore.fromDocuments(
  docs,
  new OpenAIEmbeddings()
);
// Search for the most similar document
const resultOne = await vectorStore.similaritySearch("hello world", 1);
console.log(resultOne);
API Reference:
- FaissStore from @langchain/community/vectorstores/faiss
- OpenAIEmbeddings from @langchain/openai
- TextLoader from langchain/document_loaders/fs/text
Deleting vectors
import { FaissStore } from "@langchain/community/vectorstores/faiss";
import { OpenAIEmbeddings } from "@langchain/openai";
import { Document } from "@langchain/core/documents";
const vectorStore = new FaissStore(new OpenAIEmbeddings(), {});
const ids = ["2", "1", "4"];
const idsReturned = await vectorStore.addDocuments(
  [
    new Document({
      pageContent: "my world",
      metadata: { tag: 2 },
    }),
    new Document({
      pageContent: "our world",
      metadata: { tag: 1 },
    }),
    new Document({
      pageContent: "your world",
      metadata: { tag: 4 },
    }),
  ],
  {
    ids,
  }
);
console.log(idsReturned);
/*
  [ '2', '1', '4' ]
*/
const docs = await vectorStore.similaritySearch("my world", 3);
console.log(docs);
/*
[
  Document { pageContent: 'my world', metadata: { tag: 2 } },
  Document { pageContent: 'your world', metadata: { tag: 4 } },
  Document { pageContent: 'our world', metadata: { tag: 1 } }
]
*/
await vectorStore.delete({ ids: [ids[0], ids[1]] });
const docs2 = await vectorStore.similaritySearch("my world", 3);
console.log(docs2);
/*
[ Document { pageContent: 'your world', metadata: { tag: 4 } } ]
*/
API Reference:
- FaissStore from @langchain/community/vectorstores/faiss
- OpenAIEmbeddings from @langchain/openai
- Document from @langchain/core/documents
Merging indexes and creating new index from another instance
import { FaissStore } from "@langchain/community/vectorstores/faiss";
import { OpenAIEmbeddings } from "@langchain/openai";
export const run = async () => {
  // Create an initial vector store
  const vectorStore = await FaissStore.fromTexts(
    ["Hello world", "Bye bye", "hello nice world"],
    [{ id: 2 }, { id: 1 }, { id: 3 }],
    new OpenAIEmbeddings()
  );
  // Create another vector store from texts
  const vectorStore2 = await FaissStore.fromTexts(
    ["Some text"],
    [{ id: 1 }],
    new OpenAIEmbeddings()
  );
  // merge the first vector store into vectorStore2
  await vectorStore2.mergeFrom(vectorStore);
  const resultOne = await vectorStore2.similaritySearch("hello world", 1);
  console.log(resultOne);
  // You can also create a new vector store from another FaissStore index
  const vectorStore3 = await FaissStore.fromIndex(
    vectorStore2,
    new OpenAIEmbeddings()
  );
  const resultTwo = await vectorStore3.similaritySearch("Bye bye", 1);
  console.log(resultTwo);
};
API Reference:
- FaissStore from @langchain/community/vectorstores/faiss
- OpenAIEmbeddings from @langchain/openai
Save an index to file and load it again
import { FaissStore } from "@langchain/community/vectorstores/faiss";
import { OpenAIEmbeddings } from "@langchain/openai";
// Create a vector store through any method, here from texts as an example
const vectorStore = await FaissStore.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
const loadedVectorStore = await FaissStore.load(
  directory,
  new OpenAIEmbeddings()
);
// vectorStore and loadedVectorStore are identical
const result = await loadedVectorStore.similaritySearch("hello world", 1);
console.log(result);
API Reference:
- FaissStore from @langchain/community/vectorstores/faiss
- OpenAIEmbeddings from @langchain/openai
Load the saved file from Python's implementation
import { FaissStore } from "@langchain/community/vectorstores/faiss";
import { OpenAIEmbeddings } from "@langchain/openai";
// The directory of data saved from Python
const directory = "your/directory/here";
// Load the vector store from the directory
const loadedVectorStore = await FaissStore.loadFromPython(
  directory,
  new OpenAIEmbeddings()
);
// Search for the most similar document
const result = await loadedVectorStore.similaritySearch("test", 2);
console.log("result", result);
API Reference:
- FaissStore from @langchain/community/vectorstores/faiss
- OpenAIEmbeddings from @langchain/openai