MCPcopy
hub / github.com/langchain-ai/langchain / as_retriever

Method as_retriever

libs/core/langchain_core/vectorstores.py:753–808  ·  view source on GitHub ↗

Return VectorStoreRetriever initialized from this VectorStore. Args: search_type (Optional[str]): Defines the type of search that the Retriever should perform. Can be "similarity" (default), "mmr", or "similarity_score_threshold".

(self, **kwargs: Any)

Source from the content-addressed store, hash-verified

751 return tags
752
753 def as_retriever(self, **kwargs: Any) -> VectorStoreRetriever:
754 """Return VectorStoreRetriever initialized from this VectorStore.
755
756 Args:
757 search_type (Optional[str]): Defines the type of search that
758 the Retriever should perform.
759 Can be "similarity" (default), "mmr", or
760 "similarity_score_threshold".
761 search_kwargs (Optional[Dict]): Keyword arguments to pass to the
762 search function. Can include things like:
763 k: Amount of documents to return (Default: 4)
764 score_threshold: Minimum relevance threshold
765 for similarity_score_threshold
766 fetch_k: Amount of documents to pass to MMR algorithm (Default: 20)
767 lambda_mult: Diversity of results returned by MMR;
768 1 for minimum diversity and 0 for maximum. (Default: 0.5)
769 filter: Filter by document metadata
770
771 Returns:
772 VectorStoreRetriever: Retriever class for VectorStore.
773
774 Examples:
775
776 .. code-block:: python
777
778 # Retrieve more documents with higher diversity
779 # Useful if your dataset has many similar documents
780 docsearch.as_retriever(
781 search_type="mmr",
782 search_kwargs={'k': 6, 'lambda_mult': 0.25}
783 )
784
785 # Fetch more documents for the MMR algorithm to consider
786 # But only return the top 5
787 docsearch.as_retriever(
788 search_type="mmr",
789 search_kwargs={'k': 5, 'fetch_k': 50}
790 )
791
792 # Only retrieve documents that have a relevance score
793 # Above a certain threshold
794 docsearch.as_retriever(
795 search_type="similarity_score_threshold",
796 search_kwargs={'score_threshold': 0.8}
797 )
798
799 # Only get the single most similar document from the dataset
800 docsearch.as_retriever(search_kwargs={'k': 1})
801
802 # Use a filter to only retrieve documents from a specific paper
803 docsearch.as_retriever(
804 search_kwargs={'filter': {'paper_title':'GPT-4 Technical Report'}}
805 )
806 """
807 tags = kwargs.pop("tags", None) or [] + self._get_retriever_tags()
808 return VectorStoreRetriever(vectorstore=self, tags=tags, **kwargs)
809
810

Callers 15

agent.pyFile · 0.45
chain.pyFile · 0.45
chain.pyFile · 0.45
agent.pyFile · 0.45
chain.pyFile · 0.45
chain.pyFile · 0.45
chain.pyFile · 0.45
chain.pyFile · 0.45
chain.pyFile · 0.45
chain.pyFile · 0.45
chain.pyFile · 0.45
chain.pyFile · 0.45

Calls 3

_get_retriever_tagsMethod · 0.95
popMethod · 0.80

Tested by

no test coverage detected