o
    貪gT!                     @  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 varstext_resultN)
__name__
__module____qualname____doc__
PROMPT_NUM	INPUT_NUM
RESULT_NUMMODEL_INPUTPROMPT_VARSTEXT_RESULT r   r   j/var/www/html/chatdoc2/venv/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]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   r4   g      
zLLMFnOutputsBase.__iter__r   c                 C  r2   r3   )r,   __len__r5   r   r   r   r7   k   r6   zLLMFnOutputsBase.__len__xr   c                 C     d S r3   r   r.   r8   r   r   r   __getitem__o      zLLMFnOutputsBase.__getitem__sliceSequence[LLMFnOutputEntry]c                 C  r9   r3   r   r:   r   r   r   r;   r   r<   int | slice-LLMFnOutputEntry | Sequence[LLMFnOutputEntry]c                 C  s   | j |S r3   )r,   r;   r:   r   r   r   r;   u      boolc                 C  s
   t | jS r3   )rB   r,   r5   r   r   r   __bool__y   r6   zLLMFnOutputsBase.__bool__strc                 C  s   |    S r3   )as_pandas_dataframe__str__r5   r   r   r   rF   |   rA   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 r3   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.   datatotal_keys_setoutputresultkeynext_num_rowsvaluecolumnr   r   r   as_dict   sJ   













zLLMFnOutputsBase.as_dictpandas.DataFramec                 C  s   t |  S r3   )pandas	DataFramerZ   r5   r   r   r   rE      s   z$LLMFnOutputsBase.as_pandas_dataframer3   )r%   r*   )r0   r1   )r0   r   )r8   r   r0   r   )r8   r=   r0   r>   )r8   r?   r0   r@   )r0   rB   )r0   rD   )r0   rG   )r0   r[   )r   r   r   r   r/   r4   r7   r   r;   rC   rF   rZ   rE   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)   r0   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)   r0   r_   )r   r   r   r   r`   r   r   r   r   r^      s    r^   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%   _ipython_display__ipython_display_implN)superr/   _ipython_display_fnsetattrgetattr)r.   r%   rb   	__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)rg   r5   r   r   r   re      s   z"LLMFnOutputs._ipython_display_implsinkr^   r0   r_   c                 C  s   | |  dS )zExport contents to `sink`.N)r`   )r.   rl   r   r   r   export   s   zLLMFnOutputs.export)NN)r%   r*   rb   rc   )rl   r^   r0   r_   )r   r   r   r   r/   re   rm   __classcell__r   r   rj   r   ra      s    ra   )r%   r&   )r   
__future__r   abcdataclassestypingr   r   r   r   r   r   r	    google.generativeai.notebook.libr
   r   	model_libr\   r   	dataclassr   r(   r)   ABCr^   ra   r   r   r   r   <module>   s   $

l