o
    ZhT!                     @  s   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 dZejG d	d
 d
ZdddZG dd dee ZG dd dejZG dd deZdS )zOutput of LLMFunction.    )annotationsN)overloadAnyCallableIterableIteratorMappingSequence)llmfn_output_row)modelc                   @  s(   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 varsZtext_resultN)
__name__
__module____qualname____doc__
PROMPT_NUM	INPUT_NUM
RESULT_NUMMODEL_INPUTZPROMPT_VARSZTEXT_RESULT r   r   e/var/www/html/lang_env/lib/python3.10/site-packages/google/generativeai/notebook/lib/llmfn_outputs.pyr   #   s    r   c                   @  sV   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]Z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   
 r   outputsIterable[LLMFnOutputEntry]c                 C  s   | D ]
}|j d ur dS qdS )NTF)r   )r!   entryr   r   r   _has_model_input_fieldM   s
   
r$   c                   @  s   e Zd ZdZ	d$d%ddZd&d	d
Zd'ddZed(ddZed)ddZd*ddZd+ddZ	d,ddZ
d-dd Zd.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                 C  s    |durt || _dS g | _dS )zcConstructor.

        Args:
          outputs: The contents of this LLMFnOutputs instance.
        N)list_outputsselfr!   r   r   r   __init__[   s    	zLLMFnOutputsBase.__init__returnIterator[LLMFnOutputEntry]c                 C  
   | j  S N)r(   __iter__r*   r   r   r   r0   g      
zLLMFnOutputsBase.__iter__r   c                 C  r.   r/   )r(   __len__r1   r   r   r   r3   k   r2   zLLMFnOutputsBase.__len__xr   c                 C     d S r/   r   r*   r4   r   r   r   __getitem__o      zLLMFnOutputsBase.__getitem__sliceSequence[LLMFnOutputEntry]c                 C  r5   r/   r   r6   r   r   r   r7   r   r8   int | slice-LLMFnOutputEntry | Sequence[LLMFnOutputEntry]c                 C  s   | j |S r/   )r(   r7   r6   r   r   r   r7   u      boolc                 C  s
   t | jS r/   )r>   r(   r1   r   r   r   __bool__y   r2   zLLMFnOutputsBase.__bool__strc                 C  s   |    S r/   )as_pandas_dataframe__str__r1   r   r   r   rB   |   r=   zLLMFnOutputsBase.__str__Mapping[str, Sequence[Any]]c           	      C  sj  t jg t jg t jg i}t| jrg |t j< | js|S dd | D }| jD ]}|jD ]}t	| dd D ]}d||< q7q+q&| jD ]}|jD ]}d|t	| d < qHqC|D ]}g ||< qXd}| jD ]N}|jD ]H}|t j 
|j |t j 
|j t j|v r|t j 
|j | D ]\}}|| 
| q| D ]}t||k r|
d q|d7 }qiqd|S )z'Formats returned results as dictionary.c                 S  s   i | ]}|d qS r/   r   ).0kr   r   r   
<dictcomp>   s    z,LLMFnOutputsBase.as_dict.<locals>.<dictcomp>N   )r   r   r   r   r$   r(   r   keysr   r'   appendr   r   r   itemsvalueslen)	r*   dataZtotal_keys_setoutputresultkeyZnext_num_rowsvaluecolumnr   r   r   as_dict   sJ   













zLLMFnOutputsBase.as_dictpandas.DataFramec                 C  s   t |  S r/   )pandasZ	DataFramerT   r1   r   r   r   rA      s   z$LLMFnOutputsBase.as_pandas_dataframer/   )r!   r&   )r,   r-   )r,   r   )r4   r   r,   r   )r4   r9   r,   r:   )r4   r;   r,   r<   )r,   r>   )r,   r@   )r,   rC   )r,   rU   )r   r   r   r   r+   r0   r3   r   r7   r?   rB   rT   rA   r   r   r   r   r%   T   s    





<r%   c                   @  s   e Zd ZdZd	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%   r,   Nonec                 C  s   dS )z%Writes `outputs` to some destination.Nr   r)   r   r   r   write_outputs   s    zLLMFnOutputsSink.write_outputsN)r!   r%   r,   rX   )r   r   r   r   rY   r   r   r   r   rW      s    rW   c                      s:   e Zd ZdZ		dd fddZd	d
 ZdddZ  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!   r&   ipython_display_fn%Callable[[LLMFnOutputs], None] | Nonec                   s2   t  j|d |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!   Z_ipython_display__ipython_display_implN)superr+   _ipython_display_fnsetattrgetattr)r*   r!   r[   	__class__r   r   r+      s   zLLMFnOutputs.__init__c                 C  s   |  |  dS )zActual implementation of _ipython_display_.

        This method should only be used invoked if self._ipython_display_fn is set.
        N)r_   r1   r   r   r   r]      s   z"LLMFnOutputs._ipython_display_implsinkrW   r,   rX   c                 C  s   | |  dS )zExport contents to `sink`.N)rY   )r*   rd   r   r   r   export   s   zLLMFnOutputs.export)NN)r!   r&   r[   r\   )rd   rW   r,   rX   )r   r   r   r   r+   r]   re   __classcell__r   r   rb   r   rZ      s    rZ   )r!   r"   )r   
__future__r   abcdataclassestypingr   r   r   r   r   r   r	   Z google.generativeai.notebook.libr
   r   Z	model_librV   r   	dataclassr   r$   r%   ABCrW   rZ   r   r   r   r   <module>   s   $

l