
    hh                     R    d dl mZmZmZmZ d dlmZ d dlmZ  G d de          Z	dS )    )DictIteratorListOptional)
BaseLoader)Documentc                        e Zd ZdZ	 	 ddee         dee         ddf fdZddZdeeef         ddfd	Z	de
e         fd
Zde
e         fdZdee         fdZ xZS )NeedleLoadera  
    NeedleLoader is a document loader for managing documents stored in a collection.

    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]): API key for authenticating with Needle.
        - `collection_id` (str): Needle collection to load documents from.

    Usage:
        .. code-block:: python

            from langchain_community.document_loaders.needle import NeedleLoader

            loader = NeedleLoader(
                needle_api_key="your-api-key",
                collection_id="your-collection-id"
            )

            # Load documents
            documents = loader.load()
            for doc in documents:
                print(doc.metadata)

            # Lazy load documents
            for doc in loader.lazy_load():
                print(doc.metadata)
    Nneedle_api_keycollection_idreturnc                 "   	 ddl m} n# t          $ r t          d          w xY wt                                                       || _        || _        d| _        | j        r || j                  | _        | j        st          d          dS )a  
        Initializes the NeedleLoader with API key and collection ID.

        Args:
            needle_api_key (Optional[str]): API key for authenticating with Needle.
            collection_id (Optional[str]): Identifier for the Needle collection.

        Raises:
            ImportError: If the `needle-python` library is not installed.
            ValueError: If the collection ID is not provided.
        r   )NeedleClientzDPlease install with `pip install needle-python` to use NeedleLoader.N)api_keyCollection ID must be provided.)		needle.v1r   ImportErrorsuper__init__r   r   client
ValueError)selfr   r   r   	__class__s       h/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/langchain_community/document_loaders/needle.pyr   zNeedleLoader.__init__+   s     	....... 	 	 	V  	
 	,*.2 	D&,t/BCCCDK! 	@>???	@ 	@   
 $c                 ^    | j         t          d          | j        st          d          dS )z
        Ensures the Needle collection is set and the client is initialized.

        Raises:
            ValueError: If the Needle client is not initialized or
                        if the collection ID is missing.
        Nz9NeedleClient is not initialized. Provide a valid API key.r   )r   r   r   r   s    r   _get_collectionzNeedleLoader._get_collectionM   sJ     ;K   ! 	@>???	@ 	@    filesc                 2   	 ddl m n# t          $ r t          d          w xY w|                                  | j        
J d            fd|                                D             }| j        j        j                            | j	        |           dS )an  
        Adds files to the Needle collection.

        Args:
            files (Dict[str, str]): Dictionary where keys are file names and values
                                    are file URLs.

        Raises:
            ImportError: If the `needle-python` library is not installed.
            ValueError: If the collection is not properly initialized.
        r   )	FileToAddz=Please install with `pip install needle-python` to add files.N!NeedleClient must be initialized.c                 0    g | ]\  }} ||           S ))nameurl ).0r%   r&   r"   s      r   
<listcomp>z*NeedleLoader.add_files.<locals>.<listcomp>r   s,    UUU)$		t555UUUr   )r   r    )
needle.v1.modelsr"   r   r   r   itemscollectionsr    addr   )r   r    files_to_addr"   s      @r   	add_fileszNeedleLoader.add_files\   s    	2222222 	 	 	O  	
 	{&&(K&&&UUUUu{{}}UUU%)),L 	* 	
 	
 	
 	
 	
r   c                     |                                   | j        
J d            | j        j        j                            | j                  }d |D             }|S )a
  
        Fetches metadata for documents from the Needle collection.

        Returns:
            List[Document]: A list of documents with metadata. Content is excluded.

        Raises:
            ValueError: If the collection is not properly initialized.
        Nr#   c                     g | ];}|j         d k    t          d|j        |j        t	          |dd          d          <S )indexed sizeN)sourcetitler4   )page_contentmetadata)statusr   r&   r%   getattr)r(   files     r   r)   z1NeedleLoader._fetch_documents.<locals>.<listcomp>   sh     
 
 
 {i'' "h!Y#D&$77    (''r   )r   r   r,   r    listr   )r   r    docss      r   _fetch_documentszNeedleLoader._fetch_documentsx   sr     	{&&(K&&&'-2243EFF
 
 
 
 
 r   c                 *    |                                  S )z
        Loads all documents from the Needle collection.

        Returns:
            List[Document]: A list of documents from the collection.
        r>   r   s    r   loadzNeedleLoader.load   s     $$&&&r   c              #   >   K   |                                  E d{V  dS )z
        Lazily loads documents from the Needle collection.

        Yields:
            Iterator[Document]: An iterator over the documents.
        Nr@   r   s    r   	lazy_loadzNeedleLoader.lazy_load   s2       ((***********r   )NN)r   N)__name__
__module____qualname____doc__r   strr   r   r   r/   r   r   r>   rA   r   rC   __classcell__)r   s   @r   r
   r
      s$       ! !J )-'+ @  @  @  } @ 
	 @  @  @  @  @  @D@ @ @ @
tCH~ 
$ 
 
 
 
8$x.    8'd8n ' ' ' '+8H- + + + + + + + +r   r
   N)
typingr   r   r   r   $langchain_core.document_loaders.baser   langchain_core.documentsr   r
   r'   r   r   <module>rM      s    1 1 1 1 1 1 1 1 1 1 1 1 ; ; ; ; ; ; - - - - - -]+ ]+ ]+ ]+ ]+: ]+ ]+ ]+ ]+ ]+r   