
    dh                     d    S SK JrJrJr  S SKJr  S SKJr  S SKJ	r	  S SK
JrJr   " S S\	\5      rg)	    )AnyListOptional)CallbackManagerForRetrieverRun)Document)BaseRetriever)	BaseModelFieldc                       \ rS rSr% SrSr\\   \S'    \	" SSS9r
\\   \S'   \	" SS	S9r\\   \S
'   \	" SSS9r\\   \S'   SS jrS\S\\   4S jrS\S\S\\   4S jrSrg)NeedleRetriever	   a  
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                      SSK Jn  U R                  (       d  U" U R                  S9U l        gg! [         a    [        S5      ef = f)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     ]/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/retrievers/needle.py_initialize_client"NeedleRetriever._initialize_client6   sJ    	R. {{&t/B/BCDK   	RPQQ	Rs	   0 Aqueryc                    U R                  5         U R                  c  [        S5      eU R                  R                  R	                  U R
                  XR                  S9nU Vs/ sH  n[        UR                  S9PM     nnU$ s  snf )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_collection"NeedleRetriever._search_collectionD   s     	!;;STT++))00,,5

 1 
 ELLG&fnn5GL Ms   %Brun_managerc                $    U R                  U5      $ )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_documents'NeedleRetriever._get_relevant_documentsX   s     &&u--    )r   )r   N)__name__
__module____qualname____firstlineno____doc__r   r   r   __annotations__r
   r   strr   r   intr   r   r   r)   r   r-   __static_attributes__ 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   r9   r/   r   <module>r?      s'    & & C - 3 %\.mY \.r/   