!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

LangChain v0.2 is coming soon! Preview the new docs [here](/v0.2/docs/introduction/).

[

![🦜️🔗 Langchain](/v0.1/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.1/img/brand/wordmark-dark.png)

](/v0.1/)[Docs](/v0.1/docs/get_started/introduction/)[Use cases](/v0.1/docs/use_cases/)[Integrations](/v0.1/docs/integrations/platforms/)[API Reference](https://api.js.langchain.com)

[More](#)

*   [People](/v0.1/docs/people/)
*   [Community](/v0.1/docs/community/)
*   [Tutorials](/v0.1/docs/additional_resources/tutorials/)
*   [Contributing](/v0.1/docs/contributing/)

[v0.1](#)

*   [v0.2](https://js.langchain.com/v0.2/docs/introduction)
*   [v0.1](/v0.1/docs/get_started/introduction/)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

*   [Providers](/v0.1/docs/integrations/platforms/)
    
    *   [Providers](/v0.1/docs/integrations/platforms/)
    *   [Anthropic](/v0.1/docs/integrations/platforms/anthropic/)
    *   [AWS](/v0.1/docs/integrations/platforms/aws/)
    *   [Google](/v0.1/docs/integrations/platforms/google/)
    *   [Microsoft](/v0.1/docs/integrations/platforms/microsoft/)
    *   [OpenAI](/v0.1/docs/integrations/platforms/openai/)
*   [Components](/v0.1/docs/integrations/components/)
    
    *   [LLMs](/v0.1/docs/integrations/llms/)
        
    *   [Chat models](/v0.1/docs/integrations/chat/)
        
    *   [Document loaders](/v0.1/docs/integrations/document_loaders/)
        
    *   [Document transformers](/v0.1/docs/integrations/document_transformers/)
        
    *   [Document compressors](/v0.1/docs/integrations/document_compressors/)
        
    *   [Text embedding models](/v0.1/docs/integrations/text_embedding/)
        
    *   [Vector stores](/v0.1/docs/integrations/vectorstores/)
        
        *   [Memory](/v0.1/docs/integrations/vectorstores/memory/)
        *   [AnalyticDB](/v0.1/docs/integrations/vectorstores/analyticdb/)
        *   [Astra DB](/v0.1/docs/integrations/vectorstores/astradb/)
        *   [Azure AI Search](/v0.1/docs/integrations/vectorstores/azure_aisearch/)
        *   [Azure Cosmos DB](/v0.1/docs/integrations/vectorstores/azure_cosmosdb/)
        *   [Cassandra](/v0.1/docs/integrations/vectorstores/cassandra/)
        *   [Chroma](/v0.1/docs/integrations/vectorstores/chroma/)
        *   [ClickHouse](/v0.1/docs/integrations/vectorstores/clickhouse/)
        *   [CloseVector](/v0.1/docs/integrations/vectorstores/closevector/)
        *   [Cloudflare Vectorize](/v0.1/docs/integrations/vectorstores/cloudflare_vectorize/)
        *   [Convex](/v0.1/docs/integrations/vectorstores/convex/)
        *   [Couchbase](/v0.1/docs/integrations/vectorstores/couchbase/)
        *   [Elasticsearch](/v0.1/docs/integrations/vectorstores/elasticsearch/)
        *   [Faiss](/v0.1/docs/integrations/vectorstores/faiss/)
        *   [Google Vertex AI Matching Engine](/v0.1/docs/integrations/vectorstores/googlevertexai/)
        *   [SAP HANA Cloud Vector Engine](/v0.1/docs/integrations/vectorstores/hanavector/)
        *   [HNSWLib](/v0.1/docs/integrations/vectorstores/hnswlib/)
        *   [LanceDB](/v0.1/docs/integrations/vectorstores/lancedb/)
        *   [Milvus](/v0.1/docs/integrations/vectorstores/milvus/)
        *   [Momento Vector Index (MVI)](/v0.1/docs/integrations/vectorstores/momento_vector_index/)
        *   [MongoDB Atlas](/v0.1/docs/integrations/vectorstores/mongodb_atlas/)
        *   [MyScale](/v0.1/docs/integrations/vectorstores/myscale/)
        *   [Neo4j Vector Index](/v0.1/docs/integrations/vectorstores/neo4jvector/)
        *   [Neon Postgres](/v0.1/docs/integrations/vectorstores/neon/)
        *   [OpenSearch](/v0.1/docs/integrations/vectorstores/opensearch/)
        *   [PGVector](/v0.1/docs/integrations/vectorstores/pgvector/)
        *   [Pinecone](/v0.1/docs/integrations/vectorstores/pinecone/)
        *   [Prisma](/v0.1/docs/integrations/vectorstores/prisma/)
        *   [Qdrant](/v0.1/docs/integrations/vectorstores/qdrant/)
        *   [Redis](/v0.1/docs/integrations/vectorstores/redis/)
        *   [Rockset](/v0.1/docs/integrations/vectorstores/rockset/)
        *   [SingleStore](/v0.1/docs/integrations/vectorstores/singlestore/)
        *   [Supabase](/v0.1/docs/integrations/vectorstores/supabase/)
        *   [Tigris](/v0.1/docs/integrations/vectorstores/tigris/)
        *   [Turbopuffer](/v0.1/docs/integrations/vectorstores/turbopuffer/)
        *   [TypeORM](/v0.1/docs/integrations/vectorstores/typeorm/)
        *   [Typesense](/v0.1/docs/integrations/vectorstores/typesense/)
        *   [Upstash Vector](/v0.1/docs/integrations/vectorstores/upstash/)
        *   [USearch](/v0.1/docs/integrations/vectorstores/usearch/)
        *   [Vectara](/v0.1/docs/integrations/vectorstores/vectara/)
        *   [Vercel Postgres](/v0.1/docs/integrations/vectorstores/vercel_postgres/)
        *   [Voy](/v0.1/docs/integrations/vectorstores/voy/)
        *   [Weaviate](/v0.1/docs/integrations/vectorstores/weaviate/)
        *   [Xata](/v0.1/docs/integrations/vectorstores/xata/)
        *   [Zep](/v0.1/docs/integrations/vectorstores/zep/)
    *   [Retrievers](/v0.1/docs/integrations/retrievers/)
        
    *   [Tools](/v0.1/docs/integrations/tools/)
        
    *   [Agents and toolkits](/v0.1/docs/integrations/toolkits/)
        
    *   [Chat Memory](/v0.1/docs/integrations/chat_memory/)
        
    *   [Stores](/v0.1/docs/integrations/stores/)
        

*   [](/v0.1/)
*   [Components](/v0.1/docs/integrations/components/)
*   [Vector stores](/v0.1/docs/integrations/vectorstores/)
*   Faiss

On this page

Faiss
=====

Compatibility

Only available on Node.js.

[Faiss](https://github.com/facebookresearch/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](https://python.langchain.com/docs/integrations/vectorstores/faiss#saving-and-loading).

Setup[​](#setup "Direct link to Setup")
---------------------------------------

Install the [faiss-node](https://github.com/ewfian/faiss-node), which is a Node.js bindings for [Faiss](https://github.com/facebookresearch/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](https://python.langchain.com/docs/integrations/vectorstores/faiss#saving-and-loading), the [pickleparser](https://github.com/ewfian/pickleparser) also needs to install.

*   npm
*   Yarn
*   pnpm

    npm install -S pickleparser

    yarn add pickleparser

    pnpm add pickleparser

Usage[​](#usage "Direct link to Usage")
---------------------------------------

tip

See [this section for general instructions on installing integration packages](/v0.1/docs/get_started/installation/#installing-integration-packages).

*   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[​](#create-a-new-index-from-texts "Direct link to 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](https://api.js.langchain.com/classes/langchain_community_vectorstores_faiss.FaissStore.html) from `@langchain/community/vectorstores/faiss`
*   [OpenAIEmbeddings](https://api.js.langchain.com/classes/langchain_openai.OpenAIEmbeddings.html) from `@langchain/openai`

### Create a new index from a loader[​](#create-a-new-index-from-a-loader "Direct link to 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 loaderconst loader = new TextLoader("src/document_loaders/example_data/example.txt");const docs = await loader.load();// Load the docs into the vector storeconst vectorStore = await FaissStore.fromDocuments(  docs,  new OpenAIEmbeddings());// Search for the most similar documentconst resultOne = await vectorStore.similaritySearch("hello world", 1);console.log(resultOne);

#### API Reference:

*   [FaissStore](https://api.js.langchain.com/classes/langchain_community_vectorstores_faiss.FaissStore.html) from `@langchain/community/vectorstores/faiss`
*   [OpenAIEmbeddings](https://api.js.langchain.com/classes/langchain_openai.OpenAIEmbeddings.html) from `@langchain/openai`
*   [TextLoader](https://api.js.langchain.com/classes/langchain_document_loaders_fs_text.TextLoader.html) from `langchain/document_loaders/fs/text`

### Deleting vectors[​](#deleting-vectors "Direct link to 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](https://api.js.langchain.com/classes/langchain_community_vectorstores_faiss.FaissStore.html) from `@langchain/community/vectorstores/faiss`
*   [OpenAIEmbeddings](https://api.js.langchain.com/classes/langchain_openai.OpenAIEmbeddings.html) from `@langchain/openai`
*   [Document](https://api.js.langchain.com/classes/langchain_core_documents.Document.html) from `@langchain/core/documents`

### Merging indexes and creating new index from another instance[​](#merging-indexes-and-creating-new-index-from-another-instance "Direct link to 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](https://api.js.langchain.com/classes/langchain_community_vectorstores_faiss.FaissStore.html) from `@langchain/community/vectorstores/faiss`
*   [OpenAIEmbeddings](https://api.js.langchain.com/classes/langchain_openai.OpenAIEmbeddings.html) from `@langchain/openai`

### Save an index to file and load it again[​](#save-an-index-to-file-and-load-it-again "Direct link to 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 exampleconst 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 directoryconst directory = "your/directory/here";await vectorStore.save(directory);// Load the vector store from the same directoryconst loadedVectorStore = await FaissStore.load(  directory,  new OpenAIEmbeddings());// vectorStore and loadedVectorStore are identicalconst result = await loadedVectorStore.similaritySearch("hello world", 1);console.log(result);

#### API Reference:

*   [FaissStore](https://api.js.langchain.com/classes/langchain_community_vectorstores_faiss.FaissStore.html) from `@langchain/community/vectorstores/faiss`
*   [OpenAIEmbeddings](https://api.js.langchain.com/classes/langchain_openai.OpenAIEmbeddings.html) from `@langchain/openai`

### Load the saved file from [Python's implementation](https://python.langchain.com/docs/integrations/vectorstores/faiss#saving-and-loading)[​](#load-the-saved-file-from-pythons-implementation "Direct link to load-the-saved-file-from-pythons-implementation")

    import { FaissStore } from "@langchain/community/vectorstores/faiss";import { OpenAIEmbeddings } from "@langchain/openai";// The directory of data saved from Pythonconst directory = "your/directory/here";// Load the vector store from the directoryconst loadedVectorStore = await FaissStore.loadFromPython(  directory,  new OpenAIEmbeddings());// Search for the most similar documentconst result = await loadedVectorStore.similaritySearch("test", 2);console.log("result", result);

#### API Reference:

*   [FaissStore](https://api.js.langchain.com/classes/langchain_community_vectorstores_faiss.FaissStore.html) from `@langchain/community/vectorstores/faiss`
*   [OpenAIEmbeddings](https://api.js.langchain.com/classes/langchain_openai.OpenAIEmbeddings.html) from `@langchain/openai`

* * *

#### Help us out by providing feedback on this documentation page:

[

Previous

Elasticsearch

](/v0.1/docs/integrations/vectorstores/elasticsearch/)[

Next

Google Vertex AI Matching Engine

](/v0.1/docs/integrations/vectorstores/googlevertexai/)

*   [Setup](#setup)
*   [Usage](#usage)
    *   [Create a new index from texts](#create-a-new-index-from-texts)
    *   [Create a new index from a loader](#create-a-new-index-from-a-loader)
    *   [Deleting vectors](#deleting-vectors)
    *   [Merging indexes and creating new index from another instance](#merging-indexes-and-creating-new-index-from-another-instance)
    *   [Save an index to file and load it again](#save-an-index-to-file-and-load-it-again)
    *   [Load the saved file from Python's implementation](#load-the-saved-file-from-pythons-implementation)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.