
    g3fi/                       U d Z ddlmZ ddlZddlmZmZ ddlmZ ddl	m
Z
mZmZmZ ddlmZmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZmZ ddlmZ ddlmZ e
rddlm Z m!Z! 	 ddl"m#Z# ddl$m%Z% dZ&ed   Z(d&dZ)d'dZ*d(dZ+d&dZ,	 	 	 	 d(dZ-e.e/df   Z0d)dZ1d*dZ2ejf                  e,e)dZ4de5d<   ejl                  e*dZ7de5d <   	 	 	 	 	 	 	 	 d+d!Z8d,d"Z9 G d# d$ee      Z:d-d%Z;y# e'$ r dZ&Y w xY w).zBasePrompt schema definition.    )annotationsN)ABCabstractmethod)	Formatter)TYPE_CHECKINGAnyLiteralcast)	BaseModelcreate_model)override)PromptValueStringPromptValue)BasePromptTemplate)get_colored_textmustache)	formatter)is_interactive_env)CallableSequence)meta)SandboxedEnvironmentTF)f-stringr   jinja2c               z    t         sd}t        |       t               j                  |       j                  di |S )a  Format a template using jinja2.

    *Security warning*:
        As of LangChain 0.0.329, this method uses Jinja2's
        SandboxedEnvironment by default. However, this sand-boxing should
        be treated as a best-effort approach rather than a guarantee of security.
        Do not accept jinja2 templates from untrusted sources as they may lead
        to arbitrary Python code execution.

        https://jinja.palletsprojects.com/en/3.1.x/sandbox/

    Args:
        template: The template string.
        **kwargs: The variables to format the template with.

    Returns:
        The formatted string.

    Raises:
        ImportError: If jinja2 is not installed.
    a  jinja2 not installed, which is needed to use the jinja2_formatter. Please install it with `pip install jinja2`.Please be cautious when using jinja2 templates. Do not expand jinja2 templates using unverified or user-controlled inputs as that can result in arbitrary Python code execution. )_HAS_JINJA2ImportErrorr   from_stringrender)templatekwargsmsgs      [/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_core/prompts/string.pyjinja2_formatterr%   !   sF    , L 	 #
 ?!--h7>>HHH    c                    t        |      }t        |       }||z
  }||z
  }d}|r	|d| dz  }|r|d| z  }|r&t        j                  |j	                         d       yy)zValidate that the input variables are valid for the template.

    Issues a warning if missing or extra variables are found.

    Args:
        template: The template string.
        input_variables: The input variables.
     zMissing variables:  zExtra variables:    )
stacklevelN)set#_get_jinja2_variables_from_templatewarningswarnstrip)r!   input_variablesinput_variables_setvalid_variablesmissing_variablesextra_variableswarning_messages          r$   validate_jinja2r7   G   s     o.9(CO'*==)O;OO01B0C1EE..?@@o++-!< r&   c                    t         sd}t        |      t               }|j                  |       }t	        j
                  |      S )Nzojinja2 not installed, which is needed to use the jinja2_formatter. Please install it with `pip install jinja2`.)r   r   r   parser   find_undeclared_variables)r!   r#   envasts       r$   r-   r-   `   sC    ; 	 #

 C
))H
C))#..r&   c               .    t        j                  | |      S )zFormat a template using mustache.

    Args:
        template: The template string.
        **kwargs: The variables to format the template with.

    Returns:
        The formatted string.
    )r   r    )r!   r"   s     r$   mustache_formatterr>   l   s     ??8V,,r&   c                    t               }d}t        j                  |       D ]K  \  }}|dk(  r|dz  }n1|dv r-|dk7  r(|dk(  r#|j                  |j	                  d      d          |dv sG|dz  }M |S )a  Get the top-level variables from a mustache template.

    For nested variables like `{{person.name}}`, only the top-level
    key (`person`) is returned.

    Args:
        template: The template string.

    Returns:
       The top-level variables from the template.
    r   end   >   sectionvariable	no escapeinverted section.>   rB   rE   )r,   r   tokenizeaddsplit)r!   	variablessection_depthtype_keys        r$   mustache_template_varsrN   y   s     %IM''1 

sE>QMMMs
"MM#))C.+,33QM
 r&   Defsc           	     d   i }d}g }t        j                  |       D ]  \  }}|dk(  r|dk(  r|s|j                         }%|dv r4|j                  |       |t	        |j                  d            z  }d||<   ]|dv sbd||t	        |j                  d            z   <    |j                         D ]!  \  }|xr t        fd|D               |<   # i }|rM|j                         \  }}	|}
|d	d
 D ]  }|
j                  |i       }
 |
j                  |d
   |	rdni        |rMt        d|      S )zGet the variables from a mustache template.

    Args:
        template: The template string.

    Returns:
        The variables from the template as a Pydantic model.
    r   rF   r@   >   rB   rE   F>   rC   rD   Tc              3  B   K   | ]  }|k7  s	t        |        y wN)is_subsequence).0kfkeys     r$   	<genexpr>z"mustache_schema.<locals>.<genexpr>   s#      (
()qDyN4#(
s   
Nr(   PromptInput)r   rG   popappendtuplerI   itemsanypopitem
setdefault_create_model_recursive)r!   fieldsprefixsection_stackrL   rM   fvaldefsfieldis_leafcurrentpartrV   s               @r$   mustache_schemark      sd    F F+-M''1 :
s#:E>&**,55  (eCIIcN++F"F6N//59F6E#))C.112: lln 

d 
C (
-3(
 %
 !
t
 D
)w#2J 	3D((r2G	359Gb<  #=$77r&   c                    t        dt        | fi |j                         D ci c]#  \  }}||rt        ||      d fnt	        |      d f% c}}      S c c}}w )Ntype[BaseModel])r
   r   r]   ra   type)namerf   rU   vs       r$   ra   ra      sg    	
 !JJLAq A+Aq148DGT?R	
	 	s   (Azdict[str, Callable[..., str]]DEFAULT_FORMATTER_MAPPING)r   r   zdict[str, Callable]DEFAULT_VALIDATOR_MAPPINGc                    	 t         |   }	  || |       y# t        $ r'}d|dt        t               d}t	        |      |d}~ww xY w# t        t
        f$ r}d| d}t	        |      |d}~ww xY w)a_  Check that template string is valid.

    Args:
        template: The template string.
        template_format: The template format. Should be one of "f-string" or "jinja2".
        input_variables: The input variables.

    Raises:
        ValueError: If the template format is not supported.
        ValueError: If the prompt schema is invalid.
    zInvalid template format z, should be one of rF   NzMInvalid prompt schema; check for mismatched or missing input parameters from )rr   KeyErrorlistrq   
ValueError
IndexError)r!   template_formatr1   validator_funcexcr#   s         r$   check_valid_templater{      s    '2?C'x1  '&&9 :./03 	 o3&' j! '$%Q( 	 o3&'s+   	 	A	 	A"AA	A/A**A/c                   |dk(  rt        |       }nW|dk(  r1t               j                  |       D ch c]  \  }}}}|| }}}n!|dk(  rt        |       }nd| }t	        |      |dk(  rE|D ]@  }d|v sd|v sd|v rd|d	}t	        |      |j                         s1d|d
}t	        |       t        |      S c c}}w )a3  Get the variables from the template.

    Args:
        template: The template string.
        template_format: The template format. Should be one of "f-string" or "jinja2".

    Returns:
        The variables from the template.

    Raises:
        ValueError: If the template format is not supported.
    r   r   r   zUnsupported template format: rF   []zInvalid variable name z[ in f-string template. Variable names cannot contain attribute access (.) or indexing ([]).zk in f-string template. Variable names cannot be all digits as they are interpreted as positional arguments.)r-   r   r9   rN   rv   isdigitsorted)r!   rx   r1   _rp   r#   vars          r$   get_template_variablesr      s    ("=hG	J	& ) 1 1( ;
!Q1q}A
 
 
J	&0:-o->?o *$" 	&C czSCZ3#:,SG 43 4 
 !o% {{},SG 4/ 0 
 !o%'	&* /""C
s   B> B>c                  h    e Zd ZdZed	d       Zd
dZd
dZee	dd              Z
	 d	 	 	 ddZddZy)StringPromptTemplatezAString prompt that exposes the format method, returning a prompt.c                
    g dS )ztGet the namespace of the LangChain object.

        Returns:
            `["langchain", "prompts", "base"]`
        )	langchainpromptsbaser   )clss    r$   get_lc_namespacez%StringPromptTemplate.get_lc_namespace2  s
     0/r&   c                :    t         | j                  di |      S )zFormat the prompt with the inputs.

        Args:
            **kwargs: Any arguments to be passed to the prompt template.

        Returns:
            A formatted string.
        textr   )r   formatselfr"   s     r$   format_promptz"StringPromptTemplate.format_prompt;  s     !kdkk&;F&;<<r&   c                V   K   t         | j                  di | d{         S 7 	w)zAsync format the prompt with the inputs.

        Args:
            **kwargs: Any arguments to be passed to the prompt template.

        Returns:
            A formatted string.
        Nr   r   )r   aformatr   s     r$   aformat_promptz#StringPromptTemplate.aformat_promptF  s'      !LDLL,B6,B&BCC&Bs   )'
)c                     y rR   r   r   s     r$   r   zStringPromptTemplate.formatQ  s    ,/r&   c           	         | j                   D ci c]  }|d| z   dz    }}|r-|j                         D ci c]  \  }}|t        |d       }}} | j                  di |S c c}w c c}}w )zGet a pretty representation of the prompt.

        Args:
            html: Whether to return an HTML-formatted string.

        Returns:
            A pretty representation of the prompt.
        {}yellowr   )r1   r]   r   r   )r   html	input_var
dummy_varsrU   rp   s         r$   pretty_reprz StringPromptTemplate.pretty_reprU  s     DHCWCW
6?Is	{+c11

 
 =G=M=M=O59Q#Ax00J  t{{(Z((
s
   A#A(c                J    t        | j                  t                            y)z,Print a pretty representation of the prompt.)r   N)printr   r   )r   s    r$   pretty_printz!StringPromptTemplate.pretty_printk  s    d$6$89:r&   N)return	list[str])r"   r   r   r   )r"   r   r   str)F)r   boolr   r   )r   None)__name__
__module____qualname____doc__classmethodr   r   r   r   r   r   r   r   r   r&   r$   r   r   /  s[    K0 0	=	D /  / )) 
),;r&   r   c                     t               dk(  st              dk(  ryt              t               k  ryt         fdt        t                     D              S )z.Return True if child is subsequence of parent.r   Fc              3  4   K   | ]  }|   |   k(    y wrR   r   )rT   ichildparents     r$   rW   z!is_subsequence.<locals>.<genexpr>v  s     @uQx6!9$@s   )lenallrange)r   r   s   ``r$   rS   rS   p  sI    
5zQ#f+*
6{SZ@eCJ.?@@@r&   )r!   r   r"   r   r   r   )r!   r   r1   r   r   r   )r!   r   r   zset[str])r!   r   r   rm   )ro   r   rf   rO   r   rm   )r!   r   rx   r   r1   r   r   r   )r!   r   rx   r   r   r   )r   r   r   r   r   r   )<r   
__future__r   r.   abcr   r   stringr   typingr   r   r	   r
   pydanticr   r   typing_extensionsr   langchain_core.prompt_valuesr   r   langchain_core.prompts.baser   langchain_core.utilsr   r   langchain_core.utils.formattingr   $langchain_core.utils.interactive_envr   collections.abcr   r   r   r   jinja2.sandboxr   r   r   PromptTemplateFormatr%   r7   r-   r>   rN   dictr   rO   rk   ra   r   rq   __annotations__validate_input_variablesrr   r{   r   r   rS   r   r&   r$   <module>r      s1   # "  #  4 4 , & G : ; 5 C23K ?@ #IL=2	/
-< CK$8N
   "< 8  222 . ''$'':C'	'@2#j>;-s >;BAk
  Ks   #C' 'C10C1