o
    Zh                     @   s^   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 de	eZdS )	    )AnyListOptional)CallbackManagerForRetrieverRun)Document)BaseRetriever)	BaseModelFieldc                   @   s   e Zd ZU dZdZee ed< 	 edddZ	ee
 ed< edddZee
 ed	< edd
dZee ed< dddZde
dee fddZde
dedee fddZdS )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                 C   sD   zddl m} W n ty   tdw | js || jd| _dS dS )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`.)Zapi_keyN)Z	needle.v1r   ImportErrorr   r   )selfr    r   \/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/retrievers/needle.py_initialize_client6   s   z"NeedleRetriever._initialize_clientqueryc                 C   sD   |    | jdu rtd| jjj| j|| jd}dd |D }|S )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.
        Nz4NeedleClient is not initialized. Provide an API key.)r   textr   c                 S   s   g | ]}t |jd qS ))Zpage_content)r   content).0resultr   r   r   
<listcomp>U   s    z6NeedleRetriever._search_collection.<locals>.<listcomp>)r   r   
ValueErrorcollectionssearchr   r   )r   r   resultsdocsr   r   r   _search_collectionD   s   


z"NeedleRetriever._search_collectionrun_managerc                C   s
   |  |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$   r   r   r   _get_relevant_documentsX   s   
z'NeedleRetriever._get_relevant_documents)r   N)__name__
__module____qualname____doc__r   r   r   __annotations__r	   r   strr   r   intr   r   r   r#   r   r%   r   r   r   r   r
   	   s(   
 "
r
   N)typingr   r   r   Zlangchain_core.callbacksr   Zlangchain_core.documentsr   Zlangchain_core.retrieversr   Zpydanticr   r	   r
   r   r   r   r   <module>   s    