o
    ȳg                  
   @   sd   d dl mZmZ deeeeef f deeeeeeef f f fddZdededefdd	Zd
S )    )DictUnioninputreturnc           	      C   s   zddl }W n ty   tdw t| tr&|| g}|jd |jdS t| tr\t }| D ]	}|	| |  q0||}|j}d}t }| D ]}|| ||< |d7 }qI||jdS t
dt|  )a  
    Sanitize input string or dict of strings by replacing sensitive data with
    placeholders.

    It returns the sanitized input string or dict of strings and the secure
    context as a dict following the format:
    {
        "sanitized_input": <sanitized input string or dict of strings>,
        "secure_context": <secure context>
    }

    The secure context is a bytes object that is needed to de-sanitize the response
    from the LLM.

    Args:
        input: Input string or dict of strings.

    Returns:
        Sanitized input string or dict of strings and the secure context
        as a dict following the format:
        {
            "sanitized_input": <sanitized input string or dict of strings>,
            "secure_context": <secure context>
        }

        The `secure_context` needs to be passed to the `desanitize` function.

    Raises:
        ValueError: If the input is not a string or dict of strings.
        ImportError: If the `opaqueprompts` Python package is not installed.
    r   NhCould not import the `opaqueprompts` Python package, please install it with `pip install opaqueprompts`.)sanitized_inputsecure_context   zUnexpected input type )opaquepromptsImportError
isinstancestrsanitizesanitized_textsr   dictlistappend
ValueErrortype)	r   opsanitize_responsevalueskeysanitize_values_responsesanitized_input_valuesidxr    r   g/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/langchain_community/utilities/opaqueprompts.pyr      s6   "



r   sanitized_textr   c                 C   s6   zddl }W n ty   tdw || |}|jS )z
    Restore the original sensitive data from the sanitized text.

    Args:
        sanitized_text: Sanitized text.
        secure_context: Secure context returned by the `sanitize` function.

    Returns:
        De-sanitized text.
    r   Nr   )r
   r   
desanitizedesanitized_text)r   r   r   desanitize_responser   r   r   r   Q   s   r   N)typingr   r   r   r   bytesr   r   r   r   r   <module>   s    
M