a
    ”˜ëjä'  ã                   @   sÚ  U d Z ddlZddlZddlmZ ddlmZmZmZ ddl	Z	ddl
mZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ dZe e¡Zeddœdd„Zedddd�ddœeeedœdd„ƒZddœeeeedœdd„Zddœeeedœdd„Z eedœdd„Z!ddœeeedœd d!„Z"ddœeeedœd"d#„Z#edddd�d/ddœeeef ee eed$œd%d&„ƒZ$d0ddœeeef ee eed'œd(d)„Z%ddœeeedœd*d+„Z&e#e"e&d,œZ'eeed-ef f e(d.< dS )1zLoad prompts.é    N)ÚPath)ÚCallableÚOptionalÚUnion)Ú
deprecated)ÚStrOutputParser)ÚBasePromptTemplate)ÚChatPromptTemplate)ÚFewShotPromptTemplate)ÚPromptTemplatezIhttps://raw.githubusercontent.com/hwchase17/langchain-hub/master/prompts/)ÚpathÚreturnc                 C   s>   |   ¡ rd| › d�}t|ƒ‚d| jv r:d| › d�}t|ƒ‚dS )zÂReject absolute paths and ``..`` traversal components.

    Args:
        path: The path to validate.

    Raises:
        ValueError: If the path is absolute or contains ``..`` components.
    zPath 'zÌ' is absolute. Absolute paths are not allowed when loading prompt configurations to prevent path traversal attacks. Use relative paths instead, or pass `allow_dangerous_paths=True` if you trust the input.z..zÍ' contains '..' components. Directory traversal sequences are not allowed when loading prompt configurations. Use direct relative paths instead, or pass `allow_dangerous_paths=True` if you trust the input.N)Úis_absoluteÚ
ValueErrorÚparts)r   Úmsg© r   ú]/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain_core/prompts/loading.pyÚ_validate_path   s    	
ÿ

ÿr   z0.3.86z1.0.0zkUse `dumpd`/`dumps` from `langchain_core.load` to serialize prompts and `load`/`loads` to deserialize them.)ZsinceZremovalÚalternativeF©Úallow_dangerous_paths)Úconfigr   r   c                C   sN   d| vrt  d¡ |  dd¡}|tvr:d|› d�}t|ƒ‚t| }|| |d�S )a2  Load prompt from config dict.

    Args:
        config: Dict containing the prompt configuration.
        allow_dangerous_paths: If ``False`` (default), file paths in the
            config (such as ``template_path``, ``examples``, and
            ``example_prompt_path``) are validated to reject absolute paths
            and directory traversal (``..``) sequences. Set to ``True`` only
            if you trust the source of the config.

    Returns:
        A PromptTemplate object.

    Raises:
        ValueError: If the prompt type is not supported.
    Ú_typez-No `_type` key found, defaulting to `prompt`.ÚpromptzLoading z prompt not supportedr   )ÚloggerÚwarningÚpopÚtype_to_loader_dictr   )r   r   Zconfig_typer   Zprompt_loaderr   r   r   Úload_prompt_from_config0   s    
r   )Úvar_namer   r   r   c                C   st   | › d�|v rp| |v r0d| › d| › d�}t |ƒ‚t| | › d�¡ƒ}|sPt|ƒ |jdkrd| ¡ }nt ‚||| < |S )z*Load template from the path if applicable.Ú_pathzBoth `z_path` and `z` cannot be provided.z.txt)r   r   r   r   ÚsuffixÚ	read_text)r    r   r   r   Ztemplate_pathÚtemplater   r   r   Ú_load_templateU   s    

r%   c                C   s´   t | d tƒrn t | d tƒr¤t| d ƒ}|s6t|ƒ |jdd��H}|jdkrZt |¡}n"|jdv rpt	 
|¡}nd}t|ƒ‚W d  ƒ n1 s�0    Y  || d< nd}t|ƒ‚| S )	zLoad examples if necessary.Úexampleszutf-8©Úencodingú.json>   ú.yamlú.ymlz=Invalid file format. Only json or yaml formats are supported.Nz;Invalid examples format. Only list or string are supported.)Ú
isinstanceÚlistÚstrr   r   Úopenr"   ÚjsonÚloadÚyamlÚ	safe_loadr   )r   r   r   Úfr&   r   r   r   r   Ú_load_examplesm   s"    

&
r5   )r   r   c                 C   sH   |   d¡ }rD|  d¡dk }r2d|› �}t|ƒ‚tf i |¤Ž| d< | S )zLoad output parser.Zoutput_parserr   ÚdefaultzUnsupported output parser )Úgetr   r   )r   Ú_configZoutput_parser_typer   r   r   r   Ú_load_output_parser„   s    
r9   c                C   sš   t d| |d�} t d| |d�} d| v rdd| v r8d}t|ƒ‚t|  d¡ƒ}|sRt|ƒ t||d�| d< nt| d |d�| d< t| |d�} t| ƒ} t	f i | ¤ŽS )z+Load the "few shot" prompt from the config.r"   r   ÚprefixÚexample_prompt_pathZexample_promptzGOnly one of example_prompt and example_prompt_path should be specified.)
r%   r   r   r   r   Úload_promptr   r5   r9   r
   )r   r   r   r;   r   r   r   Ú_load_few_shot_promptŽ   s.    ÿÿÿÿÿ
r=   c                C   sL   t d| |d�} t| ƒ} |  dd¡}|dkr>d|› d�}t|ƒ‚tf i | ¤ŽS )z%Load the prompt template from config.r$   r   Útemplate_formatzf-stringZjinja2zLoading templates with 'z­' format is no longer supported since it can lead to arbitrary code execution. Please migrate to using the 'f-string' template format, which does not suffer from this issue.)r%   r9   r7   r   r   )r   r   r>   r   r   r   r   Ú_load_prompt±   s    ÿ
ÿr?   )r   r(   r   r   c                C   s.   t | tƒr |  d¡r d}t|ƒ‚t| ||d�S )a�  Unified method for loading a prompt from LangChainHub or local filesystem.

    Args:
        path: Path to the prompt file.
        encoding: Encoding of the file.
        allow_dangerous_paths: If ``False`` (default), file paths referenced
            inside the loaded config (such as ``template_path``, ``examples``,
            and ``example_prompt_path``) are validated to reject absolute paths
            and directory traversal (``..``) sequences. Set to ``True`` only
            if you trust the source of the config.

    Returns:
        A PromptTemplate object.

    Raises:
        RuntimeError: If the path is a Lang Chain Hub path.
    zlc://z‘Loading from the deprecated github-based Hub is no longer supported. Please use the new LangChain Hub at https://smith.langchain.com/hub instead.r   )r,   r.   Ú
startswithÚRuntimeErrorÚ_load_prompt_from_file)r   r(   r   r   r   r   r   r<   É   s    ÿÿr<   )Úfiler(   r   r   c                C   s®   t | ƒ}|jdkrJ|j|d��}t |¡}W d  ƒ q¢1 s>0    Y  nX|j d¡rŽ|j|d��}t |¡}W d  ƒ q¢1 s‚0    Y  nd|j› �}t|ƒ‚t	||d�S )zLoad prompt from file.r)   r'   N)r*   r+   zGot unsupported file type r   )
r   r"   r/   r0   r1   Úendswithr2   r3   r   r   )rC   r(   r   Ú	file_pathr4   r   r   r   r   r   rB   ò   s    
**rB   c                C   sR   |   d¡}|r |d d   d¡nd}|   d¡ |s>d}t|ƒ‚tjf d|i| ¤ŽS )zLoad chat prompt from config.Úmessagesr   r   r$   NZinput_variablesz'Can't load chat prompt without template)r   r   r	   Zfrom_template)r   r   rF   r$   r   r   r   r   Ú_load_chat_prompt	  s    

rG   )r   Zfew_shotZchat.r   )N)N))Ú__doc__r0   ÚloggingÚpathlibr   Útypingr   r   r   r2   Zlangchain_core._apir   Z$langchain_core.output_parsers.stringr   Zlangchain_core.prompts.baser   Zlangchain_core.prompts.chatr	   Zlangchain_core.prompts.few_shotr
   Zlangchain_core.prompts.promptr   ZURL_BASEÚ	getLoggerÚ__name__r   r   ÚdictÚboolr   r.   r%   r5   r9   r=   r?   r<   rB   rG   r   Ú__annotations__r   r   r   r   Ú<module>   s”   
ýÿþ ÿþÿþ$ÿþý þü
û% þü
ûýüý