as_retriever() — langchain Function Reference
Architecture documentation for the as_retriever() function in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD f04f6652_019e_eef8_c59b_9b31d2db2ea8["as_retriever()"] 6c336ac6_f55c_1ad7_6db3_73dbd71fb625["VectorStore"] f04f6652_019e_eef8_c59b_9b31d2db2ea8 -->|defined in| 6c336ac6_f55c_1ad7_6db3_73dbd71fb625 612a5115_56cc_8662_cf37_eedd6f8624e6["_get_retriever_tags()"] f04f6652_019e_eef8_c59b_9b31d2db2ea8 -->|calls| 612a5115_56cc_8662_cf37_eedd6f8624e6 style f04f6652_019e_eef8_c59b_9b31d2db2ea8 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/vectorstores/base.py lines 905–961
def as_retriever(self, **kwargs: Any) -> VectorStoreRetriever:
"""Return `VectorStoreRetriever` initialized from this `VectorStore`.
Args:
**kwargs: Keyword arguments to pass to the search function.
Can include:
* `search_type`: Defines the type of search that the Retriever should
perform. Can be `'similarity'` (default), `'mmr'`, or
`'similarity_score_threshold'`.
* `search_kwargs`: Keyword arguments to pass to the search function.
Can include things like:
* `k`: Amount of documents to return (Default: `4`)
* `score_threshold`: Minimum relevance threshold
for `similarity_score_threshold`
* `fetch_k`: Amount of documents to pass to MMR algorithm
(Default: `20`)
* `lambda_mult`: Diversity of results returned by MMR;
`1` for minimum diversity and 0 for maximum. (Default: `0.5`)
* `filter`: Filter by document metadata
Returns:
Retriever class for `VectorStore`.
Examples:
```python
# Retrieve more documents with higher diversity
# Useful if your dataset has many similar documents
docsearch.as_retriever(
search_type="mmr", search_kwargs={"k": 6, "lambda_mult": 0.25}
)
# Fetch more documents for the MMR algorithm to consider
# But only return the top 5
docsearch.as_retriever(search_type="mmr", search_kwargs={"k": 5, "fetch_k": 50})
# Only retrieve documents that have a relevance score
# Above a certain threshold
docsearch.as_retriever(
search_type="similarity_score_threshold",
search_kwargs={"score_threshold": 0.8},
)
# Only get the single most similar document from the dataset
docsearch.as_retriever(search_kwargs={"k": 1})
# Use a filter to only retrieve documents from a specific paper
docsearch.as_retriever(
search_kwargs={"filter": {"paper_title": "GPT-4 Technical Report"}}
)
```
"""
tags = kwargs.pop("tags", None) or [*self._get_retriever_tags()]
return VectorStoreRetriever(vectorstore=self, tags=tags, **kwargs)
Domain
Subdomains
Calls
Source
Frequently Asked Questions
What does as_retriever() do?
as_retriever() is a function in the langchain codebase, defined in libs/core/langchain_core/vectorstores/base.py.
Where is as_retriever() defined?
as_retriever() is defined in libs/core/langchain_core/vectorstores/base.py at line 905.
What does as_retriever() call?
as_retriever() calls 1 function(s): _get_retriever_tags.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free