Skip to main content

Vector store integrations

还没翻译译自英文(docs.langchain.com)本页 85 段都还没译

Overview

A vector store stores embedded data and performs similarity search.

flowchart LR

    subgraph "📥 Indexing phase (store)"
        A[📄 Documents] --> B[🔢 Embedding model]
        B --> C[🔘 Embedding vectors]
        C --> D[(Vector store)]
    end

    subgraph "📤 Query phase (retrieval)"
        E[❓ Query text] --> F[🔢 Embedding model]
        F --> G[🔘 Query vector]
        G --> H[🔍 Similarity search]
        H --> D
        D --> I[📄 Top-k results]
    end

    classDef process fill:#E5F4FF,stroke:#006DDD,stroke-width:2px,color:#030710
    class A,B,C,D,E,F,G,H,I process

Interface

LangChain provides a unified interface for vector stores, allowing you to:

  • addDocuments - Add documents to the store.
  • delete - Remove stored documents by ID.
  • similaritySearch - Query for semantically similar documents.

This abstraction lets you switch between different implementations without altering your application logic.

Initialization

Most vectorstores in LangChain accept an embedding model as an argument when initializing the vector store.

import { OpenAIEmbeddings } from "@langchain/openai";
import { MemoryVectorStore } from "@langchain/classic/vectorstores/memory";

const embeddings = new OpenAIEmbeddings({
  model: "text-embedding-3-small",
});
const vectorStore = new MemoryVectorStore(embeddings);

Adding documents

You can add documents to the vector store by using the addDocuments function.

import { Document } from "@langchain/core/documents";
const document = new Document({
  pageContent: "Hello world",
});
await vectorStore.addDocuments([document]);

Deleting documents

You can delete documents from the vector store by using the delete function.

await vectorStore.delete({
  filter: {
    pageContent: "Hello world",
  },
});

Issue a semantic query using similaritySearch, which returns the closest embedded documents:

const results = await vectorStore.similaritySearch("Hello world", 10);

Many vector stores support parameters like:

  • k — number of results to return
  • filter — conditional filtering based on metadata

Similarity metrics & indexing

Embedding similarity may be computed using:

  • Cosine similarity
  • Euclidean distance
  • Dot product

Efficient search often employs indexing methods such as HNSW (Hierarchical Navigable Small World), though specifics depend on the vector store.

Metadata filtering

Filtering by metadata (e.g., source, date) can refine search results:

vectorStore.similaritySearch("query", 2, { source: "tweets" });

Support for metadata-based filtering varies between implementations. Check the documentation of your chosen vector store for details.

Top integrations

Select embedding model:

OpenAI

Azure

AWS

Google Gemini

Gemini Enterprise Agent Platform

MistralAI

Cohere

Ollama

Voyage AI

Select vector store:

Memory

MongoDB

Pinecone

Redis

Qdrant

Oracle AI Database

Weaviate

LangChain.js integrates with a variety of vector stores. You can check out a full list below:

All vector stores