o
    Zh                     @  s   d Z ddlmZ ddl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 G d	d
 d
eZG dd dejZG dd deejZG dd deejZd%ddZd&ddZd'ddZd(dd Zd)d#d$ZdS )*z2Utilities for working with post-processing tokens.    )annotationsN)AnyCallableSequence)py_utils)llm_function)llmfn_output_row)llmfn_post_processc                   @  s   e Zd ZdZdS )PostProcessParseErrorz,An error parsing the post-processing tokens.N)__name__
__module____qualname____doc__ r   r   f/var/www/html/lang_env/lib/python3.10/site-packages/google/generativeai/notebook/post_process_utils.pyr
      s    r
   c                   @  s0   e Zd ZdZejdddZejddd	Zd
S )ParsedPostProcessExprz:A post-processing expression parsed from the command line.returnstrc                 C     dS )z$Returns the name of this expression.Nr   selfr   r   r   name"       zParsedPostProcessExpr.namellm_fnllm_function.LLMFunctionc                 C  r   )zEAdds this parsed expression to `llm_fn` as a post-processing command.Nr   r   r   r   r   r   add_to_llm_function&   r   z)ParsedPostProcessExpr.add_to_llm_functionNr   r   r   r   r   r   )r   r   r   r   abcabstractmethodr   r   r   r   r   r   r      s    r   c                   @  8   e Zd ZdZdddZdd	d
ZdddZdddZdS )_ParsedPostProcessAddExprzEAn expression that returns the value of a new column to add to a row.r   r   fnCallable[[str], Any]c                 C     || _ || _dS )a  Constructor.

        Args:
          name: The name of the expression. The name of the new column will be
            derived from this.
          fn: A function that takes the result of a row and returns a new value to
            add as a new column in the row.
        N_name_fnr   r   r#   r   r   r   __init__0   s   	
z"_ParsedPostProcessAddExpr.__init__r   c                 C     | j S Nr'   r   r   r   r   r   <      z_ParsedPostProcessAddExpr.namerows-Sequence[llmfn_output_row.LLMFnOutputRowView]Sequence[Any]c                       fdd|D S )Nc                      g | ]	}  | qS r   r(   Zresult_value.0rowr   r   r   
<listcomp>@       z6_ParsedPostProcessAddExpr.__call__.<locals>.<listcomp>r   r   r/   r   r   r   __call__?      z"_ParsedPostProcessAddExpr.__call__r   r   c                 C     |j | j| dS Nr   r#   )Zadd_post_process_add_fnr'   r   r   r   r   r   B      z-_ParsedPostProcessAddExpr.add_to_llm_functionN)r   r   r#   r$   r   )r/   r0   r   r1   r   r   r   r   r   r*   r   r;   r   r   r   r   r   r"   +   s    


r"   c                   @  r!   )_ParsedPostProcessReplaceExprz:An expression that returns the new result value for a row.r   r   r#   Callable[[str], str]c                 C  r%   )zConstructor.

        Args:
          name: The name of the expression.
          fn: A function that takes the result of a row and returns the new result.
        Nr&   r)   r   r   r   r*   K   s   
z&_ParsedPostProcessReplaceExpr.__init__r   c                 C  r+   r,   r-   r   r   r   r   r   U   r.   z"_ParsedPostProcessReplaceExpr.namer/   r0   Sequence[str]c                   r2   )Nc                   r3   r   r4   r5   r   r   r   r8   Y   r9   z:_ParsedPostProcessReplaceExpr.__call__.<locals>.<listcomp>r   r:   r   r   r   r;   X   r<   z&_ParsedPostProcessReplaceExpr.__call__r   r   c                 C  r=   r>   )Zadd_post_process_replace_fnr'   r   r   r   r   r   [   r@   z1_ParsedPostProcessReplaceExpr.add_to_llm_functionN)r   r   r#   rC   r   )r/   r0   r   rD   r   rA   r   r   r   r   rB   F   s    



rB   r#   r$   c                 C     t | j| dS r>   )r"   r   r#   r   r   r   post_process_add_fn`      rG   rC   c                 C  rE   r>   )rB   r   rF   r   r   r   post_process_replace_fnd   rH   rI   tokensrD   r   Nonec                 C  s$   | st dt| dkrt dd S )Nz,Cannot have empty post-processing expression   z3Post-processing expression should be a single token)r
   len)rJ   r   r   r   'validate_one_post_processing_expressionh   s
   rN   tuple[str, Any]c              
   C  sv   t |  | d d}t }t|D ] \}}t|}||vr/tdd|d|d  || }qd| |fS )z)Returns name and the resolved expression.r   .zUnable to resolve "{}"NrL    )	rN   splitr   Zget_main_module	enumeratevarsr
   formatjoin)rJ   Ztoken_partsZcurrent_moduleZpart_numpartZcurrent_module_varsr   r   r   '_resolve_one_post_processing_expressionq   s   
rX   Sequence[Sequence[str]]Sequence[ParsedPostProcessExpr]c                 C  s^   g }| D ](}t |\}}t|tr|| qt|tr&|t||d qtd||S )a  Resolves post-processing tokens into ParsedPostProcessExprs.

    E.g. Given [["add_length"], ["to_upper"]] as input, this function will return
    a sequence of ParsedPostProcessExprs that will execute add_length() and
    to_upper() on each entry of the LLM output as post-processing operations.

    Raises:
      PostProcessParseError: An error parsing or resolving the tokens.

    Args:
      tokens: A sequence of post-processing tokens after splitting.

    Returns:
      A sequence of ParsedPostProcessExprs.
    r?   z{} is not callable)rX   
isinstancer   appendr   r"   r
   rU   )rJ   resultsZ
expressionZ	expr_nameZ
expr_valuer   r   r   resolve_post_processing_tokens   s   

r^   )r#   r$   )r#   rC   )rJ   rD   r   rK   )rJ   rD   r   rO   )rJ   rY   r   rZ   )r   
__future__r   r   typingr   r   r   Zgoogle.generativeai.notebookr   Z google.generativeai.notebook.libr   r   r	   RuntimeErrorr
   ABCr   ZLLMFnPostProcessBatchAddFnr"   ZLLMFnPostProcessBatchReplaceFnrB   rG   rI   rN   rX   r^   r   r   r   r   <module>   s*   





	