
    ; i                       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dS )%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dS )PostProcessParseErrorz,An error parsing the post-processing tokens.N)__name__
__module____qualname____doc__     /var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/google/generativeai/notebook/post_process_utils.pyr   r      s        6666r   r   c                  V    e Zd ZdZej        d	d            Zej        d
d            ZdS )ParsedPostProcessExprz:A post-processing expression parsed from the command line.returnstrc                    dS )z$Returns the name of this expression.Nr   selfs    r   namezParsedPostProcessExpr.name"         r   llm_fnllm_function.LLMFunctionc                    dS )zEAdds this parsed expression to `llm_fn` as a post-processing command.Nr   r   r   s     r   add_to_llm_functionz)ParsedPostProcessExpr.add_to_llm_function&   r   r   Nr   r   r   r   r   r   )r   r   r   r   abcabstractmethodr   r!   r   r   r   r   r      sf        DD3 3 3 3 	T T T T T Tr   r   c                  2    e Zd ZdZddZddZddZ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                "    || _         || _        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(   s      r   __init__z"_ParsedPostProcessAddExpr.__init__0   s     
r   r   c                    | j         S Nr,   r   s    r   r   z_ParsedPostProcessAddExpr.name<   
    zr   rows-Sequence[llmfn_output_row.LLMFnOutputRowView]Sequence[Any]c                       fd|D             S )Nc                ^    g | ])}                     |                                          *S r   r-   result_value.0rowr   s     r   
<listcomp>z6_ParsedPostProcessAddExpr.__call__.<locals>.<listcomp>@   1    ===))++,,===r   r   r   r4   s   ` r   __call__z"_ParsedPostProcessAddExpr.__call__?       ========r   r   r   c                :    |                     | j        |           S Nr   r(   )add_post_process_add_fnr,   r    s     r   r!   z-_ParsedPostProcessAddExpr.add_to_llm_functionB   s    --4:$-GGGr   N)r   r   r(   r)   r"   )r4   r5   r   r6   r#   r   r   r   r   r/   r   rA   r!   r   r   r   r'   r'   +   ss         PO
 
 
 
   > > > >H H H H H Hr   r'   c                  2    e Zd ZdZddZddZddZddZdS )_ParsedPostProcessReplaceExprz:An expression that returns the new result value for a row.r   r   r(   Callable[[str], str]c                "    || _         || _        dS )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   r   c                    | j         S r1   r2   r   s    r   r   z"_ParsedPostProcessReplaceExpr.nameU   r3   r   r4   r5   Sequence[str]c                       fd|D             S )Nc                ^    g | ])}                     |                                          *S r   r9   r;   s     r   r>   z:_ParsedPostProcessReplaceExpr.__call__.<locals>.<listcomp>Y   r?   r   r   r@   s   ` r   rA   z&_ParsedPostProcessReplaceExpr.__call__X   rB   r   r   r   c                :    |                     | j        |           S rD   )add_post_process_replace_fnr,   r    s     r   r!   z1_ParsedPostProcessReplaceExpr.add_to_llm_function[   s    11tzd1KKKr   N)r   r   r(   rJ   r"   )r4   r5   r   rM   r#   rG   r   r   r   rI   rI   F   ss         ED      > > > >L L L L L Lr   rI   r(   r)   c                .    t          | j        |           S rD   )r'   r   r(   s    r   post_process_add_fnrT   `   s    $"+"====r   rJ   c                .    t          | j        |           S rD   )rI   r   rS   s    r   post_process_replace_fnrV   d   s    (bkbAAAAr   tokensrM   r   Nonec                l    | 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)rW   s    r   'validate_one_post_processing_expressionr\   h   sA      T#$RSSS
6{{Q#$YZZZ r   tuple[str, Any]c                   t          |            | d                             d          }t          j                    }t	          |          D ]`\  }}t          |          }||vr@t          d                    d                    |d|dz                                          ||         }ad                    |           |fS )z)Returns name and the resolved expression.r   .zUnable to resolve "{}"NrZ    )	r\   splitr   get_main_module	enumeratevarsr   formatjoin)rW   token_partscurrent_modulepart_numpartcurrent_module_varss         r   '_resolve_one_post_processing_expressionrl   q   s     ,F333)//#&&K-//N#K00 3 3$">22***'(//^xRS|^9T0U0UVV   -T2HHVn--r   Sequence[Sequence[str]]Sequence[ParsedPostProcessExpr]c                D   g }| D ]}t          |          \  }}t          |t                    r|                    |           ?t          |t                    r%|                    t          ||                     yt          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.
    rE   z{} is not callable)rl   
isinstancer   appendr   r'   r   re   )rW   results
expression	expr_name
expr_values        r   resolve_post_processing_tokensrv      s    $ ,.G P P
 G
 S S	:j"788 	PNN:&&&&
H-- 	PNN4)
SSSTTTT'(<(C(CI(N(NOOONr   )r(   r)   )r(   rJ   )rW   rM   r   rX   )rW   rM   r   r]   )rW   rm   r   rn   )r   
__future__r   r$   typingr   r   r   google.generativeai.notebookr    google.generativeai.notebook.libr   r	   r
   RuntimeErrorr   ABCr   LLMFnPostProcessBatchAddFnr'   LLMFnPostProcessBatchReplaceFnrI   rT   rV   r\   rl   rv   r   r   r   <module>r      s   9 8 " " " " " " 



 * * * * * * * * * * 1 1 1 1 1 1 9 9 9 9 9 9 = = = = = = ? ? ? ? ? ?7 7 7 7 7L 7 7 7	T 	T 	T 	T 	TCG 	T 	T 	TH H H H H-HH H H6L L L L L-LL L L4> > > >B B B B[ [ [ [. . . .*     r   