
    
!i                    B   S r SSKJr  SSKrSSKJrJrJr  SSKJ	r	  SSK
Jr  SSK
Jr  SSK
Jr   " S	 S
\5      r " S S\R                   5      r " S S\\R$                  5      r " S S\\R(                  5      rSS jrSS jr    SS jr    SS jr    SS jrg)z2Utilities for working with post-processing tokens.    )annotationsN)AnyCallableSequence)py_utils)llm_function)llmfn_output_row)llmfn_post_processc                      \ rS rSrSrSrg)PostProcessParseError   z,An error parsing the post-processing tokens. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       i/home/james-whalen/.local/lib/python3.13/site-packages/google/generativeai/notebook/post_process_utils.pyr   r      s    6r   r   c                  h    \ rS rSrSr\R                  SS j5       r\R                  SS j5       rSr	g)	ParsedPostProcessExpr   z:A post-processing expression parsed from the command line.c                    g)z$Returns the name of this expression.Nr   selfs    r   nameParsedPostProcessExpr.name"       r   c                    g)zEAdds this parsed expression to `llm_fn` as a post-processing command.Nr   r   llm_fns     r   add_to_llm_function)ParsedPostProcessExpr.add_to_llm_function&   r   r   r   Nreturnstrr"   llm_function.LLMFunctionr&   r)   )
r   r   r   r   r   abcabstractmethodr   r#   r   r   r   r   r   r      s9    D3 3 	T Tr   r   c                  @    \ rS rSrSrS	S jrS
S jrSS jrSS jrSr	g)_ParsedPostProcessAddExpr+   zEAn expression that returns the value of a new column to add to a row.c                    Xl         X l        g)zConstructor.

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__"_ParsedPostProcessAddExpr.__init__0   s     
r   c                    U R                   $ Nr1   r   s    r   r   _ParsedPostProcessAddExpr.name<       zzr   c                j    U Vs/ s H!  o R                  UR                  5       5      PM#     sn$ s  snf r8   r2   result_valuer   rowsrows      r   __call__"_ParsedPostProcessAddExpr.__call__?   *    8<=))+,===   (0c                6    UR                  U R                  U S9$ Nr   r4   )add_post_process_add_fnr1   r!   s     r   r#   -_ParsedPostProcessAddExpr.add_to_llm_functionB   s    --4::$-GGr   r2   r1   N)r   r'   r4   Callable[[str], Any]r%   )r@   -Sequence[llmfn_output_row.LLMFnOutputRowView]r&   zSequence[Any]r(   
r   r   r   r   r   r5   r   rB   r#   r   r   r   r   r-   r-   +   s     P
>Hr   r-   c                  @    \ rS rSrSrS	S jrS
S jrSS jrSS jrSr	g)_ParsedPostProcessReplaceExprF   z:An expression that returns the new result value for a row.c                    Xl         X l        g)zConstructor.

Args:
  name: The name of the expression.
  fn: A function that takes the result of a row and returns the new result.
Nr0   r3   s      r   r5   &_ParsedPostProcessReplaceExpr.__init__K   s     
r   c                    U R                   $ r8   r9   r   s    r   r   "_ParsedPostProcessReplaceExpr.nameU   r;   r   c                j    U Vs/ s H!  o R                  UR                  5       5      PM#     sn$ s  snf r8   r=   r?   s      r   rB   &_ParsedPostProcessReplaceExpr.__call__X   rD   rE   c                6    UR                  U R                  U S9$ rG   )add_post_process_replace_fnr1   r!   s     r   r#   1_ParsedPostProcessReplaceExpr.add_to_llm_function[   s    11tzzd1KKr   rK   N)r   r'   r4   Callable[[str], str]r%   )r@   rM   r&   Sequence[str]r(   rN   r   r   r   rP   rP   F   s     E>Lr   rP   c                *    [        U R                  U S9$ rG   )r-   r   r4   s    r   post_process_add_fnr_   `   s    $"++"==r   c                *    [        U R                  U S9$ rG   )rP   r   r^   s    r   post_process_replace_fnra   d   s    (bkkbAAr   c                \    U (       d  [        S5      e[        U 5      S:  a  [        S5      eg )Nz,Cannot have empty post-processing expression   z3Post-processing expression should be a single token)r   len)tokenss    r   'validate_one_post_processing_expressionrf   h   s/     #$RSS
6{Q#$YZZ r   c                >   [        U 5        U S   R                  S5      n[        R                  " 5       n[	        U5       HH  u  p4[        U5      nXE;  a/  [        SR                  SR                  USUS-    5      5      5      eXT   nMJ     SR                  U 5      U4$ )z)Returns name and the resolved expression.r   .zUnable to resolve "{}"Nrc    )	rf   splitr   get_main_module	enumeratevarsr   formatjoin)re   token_partscurrent_modulepart_numpartcurrent_module_varss         r   '_resolve_one_post_processing_expressionru   q   s     ,F3)//#&K--/N#K0">2*'(//^xRS|9T0UV  -2 1 HHVn--r   c                   / nU  H  n[        U5      u  p4[        U[        5      (       a  UR                  U5        M8  [        U[        5      (       a  UR                  [        X4S95        Mg  [        SR                  U5      5      e   U$ )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.
rH   z{} is not callable)ru   
isinstancer   appendr   r-   r   rn   )re   results
expression	expr_name
expr_values        r   resolve_post_processing_tokensr}      sw    $ ,.G
 G
 S	j"788NN:&
H--NN4)ST'(<(C(CI(NOO  Nr   )r4   rL   )r4   r[   )re   r\   r&   None)re   r\   r&   ztuple[str, Any])re   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-   LLMFnPostProcessBatchReplaceFnrP   r_   ra   rf   ru   r}   r   r   r   <module>r      s    9 " 
 * * 1 9 = ?7L 7	TCGG 	TH-HHH6L-LLL4>B[[	[...*#$r   