a
    bg                     @   sD   d dl mZmZmZmZ d dlmZ d dlmZ G dd deZ	dS )    )DictIteratorListOptional)
BaseLoader)Documentc                       s   e Zd ZdZdee ee dd fddZddddZeeef dd	d
dZ	e
e dddZe
e dddZee dddZ  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                    sn   zddl m} W n ty*   tdY n0 t   || _|| _d| _| jr\|| jd| _| jsjt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)Zapi_keyCollection ID must be provided.)	Z	needle.v1r   ImportErrorsuper__init__r	   r
   client
ValueError)selfr	   r
   r   	__class__ y/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/document_loaders/needle.pyr   +   s    

zNeedleLoader.__init__)r   c                 C   s$   | j du r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   r   r   r   _get_collectionM   s    
zNeedleLoader._get_collection)filesr   c                    sv   zddl m  W n ty*   tdY n0 |   | jdusFJ d fdd| D }| jjjj| j	|d 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                    s   g | ]\}} ||d qS ))nameurlr   ).0r   r   r   r   r   
<listcomp>r       z*NeedleLoader.add_files.<locals>.<listcomp>)r
   r   )
Zneedle.v1.modelsr   r   r   r   itemscollectionsr   addr
   )r   r   Zfiles_to_addr   r   r   	add_files\   s    

zNeedleLoader.add_filesc                 C   s>   |    | jdusJ d| jjj| j}d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              
   S   s6   g | ].}|j d krtd|j|jt|ddddqS )Zindexed sizeN)sourcetitler(   )Zpage_contentmetadata)statusr   r   r   getattr)r    filer   r   r   r!      s   	

z1NeedleLoader._fetch_documents.<locals>.<listcomp>)r   r   r$   r   listr
   )r   r   docsr   r   r   _fetch_documentsx   s    
	zNeedleLoader._fetch_documentsc                 C   s   |   S )z
        Loads all documents from the Needle collection.

        Returns:
            List[Document]: A list of documents from the collection.
        r1   r   r   r   r   load   s    zNeedleLoader.loadc                 c   s   |   E dH  dS )z
        Lazily loads documents from the Needle collection.

        Yields:
            Iterator[Document]: An iterator over the documents.
        Nr2   r   r   r   r   	lazy_load   s    zNeedleLoader.lazy_load)NN)__name__
__module____qualname____doc__r   strr   r   r   r&   r   r   r1   r3   r   r4   __classcell__r   r   r   r   r      s   %  "	r   N)
typingr   r   r   r   Z$langchain_core.document_loaders.baser   Zlangchain_core.documentsr   r   r   r   r   r   <module>   s   