a
    ”˜ëjÇ  ã                   @  sÊ   d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	 ddl
mZ ddlmZ ddlmZ dd	lmZmZ dd
lmZmZ ddlmZ G dd„ deeef ƒZddddœdd„Zdddddœdd„ZdS )zDict prompt template.é    )ÚannotationsN)Úcached_property)ÚAnyÚLiteralÚOptional)Úmodel_validator)Úoverride©Údumpd)ÚDEFAULT_FORMATTER_MAPPINGÚget_template_variables)ÚRunnableConfigÚRunnableSerializable)Úensure_configc                   @  sä   e Zd ZU dZded< ded< edd�d dœd	d
„ƒZeddœdd„ƒZdddœdd„Z	dddœdd„Z
ed)dddddœdd„ƒZeddœdd„ƒZeddœdd„ƒZeddœd d!„ƒZeddœd"d#„ƒZd$d%œddd&œd'd(„ZdS )*ÚDictPromptTemplatez¸Template represented by a dict.

    Recognizes variables in f-string or mustache formatted string dict values. Does NOT
    recognize variables in dict keys. Applies recursively.
    údict[str, Any]ÚtemplateúLiteral['f-string', 'mustache']Útemplate_formatÚafter)Úmode)Úreturnc                 C  s   t | j| jƒ | S )zBValidate that the template structure contains only safe variables.©Ú_get_input_variablesr   r   ©Úself© r   úZ/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain_core/prompts/dict.pyÚvalidate_template   s    z$DictPromptTemplate.validate_templateú	list[str]c                 C  s   t | j| jƒS )zTemplate input variables.r   r   r   r   r   Úinput_variables%   s    z"DictPromptTemplate.input_variablesr   )Úkwargsr   c                 K  s   t | j|| jƒS ©z[Format the prompt with the inputs.

        Returns:
            A formatted dict.
        )Ú_insert_input_variablesr   r   ©r   r!   r   r   r   Úformat*   s    zDictPromptTemplate.formatc                 Ë  s   | j f i |¤ŽS r"   ©r%   r$   r   r   r   Úaformat2   s    zDictPromptTemplate.aformatNÚdictzOptional[RunnableConfig])ÚinputÚconfigr!   r   c                   s*   ˆ j ‡ fdd„|t|ƒfdˆ jdœ|¤ŽS )Nc                   s   ˆ j f i | ¤ŽS ©Nr&   )Úxr   r   r   Ú<lambda>?   ó    z+DictPromptTemplate.invoke.<locals>.<lambda>Úprompt)Zrun_typeZ
serialized)Z_call_with_configr   Ú_serialized)r   r)   r*   r!   r   r   r   Úinvoke:   s    
ýûúzDictPromptTemplate.invokeÚstrc                 C  s   dS )Nzdict-promptr   r   r   r   r   Ú_prompt_typeG   s    zDictPromptTemplate._prompt_typec                 C  s   t | ƒS r+   r	   r   r   r   r   r0   K   s    zDictPromptTemplate._serializedÚboolc                 C  s   dS )z*Return True as this class is serializable.Tr   ©Úclsr   r   r   Úis_lc_serializableO   s    z%DictPromptTemplate.is_lc_serializablec                 C  s   g d¢S )z{Get the namespace of the langchain object.

        Returns:
            ``["langchain_core", "prompts", "dict"]``
        )Zlangchain_coreZpromptsr(   r   r5   r   r   r   Úget_lc_namespaceT   s    z#DictPromptTemplate.get_lc_namespaceF)Úhtml)r9   r   c                C  s   t ‚dS )z³Human-readable representation.

        Args:
            html: Whether to format as HTML. Defaults to False.

        Returns:
            Human-readable representation.
        N)ÚNotImplementedError)r   r9   r   r   r   Úpretty_repr]   s    	zDictPromptTemplate.pretty_repr)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   r   Úpropertyr    r%   r'   r   r1   r3   r   r0   Úclassmethodr7   r8   r;   r   r   r   r   r      s*   
 ÿr   r(   r   r   )r   r   r   c                 C  sœ   g }|   ¡ D ]‚}t|tƒr*|t||ƒ7 }qt|tƒrD|t||ƒ7 }qt|ttfƒr|D ]6}t|tƒrt|t||ƒ7 }qVt|tƒrV|t||ƒ7 }qVqtt|ƒƒS r+   )	ÚvaluesÚ
isinstancer2   r   r(   r   ÚlistÚtupleÚset)r   r   r    Úvr,   r   r   r   r   i   s    



r   r   )r   Úinputsr   r   c           
      C  sô   i }t | }|  ¡ D ]Ú\}}t|tƒr<||fi |¤Ž||< qt|tƒrz|dkrhd|v rhd}tj|dd� t|||ƒ||< qt|tt	fƒræg }|D ]B}	t|	tƒr¶| 
||	fi |¤Ž¡ q�t|	tƒr�| 
t|	||ƒ¡ q�t|ƒ|ƒ||< q|||< q|S )NZ	image_urlÚpathzÁSpecifying image inputs via file path in environments with user-input paths is a security vulnerability. Out of an abundance of caution, the utility has been removed to prevent possible misuse.é   )Ú
stacklevel)r   ÚitemsrD   r2   r(   ÚwarningsÚwarnr#   rE   rF   ÚappendÚtype)
r   rI   r   Ú	formattedÚ	formatterÚkrH   ÚmsgZformatted_vr,   r   r   r   r#   {   s.    

ÿ


ÿ
r#   )r?   Ú
__future__r   rN   Ú	functoolsr   Útypingr   r   r   Zpydanticr   Ztyping_extensionsr   Zlangchain_core.loadr
   Zlangchain_core.prompts.stringr   r   Zlangchain_core.runnablesr   r   Zlangchain_core.runnables.configr   r(   r   r   r#   r   r   r   r   Ú<module>   s   T