U
    hn:                     @  s   d Z ddlmZ ddlmZ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 ejZejZejZejZejZG d
d dejZG dd dejZG dd de
jZG dd de
jZG dd de
jZedkre
   dS )zUnittest for llm_function.    )annotations)AnyCallableOptionalMappingSequence)absltest)llm_function)llmfn_inputs_source)llmfn_output_row)llmfn_outputs)modelc                   @  s2   e Zd ZdZddddZdddd	d
ddZdS )
_MockModelz1Mock model that returns a caller-provided result.Sequence[str]mock_resultsc                 C  s
   || _ d S N)_mock_results)selfr    r   V/tmp/pip-unpacked-wheel-doshhd5e/google/generativeai/notebook/lib/llm_function_test.py__init__&   s    z_MockModel.__init__Nstrzmodel_lib.ModelArguments | Nonezmodel_lib.ModelResults)model_input
model_argsreturnc                 C  s   t j|| jdS )N)r   Ztext_results)	model_libZModelResultsr   )r   r   r   r   r   r   
call_model)   s     z_MockModel.call_model)N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   #   s    r   c                   @  s   e Zd ZddddZdS )_MockInputsSourcez6tuple[Sequence[Mapping[str, str]], Callable[[], None]])r   c                 C  s   ddddddgdd fS )NapplebananaZword_oneZword_two	australiabrazilc                   S  s   d S r   r   r   r   r   r   <lambda>9       z>_MockInputsSource._to_normalized_inputs_impl.<locals>.<lambda>r   )r   r   r   r   _to_normalized_inputs_impl3   s
    z,_MockInputsSource._to_normalized_inputs_implN)r   r   r    r*   r   r   r   r   r"   1   s   r"   c                   @  s@   e Zd ZdZdddddZdd Zd	d
 Zdd Zdd ZdS )LLMFunctionBasicTestz>Test basic functionality such as execution and input-handling.z=Callable[[Optional[Sequence[tuple[str, str]]]], LLMFnOutputs]LLMFnOutputs)llm_fnr   c                 C  s   |d S r   r   )r   r-   r   r   r   _test_is_callable?   s    z&LLMFunctionBasicTest._test_is_callablec                 C  sj   t t dgd}| |}dgdgdgdgdgd}| ||  | t| t|   d S )Nzthe opposite of hot isr   Zpromptsr   
Prompt Num	Input Num
Result NumPrompttext_result)LLMFunctionImplr   	EchoModelr.   assertEqualas_dictlistkeysr   r-   resultsexpected_resultsr   r   r   test_runE   s     

 zLLMFunctionBasicTest.test_runc                 C  s   t t ddgd}|ddgddgdd	}d
d
ddgd
dd
dgd
d
d
d
gddddgddddgd}| ||  | t| t|   d S )N)A for {word_one}, B for {word_two}, C forz6if A is to {word_one} as B is to {word_two}, then C isr/   r#   r&   r$   r'   r%   )inputsr       A for apple, B for banana, C for$A for australia, B for brazil, C forz-if A is to apple as B is to banana, then C isz1if A is to australia as B is to brazil, then C isr0   )r6   r   r7   r8   r9   r:   r;   r<   r   r   r   test_inputsW   s<    



 z LLMFunctionBasicTest.test_inputsc                 C  st   t t dgd}|t }ddgddgddgddgddgd}| ||  | t| t|   d S )Nr@   r/   r   rB   rC   rD   r0   )r6   r   r7   r"   r8   r9   r:   r;   r<   r   r   r   test_inputs_source{   s*    

 z'LLMFunctionBasicTest.test_inputs_sourcec                 C  s   t tdddgddgd}| }dddgdddgdddgdddgdddgd}| ||  | t| t|   d S )	Ncoldr   The opposite of hot isr/   r   rB      r0   )r6   r   r8   r9   r:   r;   r<   r   r   r   test_one_prompt_many_results   s&    
 z1LLMFunctionBasicTest.test_one_prompt_many_resultsN)	r   r   r    r!   r.   r?   rE   rF   rJ   r   r   r   r   r+   <   s   $r+   c                   @  s(   e Zd ZdZdd Zdd Zdd ZdS )	LLMFunctionPostProcessTestzTest post-processing features.c                 C  s   t tdddgddgd}ddd	d
d}|jd|dddgi}dddgdddgdddgdddgdddgd}| ||  | t| t|   d S )NrG   freezingchillyr   The opposite of {word} isr/   Sequence[LLMFnOutputRowView]Sequence[int]rowsr   c                 S  s   t tdt| }|  |S )Nr   )r:   rangelenreverse)rR   indicesr   r   r   
reverse_fn   s    zOLLMFunctionPostProcessTest.test_add_post_process_reorder_fn.<locals>.reverse_fnrW   namefnwordhotr   rI   rB   rH   r0   )r6   r   Zadd_post_process_reorder_fnr8   r9   r:   r;   )r   r-   rW   r=   r>   r   r   r    test_add_post_process_reorder_fn   s6     
 z;LLMFunctionPostProcessTest.test_add_post_process_reorder_fnc                 C  s   t tdddgddgd}ddd	d
d}|jd|dddgi}dddgdddgdddgdddgdddgdddgd}| ||  | t| t|   d S )NrG   rL   rM   r   rN   r/   rO   rP   rQ   c                 S  s   dd | D S )Nc                 S  s   g | ]}t | qS r   )rT   result_value.0rowr   r   r   
<listcomp>   s     z[LLMFunctionPostProcessTest.test_add_post_process_add_fn.<locals>.add_fn.<locals>.<listcomp>r   rR   r   r   r   add_fn   s    zGLLMFunctionPostProcessTest.test_add_post_process_add_fn.<locals>.add_fnlengthrX   r[   r\   r   rB   rI   rH            )r1   r2   r3   r4   re   r5   )r6   r   add_post_process_add_fnr8   r9   r:   r;   )r   r-   rd   r=   r>   r   r   r   test_add_post_process_add_fn   s2    
 z7LLMFunctionPostProcessTest.test_add_post_process_add_fnc                 C  s   t tdddgddgd}ddd	d
d}|jd|dddgi}dddgdddgdddgdddgdddgd}| ||  | t| t|   d S )NrG   rL   rM   r   rN   r/   rO   r   rQ   c                 S  s   dd | D S )Nc                 S  s   g | ]}|   qS r   )r^   upperr_   r   r   r   rb      s     zcLLMFunctionPostProcessTest.test_add_post_process_replace_fn.<locals>.replace_fn.<locals>.<listcomp>r   rc   r   r   r   
replace_fn   s    zOLLMFunctionPostProcessTest.test_add_post_process_replace_fn.<locals>.replace_fnrl   rX   r[   r\   r   rB   rI   rH   ZCOLDZFREEZINGZCHILLYr0   )r6   r   Zadd_post_process_replace_fnr8   r9   r:   r;   )r   r-   rl   r=   r>   r   r   r    test_add_post_process_replace_fn   s6     
 z;LLMFunctionPostProcessTest.test_add_post_process_replace_fnN)r   r   r    r!   r]   rj   rm   r   r   r   r   rK      s   #rK   c                   @  s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )LLMCompareFunctionTestzTest LLMCompareFunction.c                 C  s   t t dgd}t t dgd}td|fd|fd}|ddd	gi}d
d
gd
dgd
d
gddidd	igddgddgddgd}| ||  | t| t|   dS )zBasic comparison test.
lhs_{word}r/   
rhs_{word}lhsrhslhs_name_and_fnrhs_name_and_fnr[   Zhelloworldr   rB   Z	lhs_hello	lhs_worldZ	rhs_hello	rhs_worldF)r1   r2   r3   Prompt varslhs_text_resultrhs_text_resultis_equalN)r6   r   r7   LLMCompareFunctionr8   r9   r:   r;   )r   lhs_fnrhs_fn
compare_fnr=   r>   r   r   r   test_basic_run  s:      	
 z%LLMCompareFunctionTest.test_basic_runc                 C  s   ddddd}t t dgdjd|d	}t t d
gdjd|d	}td|fd|fdjd|d	}|dddgi}ddgddgddgddiddigddgddgddgddgddgddgd
}| ||  | t| t|   dS )z0Comparison test with post-processing operations.rO   rP   rQ   c                 S  s   dd | D S )Nc                 S  s   g | ]}t t| qS r   rT   r   r^   r_   r   r   r   rb   6  s     zXLLMCompareFunctionTest.test_run_with_post_process.<locals>.length_fn.<locals>.<listcomp>r   rc   r   r   r   	length_fn5  s    zDLLMCompareFunctionTest.test_run_with_post_process.<locals>.length_fnro   r/   re   rX   rp   rq   rr   rs   r[   hirv   r   rB   rh   	   Zlhs_hirw   Zrhs_hirx      F)
r1   r2   r3   ry   
lhs_lengthrz   
rhs_lengthr{   re   r|   N	r6   r   r7   ri   r}   r8   r9   r:   r;   r   r   r~   r   r   r=   r>   r   r   r   test_run_with_post_process2  sT         
 z1LLMCompareFunctionTest.test_run_with_post_processc                 C  s   ddddd}t t dgdjd|d	}t t d
gdjd|d	}td|fd|fdjd|d	}|dddgi}ddgddgddgddiddigddgddgddgddgddgddgd
}| ||  | t| t|   dS )zTest with name collisions.rO   rP   rQ   c                 S  s   dd | D S )Nc                 S  s   g | ]}t t| qS r   r   r_   r   r   r   rb   ^  s     z[LLMCompareFunctionTest.test_run_with_name_collisions.<locals>.length_fn.<locals>.<listcomp>r   rc   r   r   r   r   ]  s    zGLLMCompareFunctionTest.test_run_with_name_collisions.<locals>.length_fnro   r/   re   rX   rp   rZ   rs   r[   heyrv   r   rB      r   Zlhs_heyrw   Zrhs_heyrx   r   F)
r1   r2   r3   ry   Z	fn_lengthZfn_text_resultZfn_length_1Zfn_text_result_1re   r|   Nr   r   r   r   r   test_run_with_name_collisionsZ  sT         
 z4LLMCompareFunctionTest.test_run_with_name_collisionsc           
      C  s<  ddddd}t t dgdjd|d	}t t d
gdjd|d	}dddddd}dddddd}ddddd}td|fd|fd|fd|fgdjd|d	}|dddgi}ddgddgddgddiddigd d!gd"d#gd$d%gd&d'gd(d)gd*d+gd)d(gd,}	| |	|  | t|		 t| 	  d-S ).z Test custom comparison function.rO   rP   rQ   c                 S  s   dd | D S )Nc                 S  s   g | ]}t t| qS r   r   r_   r   r   r   rb     s     zQLLMCompareFunctionTest.test_custom_compare.<locals>.length_fn.<locals>.<listcomp>r   rc   r   r   r   r     s    z=LLMCompareFunctionTest.test_custom_compare.<locals>.length_fnz{word}_{word}r/   re   rX   zabcd_{word}zMapping[str, Any]bool)rq   rr   r   c                 S  s   | d |d k S Nre   r   rq   rr   r   r   r   _is_length_less_than  s    zHLLMCompareFunctionTest.test_custom_compare.<locals>._is_length_less_thanc                 S  s   | d |d kS r   r   r   r   r   r   _is_length_greater_than  s    zKLLMCompareFunctionTest.test_custom_compare.<locals>._is_length_greater_thanz5Sequence[tuple[Mapping[str, Any], Mapping[str, Any]]]c                 S  s   dd | D S )Nc                 S  s    g | ]\}}|d  |d   qS )re   r   )r`   rq   rr   r   r   r   rb     s     zWLLMCompareFunctionTest.test_custom_compare.<locals>._sum_of_lengths.<locals>.<listcomp>r   rc   r   r   r   _sum_of_lengths  s    zCLLMCompareFunctionTest.test_custom_compare.<locals>._sum_of_lengthsrq   rr   is_shorter_thanis_longer_than)rt   ru   Zcompare_name_and_fnssum_of_lengthsr[   r   rv   r   rB   r      Zhey_heyZworld_worldrg   
   Zabcd_heyZ
abcd_worldTF      )r1   r2   r3   ry   r   rz   r   r{   r   r   r   N)
r6   r   r7   ri   r}   Zadd_compare_post_process_add_fnr8   r9   r:   r;   )
r   r   r~   r   r   r   r   r   r=   r>   r   r   r   test_custom_compare  sb      	 	 
 z*LLMCompareFunctionTest.test_custom_compareN)r   r   r    r!   r   r   r   r   r   r   r   r   rn     s
   ((rn   __main__N)!r!   
__future__r   typingr   r   r   r   r   Zabsl.testingr   Z google.generativeai.notebook.libr	   r
   r   r   r   r   r}   r6   r,   ZLLMFnOutputRowZLLMFnOutputRowViewZAbstractModelr   ZLLMFnInputsSourcer"   ZTestCaser+   rK   rn   r   mainr   r   r   r   <module>   s*   pd 6