a
    bg                     @   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	< dd
ddZe
ee dddZe
eee dddZdS )NeedleRetrievera  
    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.

    Usage:
        .. code-block:: python

            from langchain_community.retrievers.needle import NeedleRetriever

            retriever = NeedleRetriever(
                needle_api_key="your-api-key",
                collection_id="your-collection-id"
            )

            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_id)returnc                 C   sD   zddl m} W n ty*   tdY n0 | js@|| jd| _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   s/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/retrievers/needle.py_initialize_client1   s    z"NeedleRetriever._initialize_client)queryr   c                 C   s@   |    | jdu rtd| 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   textc                 S   s   g | ]}t |jd qS ))Zpage_content)r   content).0resultr   r   r   
<listcomp>P       z6NeedleRetriever._search_collection.<locals>.<listcomp>)r   r   
ValueErrorcollectionssearchr   )r   r   resultsdocsr   r   r   _search_collection?   s    

z"NeedleRetriever._search_collection)r   run_managerr   c                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_documentsS   s    z'NeedleRetriever._get_relevant_documents)__name__
__module____qualname____doc__r   r   r   __annotations__r	   r   strr   r   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
   