o
    Zh+*                     @  s   d dl mZ d dlZd dlZd dlmZmZ d dlmZ d dl	m
Z
 d dl	mZ d dlmZ d dlmZ d	Zd
ZG dd dZdS )    )annotationsN)IterableOptional)protos)caching_types)content_types)get_default_cache_client)field_mask_pb2usermodelc                	   @  s  e Zd ZdZdd ZedCddZedCdd	ZedCd
dZedDddZ	edEddZ
edEddZedEddZdd ZeZedFddZdd ZedddddddddGd0d1ZedddddddddHd2d3ZedId5d6ZedJdKd;d<ZdLd>d?Zddd@dMdAdBZdS )NCachedContentzCached content resource.c                 C  s6   t  }d|vrd| }tj|d}||}|| _dS )zFetches a `CachedContent` resource.

        Identical to `CachedContent.get`.

        Args:
            name: The resource name referring to the cached content.
        cachedContents/nameN)r   r   GetCachedContentRequestget_cached_content_proto)selfr   clientrequestresponse r   R/var/www/html/lang_env/lib/python3.10/site-packages/google/generativeai/caching.py__init__#   s   

zCachedContent.__init__returnstrc                 C     | j jS N)r   r   r   r   r   r   r   4      zCachedContent.namec                 C  r   r   )r   r   r   r   r   r   r   8   r   zCachedContent.modelc                 C  r   r   )r   display_namer   r   r   r   r    <   r   zCachedContent.display_name"protos.CachedContent.UsageMetadatac                 C  r   r   )r   usage_metadatar   r   r   r   r"   @   r   zCachedContent.usage_metadatadatetime.datetimec                 C  r   r   )r   create_timer   r   r   r   r$   D   r   zCachedContent.create_timec                 C  r   r   )r   update_timer   r   r   r   r%   H   r   zCachedContent.update_timec                 C  r   r   )r   expire_timer   r   r   r   r&   L   r   zCachedContent.expire_timec                 C  sR   t d| j d| j d| j dd d| jj dd d	| j d
| j d| j	 dS )Nz1            CachedContent(
                name='z',
                model='z!',
                display_name='z"',
                usage_metadata={z*
                    'total_token_count': z,
                }z,
                create_time=z,
                update_time=z,
                expire_time=z
            ))
textwrapdedentr   r   r    r"   Ztotal_token_countr$   r%   r&   r   r   r   r   __str__P   s*   	
zCachedContent.__str__obj+CachedContent | protos.CachedContent | dictc                 C  s"   |  | }t |_|| |S )zKCreates an instance of CachedContent form an object, without calling `get`.)__new__r   r   r   _update)clsr,   r   r   r   r   	_from_objb   s   


zCachedContent._from_objc                 C  sP   t |tr|j}t |tst|j|dd}| D ]\}}t| j|| qdS )zFUpdates this instance inplace, does not call the API's `update` methodF)Zincluding_default_value_fieldsN)
isinstancer   r   dicttypeto_dictitemssetattr)r   updateskeyvaluer   r   r   r/   j   s   

zCachedContent._updateN)r    system_instructioncontentstoolstool_configttlr&   r   r    
str | Noner;   #Optional[content_types.ContentType]r<   $Optional[content_types.ContentsType]r=   +Optional[content_types.FunctionLibraryType]r>   &Optional[content_types.ToolConfigType]r?    Optional[caching_types.TTLTypes]r&   'Optional[caching_types.ExpireTimeTypes]!protos.CreateCachedContentRequestc          
   
   C  s   |r|rt dd| vrd|  } |rt|dkrt d|r#t|}t|}|r.| }|r5t|}|rFt|}|d jsFt	|d _t
|}t
|}tj| |||||||d}	tj|	dS )	z&Prepares a CreateCachedContentRequest.LExclusive arguments: Please provide either `ttl` or `expire_time`, not both./zmodels/   z;`display_name` must be no more than 128 unicode characters.r   r    r;   r<   r=   r>   r?   r&   )cached_content)
ValueErrorlenr   Z
to_contentZto_function_libraryZto_protoZto_tool_configZto_contentsZrole
_USER_ROLEr   to_optional_ttlto_optional_expire_timer   r   ZCreateCachedContentRequest)
r   r    r;   r<   r=   r>   r?   r&   Z	tools_librM   r   r   r   _prepare_create_requestu   s@   







z%CachedContent._prepare_create_requestc             
   C  s8   t  }	| j||||||||d}
|	|
}t|}|S )a  Creates `CachedContent` resource.

        Args:
            model: The name of the `model` to use for cached content creation.
                   Any `CachedContent` resource can be only used with the
                   `model` it was created for.
            display_name: The user-generated meaningful display name
                          of the cached content. `display_name` must be no
                          more than 128 unicode characters.
            system_instruction: Developer set system instruction.
            contents: Contents to cache.
            tools: A list of `Tools` the model may use to generate response.
            tool_config: Config to apply to all tools.
            ttl: TTL for cached resource (in seconds). Defaults to 1 hour.
                 `ttl` and `expire_time` are exclusive arguments.
            expire_time: Expiration time for cached resource.
                         `ttl` and `expire_time` are exclusive arguments.

        Returns:
            `CachedContent` resource with specified name.
        rL   )r   rS   Zcreate_cached_contentr   r1   )r0   r   r    r;   r<   r=   r>   r?   r&   r   r   r   resultr   r   r   create   s   "

zCachedContent.creater   c                 C  s:   t  }d|vrd| }tj|d}||}t|}|S )zFetches required `CachedContent` resource.

        Args:
            name: The resource name referring to the cached content.

        Returns:
            `CachedContent` resource with specified `name`.
        r   r   )r   r   r   r   r   r1   )r0   r   r   r   r   rT   r   r   r   get   s   


zCachedContent.get   	page_sizeOptional[int]Iterable[CachedContent]c                 c  s8    t  }tj|d}||D ]
}t|}|V  qdS )a.  Lists `CachedContent` objects associated with the project.

        Args:
            page_size: The maximum number of permissions to return (per page).
            The service may return fewer `CachedContent` objects.

        Returns:
            A paginated list of `CachedContent` objects.
        )rX   N)r   r   ZListCachedContentsRequestZlist_cached_contentsr   r1   )r0   rX   r   r   rM   r   r   r   list   s   
zCachedContent.listNonec                 C  s"   t  }tj| jd}|| dS )z!Deletes `CachedContent` resource.r   N)r   r   ZDeleteCachedContentRequestr   Zdelete_cached_content)r   r   r   r   r   r   delete  s   
zCachedContent.delete)r?   r&   c                C  s   t  }|r|rtdt|}t|}tj| j||d}t	 }|r+|j
d n|r4|j
d ntdtj||d}||}| | dS )aX  Updates requested `CachedContent` resource.

        Args:
            ttl: TTL for cached resource (in seconds). Defaults to 1 hour.
                 `ttl` and `expire_time` are exclusive arguments.
            expire_time: Expiration time for cached resource.
                         `ttl` and `expire_time` are exclusive arguments.
        rH   )r   r?   r&   r?   r&   zQBad update name: Only `ttl`  or `expire_time` can be updated for `CachedContent`.)rM   Zupdate_maskN)r   rN   r   rQ   rR   r   r   r   r	   Z	FieldMaskpathsappendZUpdateCachedContentRequestZupdate_cached_contentr/   )r   r?   r&   r   r8   Z
field_maskr   Z
updated_ccr   r   r   update  s0   



zCachedContent.update)r   r   )r   r!   )r   r#   )r,   r-   r   r   )r   r   r    r@   r;   rA   r<   rB   r=   rC   r>   rD   r?   rE   r&   rF   r   rG   )r   r   r    r@   r;   rA   r<   rB   r=   rC   r>   rD   r?   rE   r&   rF   r   r   )r   r   r   r   )rW   )rX   rY   r   rZ   )r   r\   )r?   rE   r&   rF   r   r\   )__name__
__module____qualname____doc__r   propertyr   r   r    r"   r$   r%   r&   r+   __repr__classmethodr1   r/   staticmethodrS   rU   rV   r[   r]   r`   r   r   r   r   r       s`    62
r   )
__future__r   datetimer)   typingr   r   Zgoogle.generativeair   Zgoogle.generativeai.typesr   r   Zgoogle.generativeai.clientr   Zgoogle.protobufr	   rP   Z_MODEL_ROLEr   r   r   r   r   <module>   s   