§
    šŠtjj"  ã                   óz   — d dl Z d dlmZ d dlmZ d dlmZmZ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 )	é    N)ÚPathLike)ÚPath)ÚAnyÚCallableÚDictÚIteratorÚOptionalÚUnion)ÚDocument)Ú
BaseLoaderc                   ó  — e Zd ZdZ	 	 	 	 	 ddeeef         dedee         dee         d	ee	e
e
ge
f                  d
edefd„Zdee         fd„Zdededee         fd„Zdedefd„Zde
eef         dede
eef         fd„Zdeddfd„ZdS )Ú
JSONLoadera…  
    Load a `JSON` file using a `jq` schema.

    Setup:
        .. code-block:: bash

            pip install -U jq

    Instantiate:
        .. code-block:: python

            from langchain_community.document_loaders import JSONLoader
            import json
            from pathlib import Path

            file_path='./sample_quiz.json'
            data = json.loads(Path(file_path).read_text())
            loader = JSONLoader(
                     file_path=file_path,
                     jq_schema='.quiz',
                     text_content=False)

    Load:
        .. code-block:: python

            docs = loader.load()
            print(docs[0].page_content[:100])
            print(docs[0].metadata)

        .. code-block:: python

            {"sport": {"q1": {"question": "Which one is correct team name in
            NBA?", "options": ["New York Bulls"
            {'source': '/sample_quiz
            .json', 'seq_num': 1}

    Async load:
        .. code-block:: python

            docs = await loader.aload()
            print(docs[0].page_content[:100])
            print(docs[0].metadata)

        .. code-block:: python

            {"sport": {"q1": {"question": "Which one is correct team name in
            NBA?", "options": ["New York Bulls"
            {'source': '/sample_quizg
            .json', 'seq_num': 1}

    Lazy load:
        .. code-block:: python

            docs = []
            docs_lazy = loader.lazy_load()

            # async variant:
            # docs_lazy = await loader.alazy_load()

            for doc in docs_lazy:
                docs.append(doc)
            print(docs[0].page_content[:100])
            print(docs[0].metadata)

        .. code-block:: python

            {"sport": {"q1": {"question": "Which one is correct team name in
            NBA?", "options": ["New York Bulls"
            {'source': '/sample_quiz
            .json', 'seq_num': 1}
    NFTÚ	file_pathÚ	jq_schemaÚcontent_keyÚis_content_key_jq_parsableÚmetadata_funcÚtext_contentÚ
json_linesc                 ó   — 	 ddl }|| _         n# t          $ r t          d¦  «        ‚w xY wt          |¦  «                             ¦   «         | _        |                     |¦  «        | _        || _        || _        || _	        || _
        || _        dS )a~  Initialize the JSONLoader.

        Args:
            file_path (Union[str, PathLike]): The path to the JSON or JSON Lines file.
            jq_schema (str): The jq schema to use to extract the data or text from
                the JSON.
            content_key (str): The key to use to extract the content from
                the JSON if the jq_schema results to a list of objects (dict).
                If is_content_key_jq_parsable is True, this has to be a jq compatible
                schema. If is_content_key_jq_parsable is False, this should be a simple
                string key.
            is_content_key_jq_parsable (bool): A flag to determine if
                content_key is parsable by jq or not. If True, content_key is
                treated as a jq schema and compiled accordingly. If False or if
                content_key is None, content_key is used as a simple string.
                Default is False.
            metadata_func (Callable[Dict, Dict]): A function that takes in the JSON
                object extracted by the jq_schema and the default metadata and returns
                a dict of the updated metadata.
            text_content (bool): Boolean flag to indicate whether the content is in
                string format, default to True.
            json_lines (bool): Boolean flag to indicate whether the input is in
                JSON Lines format.
        r   Nz=jq package not found, please install it with `pip install jq`)ÚjqÚImportErrorr   Úresolver   ÚcompileÚ
_jq_schemaÚ_is_content_key_jq_parsableÚ_content_keyÚ_metadata_funcÚ_text_contentÚ_json_lines)	Úselfr   r   r   r   r   r   r   r   s	            ún/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/langchain_community/document_loaders/json_loader.pyÚ__init__zJSONLoader.__init__T   s¦   € ðD	ØˆIˆIˆIàˆDŒGˆGøÝð 	ð 	ð 	ÝØOñô ð ð	øøøõ
 ˜i™œ×0Ò0Ñ2Ô2ˆŒØŸ*š* YÑ/Ô/ˆŒØ+EˆÔ(Ø'ˆÔØ+ˆÔØ)ˆÔØ%ˆÔÐÐs   ‚ Ž(Úreturnc              #   óz  K  — d}| j         rt| j                             d¬¦  «        5 }|D ]:}|                     ¦   «         }|r"|                      ||¦  «        D ]}|V — |dz  }ŒŒ;	 ddd¦  «         dS # 1 swxY w Y   dS |                      | j                             d¬¦  «        |¦  «        D ]}|V — |dz  }ŒdS )z-Load and return documents from the JSON file.r   z	utf-8-sig)Úencodingé   N)r    r   ÚopenÚstripÚ_parseÚ	read_text)r!   ÚindexÚfÚlineÚdocs        r"   Ú	lazy_loadzJSONLoader.lazy_load‡   sB  è è € àˆØÔð 	Ø”×$Ò$¨kÐ$Ñ:Ô:ð '¸aØð 'ð '�DØŸ:š:™<œ<�DØð 'Ø#'§;¢;¨t°UÑ#;Ô#;ð 'ð '˜CØ"%˜I˜I˜IØ! Q™J˜E˜Eøð'ð'ð 'ð 'ñ 'ô 'ð 'ð 'ð 'ð 'ð 'ð 'ð 'øøøð 'ð 'ð 'ð 'ð 'ð 'ð —{’{Ø”×(Ò(°+Ð(Ñ>Ô>Àñô ð ð �ð �	�	�	Ø˜‘
��ð	ð s   §>A3Á3A7Á:A7Úcontentr,   c              #   ór  K  — | j                              t          j        |¦  «        ¦  «        }| j        �|                      |¦  «         t          ||dz   ¦  «        D ]X\  }}|                      |¬¦  «        }|                      |t          | j
        ¦  «        |¬¦  «        }t          ||¬¦  «        V — ŒYdS )z#Convert given content to documents.Nr'   )Úsample)r3   ÚsourceÚseq_num)Úpage_contentÚmetadata)r   ÚinputÚjsonÚloadsr   Ú_validate_content_keyÚ	enumerateÚ	_get_textÚ_get_metadataÚstrr   r   )r!   r1   r,   ÚdataÚir3   Útextr7   s           r"   r*   zJSONLoader._parse™   sÏ   è è € àŒ×$Ò$¥T¤Z°Ñ%8Ô%8Ñ9Ô9ˆð
 ÔÐ(Ø×&Ò& tÑ,Ô,Ð,å" 4¨°©Ñ3Ô3ð 	Að 	A‰IˆAˆvØ—>’>¨�>Ñ0Ô0ˆDØ×)Ò)Ø¥c¨$¬.Ñ&9Ô&9À1ð *ñ ô ˆHõ ¨°xÐ@Ñ@Ô@Ð@Ð@Ð@Ð@ð	Að 	Aó    r3   c                 ó  — | j         �\| j        rG| j                             | j         ¦  «        }|                     |¦  «                             ¦   «         }n|| j                  }n|}| j        r7t          |t          ¦  «        s"|� t          dt          |¦  «        › d�¦  «        ‚t          |t          ¦  «        r|S t          |t          t          f¦  «        r|rt          j        |¦  «        ndS |�t          |¦  «        ndS )zConvert sample to string formatNz%Expected page_content is string, got z‚ instead.                     Set `text_content=False` if the desired input for                     `page_content` is not a stringÚ )r   r   r   r   r8   Úfirstr   Ú
isinstancer?   Ú
ValueErrorÚtypeÚdictÚlistr9   Údumps)r!   r3   Úcompiled_content_keyr1   s       r"   r=   zJSONLoader._get_textª   s  € àÔÐ(ØÔ/ð 4Ø'+¤w§¢°tÔ7HÑ'IÔ'IÐ$Ø.×4Ò4°VÑ<Ô<×BÒBÑDÔD��à  Ô!2Ô3��àˆGàÔð 	?¥j°½#Ñ&>Ô&>ð 	?À7ÐCVÝð4½¸W¹¼ð 4ð 4ð 4ñô ð õ ˜¥Ñ%Ô%ð 	?ØˆNÝ˜¥$­ Ñ.Ô.ð 	?Ø*1Ð9•4”:˜gÑ&Ô&Ð&°rÐ9à#*Ð#6•3�w‘<”<�<¸BÐ>rC   Úadditional_fieldsc                 ó®   — | j         �M|                       ||¦  «        }t          |t          ¦  «        s t          dt	          |¦  «        › d�¦  «        ‚|S |S )zå
        Return a metadata dictionary base on the existence of metadata_func
        :param sample: single data payload
        :param additional_fields: key-word arguments to be added as metadata values
        :return:
        NzUExpected the metadata_func to return a dict but got                                 `ú`)r   rG   rJ   rH   rI   )r!   r3   rN   Úresults       r"   r>   zJSONLoader._get_metadataÄ   sq   € ð ÔÐ*Ø×(Ò(¨Ð1BÑCÔCˆFÝ˜f¥dÑ+Ô+ð Ý ð2Ý"& v¡,¤,ð2ð 2ð 2ñô ð ð ˆMà$Ð$rC   r@   c                 óÔ  — |                      ¦   «         }t          |t          ¦  «        s t          dt	          |¦  «        › d�¦  «        ‚| j        s2|                     | j        ¦  «        €t          d| j        › d�¦  «        ‚| j        r\| j         	                    | j        ¦  «         
                    |¦  «                             ¦   «         €t          d| j        › d�¦  «        ‚dS dS )zCheck if a content key is validztExpected the jq schema to result in a list of objects (dict),                     so sample must be a dict but got `rP   Nz_Expected the jq schema to result in a list of objects (dict)                     with the key `z ` which should be parsable by jq)rF   rG   rJ   rH   rI   r   Úgetr   r   r   r8   rB   )r!   r@   r3   s      r"   r;   z JSONLoader._validate_content_keyØ   s"  € ð —’‘”ˆÝ˜&¥$Ñ'Ô'ð 	ÝðGÝ7;¸F±|´|ðGð Gð Gñô ð ð Ô0ð	à—
’
˜4Ô,Ñ-Ô-Ð5åð8Ø#'Ô#4ð8ð 8ð 8ñô ð ð
 Ô,ð	à”—’ Ô 1Ñ2Ô2×8Ò8¸Ñ@Ô@×EÒEÑGÔGÐOåðWØ#'Ô#4ðWð Wð Wñô ð ð		ð 	àOÐOrC   )NFNTF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r?   r   r	   Úboolr   r   r#   r   r   r0   Úintr*   r   r=   r>   r;   © rC   r"   r   r      s‰  € € € € € ðFð FðX &*Ø5:Ø@DØ!Ø ð1&ð 1&à˜˜h˜Ô'ð1&ð ð1&ð ˜c”]ð	1&ð
 %-¨T¤Nð1&ð   ¨$°¨°tÐ);Ô <Ô=ð1&ð ð1&ð ð1&ð 1&ð 1&ð 1&ðf˜8 HÔ-ð ð ð ð ð$A˜cð A¨#ð A°(¸8Ô2Dð Að Að Að Að"? ð ?¨ð ?ð ?ð ?ð ?ð4%Ø˜3 ˜8”nð%Ø;>ð%à	ˆc�3ˆhŒð%ð %ð %ð %ð(¨#ð °$ð ð ð ð ð ð rC   r   )r9   Úosr   Úpathlibr   Útypingr   r   r   r   r	   r
   Úlangchain_core.documentsr   Ú)langchain_community.document_loaders.baser   r   rZ   rC   r"   ú<module>r`      sÄ   ðØ €€€Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø AÐ AÐ AÐ AÐ AÐ AÐ AÐ AÐ AÐ AÐ AÐ AÐ AÐ AÐ AÐ Aà -Ð -Ð -Ð -Ð -Ð -à @Ð @Ð @Ð @Ð @Ð @ðfð fð fð fð f�ñ fô fð fð fð frC   