o
    %g0                  
   @   s(  d 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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 ejdkrFdd	lmZ nejZd
ZedZdejdejfddZ	d/dedeej dejfddZ	d/deej deeef fddZdeeef deeef fddZ de	eee!f e"e! e!f de	eee!f e"e! e!f fddZ#dede$fddZ%dededededef
d d!Z&deeef d"edefd#d$Z'd%ej(d&eeef de"ej) fd'd(Z*	d/deej de$fd)d*Z+	d/deej de,fd+d,Z-	d/deej de$fd-d.Z.dS )0zNExtra utils depending on types that are shared between sync and async modules.    N)AnyCallableDictOptionalUnionget_args
get_origin   )_common)errors)types)   
   )	UnionTyper   zgoogle_genai.modelsconfigreturnc                 C   s   t | trtjdi | S | S )N )
isinstancedictr   GenerateContentConfigr   r   r   [/mnt/skqttb/ctump_chatbot/chatbot/lib/python3.10/site-packages/google/genai/_extra_utils.py%_create_generate_content_config_model(   s   
r   srcc                 C   s   t j|djp
t  }d}|jst }d| |_|jsO| dr3| 	dr3| dd  d|_|S | drH|p=t }|  d	| |_|S t
d
|  |S )z4Formats the destination uri based on the source uri.r   Ngenai_batch_job_zgs://z.jsonliz/destzbq://_dest_zUnsupported source: )r   _CreateBatchJobParametersr   CreateBatchJobConfigdisplay_namer
   timestamped_unique_namedest
startswithendswith
ValueError)r   r   unique_namer   r   r   format_destination1   s"   
r%   c                 C   sb   i }| s|S t | }|jr/|jD ]}t|r.t|r)td|j d|j d|||j< q|S )z'Returns a function map from the config.z	Function zh is a coroutine function, which is not supported for automatic function calling. Please manually invoke z to get the function response.)r   toolscallableinspectiscoroutinefunctionr   UnsupportedFunctionError__name__)r   function_mapconfig_modeltoolr   r   r   get_function_mapO   s    



r/   argsc                 C   s   dd |   D S )z:Converts float values in dict with no decimal to integers.c                 S      i | ]	\}}|t |qS r   ,convert_number_values_for_function_call_args.0keyvaluer   r   r   
<dictcomp>h       zEconvert_number_values_for_dict_function_call_args.<locals>.<dictcomp>)itemsr0   r   r   r   1convert_number_values_for_dict_function_call_argsd   s   r<   c                 C   sR   t | tr|  rt| S t | trdd |  D S t | tr'dd | D S | S )z2Converts float values with no decimal to integers.c                 S   r1   r   r2   r4   r   r   r   r8   u   r9   z@convert_number_values_for_function_call_args.<locals>.<dictcomp>c                 S   s   g | ]}t |qS r   r2   )r5   r7   r   r   r   
<listcomp>z   s    z@convert_number_values_for_function_call_args.<locals>.<listcomp>)r   float
is_integerintr   r:   listr;   r   r   r   r3   n   s   

r3   
annotationc                 C   s.   zt | ot| tjW S  ty   Y dS w )NF)r(   isclass
issubclasspydantic	BaseModel	TypeError)rB   r   r   r   is_annotation_pydantic_model   s   rH   r7   
param_name	func_namec                    s  t | tr3t|r3z|di | W S  tjy2 } ztd d  d|  d| d| 
d }~ww t | trOt|tkrOt	|d  fdd| D S t | trmt|tkrmt	|\} fd	d
| 
 D S t|ttfv rt	|D ]/}t	|rt|tu st | |st | trt|rz
t| | W   S  tjy   Y qyw qytd d  d|  d| d	t | tr|tu r| S t | |std d  d|  d| d	| S )NzFailed to parse parameter z for function z> from function call part because function call argument value z- is not compatible with parameter annotation z, due to error r   c                    s   g | ]	}t | qS r   convert_if_exist_pydantic_model)r5   item)rJ   	item_typerI   r   r   r=      s    z3convert_if_exist_pydantic_model.<locals>.<listcomp>c                    s    i | ]\}}|t | qS r   rK   )r5   kv)rJ   rI   
value_typer   r   r8      s    z3convert_if_exist_pydantic_model.<locals>.<dictcomp>z- cannot be converted to parameter annotation .r   )r   r   rH   rE   ValidationErrorr    UnknownFunctionCallArgumentErrorrA   r   r   r:   r   r   rL   r@   r>   )r7   rB   rI   rJ   e_argr   )rJ   rN   rI   rQ   r   rL      s   

rL   function_to_invokec                 C   s   t |}|j}i }|j D ]\}}|| v r#t| | |j||||< qz|di |W S  tyI } zt	d| d| d|  d| d }~ww )NzFailed to invoke function z with converted arguments z, from model returned function call argument z because of error r   )
r(   	signaturer+   
parametersr:   rL   rB   	Exceptionr   FunctionInvocationError)r0   rX   rY   rJ   converted_argsrI   paramrU   r   r   r   invoke_function_from_dict_args   s4   
r_   responser,   c           
      C   s   g }| j durot| j d jtjro| j d jjduro| j d jjD ]M}|js'q!|jj}|durn|jjdurn|| }t	|jj}z	dt
||i}W n ty` } zdt|i}W Y d}~nd}~ww tjj||d}	||	 q!|S )z6Returns the function response parts from the response.Nr   resulterror)namer`   )
candidatesr   contentr   Contentpartsfunction_callrc   r0   r<   r_   r[   strPartfrom_function_responseappend)
r`   r,   func_response_partspartrJ   funcr0   func_responserU   func_response_partr   r   r   get_function_response_parts   s6   


rr   c                 C   s   | sdS t | }|r(|jr(|jjdur(t|jjdkr(td|jj d dS |jr1|jjdu r3dS |jjrV|jjdurVd|jjv rVt|jjdkrVtd|jj d	 |jjS )
z6Returns whether automatic function calling is enabled.FNr   z6max_remote_calls in automatic_function_calling_config zu is less than or equal to 0. Disabling automatic function calling. Please set max_remote_calls to a positive integer.Tmaximum_remote_callsz`automatic_function_calling.disable` is set to `True`. And `automatic_function_calling.maximum_remote_calls` is a positive number a9  . Disabling automatic function calling. If you want to enable automatic function calling, please set `automatic_function_calling.disable` to `False` or leave it unset, and set `automatic_function_calling.maximum_remote_calls` to a positive integer or leave `automatic_function_calling.maximum_remote_calls` unset.)r   automatic_function_callingrs   r@   loggerwarningdisablemodel_fields_setr   r-   r   r   r   should_disable_afc  sH   rz   c                 C   sD   | st S 	 t| rtdt| }|jr|jjd u rt S t|jjS )NzUautomatic function calling is not enabled, but SDK is trying to get max remote calls.)_DEFAULT_MAX_REMOTE_CALLS_AFCrz   r#   r   rt   rs   r@   ry   r   r   r   get_max_remote_calls_afc;  s   r|   c                 C   s$   | sdS t | }|jsdS |jj S )NT)r   rt   ignore_call_historyry   r   r   r   should_append_afc_historyO  s   
r~   )N)/__doc__r(   loggingsystypingr   r   r   r   r   r   r   rE    r
   r   r   version_infor   _UnionGenericAliasr{   	getLoggerru   GenerateContentConfigOrDictr   r   ri   CreateBatchJobConfigOrDictr   r%   r   r/   r<   objectrA   r3   boolrH   rL   r_   GenerateContentResponserj   rr   rz   r@   r|   r~   r   r   r   r   <module>   s   $











>



#
8
