o
    bi                     @  sv   d Z ddlmZ ddlZddlmZmZmZ eZdd	d
Z	dddZ
G dd deeef ejdZG dd deZdS )zLLMFnOutputRow.    )annotationsN)AnyIteratorMappingx	type[Any]returnstrc                 C  s   t | dr| jS t| S )N__name__)hasattrr
   r	   )r    r   k/home/kim/smarthome/.venv/lib/python3.10/site-packages/google/generativeai/notebook/lib/llmfn_output_row.py_get_name_of_type   s   
r   valuer   result_typeNonec                 C  s6   |t krd S t| |stdt|tt| d S )Nz2Value of last entry must be of type "{}", got "{}")r   
isinstance
ValueErrorformatr   type)r   r   r   r   r   _validate_is_result_type    s   

r   c                   @  s`   e Zd ZdZejdddZejddd	ZejdddZejdddZ	ejdddZ
dS )LLMFnOutputRowViewz!Immutable view of LLMFnOutputRow.kr	   r   boolc                 C     dS )z)For expressions like: x in this_instance.Nr   selfr   r   r   r   __contains__0       zLLMFnOutputRowView.__contains__c                 C  r   )z)For expressions like: str(this_instance).Nr   r   r   r   r   __str__4   r   zLLMFnOutputRowView.__str__r   c                 C  r   )z.Returns the type enforced for the result cell.Nr   r   r   r   r   r   9   r   zLLMFnOutputRowView.result_typer   c                 C  r   )z!Get the value of the result cell.Nr   r   r   r   r   result_value=   r   zLLMFnOutputRowView.result_valuec                 C  r   )zGet the key of the result cell.Nr   r   r   r   r   
result_keyA   r   zLLMFnOutputRowView.result_keyNr   r	   r   r   r   r	   r   r   r   r   )r
   
__module____qualname____doc__abcabstractmethodr   r    r   r!   r"   r   r   r   r   r   ,   s    r   )	metaclassc                   @  s   e Zd ZdZ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/ddZ
d0ddZd.ddZd1d2d%d&Zd3d'd(Zd S )4LLMFnOutputRowa  Container that represents a single row in a table of outputs.

    We represent outputs as a table. This class represents a single row in the
    table like a dictionary, where the key is the column name and the value is the
    cell value.

    A single cell is designated the "result". This contains the output of the LLM
    model after running any post-processing functions specified by the user.

    In addition to behaving like a dictionary, this class provides additional
    methods, including:
    - Getting the value of the "result" cell
    - Setting the value (and optionally the key) of the "result" cell.
    - Add a new non-result cell

    Notes: As an implementation detail, the result-cell is always kept as the
    rightmost cell.
    dataMapping[str, _CELLVALUETYPE]r   r   c                 C  s@   t || _| jstd|| _t| j d }t|| j dS )a*  Constructor.

        Args:
          data: The initial value of the row. The last entry will be treated as the
            result. Cannot be empty. The value of the last entry must be `str`.
          result_type: The type of the result cell. This will be enforced at
            runtime.
        zMust provide non-empty dataN)dict_datar   _result_typelistvaluesr   )r   r.   r   r!   r   r   r   __init__Z   s   
	zLLMFnOutputRow.__init__r   Iterator[str]c                 C  
   | j  S N)r2   __iter__r   r   r   r   r:   l      
zLLMFnOutputRow.__iter__intc                 C  r8   r9   )r2   __len__r   r   r   r   r=   o   r;   zLLMFnOutputRow.__len__r   r	   _CELLVALUETYPEc                 C     | j |S r9   )r2   __getitem__r   r   r   r   r@   r      zLLMFnOutputRow.__getitem__r   c                 C  r?   r9   )r2   r   r   r   r   r   r   v   rA   zLLMFnOutputRow.__contains__c                 C  s   d | j S )NzLLMFnOutputRow: {})r   r2   r    r   r   r   r   r    y   s   zLLMFnOutputRow.__str__c                 C  s   | j S r9   )r3   r   r   r   r   r   |   s   zLLMFnOutputRow.result_typer   c                 C  s   | j |   S r9   )r2   r"   r   r   r   r   r!      s   zLLMFnOutputRow.result_valuec                 C  s   t | j d S )Nr0   )r4   r2   keysr   r   r   r   r"      s   zLLMFnOutputRow.result_keyNr   key
str | Noner   c                 C  sH   t || j |  }|du s||kr|| j|< dS | j|= || j|< dS )zSet the value of the result cell.

        Sets the value (and optionally the key) of the result cell.

        Args:
          value: The value to set the result cell today.
          key: Optionally change the key as well.
        N)r   r3   r"   r2   )r   r   rC   current_keyr   r   r   set_result_value   s   	
zLLMFnOutputRow.set_result_valuec                 C  sj   || j v rd}|}|| j v rd||}|d }|| j v s|}|  }| j |}|| j |< || j |< dS )zAdd a non-result cell.

        Adds a new non-result cell. This does not affect the result cell.

        Args:
          key: The key of the new cell to add.
          value: The value of the new cell to add.
           z{}_{}N)r2   r   r"   pop)r   rC   r   idxZcandidate_keyr"   r!   r   r   r   add   s   




zLLMFnOutputRow.add)r.   r/   r   r   )r   r7   )r   r<   )r   r	   r   r>   r#   r$   r%   r&   r9   )r   r   rC   rD   r   r   )rC   r	   r   r>   r   r   )r
   r'   r(   r)   r6   r:   r=   r@   r   r    r   r!   r"   rF   rJ   r   r   r   r   r-   F   s    








r-   )r   r   r   r	   )r   r   r   r   r   r   )r)   
__future__r   r*   typingr   r   r   r>   r   r   r	   ABCMetar   r-   r   r   r   r   <module>   s   

