
    rh                    8   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
e      Z G d dej                         Z G d deej$                        Z G d deej(                        ZddZddZ	 	 	 	 ddZ	 	 	 	 ddZ	 	 	 	 ddZy)z2Utilities for working with post-processing tokens.    )annotationsN)AnyCallableSequence)py_utils)llm_function)llmfn_output_row)llmfn_post_processc                      e Zd ZdZy)PostProcessParseErrorz,An error parsing the post-processing tokens.N)__name__
__module____qualname____doc__     /var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/google/generativeai/notebook/post_process_utils.pyr   r      s    6r   r   c                  \    e Zd ZdZej
                  dd       Zej
                  dd       Zy)ParsedPostProcessExprz:A post-processing expression parsed from the command line.c                     y)z$Returns the name of this expression.Nr   selfs    r   namezParsedPostProcessExpr.name"       r   c                     y)zEAdds this parsed expression to `llm_fn` as a post-processing command.Nr   r   llm_fns     r   add_to_llm_functionz)ParsedPostProcessExpr.add_to_llm_function&   r   r   Nreturnstrr   llm_function.LLMFunctionr    r#   )r   r   r   r   abcabstractmethodr   r   r   r   r   r   r      s9    D3 3 	T Tr   r   c                  0    e Zd ZdZddZddZd	dZd
dZy)_ParsedPostProcessAddExprzEAn expression that returns the value of a new column to add to a row.c                     || _         || _        y)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   fns      r   __init__z"_ParsedPostProcessAddExpr.__init__0   s     
r   c                    | j                   S Nr*   r   s    r   r   z_ParsedPostProcessAddExpr.name<       zzr   c                f    |D cg c]!  }| j                  |j                               # c}S c c}w r0   r+   result_valuer   rowsrows      r   __call__z"_ParsedPostProcessAddExpr.__call__?   (    8<=))+,===   &.c                <    |j                  | j                  |       S Nr   r-   )add_post_process_add_fnr*   r   s     r   r   z-_ParsedPostProcessAddExpr.add_to_llm_functionB   s    --4::$-GGr   N)r   r!   r-   Callable[[str], Any]r   )r7   -Sequence[llmfn_output_row.LLMFnOutputRowView]r    zSequence[Any]r"   r   r   r   r   r.   r   r9   r   r   r   r   r'   r'   +   s     P
>Hr   r'   c                  0    e Zd ZdZddZddZd	dZd
dZy)_ParsedPostProcessReplaceExprz:An expression that returns the new result value for a row.c                     || _         || _        y)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,   s      r   r.   z&_ParsedPostProcessReplaceExpr.__init__K   s     
r   c                    | j                   S r0   r1   r   s    r   r   z"_ParsedPostProcessReplaceExpr.nameU   r2   r   c                f    |D cg c]!  }| j                  |j                               # c}S c c}w r0   r4   r6   s      r   r9   z&_ParsedPostProcessReplaceExpr.__call__X   r:   r;   c                <    |j                  | j                  |       S r=   )add_post_process_replace_fnr*   r   s     r   r   z1_ParsedPostProcessReplaceExpr.add_to_llm_function[   s    11tzzd1KKr   N)r   r!   r-   Callable[[str], str]r   )r7   rA   r    Sequence[str]r"   rB   r   r   r   rD   rD   F   s     E>Lr   rD   c                0    t        | j                  |       S r=   )r'   r   r-   s    r   post_process_add_fnrN   `   s    $"++"==r   c                0    t        | j                  |       S r=   )rD   r   rM   s    r   post_process_replace_fnrP   d   s    (bkkbAAr   c                P    | st        d      t        |       dkD  rt        d      y )Nz,Cannot have empty post-processing expression   z3Post-processing expression should be a single token)r   len)tokenss    r   'validate_one_post_processing_expressionrU   h   s/     #$RSS
6{Q#$YZZ r   c                :   t        |        | d   j                  d      }t        j                         }t	        |      D ]H  \  }}t        |      }||vr/t        dj                  dj                  |d|dz                      ||   }J dj                  |       |fS )z)Returns name and the resolved expression.r   .zUnable to resolve "{}"NrR    )	rU   splitr   get_main_module	enumeratevarsr   formatjoin)rT   token_partscurrent_modulepart_numpartcurrent_module_varss         r   '_resolve_one_post_processing_expressionrd   q   s     ,F3)//#&K--/N#K0 3$">2**'(//^xRS|9T0UV  -T23 HHVn--r   c                   g }| D ]x  }t        |      \  }}t        |t              r|j                  |       3t        |t              r|j                  t        ||             `t        dj                  |             |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)rd   
isinstancer   appendr   r'   r   r]   )rT   results
expression	expr_name
expr_values        r   resolve_post_processing_tokensrl      s|    $ ,.G P
 G
 S	:j"78NN:&
H-NN4)
ST'(<(C(CI(NOOP Nr   )r-   r@   )r-   rJ   )rT   rK   r    None)rT   rK   r    ztuple[str, Any])rT   zSequence[Sequence[str]]r    zSequence[ParsedPostProcessExpr])r   
__future__r   r$   typingr   r   r   google.generativeai.notebookr    google.generativeai.notebook.libr   r	   r
   RuntimeErrorr   ABCr   LLMFnPostProcessBatchAddFnr'   LLMFnPostProcessBatchReplaceFnrD   rN   rP   rU   rd   rl   r   r   r   <module>rv      s    9 " 
 * * 1 9 = ?7L 7	TCGG 	TH-HHH6L-LLL4>B[[	[...*#$r   