Use this file to discover all available pages before exploring further.
LangChain offers is an in-memory, ephemeral vectorstore that stores embeddings in-memory and does an exact, linear search for the most similar embeddings. The default similarity metric is cosine similarity, but can be changed to any of the similarity metrics supported by ml-distance.As it is intended for demos, it does not yet support ids or deletion.This guide provides a quick overview for getting started with MemoryVectorStorevector stores.
To use in-memory vector stores, you’ll need to install the langchain package: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.
There are no required credentials to use in-memory vector stores.If you are using OpenAI embeddings for this guide, you’ll need to set your OpenAI key as well:
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 documents = [document1, document2, document3];await vectorStore.addDocuments(documents);
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.
* The powerhouse of the cell is the mitochondria [{"source":"https://example.com"}]* Mitochondria are made out of lipids [{"source":"https://example.com"}]
The filter is optional, and must be a predicate function that takes a document as input, and returns true or false depending on whether the document should be returned.If you want to execute a similarity search and receive the corresponding scores you can run:
* [SIM=0.165] The powerhouse of the cell is the mitochondria [{"source":"https://example.com"}]* [SIM=0.148] Mitochondria are made out of lipids [{"source":"https://example.com"}]
[ Document { pageContent: 'The powerhouse of the cell is the mitochondria', metadata: { source: 'https://example.com' }, id: undefined }, Document { pageContent: 'Mitochondria are made out of lipids', metadata: { source: 'https://example.com' }, id: undefined }]
This vector store also supports maximal marginal relevance (MMR), a technique that first fetches a larger number of results (given by searchKwargs.fetchK), with classic similarity search, then reranks for diversity and returns the top k results. This helps guard against redundant information:
[ Document { pageContent: 'The powerhouse of the cell is the mitochondria', metadata: { source: 'https://example.com' }, id: undefined }, Document { pageContent: 'Buildings are made out of brick', metadata: { source: 'https://example.com' }, id: undefined }]