o
    j6h                     @  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   ~/var/www/html/chefvision.cloud.itp360.com/venv/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(   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#   )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   r6   r   r   r   r9   Y   r:   z:_ParsedPostProcessReplaceExpr.__call__.<locals>.<listcomp>r   r;   r   r   r   r<   X   r=   z&_ParsedPostProcessReplaceExpr.__call__r   r   c                 C  r>   r?   )add_post_process_replace_fnr'   r   r   r   r   r   [   rB   z1_ParsedPostProcessReplaceExpr.add_to_llm_functionN)r   r   r#   rE   r   )r/   r0   r   rF   r   rC   r   r   r   r   rD   F   s    



rD   r#   r$   c                 C     t | j| dS r?   )r"   r   r#   r   r   r   post_process_add_fn`      rJ   rE   c                 C  rH   r?   )rD   r   rI   r   r   r   post_process_replace_fnd   rK   rL   tokensrF   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)rM   r   r   r   'validate_one_post_processing_expressionh   s
   rQ   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 "{}"NrO    )	rQ   splitr   get_main_module	enumeratevarsr
   formatjoin)rM   token_partscurrent_modulepart_numpartcurrent_module_varsr   r   r   '_resolve_one_post_processing_expressionq   s   
r`   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)r`   
isinstancer   appendr   r"   r
   rY   )rM   results
expression	expr_name
expr_valuer   r   r   resolve_post_processing_tokens   s   

ri   )r#   r$   )r#   rE   )rM   rF   r   rN   )rM   rF   r   rR   )rM   ra   r   rb   )r   
__future__r   r   typingr   r   r   google.generativeai.notebookr    google.generativeai.notebook.libr   r   r	   RuntimeErrorr
   ABCr   LLMFnPostProcessBatchAddFnr"   LLMFnPostProcessBatchReplaceFnrD   rJ   rL   rQ   r`   ri   r   r   r   r   <module>   s*   





	