U
    h                     @  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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 NumZPromptzPrompt varsZtext_resultN)
__name__
__module____qualname____doc__
PROMPT_NUM	INPUT_NUM
RESULT_NUMMODEL_INPUTZPROMPT_VARSZTEXT_RESULT r   r   R/tmp/pip-unpacked-wheel-doshhd5e/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   *   s   
r   zIterable[LLMFnOutputEntry]outputsc                 C  s   | D ]}|j d k	r dS qdS )NTF)r   )r!   entryr   r   r   _has_model_input_fieldE   s    
r#   c                   @  s   e Zd ZdZd$ddddZddd	d
ZddddZedddddZedddddZdddddZddddZ	ddddZ
dddd Zd!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.
  N!Iterable[LLMFnOutputEntry] | Noner    c                 C  s   |dk	rt |ng | _dS )zWConstructor.

    Args:
      outputs: The contents of this LLMFnOutputs instance.
    N)list_outputsselfr!   r   r   r   __init__S   s    
zLLMFnOutputsBase.__init__zIterator[LLMFnOutputEntry])returnc                 C  s
   | j  S N)r'   __iter__r)   r   r   r   r-   a   s    zLLMFnOutputsBase.__iter__r   c                 C  s
   | j  S r,   )r'   __len__r.   r   r   r   r/   e   s    zLLMFnOutputsBase.__len__r   )xr+   c                 C  s   d S r,   r   r)   r0   r   r   r   __getitem__i   s    zLLMFnOutputsBase.__getitem__slicezSequence[LLMFnOutputEntry]c                 C  s   d S r,   r   r1   r   r   r   r2   m   s    zint | slicez-LLMFnOutputEntry | Sequence[LLMFnOutputEntry]c                 C  s   | j |S r,   )r'   r2   r1   r   r   r   r2   q   s    boolc                 C  s
   t | jS r,   )r4   r'   r.   r   r   r   __bool__w   s    zLLMFnOutputsBase.__bool__strc                 C  s   |    S r,   )as_pandas_dataframe__str__r.   r   r   r   r8   z   s    zLLMFnOutputsBase.__str__zMapping[str, Sequence[Any]]c           	      C  sr  t jg t jg t jg i}t| jr*g |t j< | js4|S dd | D }| jD ]2}|jD ]&}t	| dd D ]}d||< qnqVqL| jD ]$}|jD ]}d|t	| d < qq|D ]}g ||< qd}| jD ]}|jD ]}|t j 
|j |t j 
|j t j|kr|t j 
|j | D ]\}}|| 
| q | D ]}t||k rB|
d qB|d7 }qq|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~   sF       









zLLMFnOutputsBase.as_dictzpandas.DataFramec                 C  s   t |  S r,   )pandasZ	DataFramerI   r.   r   r   r   r7      s    z$LLMFnOutputsBase.as_pandas_dataframe)N)r   r   r   r   r*   r-   r/   r   r2   r5   r8   rI   r7   r   r   r   r   r$   L   s    <r$   c                   @  s    e Zd ZdZdd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$   None)r!   r+   c                 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   r   rM   r   r   r   r   rK      s   rK   c                      sB   e Zd ZdZdddd fddZdd	 Zd
d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%   z%Callable[[LLMFnOutputs], None] | None)r!   ipython_display_fnc                   s.   t  j|d |r*|| _t| dt| d dS )ac  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!   rO   	__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)rR   r.   r   r   r   rP      s    z"LLMFnOutputs._ipython_display_implrK   rL   )sinkr+   c                 C  s   | |  dS )zExport contents to `sink`.N)rM   )r)   rW   r   r   r   export   s    zLLMFnOutputs.export)NN)r   r   r   r   r*   rP   rX   __classcell__r   r   rU   r   rN      s     rN   )r   
__future__r   abcZdataclassestypingr   r   r   r   r   r   r	   Z google.generativeai.notebook.libr
   r   Z	model_librJ   r   Z	dataclassr   r#   r$   ABCrK   rN   r   r   r   r   <module>   s   $r