还没翻译· 译自英文(docs.langchain.com ) · 本页 85 段都还没译
原文 译文 对照
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
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.
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 ) ;
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 ]) ;
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
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.
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.
Select embedding model:
OpenAI Install dependencies:
npm install @langchain/openai @langchain/core yarn add @langchain/openai @langchain/core pnpm add @langchain/openai @langchain/core Add environment variables:
OPENAI_API_KEY = your-api-key Instantiate the model:
import { OpenAIEmbeddings } from " @langchain/openai " ;
const embeddings = new OpenAIEmbeddings ( {
model : " text-embedding-3-large "
} ) ; Azure Install dependencies
npm install @langchain/openai @langchain/core yarn add @langchain/openai @langchain/core pnpm add @langchain/openai @langchain/core Add environment variables:
AZURE_OPENAI_API_INSTANCE_NAME =< YOUR_INSTANCE_NAME >
AZURE_OPENAI_API_KEY =< YOUR_KEY >
AZURE_OPENAI_API_VERSION = " 2024-02-01 " Instantiate the model:
import { AzureOpenAIEmbeddings } from " @langchain/openai " ;
const embeddings = new AzureOpenAIEmbeddings ( {
azureOpenAIApiEmbeddingsDeploymentName : " text-embedding-ada-002 "
} ) ; AWS Install dependencies:
Add environment variables:
BEDROCK_AWS_REGION = your-region Instantiate the model:
import { BedrockEmbeddings } from " @langchain/aws " ;
const embeddings = new BedrockEmbeddings ( {
model : " amazon.titan-embed-text-v1 "
} ) ; Google Gemini Install dependencies:
npm i @langchain/google-genai yarn add @langchain/google-genai pnpm add @langchain/google-genai Add environment variables:
GOOGLE_API_KEY = your-api-key Instantiate the model:
import { GoogleGenerativeAIEmbeddings } from " @langchain/google-genai " ;
const embeddings = new GoogleGenerativeAIEmbeddings ( {
model : " gemini-embedding-001 "
} ) ; Gemini Enterprise Agent Platform Install dependencies:
npm i @langchain/google-vertexai yarn add @langchain/google-vertexai pnpm add @langchain/google-vertexai Add environment variables:
GOOGLE_APPLICATION_CREDENTIALS = credentials.json Instantiate the model:
import { VertexAIEmbeddings } from " @langchain/google-vertexai " ;
const embeddings = new VertexAIEmbeddings ( {
model : " gemini-embedding-001 "
} ) ; MistralAI Install dependencies:
npm install @langchain/mistralai @langchain/core yarn add @langchain/mistralai @langchain/core pnpm add @langchain/mistralai @langchain/core Add environment variables:
MISTRAL_API_KEY = your-api-key Instantiate the model:
import { MistralAIEmbeddings } from " @langchain/mistralai " ;
const embeddings = new MistralAIEmbeddings ( {
model : " mistral-embed "
} ) ; Cohere Install dependencies:
yarn add @langchain/cohere pnpm add @langchain/cohere Add environment variables:
COHERE_API_KEY = your-api-key Instantiate the model:
import { CohereEmbeddings } from " @langchain/cohere " ;
const embeddings = new CohereEmbeddings ( {
model : " embed-english-v3.0 "
} ) ; Ollama Install dependencies:
npm install @langchain/ollama @langchain/core yarn add @langchain/ollama @langchain/core pnpm add @langchain/ollama @langchain/core Instantiate the model:
import { OllamaEmbeddings } from " @langchain/ollama " ;
const embeddings = new OllamaEmbeddings ( {
model : " llama2 " ,
baseUrl : " http://localhost:11434 " , // Default value
} ) ; Voyage AI Install dependencies:
npm install @langchain/mongodb @langchain/core yarn add @langchain/mongodb @langchain/core pnpm add @langchain/mongodb @langchain/core Add environment variables:
VOYAGE_API_KEY = your-api-key Instantiate the model:
import { VoyageEmbeddings } from " @langchain/mongodb " ;
const embeddings = new VoyageEmbeddings ( {
model : " voyage-4 "
} ) ;
Select vector store:
Memory import { MemoryVectorStore } from " @langchain/classic/vectorstores/memory " ;
const vectorStore = new MemoryVectorStore ( embeddings ) ; MongoDB npm install @langchain/mongodb mongodb @langchain/core yarn add @langchain/mongodb mongodb @langchain/core pnpm add @langchain/mongodb mongodb @langchain/core import { MongoDBAtlasVectorSearch } from " @langchain/mongodb "
import { MongoClient } from " mongodb " ;
const client = new MongoClient ( process . env . MONGODB_ATLAS_URI ! ) ;
const collection = client
. db ( process . env . MONGODB_ATLAS_DB_NAME )
. collection ( process . env . MONGODB_ATLAS_COLLECTION_NAME ) ;
const vectorStore = new MongoDBAtlasVectorSearch ( embeddings , {
collection ,
indexName : " vector_index " ,
textKey : " text " ,
embeddingKey : " embedding " ,
} ) ; npm install @langchain/mongodb mongodb @langchain/core yarn add @langchain/mongodb mongodb @langchain/core pnpm add @langchain/mongodb mongodb @langchain/core import { MongoDBAtlasVectorSearch } from " @langchain/mongodb "
import { MongoClient } from " mongodb " ;
const client = new MongoClient ( process . env . MONGODB_ATLAS_URI ! ) ;
const collection = client
. db ( process . env . MONGODB_ATLAS_DB_NAME )
. collection ( process . env . MONGODB_ATLAS_COLLECTION_NAME ) ;
const vectorStore = new MongoDBAtlasVectorSearch ( { collection } ) ; Pinecone npm install @langchain/pinecone @langchain/core @pinecone-database/pinecone yarn add @langchain/pinecone @langchain/core @pinecone-database/pinecone pnpm add @langchain/pinecone @langchain/core @pinecone-database/pinecone import { PineconeStore } from " @langchain/pinecone " ;
import { Pinecone as PineconeClient } from " @pinecone-database/pinecone " ;
const pinecone = new PineconeClient () ;
const vectorStore = new PineconeStore ( embeddings , {
pineconeIndex ,
maxConcurrency : 5 ,
} ) ; Redis npm install @langchain/redis @langchain/core redis yarn add @langchain/redis @langchain/core redis pnpm add @langchain/redis @langchain/core redis import { RedisVectorStore } from " @langchain/redis " ;
const vectorStore = new RedisVectorStore ( embeddings , {
redisClient : client ,
indexName : " langchainjs-testing " ,
} ) ; Qdrant npm install @langchain/qdrant @langchain/core yarn add @langchain/qdrant @langchain/core pnpm add @langchain/qdrant @langchain/core import { QdrantVectorStore } from " @langchain/qdrant " ;
const vectorStore = await QdrantVectorStore . fromExistingCollection ( embeddings , {
url : process . env . QDRANT_URL ,
collectionName : " langchainjs-testing " ,
} ) ; Oracle AI Database npm i @oracle/langchain-oracledb @langchain/core yarn add @oracle/langchain-oracledb @langchain/core pnpm add @oracle/langchain-oracledb @langchain/core import oracledb from " oracledb " ;
import { OracleEmbeddings , OracleVS } from " @oracle/langchain-oracledb " ;
const connection = await oracledb . getConnection ( {
user : process . env . ORACLE_USER ,
password : process . env . ORACLE_PASSWORD ,
connectionString : process . env . ORACLE_DSN ,
} ) ;
const embeddings = new OracleEmbeddings ( connection , {
provider : " database " ,
model : process . env . DEMO_ONNX_MODEL ?? " DEMO_MODEL " ,
} ) ;
const vectorStore = new OracleVS ( embeddings , {
client : connection ,
tableName : " DEMO_VECTORS " ,
query : " Find support tickets mentioning service outages. " ,
distanceStrategy : " DOT " ,
} ) ;
await vectorStore . initialize () ; Weaviate npm install @langchain/weaviate @langchain/core weaviate-client yarn add @langchain/weaviate @langchain/core weaviate-client pnpm add @langchain/weaviate @langchain/core weaviate-client import { WeaviateStore } from " @langchain/weaviate " ;
const vectorStore = new WeaviateStore ( embeddings , {
client : weaviateClient ,
indexName : " Langchainjs_test " ,
} ) ;
LangChain.js integrates with a variety of vector stores. You can check out a full list below: