
    ih                     J    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	y)    )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} t        |           || _        || _        d| _        | j
                  r || j
                        | _        | j                  st        d      y# t        $ r t        d      w xY w)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       i/var/www/html/dev/engine/venv/lib/python3.12/site-packages/langchain_community/document_loaders/needle.pyr   zNeedleLoader.__init__+   s     	. 	,*.2&t/B/BCDK!!>?? "  	V 	s   A( (A=c                 `    | j                   t        d      | j                  st        d      y)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   s:     ;;K  !!>?? "    filesc                 ^   	 ddl m} | j                          | j                  J d       |j                         D cg c]  \  }} |||       }}}| j                  j                  j                  j                  | j                  |       y# t        $ r t        d      w xY wc c}}w )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.)nameurl)r   r   )
needle.v1.modelsr!   r   r   r   itemscollectionsr   addr   )r   r   r!   r#   r$   files_to_adds         r   	add_fileszNeedleLoader.add_files\   s    	2 	{{&K(KK&GL{{}U)$	t5UU%%)),,L 	* 	
  	O 	 Vs   B B)B&c                 `   | j                          | j                  J d       | j                  j                  j                  j	                  | j
                        }|D cg c]A  }|j                  dk(  r0t        d|j                  |j                  t        |dd      d      C }}|S c c}w )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"   indexed size)sourcetitler.   )page_contentmetadata)r   r   r'   r   listr   statusr   r$   r#   getattr)r   r   filedocss       r   _fetch_documentszNeedleLoader._fetch_documentsx   s     	{{&K(KK&''--2243E3EF 
 {{i' "hh!YY#D&$7
 
 
s   !AB+c                 "    | j                         S )z
        Loads all documents from the Needle collection.

        Returns:
            List[Document]: A list of documents from the collection.
        r8   r   s    r   loadzNeedleLoader.load   s     $$&&r   c              #   @   K   | j                         E d{    y7 w)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   s      ((***s   )NN)r   N)__name__
__module____qualname____doc__r   strr   r   r   r*   r   r   r8   r;   r   r=   __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   <module>rH      s    1 1 ; -]+: ]+r   