o
    %g                  	   @   s@  d dl Z d dlmZmZ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mZ e dZ	d5dedeeef dee defddZ	d5dedeeef dee defddZ	d5dedeeef dee defddZ	d5dedeeef dee defddZ	d5dedeeef dee defddZ	d5dedeeef dee defddZ 	d5dedeeef dee defdd Z!	d5dedeeef dee defd!d"Z"	d5dedeeef dee defd#d$Z#	d5dedeeef dee defd%d&Z$	d5dedeeef dee defd'd(Z%	d5dedeeef dee defd)d*Z&	d5dedeeef dee defd+d,Z'	d5dedeeef dee defd-d.Z(	d5dedeeef dee defd/d0Z)G d1d2 d2ej*Z+G d3d4 d4ej*Z,dS )6    N)AnyOptionalUnion)	urlencode   )_api_module)_common)_extra_utils)_transformers)types)BaseApiClient)get_value_by_path)set_value_by_path)
AsyncPagerPagerzgoogle_genai.batches
api_clientfrom_objectparent_objectreturnc                 C   ~   i }t |dgd urt|dgt |dg t |dgd ur)t|ddgt |dg t |dgd ur=t|ddgt |dg |S )	NformatinstancesFormatgcs_uri	gcsSourceurisbigquery_uribigquerySourceinputUrigetvsetvr   r   r   	to_object r#   V/mnt/skqttb/ctump_chatbot/chatbot/lib/python3.10/site-packages/google/genai/batches.py_BatchJobSource_to_vertex"   s   
r%   c                 C   r   )	Nr   predictionsFormatr   gcsDestinationoutputUriPrefixr   bigqueryDestination	outputUrir   r!   r#   r#   r$   _BatchJobDestination_to_vertex8   s    

r+   c                 C   sd   i }t |dgd urt|dgt |dg t |dgd ur0t|dgt| t| t |dg| |S )Ndisplay_namedisplayNamedestoutputConfig)r   r    r+   tt_batch_job_destinationr!   r#   r#   r$   _CreateBatchJobConfig_to_vertexR   s   
r2   c                 C   s   i }t |dgd urt|dgt| t |dg t |dgd ur4t|dgt| t| t |dg| t |dgd urKt|dgt| t |dg| |S )NmodelsrcinputConfigconfig)r   r    r0   t_modelr%   t_batch_job_sourcer2   r!   r#   r#   r$   #_CreateBatchJobParameters_to_vertexj   s4   
r9   c              	   C   ^   i }t |dgd urt|ddgt| t |dg t |dgd ur-t|dgt |dg |S Nname_urlr6   r   r    r0   t_batch_job_namer!   r#   r#   r$    _GetBatchJobParameters_to_vertex      r@   c              	   C   r:   r;   r>   r!   r#   r#   r$   #_CancelBatchJobParameters_to_vertex   rA   rB   c                 C   s   i }t |dgd urt|ddgt |dg t |dgd ur*t|ddgt |dg t |dgd ur>t|ddgt |dg |S )N	page_size_querypageSize
page_token	pageTokenfilterr   r!   r#   r#   r$   _ListBatchJobsConfig_to_vertex   s   
rI   c                 C   s6   i }t |dgd urt|dgt| t |dg| |S )Nr6   )r   r    rI   r!   r#   r#   r$   "_ListBatchJobsParameters_to_vertex   s   rJ   c              	   C   r:   r;   r>   r!   r#   r#   r$   #_DeleteBatchJobParameters_to_vertex   rA   rK   c                 C   sz   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r;t|dgt |dg |S )Ndetailscodemessager   r!   r#   r#   r$   _JobError_from_vertex   s   rO   c                 C      i }t |dgd urt|dgt |dg t |ddgd ur*t|dgt |ddg t |ddgd ur?t|dgt |ddg |S )	Nr   r   r   r   r   r   r   r   r   r!   r#   r#   r$   _BatchJobSource_from_vertex  s   rQ   c                 C   rP   )	Nr&   r   r'   r(   r   r)   r*   r   r   r!   r#   r#   r$    _BatchJobDestination_from_vertex  s    rR   c                 C   s  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r;t|dgt |dg t |dgd urRt|dgt| t |dg| t |dgd uret|dgt |dg t |dgd urxt|d	gt |dg t |d
gd urt|dgt |d
g t |dgd urt|dgt |dg t |dgd urt|dgt |dg t |dgd urt|dgt| t |dg| t |dgd urt|dgt| t |dg| |S )Nr<   r-   r,   stateerror
createTimecreate_time	startTime
start_timeendTimeend_time
updateTimeupdate_timer3   r5   r4   r/   r.   )r   r    rO   rQ   rR   r!   r#   r#   r$   _BatchJob_from_vertex4  sT   r]   c                    sd   i t |dgd urtdgt |dg t |dgd ur0tdg fddt |dgD  S )NnextPageTokennext_page_tokenbatchPredictionJobs
batch_jobsc                    s   g | ]}t  |qS r#   )r]   ).0itemr   r"   r#   r$   
<listcomp>}  s    
z6_ListBatchJobsResponse_from_vertex.<locals>.<listcomp>r   )r   r   r   r#   rd   r$   "_ListBatchJobsResponse_from_vertexp  s   
	rf   c                 C   s   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r?t|dgt| t |dg| |S )Nr<   donerT   )r   r    rO   r!   r#   r#   r$   _DeleteResourceJob_from_vertex  s   rh   c                	   @      e Zd Zdddededeej dejfddZddd	edeej	 dejfd
dZ
ddd	edeej ddfddZdejdejfddZddd	edeej dejfddZdddededeej dejfddZdddeej deej fddZdS )BatchesNr6   r3   r4   r6   r   c                C   s   t j|||d}| jjstdt| j|}|d}|r#d|}nd}|d}|r5| dt| }|	dd  d }	|j
d urL|j
jd urL|j
j}	t|}t|}| jd|||	}
| jjrit| j|
}
t jj|
| d	}| j| |S 
Nr3   r4   r6   6This method is only supported in the Vertex AI client.r=   r`   rD   ?r6   postresponsekwargs)r   _CreateBatchJobParameters_api_clientvertexai
ValueErrorr9   get
format_mapr   popr6   http_optionsr   convert_to_dictencode_unserializable_typesrequestr]   BatchJob_from_response
model_dump_verify_responseselfr3   r4   r6   parameter_modelrequest_dictrequest_url_dictpathquery_paramsr{   response_dictreturn_valuer#   r#   r$   _create  sD   




zBatches._creater<   c                C      t j||d}| jjstdt| j|}|d}|r"d|}nd}|d}|r4| dt| }|	dd d}|j
durK|j
jdurK|j
j}t|}t|}| jd	|||}	| jjrht| j|	}	t jj|	| d
}
| j|
 |
S )a  Gets a batch job.

    Args:
      name (str): A fully-qualified BatchJob resource name or ID.
        Example: "projects/.../locations/.../batchPredictionJobs/456" or "456"
          when project and location are initialized in the client.

    Returns:
      A BatchJob object that contains details about the batch job.

    Usage:

    .. code-block:: python

      batch_job = client.batches.get(name='123456789')
      print(f"Batch job: {batch_job.name}, state {batch_job.state}")
    r<   r6   rn   r=   batchPredictionJobs/{name}rD   ro   r6   Nrx   rq   )r   _GetBatchJobParametersru   rv   rw   r@   rx   ry   r   rz   r6   r{   r   r|   r}   r~   r]   r   r   r   r   r   r<   r6   r   r   r   r   r   r{   r   r   r#   r#   r$   rx     sB   




zBatches.getc          
      C   s   t j||d}| jjstdt| j|}|d}|r"d|}nd}|d}|r4| dt| }|	dd d}|j
durK|j
jdurK|j
j}t|}t|}| jd	|||}	dS )
a  Cancels a batch job.

    Only available for batch jobs that are running or pending.

    Args:
      name (str): A fully-qualified BatchJob resource name or ID.
        Example: "projects/.../locations/.../batchPredictionJobs/123456789" or
          "123456789" when project and location are initialized in the client.

    Usage:

    .. code-block:: python

      client.batches.cancel(name='123456789')
    r   rn   r=   !batchPredictionJobs/{name}:cancelrD   ro   r6   Nrp   )r   _CancelBatchJobParametersru   rv   rw   rB   rx   ry   r   rz   r6   r{   r   r|   r}   r~   
r   r<   r6   r   r   r   r   r   r{   r   r#   r#   r$   cancel  s4   




zBatches.cancelc          
      C   s   t j|d}| jjstdt| j|}|d}|r!d|}nd}|d}|r3| dt| }|	dd  d }|j
d urJ|j
jd urJ|j
j}t|}t|}| jd|||}| jjrgt| j|}t jj|| d	}	| j|	 |	S 
Nrk   rn   r=   r`   rD   ro   r6   rx   rq   )r   _ListBatchJobsParametersru   rv   rw   rJ   rx   ry   r   rz   r6   r{   r   r|   r}   r~   rf   ListBatchJobsResponser   r   r   
r   r6   r   r   r   r   r   r{   r   r   r#   r#   r$   _listW  sD   




zBatches._listc                C   r   )a  Deletes a batch job.

    Args:
      name (str): A fully-qualified BatchJob resource name or ID.
        Example: "projects/.../locations/.../batchPredictionJobs/456" or "456"
          when project and location are initialized in the client.

    Returns:
      A DeleteResourceJob object that shows the status of the deletion.

    Usage:

    .. code-block:: python

      client.batches.delete(name='123456789')
    r   rn   r=   r   rD   ro   r6   Ndeleterq   )r   _DeleteBatchJobParametersru   rv   rw   rK   rx   ry   r   rz   r6   r{   r   r|   r}   r~   rh   DeleteResourceJobr   r   r   r   r#   r#   r$   r     sF   




zBatches.deletec                C   s   t ||}| j|||dS )a{  Creates a batch job.

    Args:
      model (str): The model to use for the batch job.
      src (str): The source of the batch job. Currently supports GCS URI(-s) or
        BigQuery URI. Example: "gs://path/to/input/data" or
        "bq://projectId.bqDatasetId.bqTableId".
      config (CreateBatchJobConfig): Optional configuration for the batch job.

    Returns:
      A BatchJob object that contains details about the batch job.

    Usage:

    .. code-block:: python

      batch_job = client.batches.create(
          model="gemini-1.5-flash",
          src="gs://path/to/input/data",
      )
      print(batch_job.state)
    rm   r	   format_destinationr   r   r3   r4   r6   r#   r#   r$   create  s   zBatches.createc                C   s(   |du rt  }td| j| j|d|S )a  Lists batch jobs.

    Args:
      config (ListBatchJobsConfig): Optional configuration for the list request.

    Returns:
      A Pager object that contains one page of batch jobs. When iterating over
      the pager, it automatically fetches the next page if there are more.

    Usage:

    .. code-block:: python

      batch_jobs = client.batches.list(config={"page_size": 10})
      for batch_job in batch_jobs:
        print(f"Batch job: {batch_job.name}, state {batch_job.state}")
    Nra   rk   )r   ListBatchJobsConfigr   r   r   r6   r#   r#   r$   list  s   
zBatches.list)__name__
__module____qualname__strr   r   CreateBatchJobConfigOrDictr   r   GetBatchJobConfigOrDictrx   CancelBatchJobConfigOrDictr   ListBatchJobsConfigOrDictr   r   DeleteBatchJobConfigOrDictr   r   r   r   r   r#   r#   r#   r$   rj     sp    
8
H
<
7
M
!rj   c                	   @   ri   )AsyncBatchesNrk   r3   r4   r6   r   c                   s   t j|||d}| jjstdt| j|}|d}|r$d|}nd}|d}|r6| dt| }|	dd  d }	|j
d urM|j
jd urM|j
j}	t|}t|}| jd|||	I d H }
| jjrmt| j|
}
t jj|
| d	}| j| |S rl   )r   rt   ru   rv   rw   r9   rx   ry   r   rz   r6   r{   r   r|   r}   async_requestr]   r   r   r   r   r   r#   r#   r$   r     sF   





zAsyncBatches._creater<   c                      t j||d}| jjstdt| j|}|d}|r#d|}nd}|d}|r5| dt| }|	dd d}|j
durL|j
jdurL|j
j}t|}t|}| jd	|||I dH }	| jjrlt| j|	}	t jj|	| d
}
| j|
 |
S )a  Gets a batch job.

    Args:
      name (str): A fully-qualified BatchJob resource name or ID.
        Example: "projects/.../locations/.../batchPredictionJobs/456" or "456"
          when project and location are initialized in the client.

    Returns:
      A BatchJob object that contains details about the batch job.

    Usage:

    .. code-block:: python

      batch_job = await client.aio.batches.get(name='123456789')
      print(f"Batch job: {batch_job.name}, state {batch_job.state}")
    r   rn   r=   r   rD   ro   r6   Nrx   rq   )r   r   ru   rv   rw   r@   rx   ry   r   rz   r6   r{   r   r|   r}   r   r]   r   r   r   r   r   r#   r#   r$   rx   I  sD   





zAsyncBatches.getc          
         s   t j||d}| jjstdt| j|}|d}|r#d|}nd}|d}|r5| dt| }|	dd d}|j
durL|j
jdurL|j
j}t|}t|}| jd	|||I dH }	dS )
a  Cancels a batch job.

    Only available for batch jobs that are running or pending.

    Args:
      name (str): A fully-qualified BatchJob resource name or ID.
        Example: "projects/.../locations/.../batchPredictionJobs/123456789" or
          "123456789" when project and location are initialized in the client.

    Usage:

    .. code-block:: python

      await client.aio.batches.cancel(name='123456789')
    r   rn   r=   r   rD   ro   r6   Nrp   )r   r   ru   rv   rw   rB   rx   ry   r   rz   r6   r{   r   r|   r}   r   r   r#   r#   r$   r     s6   




zAsyncBatches.cancelc          
         s   t j|d}| jjstdt| j|}|d}|r"d|}nd}|d}|r4| dt| }|	dd  d }|j
d urK|j
jd urK|j
j}t|}t|}| jd|||I d H }| jjrkt| j|}t jj|| d	}	| j|	 |	S r   )r   r   ru   rv   rw   rJ   rx   ry   r   rz   r6   r{   r   r|   r}   r   rf   r   r   r   r   r   r#   r#   r$   r     sF   





zAsyncBatches._listc                   r   )a  Deletes a batch job.

    Args:
      name (str): A fully-qualified BatchJob resource name or ID.
        Example: "projects/.../locations/.../batchPredictionJobs/456" or "456"
          when project and location are initialized in the client.

    Returns:
      A DeleteResourceJob object that shows the status of the deletion.

    Usage:

    .. code-block:: python

      await client.aio.batches.delete(name='123456789')
    r   rn   r=   r   rD   ro   r6   Nr   rq   )r   r   ru   rv   rw   rK   rx   ry   r   rz   r6   r{   r   r|   r}   r   rh   r   r   r   r   r   r#   r#   r$   r     sH   





zAsyncBatches.deletec                   s$   t ||}| j|||dI dH S )aw  Creates a batch job asynchronously.

    Args:
      model (str): The model to use for the batch job.
      src (str): The source of the batch job. Currently supports GCS URI(-s) or
        BigQuery URI. Example: "gs://path/to/input/data" or
        "bq://projectId.bqDatasetId.bqTableId".
      config (CreateBatchJobConfig): Optional configuration for the batch job.

    Returns:
      A BatchJob object that contains details about the batch job.

    Usage:

    .. code-block:: python

      batch_job = await client.aio.batches.create(
          model="gemini-1.5-flash",
          src="gs://path/to/input/data",
      )
    rm   Nr   r   r#   r#   r$   r   D  s   zAsyncBatches.createc                   s0   |du r	t  }td| j| j|dI dH |S )a(  Lists batch jobs asynchronously.

    Args:
      config (ListBatchJobsConfig): Optional configuration for the list request.

    Returns:
      A Pager object that contains one page of batch jobs. When iterating over
      the pager, it automatically fetches the next page if there are more.

    Usage:

    .. code-block:: python

      batch_jobs = await client.aio.batches.list(config={'page_size': 5})
      print(f"current page: {batch_jobs.page}")
      await batch_jobs_pager.next_page()
      print(f"next page: {batch_jobs_pager.page}")
    Nra   rk   )r   r   r   r   r   r#   r#   r$   r   c  s   zAsyncBatches.list)r   r   r   r   r   r   r   r   r   r   rx   r   r   r   r   r   r   r   r   r   r   r   r#   r#   r#   r$   r     sp    
8
H
<
7
M
 r   )N)-loggingtypingr   r   r   urllib.parser    r   r   r	   r
   r0   r   ru   r   r   r   r   r    pagersr   r   	getLoggerloggerdictobjectr%   r+   r2   r9   r@   rB   rI   rJ   rK   rO   rQ   rR   r]   rf   rh   
BaseModulerj   r   r#   r#   r#   r$   <module>   sN  








'

















?



  t