VectorStoreRetriever Class — langchain Architecture
Architecture documentation for the VectorStoreRetriever class in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 933b9cdc_669b_cec5_0950_8c1618122fc2["VectorStoreRetriever"] 2a401977_bd56_ea94_9c8f_d0b77072baae["BaseRetriever"] 933b9cdc_669b_cec5_0950_8c1618122fc2 -->|extends| 2a401977_bd56_ea94_9c8f_d0b77072baae c58e6864_9429_b081_883b_39ba15df0485["Embeddings"] 933b9cdc_669b_cec5_0950_8c1618122fc2 -->|extends| c58e6864_9429_b081_883b_39ba15df0485 c3ed6b51_a344_0c0c_cf56_b617f175e3d8["base.py"] 933b9cdc_669b_cec5_0950_8c1618122fc2 -->|defined in| c3ed6b51_a344_0c0c_cf56_b617f175e3d8 078aa58f_3f01_3490_f331_d9de4d302fbf["validate_search_type()"] 933b9cdc_669b_cec5_0950_8c1618122fc2 -->|method| 078aa58f_3f01_3490_f331_d9de4d302fbf 7da44552_333d_3ef7_39bc_dd872d28717d["_get_ls_params()"] 933b9cdc_669b_cec5_0950_8c1618122fc2 -->|method| 7da44552_333d_3ef7_39bc_dd872d28717d be54d4a7_9b51_4af0_3402_b4d3526ba186["_get_relevant_documents()"] 933b9cdc_669b_cec5_0950_8c1618122fc2 -->|method| be54d4a7_9b51_4af0_3402_b4d3526ba186 fea5e823_5e2d_b7aa_9a19_b5bab854d055["_aget_relevant_documents()"] 933b9cdc_669b_cec5_0950_8c1618122fc2 -->|method| fea5e823_5e2d_b7aa_9a19_b5bab854d055 d23fbabe_f3eb_629f_fa1c_092ec9dfda9f["add_documents()"] 933b9cdc_669b_cec5_0950_8c1618122fc2 -->|method| d23fbabe_f3eb_629f_fa1c_092ec9dfda9f 6c809cd3_26da_fb6f_b91e_5b688038c0ea["aadd_documents()"] 933b9cdc_669b_cec5_0950_8c1618122fc2 -->|method| 6c809cd3_26da_fb6f_b91e_5b688038c0ea
Relationship Graph
Source Code
libs/core/langchain_core/vectorstores/base.py lines 964–1111
class VectorStoreRetriever(BaseRetriever):
"""Base Retriever class for VectorStore."""
vectorstore: VectorStore
"""VectorStore to use for retrieval."""
search_type: str = "similarity"
"""Type of search to perform."""
search_kwargs: dict = Field(default_factory=dict)
"""Keyword arguments to pass to the search function."""
allowed_search_types: ClassVar[Collection[str]] = (
"similarity",
"similarity_score_threshold",
"mmr",
)
model_config = ConfigDict(
arbitrary_types_allowed=True,
)
@model_validator(mode="before")
@classmethod
def validate_search_type(cls, values: dict) -> Any:
"""Validate search type.
Args:
values: Values to validate.
Returns:
Validated values.
Raises:
ValueError: If `search_type` is not one of the allowed search types.
ValueError: If `score_threshold` is not specified with a float value(`0~1`)
"""
search_type = values.get("search_type", "similarity")
if search_type not in cls.allowed_search_types:
msg = (
f"search_type of {search_type} not allowed. Valid values are: "
f"{cls.allowed_search_types}"
)
raise ValueError(msg)
if search_type == "similarity_score_threshold":
score_threshold = values.get("search_kwargs", {}).get("score_threshold")
if (score_threshold is None) or (not isinstance(score_threshold, float)):
msg = (
"`score_threshold` is not specified with a float value(0~1) "
"in `search_kwargs`."
)
raise ValueError(msg)
return values
def _get_ls_params(self, **kwargs: Any) -> LangSmithRetrieverParams:
"""Get standard params for tracing."""
kwargs_ = self.search_kwargs | kwargs
ls_params = super()._get_ls_params(**kwargs_)
ls_params["ls_vector_store_provider"] = self.vectorstore.__class__.__name__
if self.vectorstore.embeddings:
ls_params["ls_embedding_provider"] = (
self.vectorstore.embeddings.__class__.__name__
)
elif hasattr(self.vectorstore, "embedding") and isinstance(
self.vectorstore.embedding, Embeddings
):
ls_params["ls_embedding_provider"] = (
self.vectorstore.embedding.__class__.__name__
)
return ls_params
@override
def _get_relevant_documents(
self, query: str, *, run_manager: CallbackManagerForRetrieverRun, **kwargs: Any
) -> list[Document]:
kwargs_ = self.search_kwargs | kwargs
if self.search_type == "similarity":
Extends
Source
Frequently Asked Questions
What is the VectorStoreRetriever class?
VectorStoreRetriever is a class in the langchain codebase, defined in libs/core/langchain_core/vectorstores/base.py.
Where is VectorStoreRetriever defined?
VectorStoreRetriever is defined in libs/core/langchain_core/vectorstores/base.py at line 964.
What does VectorStoreRetriever extend?
VectorStoreRetriever extends BaseRetriever, Embeddings.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free