
    D_i/                       % S r SSKJr  SSKrSSKJrJr  SSKJr  SSK	J
r
JrJrJr  SSKJrJr  SSKJr  SS	KJrJr  SS
KJr  SSKJrJr  SSKJr  SSKJr  \
(       a  SSKJ r J!r!   SSK"J#r#  SSK$J%r%  Sr&\S   r(S&S jr)S'S jr*S(S jr+S&S jr,    S(S jr-\.\/S4   r0S)S jr1S*S jr2\Rf                  \,\)S.r4S\5S'   \Rl                  \*S.r7S\5S '           S+S! jr8S,S" jr9 " S# S$\\5      r:S-S% jr;g! \' a    Sr& Nf = f).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                   [         (       d  Sn[        U5      e[        5       R                  U 5      R                  " S0 UD6$ )ai  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      W/home/james-whalen/.local/lib/python3.13/site-packages/langchain_core/prompts/string.pyjinja2_formatterr%   !   sE    , ;L 	 #
  !--h7>>HHH    c                    [        U5      n[        U 5      nX2-
  nX#-
  nSnU(       a	  USU S3-  nU(       a  USU 3-  nU(       a$  [        R                  " UR	                  5       SS9  gg)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01B0C1EE..?@@o++-!< r&   c                    [         (       d  Sn[        U5      e[        5       nUR                  U 5      n[        R
                  " U5      $ )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-   `   sE    ;; 	 #

 C
))H
C))#..r&   c               .    [         R                  " X5      $ )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     ??8,,r&   c                    [        5       nSn[        R                  " U 5       HS  u  p4US:X  a  US-  nO5US;   a/  US:w  a)  US:X  a#  UR                  UR	                  S5      S   5        US;   d  MN  US-  nMU     U$ )zGet 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
E>QMMMs
"MM#))C.+,33QM 2 r&   Defsc           	       ^ 0 nSn/ n[         R                  " U 5       H  u  pEUS:X  a  M  US:X  a  U(       a  UR                  5       nM,  M.  US;   a4  UR                  U5        U[	        UR                  S5      5      -  nSX'   Mh  US;   d  Mp  SX[	        UR                  S5      5      -   '   M     UR                  5        H,  u  mnU=(       a    [        U4S jU 5       5      (       + UT'   M.     0 nU(       aY  UR                  5       u  pUn
US	S
  H  nU
R                  U0 5      n
M     U
R                  US
   U	(       a  SO0 5        U(       a  MY  [        SU5      $ )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  J   >#    U  H  oT:w  d  M
  [        TU5      v   M     g 7fN)is_subsequence).0kfkeys     r$   	<genexpr>"mustache_schema.<locals>.<genexpr>   s%      (
-3Dy#N4##Vs   	##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_schemarl      sT    F F+-M''1
#:E>&**, 55  (eCIIcN++F"FN//59FE#))C.112 2 lln
d 
C (
-3(
 %
 !
t % D
)#2JD((r2G 59Gb< & #=$77r&   c                    [        S[        U 40 UR                  5        VVs0 s H&  u  p#X#(       a  [        X#5      S 4O[	        U5      S 4_M(     snnD65      $ s  snnf )Ntype[BaseModel])r
   r   r^   rb   type)namerg   rU   vs       r$   rb   rb      sh    	
 !JJL(DA A+A148DGT?R(	
	 	s   -Azdict[str, Callable[..., str]]DEFAULT_FORMATTER_MAPPING)r   r   zdict[str, Callable]DEFAULT_VALIDATOR_MAPPINGc                     [         U   n U" X5        g! [         a(  nSU< S[        [        5       S3n[	        U5      UeSnAff = f! [        [
        4 a  nSU S3n[	        U5      UeSnAff = f)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 )rs   KeyErrorlistrr   
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
A1A,,A1c                   US:X  a  [        U 5      nO\US:X  a4  [        5       R                  U 5       VVs1 s H  u  p4  o4c  M  UiM     nnnO"US:X  a  [        U 5      nOSU 3n[	        U5      eUS:X  aT  U HN  nSU;   d  SU;   d  SU;   a  SU< S	3n[	        U5      eUR                  5       (       d  M>  SU< S
3n[	        U5      e   [        U5      $ s  snnf )a  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   rw   isdigitsorted)r!   ry   r1   _rq   r#   vars          r$   get_template_variablesr      s    ("=hG	J	& ) 1 1( ;
 ;*!1A ; 	 
 
J	&0:-o->?o *$"C czSCZ3#:,SG 43 4 
 !o% {{}},SG 4/ 0 
 !o%' #* /""C
s   
CCc                  ~    \ rS rSrSr\SS j5       rSS jrSS jr\	\
SS j5       5       r S   SS jjrSS jrS	rg
)StringPromptTemplatei/  zAString prompt that exposes the format method, returning a prompt.c                
    / SQ$ )z\Get the namespace of the LangChain object.

Returns:
    `["langchain", "prompts", "base"]`
)	langchainpromptsbaser   )clss    r$   get_lc_namespace%StringPromptTemplate.get_lc_namespace2  s
     0/r&   c                4    [        U R                  " S0 UD6S9$ )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_prompt"StringPromptTemplate.format_prompt;  s     !dkk&;F&;<<r&   c                P   #    [        U R                  " S0 UD6I Sh  vN S9$  N7f)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_prompt#StringPromptTemplate.aformat_promptF  s$      !DLL,B6,B&BCC&Bs   &$&c                    g rR   r   r   s     r$   r   StringPromptTemplate.formatQ  s    ,/r&   c           	         U R                    Vs0 s H  o"SU -   S-   _M     nnU(       a/  UR                  5        VVs0 s H  u  pEU[        US5      _M     nnnU R                  " S0 UD6$ s  snf s  snnf )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   rq   s         r$   pretty_repr StringPromptTemplate.pretty_reprU  s     DHCWCW
CWis	{+c11CW 	 
 =G=M=M=O=OTQ#Ax00=O   {{(Z((
s
   A,A1c                D    [        U R                  [        5       S95        g)z,Print a pretty representation of the prompt.)r   N)printr   r   )r   s    r$   pretty_print!StringPromptTemplate.pretty_printk  s    d$6$89:r&   r   N)return	list[str])r"   r   r   r   )r"   r   r   str)F)r   boolr   r   )r   None)__name__
__module____qualname____firstlineno____doc__classmethodr   r   r   r   r   r   r   r   __static_attributes__r   r&   r$   r   r   /  s[    K0 0	=	D /  / )) 
),;r&   r   c                   ^ ^ [        T 5      S:X  d  [        T5      S:X  a  g[        T5      [        T 5      :  a  g[        U U4S j[        [        T 5      5       5       5      $ )z.Return True if child is subsequence of parent.r   Fc              3  :   >#    U  H  nTU   TU   :H  v   M     g 7frR   r   )rT   ichildparents     r$   rW   !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   rn   )rp   r   rg   rO   r   rn   )r!   r   ry   r   r1   r   r   r   )r!   r   ry   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   rl   rb   r   rr   __annotations__validate_input_variablesrs   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   (C6 6D D