Skip to content

The agent engineering platform.

community: add top_k as param to Needle Retriever (#29821)

Jan Heimes60f58df5b31e
diff --git a/libs/community/langchain_community/retrievers/needle.py b/libs/community/langchain_community/retrievers/needle.py
index 201d617fda..52a5245108 100644
--- a/libs/community/langchain_community/retrievers/needle.py
+++ b/libs/community/langchain_community/retrievers/needle.py
@@ -23,6 +23,7 @@ class NeedleRetriever(BaseRetriever, BaseModel):
- `needle_api_key` (Optional[str]): The API key for authenticating with Needle.
- `collection_id` (str): The ID of the Needle collection to search in.
- `client` (Optional[NeedleClient]): An optional instance of the NeedleClient.
+ - `top_k` (Optional[int]): Maximum number of results to return.
Usage:
.. code-block:: python
@@ -31,7 +32,8 @@ class NeedleRetriever(BaseRetriever, BaseModel):
retriever = NeedleRetriever(
needle_api_key="your-api-key",
- collection_id="your-collection-id"
+ collection_id="your-collection-id",
+ top_k=10 # optional
)
results = retriever.retrieve("example query")
@@ -45,6 +47,9 @@ class NeedleRetriever(BaseRetriever, BaseModel):
collection_id: Optional[str] = Field(
..., description="The ID of the Needle collection to search in"
)
+ top_k: Optional[int] = Field(
+ default=None, description="Maximum number of search results to return"
+ )
def _initialize_client(self) -> None:
"""
@@ -75,7 +80,7 @@ class NeedleRetriever(BaseRetriever, BaseModel):
raise ValueError("NeedleClient is not initialized. Provide an API key.")
results = self.client.collections.search(
- collection_id=self.collection_id, text=query
+ collection_id=self.collection_id, text=query, top_k=self.top_k
)
docs = [Document(page_content=result.content) for result in results]
return docs
diff --git a/libs/community/tests/unit_tests/retrievers/test_needle.py b/libs/community/tests/unit_tests/retrievers/test_needle.py
index 853250d409..1bfb9243af 100644
--- a/libs/community/tests/unit_tests/retrievers/test_needle.py
+++ b/libs/community/tests/unit_tests/retrievers/test_needle.py
@@ -17,11 +17,14 @@ class MockNeedleClient:
self.collections = self.MockCollections()
class MockCollections:
- def search(self, collection_id: str, text: str) -> list[MockSearchResult]:
- return [
+ def search(
+ self, collection_id: str, text: str, top_k: int = 10
+ ) -> list[MockSearchResult]:
+ results = [
MockSearchResult(content=f"Result for query: {text}"),
MockSearchResult(content=f"Another result for query: {text}"),
]
+ return results[:top_k]
@pytest.mark.requires("needle")