
    7|h                     d    d dl mZmZmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZmZ  G d de	e      Zy)	    )AnyListOptional)CallbackManagerForRetrieverRun)Document)BaseRetriever)	BaseModelFieldc                       e Zd ZU dZdZee   ed<   	  edd      Z	ee
   ed<    edd      Zee
   ed	<    edd
      Zee   ed<   ddZde
dee   fdZde
dedee   fdZy)NeedleRetrieveraY  
    NeedleRetriever retrieves relevant documents or context from a Needle collection
    based on a search query.

    Setup:
        Install the `needle-python` library and set your Needle API key.

        .. code-block:: bash

            pip install needle-python
            export NEEDLE_API_KEY="your-api-key"

    Key init args:
        - `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

            from langchain_community.retrievers.needle import NeedleRetriever

            retriever = NeedleRetriever(
                needle_api_key="your-api-key",
                collection_id="your-collection-id",
                top_k=10  # optional
            )

            results = retriever.retrieve("example query")
            for doc in results:
                print(doc.page_content)
    NclientzNeedle API Key)descriptionneedle_api_key.z,The ID of the Needle collection to search incollection_idz*Maximum number of search results to return)defaultr   top_kreturnc                     	 ddl m} | j                  s || j                        | _        yy# t        $ r t        d      w xY w)z
        Initialize the NeedleClient with the provided API key.

        If a client instance is already provided, this method does nothing.
        r   )NeedleClientz0Please install with `pip install needle-python`.)api_keyN)	needle.v1r   ImportErrorr   r   )selfr   s     d/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/retrievers/needle.py_initialize_clientz"NeedleRetriever._initialize_client6   sJ    	R. {{&t/B/BCDK   	RPQQ	Rs	   . Aqueryc                    | j                          | j                  t        d      | j                  j                  j	                  | j
                  || j                        }|D cg c]  }t        |j                         }}|S c c}w )z
        Search the Needle collection for relevant documents.

        Args:
            query (str): The search query used to find relevant documents.

        Returns:
            List[Document]: A list of documents matching the search query.
        z4NeedleClient is not initialized. Provide an API key.)r   textr   )page_content)	r   r   
ValueErrorcollectionssearchr   r   r   content)r   r   resultsresultdocss        r   _search_collectionz"NeedleRetriever._search_collectionD   s     	!;;STT++))00,,5

 1 
 ELL&fnn5LL Ms   (B	run_managerc                $    | j                  |      S )z
        Retrieve relevant documents based on the query.

        Args:
            query (str): The query string used to search the collection.
        Returns:
            List[Document]: A list of documents relevant to the query.
        )r'   )r   r   r(   s      r   _get_relevant_documentsz'NeedleRetriever._get_relevant_documentsX   s     &&u--    )r   N)__name__
__module____qualname____doc__r   r   r   __annotations__r
   r   strr   r   intr   r   r   r'   r   r*    r+   r   r   r   	   s     D !FHSM ,$)$<L$MNHSMM#(G$M8C=  !"NE8C= D X (..*H.	h.r+   r   N)typingr   r   r   langchain_core.callbacksr   langchain_core.documentsr   langchain_core.retrieversr   pydanticr	   r
   r   r3   r+   r   <module>r9      s'    & & C - 3 %\.mY \.r+   