o
    ThG                     @   s  d dl Z 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eeef ef deeeef  deeef fddZ	d5deeeef ef deeeef  deeef fddZ	d5deeeef ef deeeef  deeef fddZ	d5dedeeeef ef deeeef  deeef fddZ 	d5dedeeeef ef deeeef  deeef fddZ!	d5dedeeeef ef deeeef  deeef fddZ"	d5deeeef ef deeeef  deeef fdd Z#	d5deeeef ef deeeef  deeef fd!d"Z$	d5dedeeeef ef deeeef  deeef fd#d$Z%	d5deeeef ef deeeef  deeef fd%d&Z&	d5deeeef ef deeeef  deeef fd'd(Z'	d5deeeef ef deeeef  deeef fd)d*Z(	d5deeeef ef deeeef  deeef fd+d,Z)	d5deeeef ef deeeef  deeef fd-d.Z*	d5deeeef ef deeeef  deeef 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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   	to_object r"   W/home/air/segue/gemini/backup/venv/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_vertex9   s    

r*   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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   r1   
api_clientc              	   C   s   i }t |dgd urt|dgt| t |dg t |dgd ur2t|dgttt |dg| t |dgd urHt|dgtt |dg| |S )NmodelsrcinputConfigconfig)r   r   r/   t_modelr$   t_batch_job_sourcer1   r2   r   r   r!   r"   r"   r#   #_CreateBatchJobParameters_to_vertexg   s0   r:   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   r/   t_batch_job_namer9   r"   r"   r#    _GetBatchJobParameters_to_vertex      rA   c              	   C   r;   r<   r?   r9   r"   r"   r#   #_CancelBatchJobParameters_to_vertex   rB   rC   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   
rJ   c                 C   s4   i }t | dgd urt|dgtt | dg| |S )Nr6   )r   r   rJ   r    r"   r"   r#   "_ListBatchJobsParameters_to_vertex   s   rK   c              	   C   r;   r<   r?   r9   r"   r"   r#   #_DeleteBatchJobParameters_to_vertex   rB   rL   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   rP   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   rR   c                 C   rQ   )	Nr%   r   r&   r'   r   r(   r)   r   r   r    r"   r"   r#    _BatchJobDestination_from_vertex  s    rS   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rQt|dgtt | dg| t | dgd urdt|dgt | dg t | dgd urwt|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   rP   rR   rS   r    r"   r"   r#   _BatchJob_from_vertex*  sP   r^   c                    sb   i  t | dgd urt dgt | dg t | dgd ur/t 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#   
<listcomp>o  s    z6_ListBatchJobsResponse_from_vertex.<locals>.<listcomp>r   )r   r   r"   re   r#   "_ListBatchJobsResponse_from_vertexc  s   

	rg   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=   donerU   )r   r   rP   r    r"   r"   r#   _DeleteResourceJob_from_vertexx  s   ri   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sdd	nt|
j}| jjrrt|}t jj|| d
}| j| |S Nr3   r4   r6   6This method is only supported in the Vertex AI client.r>   ra   rE   ?r6   post responsekwargs)r   _CreateBatchJobParameters_api_clientvertexai
ValueErrorr:   get
format_mapr   popr6   http_optionsr   convert_to_dictencode_unserializable_typesrequestbodyjsonloadsr^   BatchJob_from_response
model_dump_verify_responseselfr3   r4   r6   parameter_modelrequest_dictrequest_url_dictpathquery_paramsr}   rt   response_dictreturn_valuer"   r"   r#   _create  sF   




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scd
nt|	j}
| jjrqt|
}
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   ro   r>   batchPredictionJobs/{name}rE   rp   r6   Nrz   rr   rs   )r   _GetBatchJobParametersrw   rx   ry   rA   rz   r{   r   r|   r6   r}   r   r~   r   r   r   r   r   r^   r   r   r   r   r   r=   r6   r   r   r   r   r   r}   rt   r   r   r"   r"   r#   rz     s@   




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   ro   r>   !batchPredictionJobs/{name}:cancelrE   rp   r6   Nrq   )r   _CancelBatchJobParametersrw   rx   ry   rC   rz   r{   r   r|   r6   r}   r   r~   r   r   
r   r=   r6   r   r   r   r   r   r}   rt   r"   r"   r#   cancel  s4   




zBatches.cancelc                C   s   t j|d}| jjstdt|}|d}|rd|}nd}|d}|r1| dt| }|	dd  d }|j
d urH|j
jd urH|j
j}t|}t|}| jd|||}|js`d	nt|j}	| jjrnt|	}	t jj|	| d
}
| j|
 |
S Nrl   ro   r>   ra   rE   rp   r6   rz   rr   rs   )r   _ListBatchJobsParametersrw   rx   ry   rK   rz   r{   r   r|   r6   r}   r   r~   r   r   r   r   r   rg   ListBatchJobsResponser   r   r   r   r6   r   r   r   r   r   r}   rt   r   r   r"   r"   r#   _listH  s:   




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   ro   r>   r   rE   rp   r6   Ndeleterr   rs   )r   _DeleteBatchJobParametersrw   rx   ry   rL   rz   r{   r   r|   r6   r}   r   r~   r   r   r   r   r   ri   DeleteResourceJobr   r   r   r   r"   r"   r#   r   w  sD   




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-2.0-flash-001",
          src="gs://path/to/input/data",
      )
      print(batch_job.state)
    rn   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}")
    Nrb   rl   )r   ListBatchJobsConfigr   r   r   r6   r"   r"   r#   list  s   
zBatches.list)__name__
__module____qualname__strr   r   CreateBatchJobConfigOrDictr   r   GetBatchJobConfigOrDictrz   CancelBatchJobConfigOrDictr   ListBatchJobsConfigOrDictr   r   DeleteBatchJobConfigOrDictr   r   r   r   r   r"   r"   r"   r#   rk     sp    
:
H
<
3
M
!rk   c                	   @   rj   )AsyncBatchesNrl   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shd	nt|
j}| jjrvt|}t jj|| d
}| j| |S rm   )r   rv   rw   rx   ry   r:   rz   r{   r   r|   r6   r}   r   r~   r   async_requestr   r   r   r^   r   r   r   r   r   r"   r"   r#   r     sH   





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sgd
nt|	j}
| jjrut|
}
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   ro   r>   r   rE   rp   r6   Nrz   rr   rs   )r   r   rw   rx   ry   rA   rz   r{   r   r|   r6   r}   r   r~   r   r   r   r   r   r^   r   r   r   r   r   r"   r"   r#   rz   8  sF   





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   ro   r>   r   rE   rp   r6   Nrq   )r   r   rw   rx   ry   rC   rz   r{   r   r|   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|}|d}|r d|}nd}|d}|r2| dt| }|	dd  d }|j
d urI|j
jd urI|j
j}t|}t|}| jd|||I d H }|jsdd	nt|j}	| jjrrt|	}	t jj|	| d
}
| j|
 |
S r   )r   r   rw   rx   ry   rK   rz   r{   r   r|   r6   r}   r   r~   r   r   r   r   r   rg   r   r   r   r   r   r"   r"   r#   r     s@   





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   ro   r>   r   rE   rp   r6   Nr   rr   rs   )r   r   rw   rx   ry   rL   rz   r{   r   r|   r6   r}   r   r~   r   r   r   r   r   ri   r   r   r   r   r   r"   r"   r#   r     sF   





zAsyncBatches.deletec                   s$   t ||}| j|||dI dH S )a{  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-2.0-flash-001",
          src="gs://path/to/input/data",
      )
    rn   Nr   r   r"   r"   r#   r   3  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}")
    Nrb   rl   )r   r   r   r   r   r"   r"   r#   r   R  s   zAsyncBatches.list)r   r   r   r   r   r   r   r   r   r   rz   r   r   r   r   r   r   r   r   r   r   r   r"   r"   r"   r#   r     sp    
:
J
<
5
M
 r   )N)/r   loggingtypingr   r   r   urllib.parser   rr   r   r   r	   r
   r/   r   rw   r   r   r   r   r   pagersr   r   	getLoggerloggerdictr   objectr$   r*   r1   r:   rA   rC   rJ   rK   rL   rP   rR   rS   r^   rg   ri   
BaseModulerk   r   r"   r"   r"   r#   <module>   s$  








%

















;



  r