
    dh                     J    S SK JrJrJrJr  S SKJr  S SKJr   " S S\5      r	g)    )DictIteratorListOptional)
BaseLoader)Documentc                      ^  \ rS rSrSr  SS\\   S\\   SS4U 4S jjjrSS jrS	\	\\4   SS4S
 jr
S\\   4S jrS\\   4S jrS\\   4S jrSrU =r$ )NeedleLoader   a  
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                   >  SSK Jn  [        TU ]  5         Xl        X l        SU l        U R
                  (       a  U" U R
                  S9U l        U R                  (       d  [        S5      eg! [         a    [        S5      ef = f)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       c/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/document_loaders/needle.pyr   NeedleLoader.__init__+   s     	. 	,*.2&t/B/BCDK!!>?? "  	V 	s   A- -Bc                 l    U R                   c  [        S5      eU R                  (       d  [        S5      eg)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_collectionNeedleLoader._get_collectionM   s:     ;;K  !!>?? "    filesc           	      V    SSK Jn  U R                  5         U R                  c   S5       eUR                  5        VVs/ sH  u  p4U" X4S9PM     nnnU R                  R                  R                  R                  U R                  US9  g! [         a    [        S5      ef = fs  snnf )a.  
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_filesNeedleLoader.add_files\   s    	2 	{{&K(KK&GL{{}U})$	t5}U%%)),,L 	* 	
  	O 	 Vs   B B%B"c                 f   U R                  5         U R                  c   S5       eU R                  R                  R                  R	                  U R
                  5      nU Vs/ sHB  nUR                  S:X  d  M  [        SUR                  UR                  [        USS5      S.S9PMD     nnU$ s  snf )z
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titler2   )page_contentmetadata)r   r   r*   r"   listr   statusr   r'   r&   getattr)r   r"   filedocss       r   _fetch_documentsNeedleLoader._fetch_documentsx   s     	{{&K(KK&''--2243E3EF 
 {{i'H"hh!YY#D&$7  	 
 
s   "B.91B.c                 "    U R                  5       $ )zx
Loads all documents from the Needle collection.

Returns:
    List[Document]: A list of documents from the collection.
r<   r   s    r   loadNeedleLoader.load   s     $$&&r!   c              #   @   #    U R                  5        Sh  vN   g N7f)zu
Lazily loads documents from the Needle collection.

Yields:
    Iterator[Document]: An iterator over the documents.
Nr?   r   s    r   	lazy_loadNeedleLoader.lazy_load   s      ((***s   )r   r   r   )NN)r   N)__name__
__module____qualname____firstlineno____doc__r   strr   r   r   r-   r   r   r<   r@   r   rC   __static_attributes____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>rQ      s    1 1 ; -]+: ]+r!   