Skip to main content

Vector store-backed retriever

A vector store retriever is a retriever that uses a vector store to retrieve documents. It is a lightweight wrapper around the Vector Store class to make it conform to the Retriever interface. It uses the search methods implemented by a vector store, like similarity search and MMR, to query the texts in the vector store.

Once you construct a Vector store, it's very easy to construct a retriever. Let's walk through an example.

const vectorStore = ...
const retriever = vectorStore.asRetriever();

Here's a more end-to-end example:

npm install @langchain/openai
import { OpenAI, OpenAIEmbeddings } from "@langchain/openai";
import { HNSWLib } from "langchain/vectorstores/hnswlib";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import * as fs from "fs";

// Initialize the LLM to use to answer the question.
const model = new OpenAI({});
const text = fs.readFileSync("state_of_the_union.txt", "utf8");
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 });
const docs = await textSplitter.createDocuments([text]);

// Create a vector store from the documents.
const vectorStore = await HNSWLib.fromDocuments(docs, new OpenAIEmbeddings());

// Initialize a retriever wrapper around the vector store
const vectorStoreRetriever = vectorStore.asRetriever();

const docs = await retriever.getRelevantDocuments(
"what did he say about ketanji brown jackson"


You can specify a maximum number of documents to retrieve as well as a vector store-specific filter to use when retrieving.

// Return up to 2 documents with `metadataField` set to `"value"`
const retriever = vectorStore.asRetriever(2, { metadataField: "value" });

const docs = retriever.getRelevantDocuments(
"what did he say about ketanji brown jackson"

Help us out by providing feedback on this documentation page: