U
    Zh4                     @  s  d dl mZ d dlZd dlmZmZmZmZ d dlm	Z	m
Z
 d dlmZmZmZ d dlZd dlZddlmZ ddlmZ dd	lmZmZmZmZmZmZ dd
lmZmZmZ ddl m!Z! ddl"m#Z#m$Z$ ddl%m&Z&m'Z' ddl(m)Z)m*Z* ddl+m,Z,m-Z- ddl.m/Z/ ddl0m1Z1m2Z2 ddl3mZ ddl4m5Z5 ddl6m7Z7 ddgZ8G dd de#Z9G dd de$Z:G dd dZ;G dd dZ<G dd dZ=G dd dZ>dS )     )annotationsN)DictListIterableOptional)UnionLiteral)FutureThreadPoolExecutoras_completed   )_legacy_response)FileChunkingStrategyParam)	NOT_GIVENBodyQueryHeadersNotGiven	FileTypes)is_givenmaybe_transformasync_maybe_transform)cached_property)SyncAPIResourceAsyncAPIResource)to_streamed_response_wrapper"async_to_streamed_response_wrapper)SyncCursorPageAsyncCursorPage)AsyncPaginatormake_request_options)
FileObject)file_batch_create_paramsfile_batch_list_files_params)VectorStoreFile)VectorStoreFileBatchFileBatchesAsyncFileBatchesc                   @  sB  e Zd ZeddddZeddddZeeddded	d
ddddddddd	ddZdddedd
d
ddddddddZdddedd
d
ddddddddZ	eedd
ddddddd Z
eeeeeddded!	d
d
d"d"d#dd$ddddd%d&d'd(Zed)d
d
ddd*d+d,Zd-g eed.d
d/d0ddddd1d2d3ZdS )4r&   FileBatchesWithRawResponsereturnc                 C  s   t | S a  
        This property can be used as a prefix for any HTTP method call to return
        the raw response object instead of the parsed content.

        For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
        )r(   self r.   O/tmp/pip-unpacked-wheel-693nel7n/openai/resources/vector_stores/file_batches.pywith_raw_response    s    zFileBatches.with_raw_response FileBatchesWithStreamingResponsec                 C  s   t | S z
        An alternative to `.with_raw_response` that doesn't eagerly read the response body.

        For more information, see https://www.github.com/openai/openai-python#with_streaming_response
        )r1   r,   r.   r.   r/   with_streaming_response*   s    z#FileBatches.with_streaming_responseN
attributeschunking_strategyextra_headersextra_query
extra_bodytimeoutstr	List[str]7Optional[Dict[str, Union[str, float, bool]]] | NotGiven$FileChunkingStrategyParam | NotGivenHeaders | NoneQuery | NoneBody | None'float | httpx.Timeout | None | NotGivenr%   	vector_store_idfile_idsr5   r6   r7   r8   r9   r:   r*   c          	   	   C  sX   |st d|ddi|pi }| jd| dt|||dtjt||||dtdS )	  
        Create a vector store file batch.

        Args:
          file_ids: A list of [File](https://platform.openai.com/docs/api-reference/files) IDs that
              the vector store should use. Useful for tools like `file_search` that can access
              files.

          attributes: Set of 16 key-value pairs that can be attached to an object. This can be useful
              for storing additional information about the object in a structured format, and
              querying for objects via API or the dashboard. Keys are strings with a maximum
              length of 64 characters. Values are strings with a maximum length of 512
              characters, booleans, or numbers.

          chunking_strategy: The chunking strategy used to chunk the file(s). If not set, will use the `auto`
              strategy. Only applicable if `file_ids` is non-empty.

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        >Expected a non-empty value for `vector_store_id` but received OpenAI-Betaassistants=v2/vector_stores//file_batchesrE   r5   r6   r7   r8   r9   r:   bodyoptionscast_to)
ValueError_postr   r"   FileBatchCreateParamsr    r%   	r-   rD   rE   r5   r6   r7   r8   r9   r:   r.   r.   r/   create3   s(    '
   zFileBatches.createrM   batch_idrD   r7   r8   r9   r:   r*   c                C  s\   |st d||s$t d|ddi|p0i }| jd| d| t||||dtdS )	X  
        Retrieves a vector store file batch.

        Args:
          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        rG   7Expected a non-empty value for `batch_id` but received rH   rI   rJ   /file_batches/rM   rP   rQ   rR   _getr    r%   r-   rX   rD   r7   r8   r9   r:   r.   r.   r/   retrievem   s       zFileBatches.retrievec                C  s^   |st d||s$t d|ddi|p0i }| jd| d| dt||||dtd	S )
  Cancel a vector store file batch.

        This attempts to cancel the processing of
        files in this batch as soon as possible.

        Args:
          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        rG   rZ   rH   rI   rJ   r[   /cancelrM   r\   rR   rS   r    r%   r_   r.   r.   r/   cancel   s       zFileBatches.cancelpoll_interval_msr6   int | NotGivenrD   rE   rf   r6   r*   c                C  s"   | j |||d}| j|j||dS )ICreate a vector store batch and poll until all files have been processed.rD   rE   r6   rD   rf   rV   pollidr-   rD   rE   rf   r6   batchr.   r.   r/   create_and_poll   s    	zFileBatches.create_and_poll	afterbeforefilterlimitorderr7   r8   r9   r:   str | NotGivenGLiteral[('in_progress', 'completed', 'failed', 'cancelled')] | NotGiven#Literal[('asc', 'desc')] | NotGivenzSyncCursorPage[VectorStoreFile]rX   rD   rs   rt   ru   rv   rw   r7   r8   r9   r:   r*   c       
         C  sz   |st d||s$t d|ddi|p0i }| jd| d| dtt t||	|
|t|||||dtjd	td
S a  
        Returns a list of vector store files in a batch.

        Args:
          after: A cursor for use in pagination. `after` is an object ID that defines your place
              in the list. For instance, if you make a list request and receive 100 objects,
              ending with obj_foo, your subsequent call can include after=obj_foo in order to
              fetch the next page of the list.

          before: A cursor for use in pagination. `before` is an object ID that defines your place
              in the list. For instance, if you make a list request and receive 100 objects,
              starting with obj_foo, your subsequent call can include before=obj_foo in order
              to fetch the previous page of the list.

          filter: Filter by file status. One of `in_progress`, `completed`, `failed`, `cancelled`.

          limit: A limit on the number of objects to be returned. Limit can range between 1 and
              100, and the default is 20.

          order: Sort order by the `created_at` timestamp of the objects. `asc` for ascending
              order and `desc` for descending order.

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        rG   rZ   rH   rI   rJ   r[   z/files)rs   rt   ru   rv   rw   )r7   r8   r9   r:   query)pagerP   model)rR   _get_api_listr   r$   r    r   r#   FileBatchListFilesParamsr-   rX   rD   rs   rt   ru   rv   rw   r7   r8   r9   r:   r.   r.   r/   
list_files   s2    /zFileBatches.list_filesrf   rX   rD   rf   r*   c                C  s   ddi}t |rt||d< | jj|||d}| }|jjdkr|t |sl|jd}|dk	rht	|}nd}| 
|d  q|S )	Wait for the given file batch to be processed.

        Note: this will return even if one of the files failed to process, you need to
        check batch.file_counts.failed_count to handle this case.
        X-Stainless-Poll-Helpertrue X-Stainless-Custom-Poll-IntervalrD   r7   r   openai-poll-after-msN  r   r;   r0   r`   parseZfile_countsZin_progressheadersgetintZ_sleepr-   rX   rD   rf   r   responserp   Zfrom_headerr.   r.   r/   rm     s$    
zFileBatches.poll   max_concurrencyrE   rf   r6   Iterable[FileTypes]r   rD   filesr   rE   rf   r6   r*   c             	     s~   g }t |d  fdd|D }W 5 Q R X t|D ]"}	|	 }
|
rJ|
||	  q6j||dd |D ||d}|S )  Uploads the given files concurrently and then creates a vector store file batch.

        If you've already uploaded certain files that you want to include in this batch
        then you can pass their IDs through the `file_ids` argument.

        By default, if any file upload fails then an exception will be eagerly raised.

        The number of concurrency uploads is configurable using the `max_concurrency`
        parameter.

        Note: this method only supports `asyncio` or `trio` as the backing async
        runtime.
        )max_workersc                   s"   g | ]} j jjj|d dqS )
assistantsfilepurpose)Zsubmit_clientr   rV   .0r   executorr-   r.   r/   
<listcomp>W  s   z/FileBatches.upload_and_poll.<locals>.<listcomp>c                 s  s   | ]}|j V  qd S Nrn   r   fr.   r.   r/   	<genexpr>i  s     z.FileBatches.upload_and_poll.<locals>.<genexpr>rD   rE   rf   r6   )r
   r   	exceptionappendresultrq   )r-   rD   r   r   rE   rf   r6   resultsZfuturesfutureexcrp   r.   r   r/   upload_and_poll=  s"    	zFileBatches.upload_and_poll__name__
__module____qualname__r   r0   r3   r   rV   r`   rd   rq   r   rm   r   r.   r.   r.   r/   r&      sR   	"A,,(O*c                   @  sB  e Zd ZeddddZeddddZeeddded	d
ddddddddd	ddZdddedd
d
ddddddddZdddedd
d
ddddddddZ	eedd
ddddddd Z
eeeeeddded!	d
d
d"d"d#dd$ddddd%d&d'd(Zed)d
d
ddd*d+d,Zd-g eed.d
d/d0ddddd1d2d3ZdS )4r'   AsyncFileBatchesWithRawResponser)   c                 C  s   t | S r+   )r   r,   r.   r.   r/   r0   q  s    z"AsyncFileBatches.with_raw_response%AsyncFileBatchesWithStreamingResponsec                 C  s   t | S r2   )r   r,   r.   r.   r/   r3   {  s    z(AsyncFileBatches.with_streaming_responseNr4   r;   r<   r=   r>   r?   r@   rA   rB   r%   rC   c          	   	     sd   |st d|ddi|pi }| jd| dt|||dtjI dH t||||dtd	I dH S )
rF   rG   rH   rI   rJ   rK   rL   NrM   rN   )rR   rS   r   r"   rT   r    r%   rU   r.   r.   r/   rV     s(    '
   zAsyncFileBatches.createrM   rW   c                  sb   |st d||s$t d|ddi|p0i }| jd| d| t||||dtdI d	H S )
rY   rG   rZ   rH   rI   rJ   r[   rM   r\   Nr]   r_   r.   r.   r/   r`     s       zAsyncFileBatches.retrievec                  sd   |st d||s$t d|ddi|p0i }| jd| d| dt||||dtd	I d
H S )ra   rG   rZ   rH   rI   rJ   r[   rb   rM   r\   Nrc   r_   r.   r.   r/   rd     s       zAsyncFileBatches.cancelre   rg   rh   c                  s.   | j |||dI dH }| j|j||dI dH S )ri   rj   Nrk   rl   ro   r.   r.   r/   rq   
  s    	z AsyncFileBatches.create_and_pollrr   rx   ry   rz   zAAsyncPaginator[VectorStoreFile, AsyncCursorPage[VectorStoreFile]]r{   c       
         C  sz   |st d||s$t d|ddi|p0i }| jd| d| dtt t||	|
|t|||||dtjd	td
S r|   )rR   r   r   r$   r    r   r#   r   r   r.   r.   r/   r     s2    /zAsyncFileBatches.list_filesr   r   c                  s   ddi}t |rt||d< | jj|||dI dH }| }|jjdkrt |sr|jd}|dk	rnt	|}nd}| 
|d I dH  q|S )	r   r   r   r   r   Nr   r   r   r   r   r.   r.   r/   rm   i  s$    
zAsyncFileBatches.pollr   r   r   r   r   c             
     s  g t  }|dkr\ddddfdd t| fdd	|D }tj| I d
H  n~|dkrdd
l}	ddddfdd}
|	|}|	 4 I d
H }|D ]}||
|| qW 5 Q I d
H R X nt	d| dj
||dd D ||dI d
H }|S )r   asynciozasyncio.Semaphorer   None)	semaphorer   r*   c              
     sD   | 4 I d H (  j jj|ddI d H }| W 5 Q I d H R X d S Nr   r   r   r   rV   r   )r   r   file_objr-   uploaded_filesr.   r/   asyncio_upload_file  s    z=AsyncFileBatches.upload_and_poll.<locals>.asyncio_upload_filec                   s   g | ]} |qS r.   r.   r   )r   r   r.   r/   r     s     z4AsyncFileBatches.upload_and_poll.<locals>.<listcomp>Ntrior   ztrio.CapacityLimiter)limiterr   r*   c              
     sD   | 4 I d H (  j jj|ddI d H }| W 5 Q I d H R X d S r   r   )r   r   r   r   r.   r/   trio_upload_file  s    z:AsyncFileBatches.upload_and_poll.<locals>.trio_upload_filezAsync runtime z8 is not supported yet. Only asyncio or trio is supportedc                 s  s   | ]}|j V  qd S r   r   r   r.   r.   r/   r     s     z3AsyncFileBatches.upload_and_poll.<locals>.<genexpr>r   )sniffioZcurrent_async_libraryr   	SemaphoreZgatherr   ZCapacityLimiterZopen_nurseryZ
start_soonRuntimeErrorrq   )r-   rD   r   r   rE   rf   r6   Zasync_libraryZtasksr   r   r   Znurseryr   rp   r.   )r   r-   r   r   r/   r     s0    

"
z AsyncFileBatches.upload_and_pollr   r.   r.   r.   r/   r'   p  sR   	"A,,(O*c                   @  s   e Zd ZdddddZdS )r(   r&   r   file_batchesr*   c                 C  sB   || _ t|j| _t|j| _t|j| _t|j| _d S r   )_file_batchesr   Zto_raw_response_wrapperrV   r`   rd   r   r-   r   r.   r.   r/   __init__  s    z#FileBatchesWithRawResponse.__init__Nr   r   r   r   r.   r.   r.   r/   r(     s   r(   c                   @  s   e Zd ZdddddZdS )r   r'   r   r   c                 C  sB   || _ t|j| _t|j| _t|j| _t|j| _d S r   )r   r   Zasync_to_raw_response_wrapperrV   r`   rd   r   r   r.   r.   r/   r     s    z(AsyncFileBatchesWithRawResponse.__init__Nr   r.   r.   r.   r/   r     s   r   c                   @  s   e Zd ZdddddZdS )r1   r&   r   r   c                 C  s:   || _ t|j| _t|j| _t|j| _t|j| _d S r   )r   r   rV   r`   rd   r   r   r.   r.   r/   r     s    z)FileBatchesWithStreamingResponse.__init__Nr   r.   r.   r.   r/   r1     s   r1   c                   @  s   e Zd ZdddddZdS )r   r'   r   r   c                 C  s:   || _ t|j| _t|j| _t|j| _t|j| _d S r   )r   r   rV   r`   rd   r   r   r.   r.   r/   r     s    z.AsyncFileBatchesWithStreamingResponse.__init__Nr   r.   r.   r.   r/   r     s   r   )?
__future__r   r   typingr   r   r   r   Ztyping_extensionsr   r   concurrent.futuresr	   r
   r   Zhttpxr    r   typesr   _typesr   r   r   r   r   r   _utilsr   r   r   _compatr   Z	_resourcer   r   	_responser   r   Z
paginationr   r   Z_base_clientr   r    Ztypes.file_objectr!   Ztypes.vector_storesr"   r#   Z"types.file_chunking_strategy_paramZ%types.vector_stores.vector_store_filer$   Z+types.vector_stores.vector_store_file_batchr%   __all__r&   r'   r(   r   r1   r   r.   r.   r.   r/   <module>   s>      S  j