FaissStore
Only available on Node.js.
Faiss is a library for efficient similarity search and clustering of dense vectors.
LangChain.js supports using Faiss as a locally-running vectorstore that can be saved to a file. It also provides the ability to read the saved file from the LangChain Python implementation.
This guide provides a quick overview for getting started with Faiss
vector stores. For detailed
documentation of all FaissStore
features and configurations head to
the API
reference.
Overviewβ
Integration detailsβ
Class | Package | PY support | Package latest |
---|---|---|---|
FaissStore | @langchain/community | β |
Setupβ
To use Faiss vector stores, youβll need to install the
@langchain/community
integration package and the
faiss-node
package as a peer
dependency.
This guide will also use OpenAI
embeddings, which require you
to install the @langchain/openai
integration package. You can also use
other supported embeddings models
if you wish.
- npm
- yarn
- pnpm
npm i @langchain/community faiss-node @langchain/openai @langchain/core
yarn add @langchain/community faiss-node @langchain/openai @langchain/core
pnpm add @langchain/community faiss-node @langchain/openai @langchain/core
Credentialsβ
Because Faiss runs locally, you do not need any credentials to use it.
If you are using OpenAI embeddings for this guide, youβll need to set your OpenAI key as well:
process.env.OPENAI_API_KEY = "YOUR_API_KEY";
If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:
// process.env.LANGCHAIN_TRACING_V2="true"
// process.env.LANGCHAIN_API_KEY="your-api-key"
Instantiationβ
import { FaissStore } from "@langchain/community/vectorstores/faiss";
import { OpenAIEmbeddings } from "@langchain/openai";
const embeddings = new OpenAIEmbeddings({
model: "text-embedding-3-small",
});
const vectorStore = new FaissStore(embeddings, {});
Manage vector storeβ
Add items to vector storeβ
import type { Document } from "@langchain/core/documents";
const document1: Document = {
pageContent: "The powerhouse of the cell is the mitochondria",
metadata: { source: "https://example.com" },
};
const document2: Document = {
pageContent: "Buildings are made out of brick",
metadata: { source: "https://example.com" },
};
const document3: Document = {
pageContent: "Mitochondria are made out of lipids",
metadata: { source: "https://example.com" },
};
const document4: Document = {
pageContent: "The 2024 Olympics are in Paris",
metadata: { source: "https://example.com" },
};
const documents = [document1, document2, document3, document4];
await vectorStore.addDocuments(documents, { ids: ["1", "2", "3", "4"] });
[ '1', '2', '3', '4' ]
Delete items from vector storeβ
await vectorStore.delete({ ids: ["4"] });
Query vector storeβ
Once your vector store has been created and the relevant documents have been added you will most likely wish to query it during the running of your chain or agent.
Query directlyβ
Performing a simple similarity search can be done as follows:
const similaritySearchResults = await vectorStore.similaritySearch(
"biology",
2
);
for (const doc of similaritySearchResults) {
console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
}
* The powerhouse of the cell is the mitochondria [{"source":"https://example.com"}]
* Mitochondria are made out of lipids [{"source":"https://example.com"}]
Filtering by metadata is currently not supported.
If you want to execute a similarity search and receive the corresponding scores you can run:
const similaritySearchWithScoreResults =
await vectorStore.similaritySearchWithScore("biology", 2);
for (const [doc, score] of similaritySearchWithScoreResults) {
console.log(
`* [SIM=${score.toFixed(3)}] ${doc.pageContent} [${JSON.stringify(
doc.metadata
)}]`
);
}
* [SIM=1.671] The powerhouse of the cell is the mitochondria [{"source":"https://example.com"}]
* [SIM=1.705] Mitochondria are made out of lipids [{"source":"https://example.com"}]
Query by turning into retrieverβ
You can also transform the vector store into a retriever for easier usage in your chains.
const retriever = vectorStore.asRetriever({
k: 2,
});
await retriever.invoke("biology");
[
{
pageContent: 'The powerhouse of the cell is the mitochondria',
metadata: { source: 'https://example.com' }
},
{
pageContent: 'Mitochondria are made out of lipids',
metadata: { source: 'https://example.com' }
}
]
Usage for retrieval-augmented generationβ
For guides on how to use this vector store for retrieval-augmented generation (RAG), see the following sections:
- Tutorials: working with external knowledge.
- How-to: Question and answer with RAG
- Retrieval conceptual docs
Merging indexesβ
Faiss also supports merging existing indexes:
// Create an initial vector store
const initialStore = 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 newStore = await FaissStore.fromTexts(
["Some text"],
[{ id: 1 }],
new OpenAIEmbeddings()
);
// merge the first vector store into vectorStore2
await newStore.mergeFrom(initialStore);
// You can also create a new vector store from another FaissStore index
const newStore2 = await FaissStore.fromIndex(newStore, new OpenAIEmbeddings());
await newStore2.similaritySearch("Bye bye", 1);
Save an index to file and load it againβ
To persist an index on disk, use the .save
and static .load
methods:
// Create a vector store through any method, here from texts as an example
const persistentStore = 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 persistentStore.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);
Reading saved files from Pythonβ
To enable the ability to read the saved file from LangChain Pythonβs
implementation,
youβll need to install the
pickleparser
package.
- npm
- yarn
- pnpm
npm i pickleparser
yarn add pickleparser
pnpm add pickleparser
Then you can use the .loadFromPython
static method:
// The directory of data saved from Python
const directoryWithSavedPythonStore = "your/directory/here";
// Load the vector store from the directory
const pythonLoadedStore = await FaissStore.loadFromPython(
directoryWithSavedPythonStore,
new OpenAIEmbeddings()
);
// Search for the most similar document
await pythonLoadedStore.similaritySearch("test", 2);
API referenceβ
For detailed documentation of all FaissStore
features and
configurations head to the API
reference
Relatedβ
- Vector store conceptual guide
- Vector store how-to guides