U
    h                     @  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dZddddZdddddZdddddZd d!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   S/tmp/pip-unpacked-wheel-doshhd5e/google/generativeai/notebook/post_process_utils.pyr
      s   r
   c                   @  s:   e Zd ZdZejddddZejddddd	Zd
S )ParsedPostProcessExprz:A post-processing expression parsed from the command line.strreturnc                 C  s   dS )z$Returns the name of this expression.Nr   selfr   r   r   name"   s    zParsedPostProcessExpr.namellm_function.LLMFunctionllm_fnr   c                 C  s   dS )zEAdds this parsed expression to `llm_fn` as a post-processing command.Nr   r   r   r   r   r   add_to_llm_function&   s    z)ParsedPostProcessExpr.add_to_llm_functionN)r   r   r   r   abcabstractmethodr   r   r   r   r   r   r      s
   r   c                   @  sN   e Zd ZdZdddddZdddd	Zd
ddddZdddddZdS )_ParsedPostProcessAddExprzEAn expression that returns the value of a new column to add to a row.r   Callable[[str], Any]r   fnc                 C  s   || _ || _dS )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   r"   r   r   r   __init__2   s    	z"_ParsedPostProcessAddExpr.__init__r   c                 C  s   | j S Nr$   r   r   r   r   r   >   s    z_ParsedPostProcessAddExpr.name-Sequence[llmfn_output_row.LLMFnOutputRowView]zSequence[Any]rowsr   c                   s    fdd|D S )Nc                   s   g | ]}  | qS r   r%   Zresult_value.0rowr   r   r   
<listcomp>D   s     z6_ParsedPostProcessAddExpr.__call__.<locals>.<listcomp>r   r   r,   r   r   r   __call__A   s    z"_ParsedPostProcessAddExpr.__call__r   r   c                 C  s   |j | j| dS Nr!   )Zadd_post_process_add_fnr$   r   r   r   r   r   F   s    z-_ParsedPostProcessAddExpr.add_to_llm_functionNr   r   r   r   r'   r   r3   r   r   r   r   r   r   -   s
   r   c                   @  sN   e Zd ZdZdddddZdddd	Zd
ddddZdddddZdS )_ParsedPostProcessReplaceExprz:An expression that returns the new result value for a row.r   Callable[[str], str]r!   c                 C  s   || _ || _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&   r   r   r   r'   Q   s    z&_ParsedPostProcessReplaceExpr.__init__r   c                 C  s   | j S r(   r)   r   r   r   r   r   [   s    z"_ParsedPostProcessReplaceExpr.namer*   Sequence[str]r+   c                   s    fdd|D S )Nc                   s   g | ]}  | qS r   r-   r.   r   r   r   r1   a   s     z:_ParsedPostProcessReplaceExpr.__call__.<locals>.<listcomp>r   r2   r   r   r   r3   ^   s    z&_ParsedPostProcessReplaceExpr.__call__r   r   c                 C  s   |j | j| dS r4   )Zadd_post_process_replace_fnr$   r   r   r   r   r   c   s    z1_ParsedPostProcessReplaceExpr.add_to_llm_functionNr5   r   r   r   r   r6   L   s
   
r6   r    r"   c                 C  s   t | j| dS r4   )r   r   r9   r   r   r   post_process_add_fnj   s    r:   r7   c                 C  s   t | j| dS r4   )r6   r   r9   r   r   r   post_process_replace_fnn   s    r;   r8   None)tokensr   c                 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)r=   r   r   r   'validate_one_post_processing_expressionr   s    r@   ztuple[str, Any]c              
   C  sv   t |  | d d}t }t|D ]@\}}t|}||kr^tdd|d|d  || }q&d| |fS )z)Returns name and the resolved expression.r   .zUnable to resolve "{}"Nr>    )	r@   splitr   Zget_main_module	enumeratevarsr
   formatjoin)r=   Ztoken_partsZcurrent_moduleZpart_numpartZcurrent_module_varsr   r   r   '_resolve_one_post_processing_expression}   s    
rI   zSequence[Sequence[str]]zSequence[ParsedPostProcessExpr]c                 C  s`   g }| D ]R}t |\}}t|tr.|| qt|trL|t||d qtd|q|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)rI   
isinstancer   appendr   r   r
   rF   )r=   resultsZ
expressionZ	expr_nameZ
expr_valuer   r   r   resolve_post_processing_tokens   s    

rM   )r   
__future__r   r   typingr   r   r   Zgoogle.generativeai.notebookr   Z google.generativeai.notebook.libr   r   r	   RuntimeErrorr
   ABCr   ZLLMFnPostProcessBatchAddFnr   ZLLMFnPostProcessBatchReplaceFnr6   r:   r;   r@   rI   rM   r   r   r   r   <module>   s,   
 
 