o
    0Xxi[                     @   s  d Z ddlZddlZddlZddlZddlZddlmZmZm	Z	m
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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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dlm)Z) ddlm*Z* zddl+m,Z, ddl+m-Z- W n e.y   ddl/m,Z, ddl/m-Z- Y nw dZ0G dd  d Z1G d!d" d"ej2Z3dS )#z-Live client. The live module is experimental.    N)AsyncIteratorOptionalSequenceUnion)ConnectionClosed   )_api_module)_common)_transformers)client)errors)types)	ApiClient)experimental_warning)get_value_by_path)set_value_by_path)_Content_from_mldev)_Content_from_vertex)_Content_to_mldev)_Content_to_vertex)_GenerateContentConfig_to_mldev) _GenerateContentConfig_to_vertex)_SafetySetting_to_mldev)_SafetySetting_to_vertex)_SpeechConfig_to_mldev)_SpeechConfig_to_vertex)_Tool_to_mldev)_Tool_to_vertex)ClientConnection)connectznFunctionResponse request must have an `id` field from the response of a ToolCall.FunctionalCalls in Google AI.c                   @   s  e Zd ZdZdejdefddZdddee	j
e	je	je	je	je	jee	j f d	ee fd
dZdee	j fddZdee dedee	j fddZde	jfddZdee dedejfddZdeeef defddZ deeef defddZ!deeef defddZ"deeef defd d!Z#deeef defd"d#Z$deeef defd$d%Z%	d+dee	j
e	je	je	je	je	je	jee	j f d	ee defd&d'Z&d(d) Z'd*S ),AsyncSessionz.AsyncSession. The live module is experimental.
api_client	websocketc                 C   s   || _ || _d S N)_api_client_ws)selfr!   r"    r'   P/home/air/biblejyuku/back/venv/lib/python3.10/site-packages/google/genai/live.py__init__D   s   
zAsyncSession.__init__F)end_of_turninputr*   c                   s*   |  ||}| jt|I dH  dS )a  Send input to the model.

    The method will send the input request to the server.

    Args:
      input: The input request to the model.
      end_of_turn: Whether the input is the last message in a turn.

    Example usage:

    .. code-block:: python

      client = genai.Client(api_key=API_KEY)

      async with client.aio.live.connect(model='...') as session:
        await session.send(input='Hello world!', end_of_turn=True)
        async for message in session.receive():
          print(message)
    N)_parse_client_messager%   sendjsondumps)r&   r+   r*   client_messager'   r'   r(   r-   H   s   !zAsyncSession.sendreturnc                 C  sL   |   I dH  }r$|jr|jjr|V  dS |V  |   I dH  }s
dS dS )a  Receive model responses from the server.

    The method will yield the model responses from the server. The returned
    responses will represent a complete model turn. When the returned message
    is function call, user must call `send` with the function response to
    continue the turn.

    The live module is experimental.

    Yields:
      The model responses from the server.

    Example usage:

    .. code-block:: python

      client = genai.Client(api_key=API_KEY)

      async with client.aio.live.connect(model='...') as session:
        await session.send(input='Hello world!', end_of_turn=True)
        async for message in session.receive():
          print(message)
    N)_receiveserver_contentturn_complete)r&   resultr'   r'   r(   receivel   s   zAsyncSession.receivestream	mime_typec                C  s   t  }t | ||| d}| sOz,t |  }t j|t | gt jdI dH  | r@|	 V  t 
dI dH  W n	 tyJ   Y nw | r|duro| sq|  z|I dH  W dS  t jyn   Y dS w dS dS )a  start a live session from a data stream.

    The interaction terminates when the input stream is complete.
    This method will start two async tasks. One task will be used to send the
    input stream to the model and the other task will be used to receive the
    responses from the model.

    The live module is experimental.

    Args:
      stream: An iterator that yields the model response.
      mime_type: The MIME type of the data in the stream.

    Yields:
      The audio bytes received from the model and server response messages.

    Example usage:

    .. code-block:: python

      client = genai.Client(api_key=API_KEY)
      config = {'response_modalities': ['AUDIO']}
      async def audio_stream():
        stream = read_audio()
        for data in stream:
          yield data
      async with client.aio.live.connect(model='...') as session:
        for audio in session.start_stream(stream = audio_stream(),
        mime_type = 'audio/pcm'):
          play_audio_chunk(audio.data)
    N)return_when-q=)asyncioEventcreate_task
_send_loopis_setr2   waitFIRST_COMPLETEDdoner5   sleepr   cancelCancelledError)r&   r7   r8   
stop_event	recv_taskr'   r'   r(   start_stream   s:   "
zAsyncSession.start_streamc                    s   t  }| jjddI d H }|r)zt|}W n tjjy(   td| w i }| j	j
r5| |}n| |}t j||S )NFdecodezFailed to parse response: )r   LiveServerMessager%   recvr.   loadsdecoderJSONDecodeError
ValueErrorr$   vertexai_LiveServerMessage_from_vertex_LiveServerMessage_from_mldev_from_response)r&   parameter_modelraw_responseresponseresponse_dictr'   r'   r(   r2      s    
zAsyncSession._receivedata_streamrF   c                    sN   |2 z3 d H W }||d}| j |dI d H  tdI d H  q6 |  d S )N)datamimeType)r+   r:   )r-   r;   rC   set)r&   rY   r8   rF   rZ   r+   r'   r'   r(   r>      s   
zAsyncSession._send_loopfrom_objectc                 C      i }t |dgd urt|dgt| jt |dg t |dgd ur,t|dgt |dg t |dgd ur?t|dgt |dg |S N	modelTurn
model_turnturnCompleter4   interrupted)getvsetvr   r$   r&   r]   	to_objectr'   r'   r(   _LiveServerContent_from_mldev      
z*AsyncSession._LiveServerContent_from_mldevc                 C   .   i }t |dgd urt|dgt |dg |S NfunctionCallsfunction_callsrd   re   rf   r'   r'   r(   _LiveToolCall_from_mldev      
z%AsyncSession._LiveToolCall_from_mldevc                 C   rj   rk   rn   rf   r'   r'   r(   _LiveToolCall_from_vertex  rp   z&AsyncSession._LiveToolCall_from_vertexc                 C      i }t |dgd urt|dg| t |dg t |dgd ur.t|dg| t |dg t |dgd urAt|dgt |dg |S NserverContentr3   toolCall	tool_calltoolCallCancellationtool_call_cancellation)rd   re   rh   ro   rf   r'   r'   r(   rS     s,   

z*AsyncSession._LiveServerMessage_from_mldevc                 C   r^   r_   )rd   re   r   r$   rf   r'   r'   r(   _LiveServerContent_from_vertex3  ri   z+AsyncSession._LiveServerContent_from_vertexc                 C   rr   rs   )rd   re   ry   rq   rf   r'   r'   r(   rR   G  s,   

z+AsyncSession._LiveServerMessage_from_vertexc                    sz  t |tr	|g}nRt |tr+d|v r+t |d tr't|d d}||d< |g}n0t |tjr?t|j	d|_	|g}nt |tr[d|v r[d|v r[ j
jsXd|vrXtt|g}t |trtdd |D r j
jsz|D ]
}d|vryttqodd	|ii}|S t |trtd
d |D ri  j
jr fddt j
|D }n fddt j
|D }d||di}|S t |trtdd |D rntdd |D rdd |D }ntdt| d| ddd|ii}|S t |trd|v rd|i}|S t |tjr2d|jddi}t |d d d d tr0dd |d d D |d d< |S t |tjrCd|jddi}|S t |tjrd j
jsZ|jd jsZttd|jddi}|S t |tjr j
jsx|jsxttdd	|jddgii}|S t |trt |d tjr j
js|d jsttdd	dd |D ii}|S tdt| d| d)NrZ   utf-8namerW   idc                 s   s*    | ]}t |tod |v od|v V  qdS )r{   rW   N
isinstancedict.0cr'   r'   r(   	<genexpr>  s    
z5AsyncSession._parse_client_message.<locals>.<genexpr>tool_responsefunction_responsesc                 s   s    | ]}t |tV  qd S r#   )r~   strr   r'   r'   r(   r     s    c                       g | ]	}t  j|qS r'   )r   r$   r   itemr&   rg   r'   r(   
<listcomp>      z6AsyncSession._parse_client_message.<locals>.<listcomp>c                    r   r'   )r   r$   r   r   r'   r(   r     r   client_content)turnsr4   c                 s   s"    | ]}t |tod |v V  qdS )rZ   Nr}   r   br'   r'   r(   r     s     c                 s   s    | ]	}t |tjV  qd S r#   )r~   r   Blobr   r'   r'   r(   r     s    c                 S      g | ]}|j d dqS Texclude_none
model_dumpr   r'   r'   r(   r     s    zUnsupported input type "z" or input content ""realtime_inputmedia_chunkscontentTr   r   c                 S   s*   g | ]}t |d  d|d dqS )rZ   rz   r8   )rZ   r8   )base64	b64encoderJ   r   r'   r'   r(   r     s    c                 S   r   r   r   r   r'   r'   r(   r     s    )r~   r   r   bytesr   r   rJ   r   r   rZ   r$   rQ   rP   _FUNCTION_RESPONSE_REQUIRES_IDr   anyt
t_contentstypeLiveClientRealtimeInputr   LiveClientContentLiveClientToolResponser   r|   FunctionResponse)r&   r+   r*   decoded_datar   r0   contentsr'   r   r(   r,   c  s   
O
A
40
*!


z"AsyncSession._parse_client_messagec                    s   | j  I d H  d S r#   )r%   close)r&   r'   r'   r(   r     s   zAsyncSession.closeN)F)(__name__
__module____qualname____doc__r   r   r   r)   r   r   ContentListUnionContentListUnionDictLiveClientContentOrDictLiveClientRealtimeInputOrDictLiveClientToolResponseOrDictFunctionResponseOrDictr   r   boolr-   r   rK   r6   r   r   rH   r2   r;   r<   r>   r   objectrh   ro   rq   rS   ry   rR   r,   r   r'   r'   r'   r(   r    A   s    
$
>












(
wr    c                	   @   sx   e Zd ZdZ	ddedeej fddZ	ddedeej fddZ	e
d	ejdd
dedeej defddZdS )	AsyncLivez+AsyncLive. The live module is experimental.Nmodelconfigc                    s  t |tjr|jdd}n|}i t|dgd ur)tdgt jt|dg t|dgd urJtdgd urB|d d d< nd|d id< t|dgd urtdgd urot jt	
 jt|dgd d< ndt jt	
 jt|dgid< t|d	gd urtd
gt jt	 jt|d	g t|dgd urtdg fddt|dgD  dd|ii}|d  |S )NTr   generation_configgenerationConfigresponse_modalitiesresponseModalitiesspeech_configspeechConfigsystem_instructionsystemInstructiontoolsc                    r   r'   )r   r$   r   r   r'   r(   r   "  r   z1AsyncLive._LiveSetup_to_mldev.<locals>.<listcomp>setupr   )r~   r   LiveConnectConfigr   rd   re   r   r$   r   r   t_speech_configr   	t_contentupdater&   r   r   r]   return_valuer'   r   r(   _LiveSetup_to_mldev  sz   
		

	zAsyncLive._LiveSetup_to_mldevc                    s  t |tjr|jdd}n|}i t|dgd ur)tdgt jt|dg t|dgd urKtdgd urB|d d d< n&d|d id< ntdgd ur^d ddgi n
dddgii t|dgd urtdgd urt	 jt
 jt|dgd d	< nd	t	 jt
 jt|dgid< t|d
gd urtdgt jt
 jt|d
g t|dgd urtdg fddt|dgD  dd|ii}|d  |S )NTr   r   r   r   r   AUDIOr   r   r   r   r   c                    r   r'   )r   r$   r   r   r'   r(   r   t  r   z2AsyncLive._LiveSetup_to_vertex.<locals>.<listcomp>r   r   )r~   r   r   r   rd   re   r   r$   r   r   r   r   r   r   r   r'   r   r(   _LiveSetup_to_vertex,  s   
	
		
	zAsyncLive._LiveSetup_to_vertexz?The live API is experimental and may change in future versions.)r   r1   c             	   C  s  | j  }| j jr6| j j}| j jd }| d| d| }| j jd }t| j |}t| j||d}	nXt	j
jdgd\}
}t	j
jj }|
| |
j}dd	|d
}| j jd }| d| d}| j j}| j j}t| j |}|drd| d| d| }t| j||d}	t||d4 I dH (}||	I dH  t|jddI dH  t| j |dV  W d  I dH  dS 1 I dH sw   Y  dS )a  Connect to the live server.

    The live module is experimental.

    Usage:

    .. code-block:: python

      client = genai.Client(api_key=API_KEY)
      config = {}
      async with client.aio.live.connect(model='...', config=config) as session:
        await session.send(input='Hello world!', end_of_turn=True)
        async for message in session.receive():
          print(message)
    api_versionz!/ws/google.ai.generativelanguage.z+.GenerativeService.BidiGenerateContent?key=headers)r   r   z.https://www.googleapis.com/auth/cloud-platform)scopeszapplication/jsonz	Bearer {})zContent-TypeAuthorizationz/ws/google.cloud.aiplatform.z#.LlmBidiService/BidiGenerateContentzpublishers/z	projects/z/locations//)additional_headersNFrI   )r!   r"   )r$   _websocket_base_urlapi_key_http_optionsr   t_modelr.   r/   r   googleauthdefault	transportrequestsRequestrefreshtokenformatlocationproject
startswithr   r   r-   logginginforL   r    )r&   r   r   base_urlr   versionurir   transformed_modelrequestcreds_auth_reqbearer_tokenr   r   wsr'   r'   r(   r   ~  sH   



.zAsyncLive.connectr#   )r   r   r   r   r   r   r   LiveConnectConfigOrDictr   r   r   
contextlibasynccontextmanagerr    r   r'   r'   r'   r(   r     s4    
K
Rr   )4r   r;   r   r   r.   r   typingr   r   r   r   google.authr   
websocketsr    r   r	   r
   r   r   r   r   r$   r   r   r   rd   r   re   modelsr   r   r   r   r   r   r   r   r   r   r   r   websockets.asyncio.clientr   r   ModuleNotFoundErrorwebsockets.clientr   r    
BaseModuler   r'   r'   r'   r(   <module>   sZ      !