o
    ß­j«  ã                   @  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   úl/var/www/html/CropPilot/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%   )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)   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*   
ÿ
ÿ



	