
    g3fi*                        d 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 ddlmZmZmZmZmZmZ erddlmZ  G d	 d
e      Zy)zPrompt schema definition.    )annotations)Path)TYPE_CHECKINGAny)	BaseModelmodel_validator)override)DEFAULT_FORMATTER_MAPPINGPromptTemplateFormatStringPromptTemplatecheck_valid_templateget_template_variablesmustache_schema)RunnableConfigc                  d    e Zd ZU dZeedd              Zeedd              Zde	d<   	 dZ
de	d<   	 d	Zd
e	d<   	  ed      edd              Zedd fd       ZddZedd       ZddZe	 	 d 	 	 	 	 	 	 	 	 	 	 	 	 	 d!d       Ze	 d	 	 	 	 	 	 	 d"d       Zeddd	 	 	 	 	 	 	 	 	 d#d       Z xZS )$PromptTemplatea  Prompt template for a language model.

    A prompt template consists of a string template. It accepts a set of parameters
    from the user that can be used to generate a prompt for a language model.

    The template can be formatted using either f-strings (default), jinja2,
    or mustache syntax.

    *Security warning*:
        Prefer using `template_format="f-string"` instead of
        `template_format="jinja2"`, or make sure to NEVER accept jinja2 templates
        from untrusted sources as they may lead to arbitrary Python code execution.

        As of LangChain 0.0.329, Jinja2 templates will be rendered using
        Jinja2's SandboxedEnvironment by default. This sand-boxing should
        be treated as a best-effort approach rather than a guarantee of security,
        as it is an opt-out rather than opt-in approach.

        Despite the sand-boxing, we recommend to never use jinja2 templates
        from untrusted sources.

    Example:
        ```python
        from langchain_core.prompts import PromptTemplate

        # Instantiation using from_template (recommended)
        prompt = PromptTemplate.from_template("Say {foo}")
        prompt.format(foo="bar")

        # Instantiation using initializer
        prompt = PromptTemplate(template="Say {foo}")
        ```
    c                    d| j                   iS )Ntemplate_formatr   selfs    [/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_core/prompts/prompt.pylc_attributeszPromptTemplate.lc_attributes;   s     t33
 	
    c                
    g dS )zvGet the namespace of the LangChain object.

        Returns:
            `["langchain", "prompts", "prompt"]`
        )	langchainpromptsprompt )clss    r   get_lc_namespacezPromptTemplate.get_lc_namespaceB   s
     21r   strtemplatef-stringr   r   Fboolvalidate_templatebefore)modec                   |j                  d      |S |j                  dd       |j                  di        |j                  d      rM|d   dk(  rd}t        |      d|vrd	}t        |      |d   t        |d         z   }t	        |d   |d   |       |d   r*t        |d   |d         D cg c]  }||d   vr| c}|d<   |S c c}w )
z7Check that template and input variables are consistent.r#   r   r$   partial_variablesr&   mustachez'Mustache templates cannot be validated.input_variablesz:Input variables must be provided to validate the template.)get
setdefault
ValueErrorlistr   r   )r    valuesmsg
all_inputsvars        r   pre_init_validationz"PromptTemplate.pre_init_validationV   s    ::j!) M 	+Z8-r2::)*'(J6? o% .R o% 12T&AT:U5VVJ z"F+<$=z #$ 2:&/@(A) f%899	 )F$% )s   /CNc                j    | j                   dk7  rt        | 	  |      S t        | j                        S )zGet the input schema for the prompt.

        Args:
            config: The runnable configuration.

        Returns:
            The input schema for the prompt.
        r+   )r   superget_input_schemar   r#   )r   config	__class__s     r   r8   zPromptTemplate.get_input_schema|   s2     :-7+F33t}}--r   c                   t        |t              r| j                  |j                  k7  rd}t        |      t	        t        | j                        t        |j                        z        }| j                  |j                  z   }| j                  xr |j                  }t        | j                  j                               }|j                  j                         D ]  \  }}||v rd}t        |      |||<    t        |||| j                  |      S t        |t              r&t        j                  || j                        }	| |	z   S dt        |       }t        |      )a  Override the + operator to allow for combining prompt templates.

        Raises:
            ValueError: If the template formats are not f-string or if there are
                conflicting partial variables.
            NotImplementedError: If the other object is not a `PromptTemplate` or str.

        Returns:
            A new `PromptTemplate` that is the combination of the two.
        z)Cannot add templates of different formatsz*Cannot have same variable partialed twice.)r#   r,   r*   r   r&   r   z Unsupported operand type for +: )
isinstancer   r   r/   r0   setr,   r#   r&   dictr*   itemsr"   from_templatetypeNotImplementedError)
r   otherr2   r,   r#   r&   r*   kvr   s
             r   __add__zPromptTemplate.__add__   s[    e^,##u'<'<<A o%"D(()C0E0E,FFO }}u~~5H $ 6 6 R5;R;R $T%;%;%A%A%C D//557 )1))FC$S/)'(!!$	)
 "! /"3 $ 4 4"3  eS!#11 $ 4 4 2 F &= 0e>!#&&r   c                     y)zReturn the prompt type key.r   r   r   s    r   _prompt_typezPromptTemplate._prompt_type   s     r   c                l     | j                   di |}t        | j                     | j                  fi |S )zFormat the prompt with the inputs.

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

        Returns:
            A formatted string.
        r   )!_merge_partial_and_user_variablesr
   r   r#   )r   kwargss     r   formatzPromptTemplate.format   s9     877A&A()=)=>t}}WPVWWr   c                D    |j                  |g||      } | d||d|S )a  Take examples in list format with prefix and suffix to create a prompt.

        Intended to be used as a way to dynamically create a prompt from examples.

        Args:
            examples: List of examples to use in the prompt.
            suffix: String to go after the list of examples. Should generally
                set up the user's input.
            input_variables: A list of variable names the final prompt template
                will expect.
            example_separator: The separator to use in between examples. Defaults
                to two new line characters.
            prefix: String that should go before any examples. Generally includes
                examples.

        Returns:
            The final prompt generated.
        )r,   r#   r   )join)r    examplessuffixr,   example_separatorprefixrK   r#   s           r   from_exampleszPromptTemplate.from_examples   s6    8 %))6*EH*Ef*EFP?XPPPr   c                `    t        |      j                  |      } | j                  dd|i|S )aD  Load a prompt from a file.

        Args:
            template_file: The path to the file containing the prompt template.
            encoding: The encoding system for opening the template file.
                If not provided, will use the OS default.

        Returns:
            The prompt loaded from the file.
        )encodingr#   r   )r   	read_textr@   )r    template_filerU   rK   r#   s        r   	from_filezPromptTemplate.from_file   s7    " &00(0C s  =(=f==r   )r   r*   c               v    t        ||      }|xs i }|r|D cg c]	  }||vs| }} | d||||d|S c c}w )a  Load a prompt template from a template.

        *Security warning*:
            Prefer using `template_format="f-string"` instead of
            `template_format="jinja2"`, or make sure to NEVER accept jinja2 templates
            from untrusted sources as they may lead to arbitrary Python code execution.

            As of LangChain 0.0.329, Jinja2 templates will be rendered using
            Jinja2's SandboxedEnvironment by default. This sand-boxing should
            be treated as a best-effort approach rather than a guarantee of security,
            as it is an opt-out rather than opt-in approach.

            Despite the sand-boxing, we recommend never using jinja2 templates
            from untrusted sources.

        Args:
            template: The template to load.
            template_format: The format of the template. Use `jinja2` for jinja2,
                `mustache` for mustache, and `f-string` for f-strings.
            partial_variables: A dictionary of variables that can be used to partially
                fill in the template. For example, if the template is
                `"{variable1} {variable2}"`, and `partial_variables` is
                `{"variable1": "foo"}`, then the final prompt will be
                `"foo {variable2}"`.
            **kwargs: Any other arguments to pass to the prompt template.

        Returns:
            The prompt template loaded from the template.
        )r,   r#   r   r*   r   )r   )r    r#   r   r*   rK   r,   partial_variables_r4   s           r   r@   zPromptTemplate.from_template   sp    L 1?K.4".#=O2OO   
++0	

 
 	
	s   	66)returnzdict[str, Any])r[   	list[str])r1   r>   r[   r   )N)r9   zRunnableConfig | Noner[   ztype[BaseModel])rC   r   r[   r   )r[   r"   )rK   r   r[   r"   )z

 )rO   r\   rP   r"   r,   r\   rQ   r"   rR   r"   rK   r   r[   r   )rW   z
str | PathrU   z
str | NonerK   r   r[   r   )
r#   r"   r   r   r*   zdict[str, Any] | NonerK   r   r[   r   )__name__
__module____qualname____doc__propertyr	   r   classmethodr!   __annotations__r   r&   r   r5   r8   rF   rH   rL   rS   rX   r@   __classcell__)r:   s   @r   r   r      s    D 
  

 2  2 M,6O)66 $t#8(#"  $"H . .*'X  
X  "(QQ Q #	Q
 Q Q Q 
Q Q<   $>!> > 	>
 
> >& 
 1;373
3
 .	3

 13
 3
 
3
 3
r   r   N)ra   
__future__r   pathlibr   typingr   r   pydanticr   r   typing_extensionsr	   langchain_core.prompts.stringr
   r   r   r   r   r   langchain_core.runnables.configr   r   r   r   r   <module>rm      s8     "  % / &  >W
) W
r   