o
    ß­j~  ã                   @  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__© úg/var/www/html/CropPilot/venv/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   Úresult_type)Ú
prompt_numÚ	input_numÚprompt_varsÚoutput_rows)r   Úto_normalized_inputsÚlenr   ÚRuntimeErrorÚformatÚ	enumerateÚzipr   ÚLLMFnOutputRowr   ÚColumnNamesÚ
RESULT_NUMÚTEXT_RESULTÚstrÚappendÚLLMFnOutputEntry)r   r!   Únormalized_inputsÚoutputsÚidxÚvaluer'   Ú
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   )ÚdisplayÚas_pandas_dataframe)Úx©rA   r   r   Ú<lambda>O   s    z)_get_ipython_display_fn.<locals>.<lambda>r   rG   r   rG   r   Ú_get_ipython_display_fnL   s   rI   Úmodelsúmodel_registry.ModelRegistryúipython_env.IPythonEnv | NoneÚparsed_argsúparsed_args_lib.ParsedArgsÚcell_contentr3   Úpost_processing_fnsú2Sequence[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)
rI   r   ÚLLMFunctionImplÚ	get_modelÚ
model_typerT   rV   Úadd_post_process_reorder_fnr   Úadd_to_llm_function)	rJ   rA   rM   rO   rP   rU   Úllmfn_outputs_display_fnÚllm_fnrX   r   r   r   Úcreate_llm_functionR   s   
ür`   Ú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   )Úresult_value)rF   Úy©Ú	simple_fnr   r   rH   p   s    z,_convert_simple_compare_fn.<locals>.<lambda>r   r   )ra   Únew_fnr   rg   r   Ú_convert_simple_compare_fnl   s   rj   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   ©rj   ©Ú.0rF   r   r   r   Ú
<listcomp>   ó    z/create_llm_compare_function.<locals>.<listcomp>©Úlhs_name_and_fnÚrhs_name_and_fnÚcompare_name_and_fnsr   )rI   r   ÚLLMCompareFunctionrs   rt   Ú
compare_fnr]   )rA   rM   rP   r^   Ú
llm_cmp_fnrX   r   r   r   Úcreate_llm_compare_functiont   s   üry   c           	      C  sX   |rt |ƒnd}t| ||||d�}t|jd�}tjd|fd|fdd„ |jD ƒ|d�}|S )	rk   N)rJ   rA   rM   rO   rP   )r   ÚactualÚground_truthc                 S  rl   r   rm   rn   r   r   r   rp       rq   z,create_llm_eval_function.<locals>.<listcomp>rr   )rI   r`   r   r{   r   rv   rw   )	rJ   rA   rM   rO   rP   r^   r_   Úground_truth_fnrx   r   r   r   Úcreate_llm_eval_functionˆ   s    û	ür}   N)rA   rB   r   rC   )rJ   rK   rA   rL   rM   rN   rO   r3   rP   rQ   r   rR   )ra   rb   r   rc   )rA   rL   rM   rN   rP   rQ   r   rR   )r?   Ú
__future__r   Útypingr   r   r   r   Úgoogle.generativeai.notebookr   r   r	   r
   Ú google.generativeai.notebook.libr   r   r   r   r   ÚLLMFunctionr   rI   r`   rj   ry   r}   r   r   r   r   Ú<module>   s$   
*


