
    rhG                       d Z ddlmZ ddlZddlZddlmZmZmZ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 ddlmZ eej.                  ej.                  gef   Z	 	 	 	 	 	 ddZ	 	 	 	 ddZej6                   G d d             Z	 	 	 	 	 	 ddZ G d deeej<                  df   gej>                  f   ej@                        Z! G d de!      Z" G d de!      Z#y)zLLMFunction.    )annotationsN)AbstractSetAnyCallableIterableMappingOptionalSequenceUnion)llmfn_input_utils)llmfn_output_row)llmfn_outputs)llmfn_post_process)llmfn_post_process_cmds)model)prompt_utilsc                D    | j                         |j                         k(  S )z-Default function used when comparing outputs.)result_value)lhsrhss     /var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/google/generativeai/notebook/lib/llm_function.py_is_equal_fnr   4   s!    
 !1!1!333    c                     	 	 	 	 d fd}|S )z1Vectorize a single-row-based comparison function.c                H    | D cg c]  \  }} ||       c}}S c c}}w N )lhs_and_rhs_rowsr   r   fns      r   _fnz0_convert_compare_fn_to_batch_add_fn.<locals>._fnG   s#     .>>c3>>>s   )r   zYSequence[tuple[llmfn_output_row.LLMFnOutputRowView, llmfn_output_row.LLMFnOutputRowView]]returnzSequence[Any]r   )r   r    s   ` r   #_convert_compare_fn_to_batch_add_fnr"   <   s#    ?
? 
? Jr   c                  @    e Zd ZU ded<   ded<   ded<   ded<   ded<   y	)
_PromptInfoint
prompt_numstrprompt	input_numzMapping[str, str]prompt_varsmodel_inputN)__name__
__module____qualname____annotations__r   r   r   r$   r$   T   s    OKN""r   r$   c           	   #     K   g }|t        j                  |      }|si g}t        |       D ]<  \  }}t        |      D ])  \  }} |j                  di |}t	        |||||       + > yw)a  Generate a tuple of fields needed for processing prompts.

    Args:
      prompts: A list of prompts, with optional keyword placeholders.
      inputs: A list of key/value pairs to substitute into placeholders in
        `prompts`.

    Yields:
      A _PromptInfo instance.
    N)r&   r(   r)   r*   r+   r   )r   to_normalized_inputs	enumerateformatr$   )promptsinputsnormalized_inputsr&   r(   r)   r*   r+   s           r   _generate_promptsr7   ]   s      68-BB6J D'0 

F&/0A&B 		"I{'&--6+6K%#'' 		
s   A*A,c                      e Zd ZdZ	 d
	 ddZej                  dd       Zej                  	 	 	 	 dd       Z	 d
	 	 	 ddZ		 	 	 	 	 	 ddZ
	 	 	 	 	 	 ddZ	 	 	 	 	 	 dd	Zy)LLMFunctionz6Base class for LLMFunctionImpl and LLMCompareFunction.Nc                     g | _         || _        y)a  Constructor.

        Args:
          outputs_ipython_display_fn: Optional function that will be used to
            override how the outputs of this LLMFunction will be displayed in a
            notebook (See further documentation in LLMFnOutputs.__init__().)
        N)_post_process_cmds_outputs_ipython_display_fn)selfoutputs_ipython_display_fns     r   __init__zLLMFunction.__init__   s     Z\+E(r   c                     y)zLReturns the placeholders that should be present in inputs for this function.Nr   r=   s    r   get_placeholderszLLMFunction.get_placeholders       r   c                     y)z&Concrete implementation of __call__().Nr   )r=   r5   s     r   
_call_implzLLMFunction._call_impl   rC   r   c                f    | j                  |      }t        j                  || j                        S )z+Runs and returns results based on `inputs`.)outputsipython_display_fn)rE   r   LLMFnOutputsr<   )r=   r5   rG   s      r   __call__zLLMFunction.__call__   s0     //&)))0P0P
 	
r   c                f    | j                   j                  t        j                  ||             | S Nnamer   )r;   appendr   LLMFnPostProcessReorderCommandr=   rN   r   s      r   add_post_process_reorder_fnz'LLMFunction.add_post_process_reorder_fn   s0     	&&#BBQST	
 r   c                f    | j                   j                  t        j                  ||             | S rL   )r;   rO   r   LLMFnPostProcessAddCommandrQ   s      r   add_post_process_add_fnz#LLMFunction.add_post_process_add_fn   s/    
 	&&#>>DRP	
 r   c                f    | j                   j                  t        j                  ||             | S rL   )r;   rO   r   LLMFnPostProcessReplaceCommandrQ   s      r   add_post_process_replace_fnz'LLMFunction.add_post_process_replace_fn   s0    
 	&&#BBQST	
 r   r   )r>   3Callable[[llmfn_outputs.LLMFnOutputs], None] | Noner!   zAbstractSet[str]r5   *llmfn_input_utils.LLMFunctionInputs | Noner!   z(Sequence[llmfn_outputs.LLMFnOutputEntry])r5   r\   r!   zllmfn_outputs.LLMFnOutputs)rN   r'   r   z1llmfn_post_process.LLMFnPostProcessBatchReorderFnr!   r9   )rN   r'   r   z-llmfn_post_process.LLMFnPostProcessBatchAddFnr!   r9   )rN   r'   r   z1llmfn_post_process.LLMFnPostProcessBatchReplaceFnr!   r9   )r,   r-   r.   __doc__r?   abcabstractmethodrB   rE   rJ   rR   rU   rX   r   r   r   r9   r9      s     A [_F$WF 	[ [ 	5@5	15 5 DH
@
	#
N	 : 
	 > 
	r   r9   )	metaclassc                  `     e Zd ZdZ	 	 d	 	 	 	 	 	 	 d fdZ	 	 	 	 ddZd	dZ	 	 	 	 d
dZ xZS )LLMFunctionImpla  Callable class that executes the contents of a Magics cell.

    An LLMFunction is constructed from the Magics command line and cell contents
    specified by the user. It is defined by:
    - A model instance,
    - Model arguments
    - A prompt template (e.g. "the opposite of hot is {word}") with an optional
      keyword placeholder.

    The LLMFunction takes as its input a sequence of dictionaries containing
    values for keyword replacement, e.g. [{"word": "hot"}, {"word": "tall"}].

    This will cause the model to be executed with the following prompts:
      "The opposite of hot is"
      "The opposite of tall is"

    The results will be returned in a LLMFnOutputs instance.
    c                $   t         |   |       || _        || _        |t	        j
                         n|| _        t        i       | _        | j                  D ]5  }| j                  j                  t        j                  |            | _        7 y)a  Constructor.

        Args:
          model: The model that the prompts will execute on.
          prompts: A sequence of prompt templates with optional placeholders. The
            placeholders will be replaced by the inputs passed into this function.
          model_args: Optional set of model arguments to configure how the model
            executes the prompts.
          outputs_ipython_display_fn: See documentation in LLMFunction.__init__().
        r>   N)superr?   _model_prompts	model_libModelArguments_model_args	frozenset_placeholdersunionr   rB   )r=   r   r4   
model_argsr>   r(   	__class__s         r   r?   zLLMFunctionImpl.__init__   s    " 	4NO9C9K9335Q[ 'r]mm 	aF!%!3!3!9!9,:W:WX^:_!`D	ar   c           
        | j                   D ]\  }	 t        |t        j                        r|j	                  |      }n-t        j                  dj                  t        |                  ^ |S # t
        j                  $ r  t        $ rL}t        j                  dj                  |j                         t        |      j                  |            d}~ww xY wz-Runs post-processing commands over `results`.z)Unsupported post-process command type: {}z Error executing "{}", got {}: {}N)r;   
isinstancer   LLMFnImplPostProcessCommandrunr   PostProcessExecutionErrorr3   typeRuntimeErrorrN   r,   )r=   resultscmdes       r   _run_post_processing_cmdsz)LLMFunctionImpl._run_post_processing_cmds   s     ** 	Cc#:#V#VW!ggg.G,FFCJJ4PS9U  	  &??  (BB6==chhj$q'JZJZ\]^ s   AA..C
ACCc                    | j                   S r   rl   rA   s    r   rB   z LLMFunctionImpl.get_placeholders
      !!!r   c                z   g }t        | j                  |      D ]  }| j                  j                  |j                  | j
                        }g }t        |j                        D ]c  \  }}|j                  t        j                  t        j                  j                  |t        j                  j                  |it                     e |j                  t        j                   |j"                  |j$                  |j&                  |j(                  |j                  || j+                  |                     |S )N)r4   r5   )r+   rn   dataresult_type)r&   r)   r(   r*   r+   model_resultsoutput_rows)r7   rg   rf   
call_modelr+   rj   r2   text_resultsrO   r   LLMFnOutputRowr   ColumnNames
RESULT_NUMTEXT_RESULTr'   LLMFnOutputEntryr&   r)   r(   r*   r{   )r=   r5   rx   infor   r   
result_numtext_results           r   rE   zLLMFunctionImpl._call_impl  s    9;%dmmFK 	D KK22 ,,9I9I 3 M BDK+4]5O5O+P 	'
K""$33)55@@*)55AA; %(	 NN..#"nn;; $ 0 0 $ 0 0"/ $ > >{ K
	4 r   NN)r   zmodel_lib.AbstractModelr4   Sequence[str]rn   zmodel_lib.ModelArguments | Noner>   rY   )rx   )Sequence[llmfn_output_row.LLMFnOutputRow]r!   r   rZ   r[   )	r,   r-   r.   r]   r?   r{   rB   rE   __classcell__ro   s   @r   rb   rb      sp    . 7;Z^a&a a 4	a
 %Xa6@	2("@	1r   rb   c                  |     e Zd ZdZ	 	 d	 	 	 	 	 	 	 d fdZ	 	 	 	 	 	 	 	 d	dZd
dZ	 	 	 	 ddZ	 	 	 	 	 	 ddZ xZ	S )LLMCompareFunctionzLLMFunction for comparisons.

    LLMCompareFunction runs an input over a pair of LLMFunctions and compares the
    result.
    c                   t         |   |       |d   | _        |d   | _        |d   | _        |d   | _        t        | j                  j                               j                  | j
                  j                               | _	        |sd| _
        t        | _        y|d   \  }}|| _
        || _        |dd D ]!  \  }}| j                  |t        |             # y)a  Constructor.

        Args:
          lhs_name_and_fn: Name and function for the left-hand side of the
            comparison.
          rhs_name_and_fn: Name and function for the right-hand side of the
            comparison.
          compare_name_and_fns: Optional names and functions for comparing the
            results of the left- and right-hand sides.
          outputs_ipython_display_fn: See documentation in LLMFunction.__init__().
        rd   r      is_equalNrM   )re   r?   	_lhs_name_lhs_fn	_rhs_name_rhs_fnrk   rB   rm   rl   _result_namer   _result_compare_fnadd_compare_post_process_add_fnr"   )	r=   lhs_name_and_fnrhs_name_and_fncompare_name_and_fnsr>   rN   r   cmp_fnro   s	           r   r?   zLLMCompareFunction.__init__5  s    $ 	4NO-a0$3A$6-a0$3A$6&t||'D'D'FGMMLL))+
 $ *D&2D# ,B/HD" $D&(D# !5Sb 9 f44"Ef"M 5 r   c           
        | j                   D ]  }	 t        |t        j                        r|j	                  |      }nct        |t        j
                        r&|j	                  t        t        |||                  }n#t        dj                  t        |                   |S # t        j                  $ r  t        $ rL}t        j                  dj                  |j                         t        |      j                  |            d}~ww xY wrq   )r;   rr   r   rs   rt   LLMCompareFnPostProcessCommandlistziprw   r3   rv   r   ru   rN   r,   )r=   lhs_output_rowsrhs_output_rowsrx   ry   rz   s         r   r{   z,LLMCompareFunction._run_post_processing_cmds`  s     ** 	Cc#:#V#VW!ggg.G%<%[%[\!ggd3QX+Y&Z[G&CJJ4PS9U 	   &??  (BB6==chhj$q'JZJZ\]^ s   BB$$D ADDc                    | j                   S r   r}   rA   s    r   rB   z#LLMCompareFunction.get_placeholdersy  r~   r   c                   | j                  |      }| j                  |      }g }t        ||      D ]  \  }}|j                  |j                  k7  r/t	        dj                  |j                  |j                              |j                  |j                  k7  r/t	        dj                  |j                  |j                              |j                  |j                  k7  r/t	        dj                  |j                  |j                              t        t        |j                        t        |j                              }|j                  d | }|j                  d | }	g }
t        t        ||	            D ]  \  }}|\  }}t        j                  j                  || j                  | j!                  ||      i}t#        j$                  |t&              }|j)                  t        j                  j*                  |j                         | j,                  |f| j.                  |ffD ]]  \  }}|j1                         D ]E  \  }}|t        j                  j                  k7  s$|j)                  dj                  ||      |       G _ |
j3                  |       " |j3                  t        j4                  |j                  |j                  |j                  | j7                  ||	|
                    |S )NzPrompt num mismatch: {} vs {}zInput num mismatch: {} vs {}zPrompt vars mismatch: {} vs {}r   z{}_{})r   r   rx   )r&   r)   r*   r   )r   r   r   r&   rw   r3   r)   r*   minlenr   r2   r   r   r   r   r   r   r   r   addPROMPT_VARSr   r   itemsrO   r   r{   )r=   r5   lhs_resultsrhs_resultsrG   	lhs_entry	rhs_entrynum_output_rowsr   r   r   r   lhs_and_rhs_output_rowlhs_output_rowrhs_output_rowrow_data
output_rowrN   rowkvs                        r   rE   zLLMCompareFunction._call_impl|  s    ll6*ll6* 9;$'[$A E	 Iy##y';';;"3::!,,i.B.B 
 ""i&9&99"299):M:MyObObc  $$	(=(=="4;;!--y/D/D  "#i&;&;"<c)BWBW>XYO'334D_EO'334D_EOACK6?O_57 /2
2 2H. "--88*%%t'>'>~~'^, .<<(X[\
 }88DDiF[F[\ ^^^4^^^4" GID# !$		 G1 9 9 D DD 'NN7>>$+BAF	G	G "":.?/B NN..(33'11 ) 5 5 $ > >(7(7 + !? !		uE	L r   c                f    | j                   j                  t        j                  ||             | S rL   )r;   rO   r   !LLMCompareFnPostProcessAddCommandrQ   s      r   r   z2LLMCompareFunction.add_compare_post_process_add_fn  s0    
 	&&#EE4TVW	
 r   r   )r   tuple[str, LLMFunction]r   r   r   z&Sequence[tuple[str, CompareFn]] | Noner>   rY   )r   r   r   r   rx   r   r!   r   rZ   r[   )rN   r'   r   4llmfn_post_process.LLMCompareFnPostProcessBatchAddFnr!   r9   )
r,   r-   r.   r]   r?   r{   rB   rE   r   r   r   s   @r   r   r   .  s     HLZ^)0) 1) E	)
 %X)VB C ;	
 
32"N@N	1N` A 
	r   r   )r   #llmfn_output_row.LLMFnOutputRowViewr   r   r!   bool)r   zYCallable[[llmfn_output_row.LLMFnOutputRowView, llmfn_output_row.LLMFnOutputRowView], Any]r!   r   )r4   r   r5   r\   r!   zIterable[_PromptInfo])$r]   
__future__r   r^   dataclassestypingr   r   r   r   r   r	   r
   r    google.generativeai.notebook.libr   r   r   r   r   r   rh   r   LLMFnOutputRowView	CompareFnr   r"   	dataclassr$   r7   LLMFunctionInputsrI   ABCMetar9   rb   r   r   r   r   <module>r      s7    " 
 	 	 	 ? = : ? D ? 9 ((*:*M*MN		4	,4	,4 
4	 :0   $NDE	 22D8	9:""	$ kkEPdk dNf fr   