
    ; iT!                    *   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 ddlmZ ddlmZ ddlZ G d d          Zej         G d	 d
                      ZddZ G d dee                   Z G d dej                  Z G d de          ZdS )zOutput of LLMFunction.    )annotationsN)overloadAnyCallableIterableIteratorMappingSequence)llmfn_output_row)modelc                  *    e Zd ZdZdZdZdZdZdZdZ	dS )	ColumnNamesz3Names of columns that are used to represent output.z
Prompt Numz	Input Numz
Result NumPromptzPrompt varstext_resultN)
__name__
__module____qualname____doc__
PROMPT_NUM	INPUT_NUM
RESULT_NUMMODEL_INPUTPROMPT_VARSTEXT_RESULT     /var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/google/generativeai/notebook/lib/llmfn_outputs.pyr   r   #   s4        ==JIJ
 KKKKKr   r   c                  f    e Zd ZU dZded<   ded<   ded<   ded<   d	Zd
ed<   d	Zd
ed<   d	Zded<   d	S )LLMFnOutputEntrya  The output of a single model input from LLMFunction.

    A model input is a prompt where the keyword placeholders have been
    substituted (by `prompt_vars`).

    E.g. If we have:
      prompt: "the opposite of {word} is"
      prompt_vars: {"word", "hot"}
    Then we will have the following model input:
      model_input: "the opposite of hot is"

    Note: The model may produce one-or-more results for a given model_input.
    This is represented by the sequence `output_rows`.
    int
prompt_num	input_numzMapping[str, str]prompt_varsz)Sequence[llmfn_output_row.LLMFnOutputRow]output_rowsNz
str | Nonepromptmodel_inputzmodel_lib.ModelResults | Nonemodel_results)r   r   r   r   __annotations__r%   r&   r'   r   r   r   r   r   2   s{           OOONNN""""::::F"K""""37M777777r   r   outputsIterable[LLMFnOutputEntry]c                $    | D ]}|j          dS dS )NTF)r&   )r)   entrys     r   _has_model_input_fieldr-   M   s+      (44 )5r   c                      e Zd ZdZ	 dddZddZdd
Zed d            Zed!d            Zd"dZd#dZ	d$dZ
d%dZd&dZdS )'LLMFnOutputsBasezParent class for LLMFnOutputs.

    This class exists mainly to avoid a circular dependency between LLMFnOutputs
    and LLMFnOutputsSink. Most users should use LLMFnOutputs directly instead.
    Nr)   !Iterable[LLMFnOutputEntry] | Nonec                6    |t          |          ng | _        dS )zcConstructor.

        Args:
          outputs: The contents of this LLMFnOutputs instance.
        N)list_outputsselfr)   s     r   __init__zLLMFnOutputsBase.__init__[   s!     BIATWZ\r   returnIterator[LLMFnOutputEntry]c                4    | j                                         S N)r3   __iter__r5   s    r   r;   zLLMFnOutputsBase.__iter__g   s    }%%'''r   r    c                4    | j                                         S r:   )r3   __len__r<   s    r   r>   zLLMFnOutputsBase.__len__k   s    }$$&&&r   xr   c                    d S r:   r   r5   r?   s     r   __getitem__zLLMFnOutputsBase.__getitem__o   s    7:sr   sliceSequence[LLMFnOutputEntry]c                    d S r:   r   rA   s     r   rB   zLLMFnOutputsBase.__getitem__r   s    CF3r   int | slice-LLMFnOutputEntry | Sequence[LLMFnOutputEntry]c                6    | j                             |          S r:   )r3   rB   rA   s     r   rB   zLLMFnOutputsBase.__getitem__u   s    }((+++r   boolc                *    t          | j                  S r:   )rI   r3   r<   s    r   __bool__zLLMFnOutputsBase.__bool__y   s    DM"""r   strc                N    |                                                                  S r:   )as_pandas_dataframe__str__r<   s    r   rO   zLLMFnOutputsBase.__str__|   s     ''))11333r   Mapping[str, Sequence[Any]]c                >   t           j        g t           j        g t           j        g i}t	          | j                  rg |t           j        <   | j        s|S d |                                D             }| j        D ]=}|j        D ]3}t          |                                          dd         D ]}d||<   4>| j        D ]6}|j        D ],}d|t          |                                          d         <   -7|D ]}g ||<   d}| j        D ]}|j        D ]}|t           j                 
                    |j                   |t           j                 
                    |j                   t           j        |v r*|t           j                 
                    |j                   |                                D ] \  }}||         
                    |           !|                                D ]*}t!          |          |k     r|
                    d           +|dz  }
|S )z'Formats returned results as dictionary.c                    i | ]}|d S r:   r   ).0ks     r   
<dictcomp>z,LLMFnOutputsBase.as_dict.<locals>.<dictcomp>   s    *H*H*Hq1d*H*H*Hr   N   )r   r   r   r   r-   r3   r   keysr$   r2   appendr!   r"   r&   itemsvalueslen)	r5   datatotal_keys_setoutputresultkeynext_num_rowsvaluecolumns	            r   as_dictzLLMFnOutputsBase.as_dict   s[    "B!2 "B
 "$-00 	/,.D()} 	K +I*HDIIKK*H*H*Hm 	/ 	/F , / /..ss3 / /C*.N3''// m 	? 	?F , ? ?:>tFKKMM222677?
 " 	 	CDIIm 	# 	#F , # #[+,33F4EFFF[*+2263CDDD*d2201889KLLL"(,,.. , ,JCI$$U++++ #kkmm , ,F6{{]22d+++"#  r   pandas.DataFramec                N    t          j        |                                           S r:   )pandas	DataFramere   r<   s    r   rN   z$LLMFnOutputsBase.as_pandas_dataframe   s    ///r   r:   )r)   r0   )r7   r8   )r7   r    )r?   r    r7   r   )r?   rC   r7   rD   )r?   rF   r7   rG   )r7   rI   )r7   rL   )r7   rP   )r7   rf   )r   r   r   r   r6   r;   r>   r   rB   rK   rO   re   rN   r   r   r   r/   r/   T   s          6:	] 	] 	] 	] 	]( ( ( (' ' ' ' ::: X:FFF XF, , , ,# # # #4 4 4 4: : : :x0 0 0 0 0 0r   r/   c                      e Zd ZdZddZdS )	LLMFnOutputsSinkzAbstract class representing an exporter for the output of LLMFunction.

    This class could be extended to write to external documents, such as
    Google Sheets.
    r)   r/   r7   Nonec                    dS )z%Writes `outputs` to some destination.Nr   r4   s     r   write_outputszLLMFnOutputsSink.write_outputs   s      r   N)r)   r/   r7   rl   )r   r   r   r   rn   r   r   r   rk   rk      s2         4 4 4 4 4 4r   rk   c                  8     e Zd ZdZ	 	 dd fdZd ZddZ xZS )LLMFnOutputsa  A sequence of LLMFnOutputEntry instances.

    Notes:
    - Each LLMFnOutputEntry represents the results of running one model
      input (see documentation for LLMFnOutputEntry for what "model input"
      means.)
    - A single model input may produce more-than-one text results.
    Nr)   r0   ipython_display_fn%Callable[[LLMFnOutputs], None] | Nonec                    t                                          |           |r(|| _        t          | dt	          | d                     dS dS )a  Constructor.

        Args:
          outputs: The contents of this LLMFnOutputs instance.
          ipython_display_fn: An optional function for pretty-printing this instance
            when it is the output of a cell in a notebook. If this argument is not
            None, the _ipython_display_ method will be defined which will in turn
            invoke this function.
        )r)   _ipython_display__ipython_display_implN)superr6   _ipython_display_fnsetattrgetattr)r5   r)   rq   	__class__s      r   r6   zLLMFnOutputs.__init__   sl     	))) 
	'9D$
 #566    
	 
	r   c                0    |                      |            dS )zActual implementation of _ipython_display_.

        This method should only be used invoked if self._ipython_display_fn is set.
        N)rw   r<   s    r   ru   z"LLMFnOutputs._ipython_display_impl   s    
 	  &&&&&r   sinkrk   r7   rl   c                0    |                     |            dS )zExport contents to `sink`.N)rn   )r5   r|   s     r   exportzLLMFnOutputs.export   s    4     r   )NN)r)   r0   rq   rr   )r|   rk   r7   rl   )r   r   r   r   r6   ru   r~   __classcell__)rz   s   @r   rp   rp      sw          6:DH      8' ' '! ! ! ! ! ! ! !r   rp   )r)   r*   )r   
__future__r   abcdataclassestypingr   r   r   r   r   r	   r
    google.generativeai.notebook.libr   r   	model_librh   r   	dataclassr   r-   r/   ABCrk   rp   r   r   r   <module>r      s     " " " " " " 



                      > = = = = = ? ? ? ? ? ?                 8 8 8 8 8 8 8 84   i0 i0 i0 i0 i0x 01 i0 i0 i0X4 4 4 4 4sw 4 4 4/! /! /! /! /!# /! /! /! /! /!r   