a
    ”˜ëj‰  ã                   @  sÐ   d Z ddlmZ ddlZddlZddlmZmZmZ ddl	m
Z
 dddœd	d
„Zdddœdd„Zddœddddœdd„Ze dej¡Zedœddddœdd„ZdZedœddddœdd „Zdd!dd"œd#d$„ZdS )%zUtilities for JSON.é    )ÚannotationsN)ÚAnyÚCallableÚUnion)ÚOutputParserExceptionzre.Match[str]Ústr)ÚmatchÚreturnc                 C  sZ   |   d¡}t dd|¡}t dd|¡}t dd|¡}t dd	|¡}|   d
¡| |   d¡ S )Né   ú\nz\\nz\rz\\rz\tz\\tz(?<!\\)"z\"é   é   )ÚgroupÚreÚsub)r   Úvalue© r   úX/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain_core/utils/json.pyÚ_replace_new_line   s    
r   zUnion[str, bytes, bytearray])Úmultiline_stringr	   c                 C  s*   t | ttfƒr|  ¡ } tjdt| tjd�S )at  Custom parser for multiline strings.

    The LLM response for `action_input` may be a multiline
    string containing unescaped newlines, tabs or quotes. This function
    replaces those characters with their escaped counterparts.
    (newlines in JSON must be double-escaped: `\\n`).

    Returns:
        The modified string with escaped newlines, tabs and quotes.
    z("action_input"\:\s*")(.*?)("))Úflags)Ú
isinstanceÚbytesÚ	bytearrayÚdecoder   r   r   ÚDOTALL)r   r   r   r   Ú_custom_parser   s    ür   F©ÚstrictÚboolr   )Úsr   r	   c                C  sf  zt j| |d�W S  t jy$   Y n0 g }g }d}d}| D ]²}|}|r€|dkrX|sXd}qâ|dkrj|sjd}qâ|dkrz| }qâd}nb|dkr’d}d}nP|dkr¦| d	¡ n<|d
krº| d¡ n(|dv râ|rÜ|d |krÜ| ¡  n dS | |¡ q:|�r|�r| ¡  | d¡ | ¡  |�rXzt jd || ¡|d�W S  t j�yR   | ¡  Y n0 �qt j| |d�S )zóParse a JSON string that may be missing closing braces.

    Args:
        s: The JSON string to parse.
        strict: Whether to use strict parsing. Defaults to False.

    Returns:
        The parsed JSON object as a Python dictionary.
    r   Fú"Ú
r   ú\TÚ{Ú}ú[ú]>   r%   r'   éÿÿÿÿNÚ )ÚjsonÚloadsÚJSONDecodeErrorÚappendÚpopÚreverseÚjoin)r    r   Z	new_charsÚstackZis_inside_stringÚescapedÚcharZnew_charr   r   r   Úparse_partial_json0   sT    ÿ

r4   z```(json)?(.*)©ÚparserzCallable[[str], Any]Údict)Újson_stringr6   r	   c                C  sP   zt | |d�W S  tjyB   t | ¡}|du r4| n| d¡}Y n0 t ||d�S )zïParse a JSON string from a Markdown string.

    Args:
        json_string: The Markdown string.
        parser: The parser to use. Defaults to `parse_partial_json`.

    Returns:
        The parsed JSON object as a Python dictionary.
    r5   Nr
   )Ú_parse_jsonr*   r,   Ú_json_markdown_reÚsearchr   )r8   r6   r   Újson_strr   r   r   Úparse_json_markdown„   s    
r=   z 
	`)r<   r6   r	   c                C  s   |   t¡} t| ƒ} || ƒS )N)ÚstripÚ_json_strip_charsr   )r<   r6   r   r   r   r9   Ÿ   s    
r9   z	list[str])ÚtextÚexpected_keysr	   c              
   C  sž   zt | ƒ}W n: tjyF } z d|› �}t|ƒ|‚W Y d}~n
d}~0 0 t|tƒspdt|ƒj› d�}t|| d�‚|D ]$}||vrtd|› d|› �}t|ƒ‚qt|S )a—  Parse and check a JSON string from a Markdown string.

    Checks that it contains the expected keys.

    Args:
        text: The Markdown string.
        expected_keys: The expected keys in the JSON string.

    Returns:
        The parsed JSON object as a Python dictionary.

    Raises:
        OutputParserException: If the JSON string is invalid or does not contain
            the expected keys.
    z Got invalid JSON object. Error: Nz&Expected JSON object (dict), but got: z. )Z
llm_outputz)Got invalid return object. Expected key `z` to be present, but got )r=   r*   r,   r   r   r7   ÚtypeÚ__name__)r@   rA   Zjson_objÚeÚmsgÚerror_messageÚkeyr   r   r   Úparse_and_check_json_markdown¬   s"    
 
ÿÿÿ
rH   )Ú__doc__Ú
__future__r   r*   r   Útypingr   r   r   Zlangchain_core.exceptionsr   r   r   r4   Úcompiler   r:   r=   r?   r9   rH   r   r   r   r   Ú<module>   s   
Qÿÿ