o
    ß­jT!  ã                   @  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   úk/var/www/html/CropPilot/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 )/ÚLLMFnOutputsBasezÄParent 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 )
ÚLLMFnOutputsSinkz¨Abstract 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 )z‰Actual 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