o
    Zh~                     @  s   d Z ddlmZ ddlmZ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 ddl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jZd+ddZd,dd Zd-d$d%Zd.d&d'Zd,d(d)Zd*S )/z\Utilities for Commands.

Common methods for Commands such as RunCommand and CompileCommand.
    )annotations)AbstractSetAnyCallableSequence)ipython_env)model_registry)parsed_args_lib)post_process_utils)llm_function)llmfn_input_utils)llmfn_output_row)llmfn_outputs)	unique_fnc                      s6   e Zd ZdZd fddZddd	ZdddZ  ZS )_GroundTruthLLMFunctionz9LLMFunction that returns pre-generated ground truth data.dataSequence[str]c                   s   t  jd d || _d S )N)outputs_ipython_display_fn)super__init___data)selfr   	__class__ a/var/www/html/lang_env/lib/python3.10/site-packages/google/generativeai/notebook/command_utils.pyr   %   s   
z _GroundTruthLLMFunction.__init__returnAbstractSet[str]c                 C  s   t i S N)	frozenset)r   r   r   r   get_placeholders)   s   z(_GroundTruthLLMFunction.get_placeholdersinputs*llmfn_input_utils.LLMFunctionInputs | None(Sequence[llmfn_outputs.LLMFnOutputEntry]c              	   C  s   t |}t| jt|krtdt| jt|g }tt| j|D ]"\}\}}tj	t
jjdt
jj|itd}|t
jd|||gd q%|S )NzCGround truth should have same number of entries as inputs: {} vs {}r   )r   Zresult_type)Z
prompt_numZ	input_numprompt_varsZoutput_rows)r   Zto_normalized_inputslenr   RuntimeErrorformat	enumeratezipr   ZLLMFnOutputRowr   ZColumnNamesZ
RESULT_NUMZTEXT_RESULTstrappendZLLMFnOutputEntry)r   r!   Znormalized_inputsZoutputsidxvaluer$   Z
output_rowr   r   r   
_call_impl-   s0   
z"_GroundTruthLLMFunction._call_impl)r   r   )r   r   )r!   r"   r   r#   )__name__
__module____qualname____doc__r   r    r.   __classcell__r   r   r   r   r   "   s
    
r   envipython_env.IPythonEnvr   ,Callable[[llmfn_outputs.LLMFnOutputs], None]c                   s    fddS )Nc                   s     |  S r   )displayZas_pandas_dataframe)xr4   r   r   <lambda>O   s    z)_get_ipython_display_fn.<locals>.<lambda>r   r9   r   r9   r   _get_ipython_display_fnL   s   r;   modelsmodel_registry.ModelRegistryipython_env.IPythonEnv | Noneparsed_argsparsed_args_lib.ParsedArgscell_contentr*   post_processing_fns2Sequence[post_process_utils.ParsedPostProcessExpr]llm_function.LLMFunctionc           	      C  s`   |g}|r	t |nd}tj| |j|j||d}|jr$|jdtjd}|D ]}|	|}q&|S )z8Creates an LLMFunction from Command.execute() arguments.N)model
model_argspromptsr   unique)namefn)
r;   r   ZLLMFunctionImplZ	get_modelZ
model_typerF   rH   Zadd_post_process_reorder_fnr   add_to_llm_function)	r<   r4   r?   rA   rB   rG   llmfn_outputs_display_fnllm_fnrJ   r   r   r   create_llm_functionR   s   
rN   name_and_simple_fn%tuple[str, Callable[[str, str], Any]]"tuple[str, llm_function.CompareFn]c                   s    | d   fdd}| d |fS )N   c                   s    |   |  S r   )Zresult_value)r8   yZ	simple_fnr   r   r:   p   s    z,_convert_simple_compare_fn.<locals>.<lambda>r   r   )rO   Znew_fnr   rT   r   _convert_simple_compare_fnl   s   rU   c                 C  sJ   | rt | nd}tj|j|jdd |jD |d}|D ]}||}q|S )?Creates an LLMCompareFunction from Command.execute() arguments.Nc                 S     g | ]}t |qS r   rU   .0r8   r   r   r   
<listcomp>       z/create_llm_compare_function.<locals>.<listcomp>lhs_name_and_fnrhs_name_and_fnZcompare_name_and_fnsr   )r;   r   LLMCompareFunctionr^   r_   
compare_fnrK   )r4   r?   rB   rL   
llm_cmp_fnrJ   r   r   r   create_llm_compare_functiont   s   rc   c           	      C  sX   |rt |nd}t| ||||d}t|jd}tjd|fd|fdd |jD |d}|S )	rV   N)r<   r4   r?   rA   rB   )r   actualground_truthc                 S  rW   r   rX   rY   r   r   r   r[      r\   z,create_llm_eval_function.<locals>.<listcomp>r]   )r;   rN   r   re   r   r`   ra   )	r<   r4   r?   rA   rB   rL   rM   Zground_truth_fnrb   r   r   r   create_llm_eval_function   s    	rf   N)r4   r5   r   r6   )r<   r=   r4   r>   r?   r@   rA   r*   rB   rC   r   rD   )rO   rP   r   rQ   )r4   r>   r?   r@   rB   rC   r   rD   )r2   
__future__r   typingr   r   r   r   Zgoogle.generativeai.notebookr   r   r	   r
   Z google.generativeai.notebook.libr   r   r   r   r   ZLLMFunctionr   r;   rN   rU   rc   rf   r   r   r   r   <module>   s$   
*


