o
    0Xxih!                     @   s   d dl mZmZmZ d dl mZ ddlmZ ddlm	Z	m
Z
 ddlmZmZmZmZmZmZ dedefd	d
ZG dd dZG dd deZG dd dZG dd deZG dd dZdS )    )AsyncIterator	AwaitableOptional)Union   )_transformers)AsyncModelsModels)ContentContentDictGenerateContentConfigOrDictGenerateContentResponsePartPartUnionDictresponsereturnc                 C   sn   | j sdS | j d jsdS | j d jjsdS | j d jjD ]}|t kr' dS |jd ur4|jdkr4 dS qdS )NFr    T)
candidatescontentpartsr   text)r   part r   Q/home/air/biblejyuku/back/venv/lib/python3.10/site-packages/google/genai/chats.py_validate_response   s   
r   c                
   @   s<   e Zd ZdZdddeeef dedede	e
 fdd	ZdS )
	_BaseChatzBase chat session.N)configmodulesmodelr   historyc                C   s   || _ || _|| _|| _d S N)_modules_model_config_curated_history)selfr   r   r   r   r   r   r   __init__*   s   
z_BaseChat.__init__)__name__
__module____qualname____doc__r   r	   r   strr   listr
   r&   r   r   r   r   r   '   s    
r   c                   @   s`   e Zd ZdZ	d
deee ef dee de	fddZ
	d
deee ef dee fdd	ZdS )ChatzChat session.Nmessager   r   c                 C   sx   t | jj|}| jj| j| j|g |r|n| jd}t|r:|j	r*| j
|j	 n| j| | j|jd j |S )a  Sends the conversation history with the additional message and returns the model's response.

    Args:
      message: The message to send to the model.
      config:  Optional config to override the default Chat config for this
        request.

    Returns:
      The model's response.

    Usage:

    .. code-block:: python

      chat = client.chats.create(model='gemini-1.5-flash')
      response = chat.send_message('tell me a story')
    r   contentsr   r   t	t_contentr!   _api_clientgenerate_contentr"   r$   r#   r   "automatic_function_calling_historyextendappendr   r   r%   r.   r   input_contentr   r   r   r   send_message;   s   
zChat.send_messagec                 c   s    t | jj|}g }d}| jj| j| j|g |r|n| jdD ]!}t|r/|	|j
d j |j
r>|j
d jr>|j
d j}|V  q |rT|rV| j	| | j| dS dS dS )a  Sends the conversation history with the additional message and yields the model's response in chunks.

    Args:
      message: The message to send to the model.
      config: Optional config to override the default Chat config for this
        request.

    Yields:
      The model's response in chunks.

    Usage:

    .. code-block:: python

      chat = client.chats.create(model='gemini-1.5-flash')
      for chunk in chat.send_message_stream('tell me a story'):
        print(chunk.text)
    Nr/   r   )r2   r3   r!   r4   generate_content_streamr"   r$   r#   r   r8   r   r   finish_reasonr7   )r%   r.   r   r:   output_contentsr=   chunkr   r   r   send_message_streamb   s$   

zChat.send_message_streamr    )r'   r(   r)   r*   r   r,   r   r   r   r   r;   r@   r   r   r   r   r-   8   s     
*r-   c                	   @   H   e Zd ZdZdefddZddddeded	ee	e
  d
efddZdS )Chatsz%A util class to create chat sessions.r   c                 C   
   || _ d S r    r!   r%   r   r   r   r   r&         
zChats.__init__Nr   r   r   r   r   r   c                C      t | j|||r|dS g dS zCreates a new chat session.

    Args:
      model: The model to use for the chat.
      config: The configuration to use for the generate content request.
      history: The history to use for the chat.

    Returns:
      A new chat session.
    )r   r   r   r   )r-   r!   r%   r   r   r   r   r   r   create      zChats.create)r'   r(   r)   r*   r	   r&   r+   r   r   r,   r
   r-   rK   r   r   r   r   rB          
rB   c                	   @   sl   e Zd ZdZ	d
deee ef dee de	fddZ
	d
deee ef dee deee	  fdd	ZdS )	AsyncChatzAsync chat session.Nr.   r   r   c                    s   t | jj|}| jj| j| j|g |r|n| jdI dH }t|r>|j	r.| j
|j	 n| j| | j|jd j |S )a  Sends the conversation history with the additional message and returns model's response.

    Args:
      message: The message to send to the model.
      config: Optional config to override the default Chat config for this
        request.

    Returns:
      The model's response.

    Usage:

    .. code-block:: python

      chat = client.aio.chats.create(model='gemini-1.5-flash')
      response = await chat.send_message('tell me a story')
    r/   Nr   r1   r9   r   r   r   r;      s   
zAsyncChat.send_messagec                    s(   t jj| fdd}| S )a  Sends the conversation history with the additional message and yields the model's response in chunks.

    Args:
      message: The message to send to the model.
      config: Optional config to override the default Chat config for this
        request.

    Yields:
      The model's response in chunks.

    Usage:

    .. code-block:: python
      chat = client.aio.chats.create(model='gemini-1.5-flash')
      async for chunk in await chat.send_message_stream('tell me a story'):
        print(chunk.text)
    c                    s   g } d }j jjjg  r njdI d H 2 z%3 d H W }t|r.| |jd j |jr=|jd j	r=|jd j	}|V  q6 | rT|rVj j
|  d S d S d S )Nr/   r   )r!   r<   r"   r$   r#   r   r8   r   r   r=   r7   )r>   r=   r?   r   r:   r%   r   r   async_generator   s$   
z6AsyncChat.send_message_stream.<locals>.async_generator)r2   r3   r!   r4   )r%   r.   r   rP   r   rO   r   r@      s   zAsyncChat.send_message_streamr    )r'   r(   r)   r*   r   r,   r   r   r   r   r;   r   r   r@   r   r   r   r   rN      s$    
*
rN   c                	   @   rA   )
AsyncChatsz+A util class to create async chat sessions.r   c                 C   rC   r    rD   rE   r   r   r   r&     rF   zAsyncChats.__init__NrG   r   r   r   r   c                C   rH   rI   )rN   r!   rJ   r   r   r   rK     rL   zAsyncChats.create)r'   r(   r)   r*   r   r&   r+   r   r   r,   r
   rN   rK   r   r   r   r   rQ     rM   rQ   N)typingr   r   r   r   r   r   r2   modelsr   r	   typesr
   r   r   r   r   r   boolr   r   r-   rB   rN   rQ   r   r   r   r   <module>   s    TW