
    ci                     4    d Z ddlZddlmZ dZ G d d      Zy)z4Prompt template for creating prompts with variables.    N)Setz\{([_a-zA-Z][_a-zA-Z0-9]*)\}c                   N    e Zd ZdZdefdZdee   fdZd
dZdefdZ	defdZ
y	)PromptTemplatea  A prompt template for creating prompts with variables.

    The `PromptTemplate` class allows users to define a template string with
    variables represented in curly braces `{variable}`. The variable
    names cannot contain spaces and must start with a letter or underscore,
    followed by letters, digits, or underscore. These variables can be
    replaced with specific values using the `assemble` method, providing
    flexibility in generating dynamic prompts.

    Usage:

        ```
        template_str = "Hello, {name}! Today is {day}. How are you?"
        prompt_template = PromptTemplate(template_str)
        completed_prompt = prompt_template.assemble(name="John", day="Monday")
        print(completed_prompt)
        ```
    templatec                 N    t        |      | _        | j                         | _        y)zInitializes the PromptTemplate with a given template.

        Args:
            template: The template string with variables. Variables should be
              represented in curly braces `{variable}`.
        N)strr   _get_variables	variables)selfr   s     J/tmp/pip-target-z3e9_cxr/lib/python/vertexai/evaluation/prompt_template.py__init__zPromptTemplate.__init__-   s     H,,.    returnc                 \    t        t        j                  t        | j                              S )z>Extracts and return a set of variable names from the template.)setrefindall_VARIABLE_NAME_REGEXr   r   s    r   r	   zPromptTemplate._get_variables7   s    2::2DMMBCCr   c                     | j                   }|j                         D ]Q  \  }}|| j                  vrt        d| d| j                         d|z   dz   }|j	                  |t        |            }S t        |      S )a8  Replaces only the provided variables in the template with specific values.

        Args:
            **kwargs: Keyword arguments where keys are placeholder names and values
              are the replacements.

        Returns:
            A new PromptTemplate instance with the updated template string.
        zInvalid variable name 'z'. Valid variables are: {})r   itemsr
   
ValueErrorreplacer   r   )r   kwargsassembled_stringvariable_namevalueplaceholders         r   assemblezPromptTemplate.assemble;   s      ==$*LLN 	Q M5DNN2 -m_ =,,0NN+;=  -3K/77SZP	Q .//r   c                     | j                   S )zReturns the template string.r   r   s    r   __str__zPromptTemplate.__str__P   s    }}r   c                 "    d| j                    dS )z6Returns a string representation of the PromptTemplate.zPromptTemplate('z')r#   r   s    r   __repr__zPromptTemplate.__repr__T   s    !$--33r   N)r   r   )__name__
__module____qualname____doc__r   r   r   r	   r!   r$   r&    r   r   r   r      sB    &/ /DC D0* 4# 4r   r   )r*   r   typingr   r   r   r+   r   r   <module>r-      s    " ; 	 6 =4 =4r   