Ë
    ümxi$„  ã                  óÐ   — d Z ddlmZ ddlmZ ddlZddlmZmZm	Z	 ddl
Z
ddl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dlmZ ddlmZ dZdZ G d„ d«      Z G d„ d«      Zy)z+Classes for working with the Gemini models.é    )Úannotations)ÚIterableN)ÚAnyÚUnionÚoverload)Úprotos)Úclient)Úcaching)Úcontent_types)Úgeneration_types)Úhelper_types)Úsafety_typesÚuserÚmodelc                  ó  — e Zd ZdZ	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 dd„Zedd„«       Zed„ «       Zd„ ZeZ	dddœ	 	 	 	 	 	 	 	 	 	 	 dd„Z
	 	 	 	 dd	„Zeedddœ	 	 	 	 	 	 	 dd
„«       «       Zeedddœ	 	 	 	 	 	 	 dd„«       «       Zedddœ	 	 	 	 	 	 	 dd„«       Zdddddddœ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Zdddddddœ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Z	 d ddddddœ	 	 	 	 	 	 	 	 	 	 	 	 	 d!d„Z	 d ddddddœ	 	 	 	 	 	 	 	 	 	 	 	 	 d!d„Zdddœ	 	 	 	 	 d"d„Zy)#ÚGenerativeModelaã  
    The `genai.GenerativeModel` class wraps default parameters for calls to
    `GenerativeModel.generate_content`, `GenerativeModel.count_tokens`, and
    `GenerativeModel.start_chat`.

    This family of functionality is designed to support multi-turn conversations, and multimodal
    requests. What media-types are supported for input and output is model-dependant.

    >>> import google.generativeai as genai
    >>> import PIL.Image
    >>> genai.configure(api_key='YOUR_API_KEY')
    >>> model = genai.GenerativeModel('models/gemini-1.5-flash')
    >>> result = model.generate_content('Tell me a story about a magic backpack')
    >>> result.text
    "In the quaint little town of Lakeside, there lived a young girl named Lily..."

    Multimodal input:

    >>> model = genai.GenerativeModel('models/gemini-1.5-flash')
    >>> result = model.generate_content([
    ...     "Give me a recipe for these:", PIL.Image.open('scones.jpeg')])
    >>> result.text
    "**Blueberry Scones** ..."

    Multi-turn conversation:

    >>> chat = model.start_chat()
    >>> response = chat.send_message("Hi, I have some questions for you.")
    >>> response.text
    "Sure, I'll do my best to answer your questions..."

    To list the compatible model names use:

    >>> for m in genai.list_models():
    ...     if 'generateContent' in m.supported_generation_methods:
    ...         print(m.name)

    Arguments:
         model_name: The name of the model to query. To list compatible models use
         safety_settings: Sets the default safety filters. This controls which content is blocked
             by the api before being returned.
         generation_config: A `genai.GenerationConfig` setting the default generation parameters to
             use.
    Nc                ól  — d|vrd|z   }|| _         t        j                  |«      | _        t	        j
                  |«      | _        t        j                  |«      | _	        |€d | _
        nt        j                  |«      | _
        |€d | _        nt        j                  |«      | _        d | _        d | _        y )Nú/zmodels/)Ú_model_namer   Úto_easy_safety_dictÚ_safety_settingsr   Úto_generation_config_dictÚ_generation_configr   Úto_function_libraryÚ_toolsÚ_tool_configÚto_tool_configÚ_system_instructionÚ
to_contentÚ_clientÚ_async_client)ÚselfÚ
model_nameÚsafety_settingsÚgeneration_configÚtoolsÚtool_configÚsystem_instructions          ú\/home/htdocs/ttos/venv/lib/python3.12/site-packages/google/generativeai/generative_models.pyÚ__init__zGenerativeModel.__init__I   s©   € ð �jÑ Ø" ZÑ/ˆJØ%ˆÔÜ ,× @Ñ @ÀÓ QˆÔÜ"2×"LÑ"LÐM^Ó"_ˆÔÜ#×7Ñ7¸Ó>ˆŒàÐØ $ˆDÕä -× <Ñ <¸[Ó IˆDÔàÐ%Ø'+ˆDÕ$ä'4×'?Ñ'?Ð@RÓ'SˆDÔ$àˆŒØ!ˆÕó    c                ó   — t        | dd «      S )NÚ_cached_content)Úgetattr©r"   s    r)   Úcached_contentzGenerativeModel.cached_contentf   s   € ä�tÐ.°Ó5Ð5r+   c                ó   — | j                   S ©N)r   r/   s    r)   r#   zGenerativeModel.model_namej   s   € à×ÑÐr+   c                óÜ   — d„ }t        j                  d| j                  › d| j                  › d| j                  › d| j
                  › d || j                  «      › d| j                  › d�«      S )	Nc                ó€   — | r;t        | j                  «      r&| j                  d   j                  x}rt        |«      S | S ©Nr   )ÚlenÚpartsÚtextÚrepr)ÚcontentÚts     r)   Ú
maybe_textz+GenerativeModel.__str__.<locals>.maybe_texto   s7   € Ùœ3˜wŸ}™}Ô-¸¿¹ÀaÑ8H×8MÑ8MÐ3M°1Ð3MÜ˜A“w�ØˆNr+   z?            genai.GenerativeModel(
                model_name='z%',
                generation_config=z",
                safety_settings=z,
                tools=z%,
                system_instruction=z!,
                cached_content=z
            ))ÚtextwrapÚdedentr#   r   r   r   r   r0   )r"   r<   s     r)   Ú__str__zGenerativeModel.__str__n   s‹   € ò	ô
 �‰ðà!Ÿ_™_Ð-ð .#Ø#'×#:Ñ#:Ð";ð <!Ø!%×!6Ñ!6Ð 7ð 8Ø—{‘{�mð $$Ù$.¨t×/GÑ/GÓ$HÐ#Ið J Ø $× 3Ñ 3Ð4ð 5ðó

ð 
	
r+   )r%   r$   c          
     ó¸  — t        | d«      r#t        | j                  ||g«      rt        d«      ‚| j	                  |«      }|�|j                  «       }|€| j                  }nt        j                  |«      }t        j                  |«      }t        j                  |«      }| j                  j                  «       }|j                  |«       t        j                   |«      }| j"                  j                  «       }|j                  |«       t        j$                  |«      }t'        j(                  | j*                  |||||| j                  | j,                  ¬«      S )z:Creates a `protos.GenerateContentRequest` from raw inputs.r-   zx`tools`, `tool_config`, `system_instruction` cannot be set on a model instantiated with `cached_content` as its context.)r   Úcontentsr%   r$   r&   r'   r(   r0   )ÚhasattrÚanyr   Ú
ValueErrorÚ_get_tools_libÚto_protor   r   r   Úto_contentsr   r   r   ÚcopyÚupdater   r   r   Únormalize_safety_settingsr   ÚGenerateContentRequestr   r0   )	r"   rA   r%   r$   r&   r'   Ú	tools_libÚ	merged_gcÚ	merged_sss	            r)   Ú_prepare_requestz GenerativeModel._prepare_request‚   sB  € ô �4Ð*Ô+´°T×5MÑ5MÈuÐVaÐ4bÔ0cÜð Kóð ð ×'Ñ'¨Ó.ˆ	ØÐ Ø!×*Ñ*Ó,ˆIàÐØ×+Ñ+‰Kä'×6Ñ6°{ÓCˆKä ×,Ñ,¨XÓ6ˆä,×FÑFÐGXÓYÐØ×+Ñ+×0Ñ0Ó2ˆ	Ø×ÑÐ*Ô+ä&×:Ñ:¸?ÓKˆØ×)Ñ)×.Ñ.Ó0ˆ	Ø×Ñ˜Ô)Ü ×:Ñ:¸9ÓEˆ	ä×,Ñ,Ø×"Ñ"ØØ'Ø%ØØ#Ø#×7Ñ7Ø×.Ñ.ô	
ð 		
r+   c                óH   — |€| j                   S t        j                  |«      S r2   )r   r   r   )r"   r&   s     r)   rE   zGenerativeModel._get_tools_lib°   s$   € ð ˆ=Ø—;‘;Ðä ×4Ñ4°UÓ;Ð;r+   c                ó   — y r2   © ©Úclsr0   r%   r$   s       r)   Úfrom_cached_contentz#GenerativeModel.from_cached_content¸   ó   € ð r+   c                ó   — y r2   rR   rS   s       r)   rU   z#GenerativeModel.from_cached_contentÂ   rV   r+   c               ó¾   — t        |t        «      r t        j                  j	                  |¬«      } | |j
                  ||¬«      }t        |d|j                  «       |S )au  Creates a model with `cached_content` as model's context.

        Args:
            cached_content: context for the model.
            generation_config: Overrides for the model's generation config.
            safety_settings: Overrides for the model's safety settings.

        Returns:
            `GenerativeModel` object with `cached_content` as its context.
        )Úname)r#   r%   r$   r-   )Ú
isinstanceÚstrr
   ÚCachedContentÚgetr   ÚsetattrrY   )rT   r0   r%   r$   r"   s        r)   rU   z#GenerativeModel.from_cached_contentÌ   s\   € ô$ �n¤cÔ*Ü$×2Ñ2×6Ñ6¸NÐ6ÓKˆNñ Ø%×+Ñ+Ø/Ø+ô
ˆô 	�Ð'¨×)<Ñ)<Ô=Øˆr+   F©r%   r$   Ústreamr&   r'   Úrequest_optionsc               ó  — |st        d«      ‚| j                  |||||¬«      }|j                  r1|j                  d   j                  st        |j                  d   _        | j
                  €t        j                  «       | _        |€i }	 |rYt        j                  «       5   | j
                  j                  |fi |¤Ž}	ddd«       t        j                  j                  	«      S  | j
                  j                  |fi |¤Ž}
t        j                  j                  |
«      S # 1 sw Y   ŒdxY w# t        j                   j"                  j$                  $ r6}|j&                  j)                  d«      r|xj&                  dz  c_        ‚ d}~ww xY w)aÇ	  A multipurpose function to generate responses from the model.

        This `GenerativeModel.generate_content` method can handle multimodal input, and multi-turn
        conversations.

        >>> model = genai.GenerativeModel('models/gemini-1.5-flash')
        >>> response = model.generate_content('Tell me a story about a magic backpack')
        >>> response.text

        ### Streaming

        This method supports streaming with the `stream=True`. The result has the same type as the non streaming case,
        but you can iterate over the response chunks as they become available:

        >>> response = model.generate_content('Tell me a story about a magic backpack', stream=True)
        >>> for chunk in response:
        ...   print(chunk.text)

        ### Multi-turn

        This method supports multi-turn chats but is **stateless**: the entire conversation history needs to be sent with each
        request. This takes some manual management but gives you complete control:

        >>> messages = [{'role':'user', 'parts': ['hello']}]
        >>> response = model.generate_content(messages) # "Hello, how can I help"
        >>> messages.append(response.candidates[0].content)
        >>> messages.append({'role':'user', 'parts': ['How does quantum physics work?']})
        >>> response = model.generate_content(messages)

        For a simpler multi-turn interface see `GenerativeModel.start_chat`.

        ### Input type flexibility

        While the underlying API strictly expects a `list[protos.Content]` objects, this method
        will convert the user input into the correct type. The hierarchy of types that can be
        converted is below. Any of these objects can be passed as an equivalent `dict`.

        * `Iterable[protos.Content]`
        * `protos.Content`
        * `Iterable[protos.Part]`
        * `protos.Part`
        * `str`, `Image`, or `protos.Blob`

        In an `Iterable[protos.Content]` each `content` is a separate message.
        But note that an `Iterable[protos.Part]` is taken as the parts of a single message.

        Arguments:
            contents: The contents serving as the model's prompt.
            generation_config: Overrides for the model's generation config.
            safety_settings: Overrides for the model's safety settings.
            stream: If True, yield response chunks as they are generated.
            tools: `protos.Tools` more info coming soon.
            request_options: Options for the request.
        úcontents must not be empty©rA   r%   r$   r&   r'   éÿÿÿÿNú'Request payload size exceeds the limit:ú® The file size is too large. Please use the File API to upload your files instead. Example: `f = genai.upload_file(path); m.generate_content(['tell me about this file:', f])`)Ú	TypeErrorrO   rA   ÚroleÚ
_USER_ROLEr    r	   Úget_default_generative_clientr   Úrewrite_stream_errorÚstream_generate_contentÚGenerateContentResponseÚfrom_iteratorÚgenerate_contentÚfrom_responseÚgoogleÚapi_coreÚ
exceptionsÚInvalidArgumentÚmessageÚ
startswith©r"   rA   r%   r$   r`   r&   r'   ra   ÚrequestÚiteratorÚresponseÚes               r)   rp   z GenerativeModel.generate_contentí   sx  € ñB ÜÐ8Ó9Ð9à×'Ñ'ØØ/Ø+ØØ#ð (ó 
ˆð ×Ò G×$4Ñ$4°RÑ$8×$=Ò$=Ü(2ˆG×Ñ˜RÑ Ô%à�<‰<ÐÜ!×?Ñ?ÓAˆDŒLàÐ"Ø ˆOð	ÙÜ%×:Ñ:Ó<ñ ØC˜tŸ|™|×CÑCØñ à)ñ �H÷ô
 (×?Ñ?×MÑMÈhÓWÐWà8˜4Ÿ<™<×8Ñ8Øñà%ñ�ô (×?Ñ?×MÑMÈhÓWÐW÷ð ûô �‰×)Ñ)×9Ñ9ò 	Ø�y‰y×#Ñ#Ð$MÔNØ—	’	ðrñ•	ð ûð	ús6   ÂD. Â!D"Â?&D. Ã&;D. Ä"D+Ä'D. Ä.'FÅ1FÆFc             ƒ  ó`  K  — |st        d«      ‚| j                  |||||¬«      }|j                  r1|j                  d   j                  st        |j                  d   _        | j
                  €t        j                  «       | _        |€i }	 |rit        j                  «       5   | j
                  j                  |fi |¤Žƒ d{  –—† }	ddd«       t        j                  j                  	«      ƒ d{  –—† S  | j
                  j                  |fi |¤Žƒ d{  –—† }
t        j                  j                  |
«      S 7 Œw# 1 sw Y   ŒvxY w7 ŒV7 Œ3# t        j                   j"                  j$                  $ r6}|j&                  j)                  d«      r|xj&                  dz  c_        ‚ d}~ww xY w­w)z8The async version of `GenerativeModel.generate_content`.rc   rd   re   Nrf   rg   )rh   rO   rA   ri   rj   r!   r	   Ú#get_default_generative_async_clientr   rl   rm   ÚAsyncGenerateContentResponseÚfrom_aiteratorrp   rq   rr   rs   rt   ru   rv   rw   rx   s               r)   Úgenerate_content_asyncz&GenerativeModel.generate_content_asyncX  s£  è ø€ ñ ÜÐ8Ó9Ð9à×'Ñ'ØØ/Ø+ØØ#ð (ó 
ˆð ×Ò G×$4Ñ$4°RÑ$8×$=Ò$=Ü(2ˆG×Ñ˜RÑ Ô%à×ÑÐ%Ü!'×!KÑ!KÓ!MˆDÔàÐ"Ø ˆOð	ÙÜ%×:Ñ:Ó<ñ Ø%O T×%7Ñ%7×%OÑ%OØñ&à)ñ&÷  �H÷ô
 .×JÑJ×YÑYÐZbÓc×cÐcà!D ×!3Ñ!3×!DÑ!DØñ"à%ñ"÷ �ô (×DÑD×RÑRÐS[Ó\Ð\ð ø÷ð úð
 døðùô
 �‰×)Ñ)×9Ñ9ò 	Ø�y‰y×#Ñ#Ð$MÔNØ—	’	ðrñ•	ð ûð	üs~   ‚B
F.ÂE Â#!D>ÃD<ÃD>Ã	*E Ã3E
Ã4E Ã7F.Ã8 E ÄEÄ"E Ä;F.Ä<D>Ä>EÅE ÅE Å'F+Å51F&Æ&F+Æ+F.)r%   r$   r&   r'   ra   c          
     óø   — |€i }| j                   €t        j                  «       | _         t        j                  | j
                  | j                  |||||¬«      ¬«      } | j                   j                  |fi |¤ŽS ©Nrd   )r   Úgenerate_content_request)r    r	   rk   r   ÚCountTokensRequestr#   rO   Úcount_tokens©r"   rA   r%   r$   r&   r'   ra   ry   s           r)   r†   zGenerativeModel.count_tokens�  s€   € ð Ð"Ø ˆOà�<‰<ÐÜ!×?Ñ?ÓAˆDŒLä×+Ñ+Ø—/‘/Ø%)×%:Ñ%:Ø!Ø"3Ø /ØØ'ð &;ó &
ôˆð )ˆt�|‰|×(Ñ(¨ÑD°OÑDÐDr+   c          
   ƒ  ó  K  — |€i }| j                   €t        j                  «       | _         t        j                  | j
                  | j                  |||||¬«      ¬«      } | j                   j                  |fi |¤Žƒ d {  –—† S 7 Œ­wrƒ   )r!   r	   r~   r   r…   r#   rO   r†   r‡   s           r)   Úcount_tokens_asyncz"GenerativeModel.count_tokens_asyncª  s�   è ø€ ð Ð"Ø ˆOà×ÑÐ%Ü!'×!KÑ!KÓ!MˆDÔä×+Ñ+Ø—/‘/Ø%)×%:Ñ%:Ø!Ø"3Ø /ØØ'ð &;ó &
ôˆð 5�T×'Ñ'×4Ñ4°WÑPÀÑP×PÐPÐPús   ‚A?BÂBÂB)ÚhistoryÚ!enable_automatic_function_callingc               ór   — | j                   j                  dd«      dkD  rt        d«      ‚t        | ||¬«      S )aM  Returns a `genai.ChatSession` attached to this model.

        >>> model = genai.GenerativeModel()
        >>> chat = model.start_chat(history=[...])
        >>> response = chat.send_message("Hello?")

        Arguments:
            history: An iterable of `protos.Content` objects, or equivalents to initialize the session.
        Úcandidate_counté   ú`Invalid configuration: The chat functionality does not support `candidate_count` greater than 1.)r   rŠ   r‹   )r   r]   rD   ÚChatSession)r"   rŠ   r‹   s      r)   Ú
start_chatzGenerativeModel.start_chatÇ  sH   € ð ×"Ñ"×&Ñ&Ð'8¸!Ó<¸qÒ@ÜØróð ô ØØØ.Oô
ð 	
r+   )zgemini-1.5-flash-002NNNNN)r#   r[   r$   ú(safety_types.SafetySettingOptions | Noner%   ú,generation_types.GenerationConfigType | Noner&   ú(content_types.FunctionLibraryType | Noner'   ú#content_types.ToolConfigType | Noner(   z content_types.ContentType | None©Úreturnr[   )rA   úcontent_types.ContentsTyper%   r“   r$   r’   r&   r”   r'   r•   r—   zprotos.GenerateContentRequest)r&   z!content_types.FunctionLibraryTyper—   z$content_types.FunctionLibrary | None)r0   r[   r%   r“   r$   r’   r—   r   )r0   zcaching.CachedContentr%   r“   r$   r’   r—   r   )r0   zstr | caching.CachedContentr%   r“   r$   r’   r—   r   )rA   r˜   r%   r“   r$   r’   r`   Úboolr&   r”   r'   r•   ra   ú&helper_types.RequestOptionsType | Noner—   ú(generation_types.GenerateContentResponse)rA   r˜   r%   r“   r$   r’   r`   r™   r&   r”   r'   r•   ra   rš   r—   ú-generation_types.AsyncGenerateContentResponser2   )rA   r˜   r%   r“   r$   r’   r&   r”   r'   r•   ra   rš   r—   zprotos.CountTokensResponse)rŠ   ú0Iterable[content_types.StrictContentType] | Noner‹   r™   r—   r�   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r*   Úpropertyr0   r#   r?   Ú__repr__rO   rE   r   ÚclassmethodrU   rp   r�   r†   r‰   r‘   rR   r+   r)   r   r      sÍ  „ ñ+ð^ 1ØDHØJNØ:>Ø;?Ø?Cð"àð"ð Bð"ð Hð	"ð
 8ð"ð 9ð"ð =ó"ð: ò6ó ð6ð ñ ó ð ò
ð$ €Hð KOØDHñ,
ð -ð,
ð Hð	,
ð
 Bð,
ð 8ð,
ð 9ð,
ð 
'ó,
ð\<Ø6ð<à	-ó<ð Øð
 KOØDHñàðð Hð	ð
 Bðð 
òó ó ðð Øð
 KOØDHñà-ðð Hð	ð
 Bðð 
òó ó ðð ð
 KOØDHñà3ðð Hð	ð
 Bðð 
òó ððH KOØDHØØ:>Ø;?ØBFñià,ðið Hð	ið
 Bðið ðið 8ðið 9ðið @ðið 
2óið^ KOØDHØØ:>Ø;?ØBFñ4à,ð4ð Hð	4ð
 Bð4ð ð4ð 8ð4ð 9ð4ð @ð4ð 
7ó4ðr 04ðEð KOØDHØ:>Ø;?ØBFñEà,ðEð Hð	Eð
 BðEð 8ðEð 9ðEð @ðEð 
$óEð: 04ðQð KOØDHØ:>Ø;?ØBFñQà,ðQð Hð	Qð
 BðQð 8ðQð 9ðQð @ðQð 
$óQð@ EIØ27ñ	
ð Bð
ð ,0ð	
ð
 
ô
r+   r   c                  ó  — e Zd ZdZ	 	 d	 	 	 	 	 dd„Zdddddddœ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Zd„ Zdd„Z	 	 dd	„Zdddddddœ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd
„Z		 	 dd„Z
d„ Zdd„Zedd„«       Zedd„«       Zej                   d„ «       Zdd„Zy)r�   aN  Contains an ongoing conversation with the model.

    >>> model = genai.GenerativeModel('models/gemini-1.5-flash')
    >>> chat = model.start_chat()
    >>> response = chat.send_message("Hello")
    >>> print(response.text)
    >>> response = chat.send_message("Hello again")
    >>> print(response.text)
    >>> response = chat.send_message(...

    This `ChatSession` object collects the messages sent and received, in its
    `ChatSession.history` attribute.

    Arguments:
        model: The model to use in the chat.
        history: A chat history to initialize the object with.
    NFc                óp   — || _         t        j                  |«      | _        d | _        d | _        || _        y r2   )r   r   rG   Ú_historyÚ
_last_sentÚ_last_receivedr‹   )r"   r   rŠ   r‹   s       r)   r*   zChatSession.__init__ô  s4   € ð ',ˆŒ
Ü.;×.GÑ.GÈÓ.PˆŒØ15ˆŒØSWˆÔØ1RˆÕ.r+   r_   c          	     ó\  — |€i }| j                   r|rt        d«      ‚| j                  j                  |«      }t	        j
                  |«      }|j                  st        |_        | j                  dd }	|	j                  |«       t        j                  |«      }|j                  dd«      dkD  rt        d«      ‚| j                  j                  |	||||||¬«      }
| j                  |
|¬«       | j                   r#|�!| j!                  |
|	|||||¬«      \  | _        }}
|| _        |
| _        |
S )	aP  Sends the conversation history with the added message and returns the model's response.

        Appends the request and response to the conversation history.

        >>> model = genai.GenerativeModel('models/gemini-1.5-flash')
        >>> chat = model.start_chat()
        >>> response = chat.send_message("Hello")
        >>> print(response.text)
        "Hello! How can I assist you today?"
        >>> len(chat.history)
        2

        Call it with `stream=True` to receive response chunks as they are generated:

        >>> chat = model.start_chat()
        >>> response = chat.send_message("Explain quantum physics", stream=True)
        >>> for chunk in response:
        ...   print(chunk.text, end='')

        Once iteration over chunks is complete, the `response` and `ChatSession` are in states identical to the
        `stream=False` case. Some properties are not available until iteration is complete.

        Like `GenerativeModel.generate_content` this method lets you override the model's `generation_config` and
        `safety_settings`.

        Arguments:
             content: The message contents.
             generation_config: Overrides for the model's generation config.
             safety_settings: Overrides for the model's safety settings.
             stream: If True, yield response chunks as they are generated.
        Nú¢Unsupported configuration: The `google.generativeai` SDK currently does not support the combination of `stream=True` and `enable_automatic_function_calling=True`.r�   rŽ   r�   ©rA   r%   r$   r`   r&   r'   ra   ©r{   r`   ©r{   rŠ   r%   r$   r`   rL   ra   )r‹   ÚNotImplementedErrorr   rE   r   r   ri   rj   rŠ   Úappendr   r   r]   rD   rp   Ú_check_responseÚ_handle_afcr¨   r©   ©r"   r:   r%   r$   r`   r&   r'   ra   rL   rŠ   r{   s              r)   Úsend_messagezChatSession.send_message   sO  € ðT Ð"Ø ˆOà×1Ò1±fÜ%ð uóð ð —J‘J×-Ñ-¨eÓ4ˆ	ä×*Ñ*¨7Ó3ˆà�|Š|Ü%ˆGŒLà—,‘,™q�/ˆØ�‰�wÔä,×FÑFÐGXÓYÐØ× Ñ Ð!2°AÓ6¸Ò:ÜØróð ð —:‘:×.Ñ.ØØ/Ø+ØØØ#Ø+ð /ó 
ˆð 	×Ñ h°vÐÔ>à×1Ò1°iÐ6KØ.2×.>Ñ.>Ø!ØØ"3Ø /ØØ#Ø /ð /?ó /Ñ+ˆDŒL˜' 8ð "ˆŒØ&ˆÔàˆr+   c               óÀ  — |j                   j                  rt        j                  |j                   «      ‚|s§|j                  d   j
                  t        j                  j                  j                  t        j                  j                  j                  t        j                  j                  j                  fvr"t        j                  |j                  d   «      ‚y y r5   )Úprompt_feedbackÚblock_reasonr   ÚBlockedPromptExceptionÚ
candidatesÚfinish_reasonr   Ú	CandidateÚFinishReasonÚFINISH_REASON_UNSPECIFIEDÚSTOPÚ
MAX_TOKENSÚStopCandidateException)r"   r{   r`   s      r)   r±   zChatSession._check_response^  s±   € Ø×#Ñ#×0Ò0Ü"×9Ñ9¸(×:RÑ:RÓSÐSáØ×"Ñ" 1Ñ%×3Ñ3Ü× Ñ ×-Ñ-×GÑGÜ× Ñ ×-Ñ-×2Ñ2Ü× Ñ ×-Ñ-×8Ñ8ð<ñ ô
 '×=Ñ=¸h×>QÑ>QÐRSÑ>TÓUÐUðð r+   c                óè   — |j                   }t        |«      dk7  rt        dt        |«      › d�«      ‚|d   j                  j                  }|D �cg c]  }|sŒd|v sŒ|j
                  ‘Œ }}|S c c}w )NrŽ   zZInvalid number of candidates: Automatic function calling only works with 1 candidate, but z were provided.r   Úfunction_call)r¹   r6   rD   r:   r7   rÂ   )r"   r{   r¹   r7   ÚpartÚfunction_callss         r)   Ú_get_function_callszChatSession._get_function_callsj  sˆ   € Ø×(Ñ(ˆ
Üˆz‹?˜aÒÜØlÔmpÐq{Óm|Ðl}ð  ~Mð  Nóð ð ˜1‘×%Ñ%×+Ñ+ˆØ9>Öc°Â$È?Ð^bÒKb˜$×,Ó,ÐcˆÐcØÐùò ds   ÁA/ÁA/ÁA/c               óö  ‡— | j                  |«      x}rÜt        ˆfd„|D «       «      snÇ|j                  |j                  d   j                  «       g }	|D ]$  }
 ‰|
«      }|€J d«       ‚|	j                  |«       Œ& t        j                  t        |	¬«      }|j                  |«       | j                  j                  ||||‰|¬«      }| j                  ||¬«       | j                  |«      x}rŒÜ|�^ }}|||fS )Nc              3  ó:   •K  — | ]  }t        ‰|   «      –— Œ y ­wr2   ©Úcallable©Ú.0ÚfcrL   s     €r)   ú	<genexpr>z*ChatSession._handle_afc.<locals>.<genexpr>�  ó   øè ø€ ÒH°2”x 	¨"¡×.ÑHùó   ƒr   ú¨Unexpected state: The function reference (fr) should never be None. It should only return None if the declaration is not callable, which is checked earlier in the code.©ri   r7   ©rA   r%   r$   r`   r&   ra   r­   )rÅ   Úallr°   r¹   r:   r   ÚContentrj   r   rp   r±   ©r"   r{   rŠ   r%   r$   r`   rL   ra   rÄ   Úfunction_response_partsrÌ   ÚfrÚsendr:   s         `       r)   r²   zChatSession._handle_afct  s  ø€ ð !%× 8Ñ 8¸Ó BÐBˆnÐBÜÓH¸ÔHÔHØØ�N‰N˜8×.Ñ.¨qÑ1×9Ñ9Ô:à9;Ð#Ø$ò 3�Ù˜r“]�Ø�~ð ðMó�~ð (×.Ñ.¨rÕ2ð3ô —>‘>¤zÐ9PÔQˆDØ�N‰N˜4Ô à—z‘z×2Ñ2Ø Ø"3Ø /ØØØ /ð 3ó ˆHð × Ñ ¨(¸6Ð ÔBð5 !%× 8Ñ 8¸Ó BÐBˆnÑBð8 $Ñˆ�'Ø˜ Ð)Ð)r+   c          	   ƒ  óŒ  K  — |€i }| j                   r|rt        d«      ‚| j                  j                  |«      }t	        j
                  |«      }|j                  st        |_        | j                  dd }	|	j                  |«       t        j                  |«      }|j                  dd«      dkD  rt        d«      ‚| j                  j                  |	||||||¬«      ƒ d{  –—† }
| j                  |
|¬«       | j                   r+|�)| j!                  |
|	|||||¬«      ƒ d{  –—† \  | _        }}
|| _        |
| _        |
S 7 Œ^7 Œ­w)	z0The async version of `ChatSession.send_message`.Nr«   r�   rŽ   r�   r¬   r­   r®   )r‹   r¯   r   rE   r   r   ri   rj   rŠ   r°   r   r   r]   rD   r�   r±   Ú_handle_afc_asyncr¨   r©   r³   s              r)   Úsend_message_asynczChatSession.send_message_asyncŸ  sh  è ø€ ð Ð"Ø ˆOà×1Ò1±fÜ%ð uóð ð —J‘J×-Ñ-¨eÓ4ˆ	ä×*Ñ*¨7Ó3ˆà�|Š|Ü%ˆGŒLà—,‘,™q�/ˆØ�‰�wÔä,×FÑFÐGXÓYÐØ× Ñ Ð!2°AÓ6¸Ò:ÜØróð ð Ÿ™×:Ñ:ØØ/Ø+ØØØ#Ø+ð ;ó 
÷ 
ˆð 	×Ñ h°vÐÔ>à×1Ò1°iÐ6KØ48×4JÑ4JØ!ØØ"3Ø /ØØ#Ø /ð 5Kó 5÷ /Ñ+ˆDŒL˜' 8ð "ˆŒØ&ˆÔàˆð5
øð/ús%   ‚CEÃ!E Ã"A EÄ"EÄ#EÅEc             ƒ  ó  ‡K  — | j                  |«      x}rät        ˆfd„|D «       «      snÏ|j                  |j                  d   j                  «       g }	|D ]$  }
 ‰|
«      }|€J d«       ‚|	j                  |«       Œ& t        j                  t        |	¬«      }|j                  |«       | j                  j                  ||||‰|¬«      ƒ d {  –—† }| j                  ||¬«       | j                  |«      x}rŒä|�^ }}|||fS 7 Œ5­w)Nc              3  ó:   •K  — | ]  }t        ‰|   «      –— Œ y ­wr2   rÈ   rÊ   s     €r)   rÍ   z0ChatSession._handle_afc_async.<locals>.<genexpr>ì  rÎ   rÏ   r   rÐ   rÑ   rÒ   r­   )rÅ   rÓ   r°   r¹   r:   r   rÔ   rj   r   r�   r±   rÕ   s         `       r)   rÚ   zChatSession._handle_afc_asyncß  s,  øè ø€ ð !%× 8Ñ 8¸Ó BÐBˆnÐBÜÓH¸ÔHÔHØØ�N‰N˜8×.Ñ.¨qÑ1×9Ñ9Ô:à9;Ð#Ø$ò 3�Ù˜r“]�Ø�~ð ðMó�~ð (×.Ñ.¨rÕ2ð3ô —>‘>¤zÐ9PÔQˆDØ�N‰N˜4Ô à!ŸZ™Z×>Ñ>Ø Ø"3Ø /ØØØ /ð ?ó ÷ ˆHð × Ñ ¨(¸6Ð ÔBð5 !%× 8Ñ 8¸Ó BÐBˆnÑBð8 $Ñˆ�'Ø˜ Ð)Ð)ðús   ƒCDÃDÃ*DÃ;Dc                óV   — t        | j                  t        | j                  «      ¬«      S )N)r   rŠ   )r�   r   ÚlistrŠ   r/   s    r)   Ú__copy__zChatSession.__copy__
  s"   € ÜØ—*‘*ä˜Ÿ™Ó&ô
ð 	
r+   c                ó  — | j                   €7| j                  j                  d«      | j                  j                  «       f}|S | j                  | j                   j                  d   j
                  f}d| _        d| _         |S )z=Removes the last request/response pair from the chat history.Néþÿÿÿr   )r©   r§   Úpopr¨   r¹   r:   )r"   Úresults     r)   ÚrewindzChatSession.rewind  sr   € à×ÑÐ&Ø—]‘]×&Ñ& rÓ*¨D¯M©M×,=Ñ,=Ó,?Ð?ˆFØˆMà—_‘_ d×&9Ñ&9×&DÑ&DÀQÑ&G×&OÑ&OÐOˆFØ"ˆDŒOØ"&ˆDÔØˆMr+   c                ó   — | j                   S )z9returns the last received `genai.GenerateContentResponse`)r©   r/   s    r)   ÚlastzChatSession.last  s   € ð ×"Ñ"Ð"r+   c                óÎ  — | j                   }|€| j                  S |j                  d   j                  t        j
                  j                  j                  t        j
                  j                  j                  t        j
                  j                  j                  fvr)t        j                  |j                  d   «      }||_        |j                  � t        j                  d«      |j                  ‚| j                  }|j                  d   j                  }|j                   st"        |_        | j                  j%                  ||g«       d| _        d| _         | j                  S )zThe chat history.Nr   a<  Unable to build a coherent chat history due to a broken streaming response. Refer to the previous exception for details. To inspect the last response object, use `chat.last`. To remove the last request/response `Content` objects from the chat, call `last_send, last_received = chat.rewind()` and continue without it.)r©   r§   r¹   rº   r   r»   r¼   r½   r¾   r¿   r   rÀ   Ú_errorÚBrokenResponseErrorr¨   r:   ri   Ú_MODEL_ROLEÚextend)r"   rç   ÚerrorÚsentÚreceiveds        r)   rŠ   zChatSession.history!  s$  € ð ×"Ñ"ˆØˆ<Ø—=‘=Ð à�?‰?˜1Ñ×+Ñ+Ü×Ñ×)Ñ)×CÑCÜ×Ñ×)Ñ)×.Ñ.Ü×Ñ×)Ñ)×4Ñ4ð4
ñ 
ô
 %×;Ñ;¸D¿O¹OÈAÑ<NÓOˆEØˆDŒKà�;‰;Ð"Ü"×6Ñ6ð[óð —;‘;ðð �‰ˆØ—?‘? 1Ñ%×-Ñ-ˆØ�}Š}Ü'ˆHŒMØ�‰×Ñ˜d HÐ-Ô.àˆŒØ"ˆÔà�}‰}Ðr+   c                óT   — t        j                  |«      | _        d | _        d | _        y r2   )r   rG   r§   r¨   r©   )r"   rŠ   s     r)   rŠ   zChatSession.historyD  s#   € ä%×1Ñ1°'Ó:ˆŒØˆŒØ"ˆÕr+   c                óŠ  ‡— t        j                  «       Št        | j                  «      j	                  dd«      }ˆfd„}	 t        | j                  «      }| j                  �|j                  | j                  «       |D �cg c]
  } ||«      ‘Œ }}| j                  }|�/|j                  �|j                  d«       n|j                  d«       dddj                  |«      › d	�z   }t!        j"                  d
«      |z   |z   S # t        j                  t        j                  f$ r t        | j                  «      }Y Œæw xY wc c}w )Nú
z
    c                ó^   •— d‰j                  t        | «      j                  | «      «      › d�S )Nzprotos.Content(ú))r9   ÚtypeÚto_dict)ÚxÚ
_dict_reprs    €r)   Úcontent_reprz*ChatSession.__repr__.<locals>.content_reprN  s)   ø€ Ø$ Z§_¡_´T¸!³W·_±_ÀQÓ5GÓ%HÐ$IÈÐKÐKr+   z<STREAMING ERROR>z<STREAMING IN PROGRESS>z,
    z	history=[z, z]
)z7                ChatSession(
                    model=)ÚreprlibÚReprr[   r   Úreplacerß   rŠ   r   rê   ÚIncompleteIterationErrorr§   r¨   r°   r©   ré   Újoinr=   r>   )r"   Ú_modelrù   rŠ   r÷   Úlast_receivedr§   rø   s          @r)   r£   zChatSession.__repr__J  s)  ø€ Ü—\‘\“^ˆ
Ü�T—Z‘Z“×(Ñ(¨¨~Ó>ˆô	Lð	*Ü˜4Ÿ<™<Ó(ˆGð �?‰?Ð&Ø�N‰N˜4Ÿ?™?Ô+Ø,3Ö4 q‘< •?Ð4ˆÐ4à×+Ñ+ˆØÐ$Ø×#Ñ#Ð/Ø—‘Ð2Õ3à—‘Ð8Ô9à ¨4¯9©9°WÓ+=Ð*>¸dÐCÑCˆô �O‰Oðóð
 ñð ñð	
øô! !×4Ñ4Ô6F×6_Ñ6_Ð`ò 	*Ü˜4Ÿ=™=Ó)ŠGð	*üò
 5s   ÁD ÂE Ä8D=Ä<D=)NF)r   r   rŠ   r�   r‹   r™   )r:   úcontent_types.ContentTyper%   ú%generation_types.GenerationConfigTyper$   ú!safety_types.SafetySettingOptionsr`   r™   r&   r”   r'   r•   ra   rš   r—   r›   )r—   zlist[protos.FunctionCall])r—   zYtuple[list[protos.Content], protos.Content, generation_types.BaseGenerateContentResponse])r:   r  r%   r  r$   r  r`   r™   r&   r”   r'   r•   ra   rš   r—   rœ   )r—   z%tuple[protos.Content, protos.Content])r—   z3generation_types.BaseGenerateContentResponse | None)r—   zlist[protos.Content]r–   )rž   rŸ   r    r¡   r*   r´   r±   rÅ   r²   rÛ   rÚ   rà   rå   r¢   rç   rŠ   Úsetterr£   rR   r+   r)   r�   r�   á  s•  „ ñð* EIØ27ð	
Sàð
Sð Bð
Sð ,0ó	
Sð  DHØ=AØØ:>Ø;?ØBFñ\à*ð\ð Að	\ð
 ;ð\ð ð\ð 8ð\ð 9ð\ð @ð\ð 
2ó\ò|
Vóð)*ð 
có)*ð^ DHØ=AØØ:>Ø;?ØBFñ>à*ð>ð Að	>ð
 ;ð>ð ð>ð 8ð>ð 9ð>ð @ð>ð 
7ó>ð@)*ð 
có)*òV
ó	ð ò#ó ð#ð ò ó ð ðD ‡^�^ñ#ó ð#ô
!
r+   r�   )r¡   Ú
__future__r   Úcollections.abcr   r=   Útypingr   r   r   rú   Úgoogle.api_core.exceptionsrr   Úgoogle.generativeair   r	   r
   Úgoogle.generativeai.typesr   r   r   r   rj   rë   r   r�   rR   r+   r)   ú<module>r     sW   ðÙ 1å "å $Û ß 'Ñ 'Û ó
 "Ý &Ý &å 'Ý 3Ý 6Ý 2Ý 2à€
Ø€÷C
ñ C
÷LJ
ò J
r+   