Ë
    ümxi«  ã                  ó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__© ó    úf/home/htdocs/ttos/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<   ó   € Ø�z‰zÐ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)z¤Constructor.

        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Ñ1°t·z±zÀdÐ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   € Ü(¨b¯k©k¸bÔ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ÐØÐ*Ñ*Ü'Ø(×/Ñ/°·±¸À^ÀxÐRSÁ|Ð9TÓ0UÓVóð ð -¨TÑ2‰ð3ð �H‰H�VÓ˜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Ø�N‰N˜:Õ&Ü˜
¤HÔ-à�N‰NÔ4¸)È
ÔSÕTä'Ð(<×(CÑ(CÀIÓ(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Ý =Ý ?ô7˜Lô 7ô	T˜CŸG™Gô 	TôHØÐ-×HÑHôHô6LØÐ-×LÑLôLó4>óBð[Øð[à	ó[ð.Øð.àó.ð*Ø#ðà$ôr   