Repository: deepset-ai/haystack

Issue:
Docstrings - `haystack.components.retrievers`



Discussion:

Resolving patch:
diff --git a/haystack/components/retrievers/filter_retriever.py b/haystack/components/retrievers/filter_retriever.py
--- a/haystack/components/retrievers/filter_retriever.py
+++ b/haystack/components/retrievers/filter_retriever.py
@@ -33,9 +33,7 @@ class FilterRetriever:
     # if passed in the run method, filters will override those provided at initialization
     result = retriever.run(filters={"field": "lang", "operator": "==", "value": "de"})
 
-    assert "documents" in result
-    assert len(result["documents"]) == 1
-    assert result["documents"][0].content == "python ist eine beliebte Programmiersprache"
+    print(result["documents"])
     ```
     """
 
@@ -43,8 +41,10 @@ def __init__(self, document_store: DocumentStore, filters: Optional[Dict[str, An
         """
         Create the FilterRetriever component.
 
-        :param document_store: An instance of a DocumentStore.
-        :param filters: A dictionary with filters to narrow down the search space. Defaults to `None`.
+        :param document_store:
+            An instance of a DocumentStore.
+        :param filters:
+            A dictionary with filters to narrow down the search space.
         """
         self.document_store = document_store
         self.filters = filters
@@ -57,7 +57,10 @@ def _get_telemetry_data(self) -> Dict[str, Any]:
 
     def to_dict(self) -> Dict[str, Any]:
         """
-        Serialize this component to a dictionary.
+        Serializes the component to a dictionary.
+
+        :returns:
+            Dictionary with serialized data.
         """
         docstore = self.document_store.to_dict()
         return default_to_dict(self, document_store=docstore, filters=self.filters)
@@ -65,7 +68,12 @@ def to_dict(self) -> Dict[str, Any]:
     @classmethod
     def from_dict(cls, data: Dict[str, Any]) -> "FilterRetriever":
         """
-        Deserialize this component from a dictionary.
+        Deserializes the component from a dictionary.
+
+        :param data:
+            The dictionary to deserialize from.
+        :returns:
+            The deserialized component.
         """
         init_params = data.get("init_parameters", {})
         if "document_store" not in init_params:
@@ -90,8 +98,10 @@ def run(self, filters: Optional[Dict[str, Any]] = None):
         """
         Run the FilterRetriever on the given input data.
 
-        :param filters: A dictionary with filters to narrow down the search space.
+        :param filters:
+            A dictionary with filters to narrow down the search space.
             If not specified, the FilterRetriever uses the value provided at initialization.
-        :return: The retrieved documents.
+        :returns:
+            The retrieved documents.
         """
         return {"documents": self.document_store.filter_documents(filters=filters or self.filters)}
diff --git a/haystack/components/retrievers/in_memory/bm25_retriever.py b/haystack/components/retrievers/in_memory/bm25_retriever.py
--- a/haystack/components/retrievers/in_memory/bm25_retriever.py
+++ b/haystack/components/retrievers/in_memory/bm25_retriever.py
@@ -7,9 +7,27 @@
 @component
 class InMemoryBM25Retriever:
     """
-    Uses the BM25 algorithm to retrieve documents from the InMemoryDocumentStore.
+    Retrieves documents using the BM25 algorithm.
 
-    Needs to be connected to the InMemoryDocumentStore to run.
+    Usage example:
+    ```python
+    from haystack import Document
+    from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
+    from haystack.document_stores.in_memory import InMemoryDocumentStore
+
+    docs = [
+        Document(content="Python is a popular programming language"),
+        Document(content="python ist eine beliebte Programmiersprache"),
+    ]
+
+    doc_store = InMemoryDocumentStore()
+    doc_store.write_documents(docs)
+    retriever = InMemoryBM25Retriever(doc_store)
+
+    result = retriever.run(query="Programmiersprache")
+
+    print(result["documents"])
+    ```
     """
 
     def __init__(
@@ -22,13 +40,17 @@ def __init__(
         """
         Create the InMemoryBM25Retriever component.
 
-        :param document_store: An instance of InMemoryDocumentStore.
-        :param filters: A dictionary with filters to narrow down the search space. Defaults to `None`.
-        :param top_k: The maximum number of documents to retrieve. Defaults to `10`.
-        :param scale_score: Scales the BM25 score to a unit interval in the range of 0 to 1, where 1 means extremely relevant. If set to `False`, uses raw similarity scores.
-        Defaults to `False`.
-
-        :raises ValueError: If the specified `top_k` is not > 0.
+        :param document_store:
+            An instance of InMemoryDocumentStore.
+        :param filters:
+            A dictionary with filters to narrow down the search space.
+        :param top_k:
+            The maximum number of documents to retrieve.
+        :param scale_score:
+            Scales the BM25 score to a unit interval in the range of 0 to 1, where 1 means extremely relevant. If set to `False`, uses raw similarity scores.
+
+        :raises ValueError:
+            If the specified `top_k` is not > 0.
         """
         if not isinstance(document_store, InMemoryDocumentStore):
             raise ValueError("document_store must be an instance of InMemoryDocumentStore")
@@ -50,7 +72,10 @@ def _get_telemetry_data(self) -> Dict[str, Any]:
 
     def to_dict(self) -> Dict[str, Any]:
         """
-        Serialize this component to a dictionary.
+        Serializes the component to a dictionary.
+
+        :returns:
+            Dictionary with serialized data.
         """
         docstore = self.document_store.to_dict()
         return default_to_dict(
@@ -60,7 +85,12 @@ def to_dict(self) -> Dict[str, Any]:
     @classmethod
     def from_dict(cls, data: Dict[str, Any]) -> "InMemoryBM25Retriever":
         """
-        Deserialize this component from a dictionary.
+        Deserializes the component from a dictionary.
+
+        :param data:
+            The dictionary to deserialize from.
+        :returns:
+            The deserialized component.
         """
         init_params = data.get("init_parameters", {})
         if "document_store" not in init_params:
@@ -83,14 +113,20 @@ def run(
         """
         Run the InMemoryBM25Retriever on the given input data.
 
-        :param query: The query string for the Retriever.
-        :param filters: A dictionary with filters to narrow down the search space.
-        :param top_k: The maximum number of documents to return.
-        :param scale_score: Scales the BM25 score to a unit interval in the range of 0 to 1, where 1 means extremely relevant. If set to `False`, uses raw similarity scores.
+        :param query:
+            The query string for the Retriever.
+        :param filters:
+            A dictionary with filters to narrow down the search space.
+        :param top_k:
+            The maximum number of documents to return.
+        :param scale_score:
+            Scales the BM25 score to a unit interval in the range of 0 to 1, where 1 means extremely relevant. If set to `False`, uses raw similarity scores.
             If not specified, the value provided at initialization is used.
-        :return: The retrieved documents.
+        :returns:
+            The retrieved documents.
 
-        :raises ValueError: If the specified DocumentStore is not found or is not a InMemoryDocumentStore instance.
+        :raises ValueError:
+            If the specified DocumentStore is not found or is not a InMemoryDocumentStore instance.
         """
         if filters is None:
             filters = self.filters
diff --git a/haystack/components/retrievers/in_memory/embedding_retriever.py b/haystack/components/retrievers/in_memory/embedding_retriever.py
--- a/haystack/components/retrievers/in_memory/embedding_retriever.py
+++ b/haystack/components/retrievers/in_memory/embedding_retriever.py
@@ -7,9 +7,36 @@
 @component
 class InMemoryEmbeddingRetriever:
     """
-    Uses a vector similarity metric to retrieve documents from the InMemoryDocumentStore.
-
-    Needs to be connected to the InMemoryDocumentStore to run.
+    Retrieves documents using vector similarity.
+
+    Usage example:
+    ```python
+    from haystack import Document
+    from haystack.components.embedders import SentenceTransformersDocumentEmbedder, SentenceTransformersTextEmbedder
+    from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
+    from haystack.document_stores.in_memory import InMemoryDocumentStore
+
+    docs = [
+        Document(content="Python is a popular programming language"),
+        Document(content="python ist eine beliebte Programmiersprache"),
+    ]
+    doc_embedder = SentenceTransformersDocumentEmbedder()
+    doc_embedder.warm_up()
+    docs_with_embeddings = doc_embedder.run(docs)["documents"]
+
+    doc_store = InMemoryDocumentStore()
+    doc_store.write_documents(docs_with_embeddings)
+    retriever = InMemoryEmbeddingRetriever(doc_store)
+
+    query="Programmiersprache"
+    text_embedder = SentenceTransformersTextEmbedder()
+    text_embedder.warm_up()
+    query_embedding = text_embedder.run(query)["embedding"]
+
+    result = retriever.run(query_embedding=query_embedding)
+
+    print(result["documents"])
+    ```
     """
 
     def __init__(
@@ -23,14 +50,19 @@ def __init__(
         """
         Create the InMemoryEmbeddingRetriever component.
 
-        :param document_store: An instance of InMemoryDocumentStore.
-        :param filters: A dictionary with filters to narrow down the search space. Defaults to `None`.
-        :param top_k: The maximum number of documents to retrieve. Defaults to `10`.
-        :param scale_score: Scales the BM25 score to a unit interval in the range of 0 to 1, where 1 means extremely relevant. If set to `False`, uses raw similarity scores.
-        Defaults to `False`.
-        :param return_embedding: Whether to return the embedding of the retrieved Documents. Default is `False`.
-
-        :raises ValueError: If the specified top_k is not > 0.
+        :param document_store:
+            An instance of InMemoryDocumentStore.
+        :param filters:
+            A dictionary with filters to narrow down the search space.
+        :param top_k:
+            The maximum number of documents to retrieve.
+        :param scale_score:
+            Scales the BM25 score to a unit interval in the range of 0 to 1, where 1 means extremely relevant. If set to `False`, uses raw similarity scores.
+        :param return_embedding:
+            Whether to return the embedding of the retrieved Documents.
+
+        :raises ValueError:
+            If the specified top_k is not > 0.
         """
         if not isinstance(document_store, InMemoryDocumentStore):
             raise ValueError("document_store must be an instance of InMemoryDocumentStore")
@@ -53,7 +85,10 @@ def _get_telemetry_data(self) -> Dict[str, Any]:
 
     def to_dict(self) -> Dict[str, Any]:
         """
-        Serialize this component to a dictionary.
+        Serializes the component to a dictionary.
+
+        :returns:
+            Dictionary with serialized data.
         """
         docstore = self.document_store.to_dict()
         return default_to_dict(
@@ -68,7 +103,12 @@ def to_dict(self) -> Dict[str, Any]:
     @classmethod
     def from_dict(cls, data: Dict[str, Any]) -> "InMemoryEmbeddingRetriever":
         """
-        Deserialize this component from a dictionary.
+        Deserializes the component from a dictionary.
+
+        :param data:
+            The dictionary to deserialize from.
+        :returns:
+            The deserialized component.
         """
         init_params = data.get("init_parameters", {})
         if "document_store" not in init_params:
@@ -92,15 +132,22 @@ def run(
         """
         Run the InMemoryEmbeddingRetriever on the given input data.
 
-        :param query_embedding: Embedding of the query.
-        :param filters: A dictionary with filters to narrow down the search space.
-        :param top_k: The maximum number of documents to return.
-        :param scale_score: Scales the similarity score to a unit interval in the range of 0 to 1, where 1 means extremely relevant. If set to `False`, uses raw similarity scores.
+        :param query_embedding:
+            Embedding of the query.
+        :param filters:
+            A dictionary with filters to narrow down the search space.
+        :param top_k:
+            The maximum number of documents to return.
+        :param scale_score:
+            Scales the similarity score to a unit interval in the range of 0 to 1, where 1 means extremely relevant. If set to `False`, uses raw similarity scores.
             If not specified, the value provided at initialization is used.
-        :param return_embedding: Whether to return the embedding of the retrieved Documents.
-        :return: The retrieved documents.
+        :param return_embedding:
+            Whether to return the embedding of the retrieved Documents.
+        :returns:
+            The retrieved documents.
 
-        :raises ValueError: If the specified DocumentStore is not found or is not an InMemoryDocumentStore instance.
+        :raises ValueError:
+            If the specified DocumentStore is not found or is not an InMemoryDocumentStore instance.
         """
         if filters is None:
             filters = self.filters


Test patch: