
    LiA                        d dl mZ d dlZd dlmZmZ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 ej,                  d	k\  rd d
lmZ nd d
lmZ dedefdZdee   defdZdedefdZdee   dee   fdZ G d d      Zdeee   ef   deej@                     fdZ! G d de      Z" G d d      Z# G d de      Z$ G d d      Z%y)    )IteratorN)AsyncIterator	AwaitableOptionalUnionget_args   )_transformers)types)AsyncModelsModels)ContentContentOrDictGenerateContentConfigOrDictGenerateContentResponsePartPartUnionDict)   
   )	TypeGuardcontentreturnc                     | j                   sy| j                   D ]/  }|t               k(  r y|j                  |j                  dk(  s/ y y)NF T)partsr   text)r   parts     P/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/google/genai/chats.py_validate_contentr       sG    	mm dtv~yyb	
 
    contentsc                 2    | sy| D ]  }t        |      r y y)NFT)r   )r!   r   s     r   _validate_contentsr#   +   s'    	 gW% 
r    responsec                     | j                   sy| j                   d   j                  syt        | j                   d   j                        S )NFr   )
candidatesr   r   )r$   s    r   _validate_responser'   4   sA    						Q		'	'	8..q199	::r    comprehensive_historyc                    | sg S g }t        |       }d}| |   }||k  r| |   j                  dvrt        d| |   j                         | |   j                  dk(  r| |   }|j                  |       |dz  }ng }d}||k  rU| |   j                  dk(  rC|j                  | |          |rt	        | |         sd}|dz  }||k  r| |   j                  dk(  rC|r|j                  |       n|r|j                          ||k  r|S )	a  Extracts the curated (valid) history from a comprehensive history.

  The comprehensive history contains all turns (user input and model responses),
  including any invalid or rejected model outputs. This function filters that
  history to return only the valid turns.

  Args:
      comprehensive_history: A list representing the complete chat history.
        Including invalid turns.

  Returns:
      curated history, which is a list of valid turns.
  r   )usermodelz$Role must be user or model, but got r*   r	   Tr+   F)lenrole
ValueErrorappendr   extendpop)r(   curated_historylengthicurrent_inputcurrent_outputis_valids          r   _extract_curated_historyr8   <   sF     
I/$%&!'*-	F
Q$$,==01Fq1I1N1N0O
P  Q$$.+A.m]+1fanhJ0388GC3A67-.CA.FG(	Q	 J0388GC
 
~.+ 	
F
, 
r    c            
       v    e Zd ZdZdddedee   dee   fdZ	de
d	ee
   d
ee
   deddf
dZddedee
   fdZy)	_BaseChatzBase chat session.Nconfigr+   r<   historyc                    || _         || _        g }|D ];  }t        |t              st        j                  |      }n|}|j                  |       = || _        	 t        |      | _        y N)	_model_config
isinstancer   model_validater/   _comprehensive_historyr8   _curated_history)selfr+   r<   r=   content_modelsr   content_models          r   __init__z_BaseChat.__init__n   st     DKDLN +)..w7M*+ #1D4^DDr    
user_inputmodel_output"automatic_function_calling_historyr7   r   c                 @   |r|t        | j                        d n|g}|r|nt        dg       g}| j                  j	                  |       | j                  j	                  |       |r7| j                  j	                  |       | j                  j	                  |       yy)a/  Records the chat history.

    Maintaining both comprehensive and curated histories.

    Args:
      user_input: The user's input content.
      model_output: A list of `Content` from the model's response. This can be
        an empty list if the model produced no output.
      automatic_function_calling_history: A list of `Content` representing the
        history of automatic function calls, including the user input as the
        first entry.
      is_valid: A boolean flag indicating whether the current model output is
        considered valid.
    Nr+   )r-   r   )r,   rE   r   rD   r0   )rF   rJ   rK   rL   r7   input_contentsoutput_contentss          r   record_historyz_BaseChat.record_history   s    4 . 	+3t/D/D+E+GH\  %7r+J*K  	&&~6&&7
"">2
""?3 r    curatedc                 6    |r| j                   S | j                  S )aH  Returns the chat history.

    Args:
        curated: A boolean flag indicating whether to return the curated (valid)
          history or the comprehensive (all turns) history. Defaults to False
          (returns the comprehensive history).

    Returns:
        A list of `Content` objects representing the chat history.
    )rE   rD   )rF   rQ   s     r   get_historyz_BaseChat.get_history   s     """(((r    )F)__name__
__module____qualname____doc__strr   r   listr   rI   r   boolrP   rS    r    r   r:   r:   k   s     7;	  23	
 M".&4&4 M&4 +/w-	&4
 &4 &4P) )$w- )r    r:   c                     t        | t              rt        d | D              S t        t        j
                        }t        |       |v ryt        j                  t        | t        j                        ryy)Nc              3   2   K   | ]  }t        |        y wr?   )_is_part_type).0r   s     r   	<genexpr>z _is_part_type.<locals>.<genexpr>   s     8t}T"8s   TF)rB   rY   allr   r   	PartUniontype	PIL_Image)r!   allowed_part_typess     r   r^   r^      s]     $8x888!%//2H~++ 
	$Heoo)Nr    c            
            e Zd ZdZdddededee   dee	   f fdZ
	 dd	eee   ef   dee   d
efdZ	 dd	eee   ef   dee   d
ee   fdZ xZS )ChatzChat session.Nr;   modulesr+   r<   r=   c                8    || _         t        | 	  |||       y N)r+   r<   r=   _modulessuperrI   rF   rh   r+   r<   r=   	__class__s        r   rI   zChat.__init__   '     DM	G  r    messager   c           
      2   t        |      s9t        dt        j                   dt        j                   dt        |             t        j                  |      }| j                  j                  | j                  | j                  |gz   |r|n| j                        }|j                  r3|j                  d   j                  r|j                  d   j                  gng }|j                  r|j                  ng }| j!                  |||t#        |             |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-2.0-flash')
      response = chat.send_message('tell me a story')
    #Message must be a valid part type:  or , got r+   r!   r<   r   rJ   rK   rL   r7   r^   r.   r   rb   r   rc   t	t_contentrl   generate_contentr@   rE   rA   r&   r   rL   rP   r'   rF   rq   r<   input_contentr$   rK   rL   s          r   send_messagezChat.send_message   s   . !//@ A!!"&g9  KK(M}}--kk&&-8vT\\ . H 8#6#6q#9#A#A 
		Q		'	'(  66 	33 '
 	 !+M#H-	   Or    c           
   #   J  K   t        |      s9t        dt        j                   dt        j                   dt        |             t        j                  |      }g }d}d}d}t        | j                  t              r#| j                  j                  | j                  | j                  |gz   |r|n| j                        D ]  }t        |      sd}|j                   rA|j                   d   j"                  r(|j%                  |j                   d   j"                         |j                   r2|j                   d   j&                  r|j                   d   j&                  }|  ||j(                  r|j(                  ng }| j+                  ||||xr
 |duxr |du	       yyw)
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-2.0-flash')
      for chunk in chat.send_message_stream('tell me a story'):
        print(chunk.text)
    rs   rt   ru   NTrv   Fr   rw   )r^   r.   r   rb   r   rc   ry   rz   rB   rl   r   generate_content_streamr@   rE   rA   r'   r&   r   r/   finish_reasonrL   rP   )	rF   rq   r<   r}   rO   r   r7   chunkrL   s	            r   send_message_streamzChat.send_message_stream  s    0 !//@ A!!"&g9  KK(MOMHE$--(==88((M?:!t|| 9  %
 "%(( 0 0 3 ; ;

 
 !1!1!!4!<!<
= 0 0 3 A A**1-;;- 5#K#K 
2
2 )
 "&-O (T)(4'  % )s   F!F#r?   )rT   rU   rV   rW   r   rX   r   r   rY   r   rI   r   r   r   r~   r   r   __classcell__ro   s   @r   rg   rg      s     7;  	
 23 M"$ 7;2T-(-782 232 	2n 7;;T-(-78; 23; '(	;r    rg   c            	       N    e Zd ZdZdefdZddddedee   dee	e
      d	efd
Zy)Chatsz%A util class to create chat sessions.rh   c                     || _         y r?   rl   rF   rh   s     r   rI   zChats.__init__V  	    DMr    Nr<   r=   r+   r<   r=   r   c                F    t        | j                  |||r|      S g       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.
    )rh   r+   r<   r=   )rg   rl   rF   r+   r<   r=   s       r   createzChats.createY  s2    " "	  )+	 r    )rT   rU   rV   rW   r   rI   rX   r   r   rY   r   rg   r   r[   r    r   r   r   S  sV    -f  7;/3  23	
 ]+, r    r   c            
            e Zd ZdZdddededee   dee	   f fdZ
	 dd	eee   ef   dee   d
efdZ	 dd	eee   ef   dee   d
ee   fdZ xZS )	AsyncChatzAsync chat session.Nr;   rh   r+   r<   r=   c                8    || _         t        | 	  |||       y rj   rk   rn   s        r   rI   zAsyncChat.__init__u  rp   r    rq   r   c           
      N  K   t        |      s9t        dt        j                   dt        j                   dt        |             t        j                  |      }| j                  j                  | j                  | j                  |gz   |r|n| j                         d{   }|j                  r3|j                  d   j                  r|j                  d   j                  gng }|j                  r|j                  ng }| j!                  |||t#        |             |S 7 w)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-2.0-flash')
      response = await chat.send_message('tell me a story')
    rs   rt   ru   rv   Nr   rw   rx   r|   s          r   r~   zAsyncChat.send_message  s)    , !//@ A!!"&g9  KK(M]]33kk&&-8vT\\ 4  H 8#6#6q#9#A#A 
		Q		'	'(  66 	33 '
 	 !+M#H-	   O+s   B!D%#D#$B D%c           
          K   t        |      s9t        dt        j                   dt        j                   dt        |             t        j                  |       fd} |       S w)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-2.0-flash')
      async for chunk in await chat.send_message_stream('tell me a story'):
        print(chunk.text)
    rs   rt   ru   c                 x  K   g } d }d}d }j                   j                  j                  j                  gz   rnj                         d {   2 3 d {   }t        |      sd}|j                  rA|j                  d   j                  r(| j                  |j                  d   j                         |j                  r2|j                  d   j                  r|j                  d   j                  }| 7 7 6 | r|d}j                  | ||j                  r|j                  ng |       y w)NTrv   Fr   rw   )rl   r   r@   rE   rA   r'   r&   r   r/   r   rP   rL   )rO   r   r7   r   r<   r}   rF   s       r   async_generatorz6AsyncChat.send_message_stream.<locals>.async_generator  s?    omhe#}}DD((M?:!t||  E     
 "%(( 0 0 3 ; ;

 
 !1!1!!4!<!<
= 0 0 3 A A**1-;;-    5
"&5#K#K .3-U-U  s7   AD:C?D:DDD B D:D7D:)r^   r.   r   rb   r   rc   ry   rz   )rF   rq   r<   r   r}   s   ` ` @r   r   zAsyncChat.send_message_stream  sk     0 !//@ A!!"&g9  KK(M< s   A(A-r?   )rT   rU   rV   rW   r   rX   r   r   rY   r   rI   r   r   r   r~   r   r   r   r   s   @r   r   r   r  s     7;  	
 23 M"$ 7;1T-(-781 231 	1l 7;=T-(-78= 23= ,-	=r    r   c            	       N    e Zd ZdZdefdZddddedee   dee	e
      d	efd
Zy)
AsyncChatsz+A util class to create async chat sessions.rh   c                     || _         y r?   r   r   s     r   rI   zAsyncChats.__init__  r   r    Nr   r+   r<   r=   r   c                F    t        | j                  |||r|      S g       S r   )r   rl   r   s       r   r   zAsyncChats.create  s2    " "	  )+	 r    )rT   rU   rV   rW   r   rI   rX   r   r   rY   r   r   r   r[   r    r   r   r     sV    3k  7;/3  23	
 ]+, r    r   )&collections.abcr   systypingr   r   r   r   r   r   r
   ry   r   modelsr   r   r   r   r   r   r   r   version_infor   typing_extensionsrZ   r   rY   r#   r'   r8   r:   ContentTyper^   rg   r   r   r   r[   r    r   <module>r      s     % 
 F F    ' t t w)w 4 g 4 ;!8 ;T ;,=,	'],^P) P)fD'67q}}"A9 AH >B	 BJ r    