U
    ·ÜŸj   ã                   @  sh  d dl mZ d dlZd dlZd dlZd dlmZ d dlm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mZmZmZ dd	d
œdd„Zddd
œdd„Zddœddddœdd„Zedddœdd„ƒZedddœdd„ƒZdddœdd„Zdd d!œd"d#„Zd$d%d!œd&d'„Zedddœd(d)„ƒZedddœd*d)„ƒZdddœd+d)„Zdd d!œd,d-„Zd$d%d!œd.d/„ZdS )0é    )ÚannotationsN)Úoverload)Ú	TypeGuardé   )Ú	FileTypesÚFileContentÚRequestFilesÚHttpxFileTypesÚBase64FileInputÚHttpxFileContentÚHttpxRequestFiles)Ú
is_tuple_tÚis_mapping_tÚis_sequence_tÚobjectzTypeGuard[Base64FileInput])ÚobjÚreturnc                 C  s   t | tjƒpt | tjƒS ©N)Ú
isinstanceÚioÚIOBaseÚosÚPathLike©r   © r   ú1/tmp/pip-unpacked-wheel-2qj3ofc_/openai/_files.pyÚis_base64_file_input   s    r   zTypeGuard[FileContent]c                 C  s,   t | tƒp*t | tƒp*t | tjƒp*t | tjƒS r   )r   ÚbytesÚtupler   r   r   r   r   r   r   r   Úis_file_content   s    *ÿr   )Úkeyz
str | NoneÚNone)r   r    r   c                C  sF   t | ƒsB|d k	rd|› d�n
d| ›d�}t|› dt| ƒ› d�ƒd ‚d S )NzExpected entry at `ú`zExpected file input `zF to be bytes, an io.IOBase instance, PathLike or a tuple but received zL instead. See https://github.com/openai/openai-python/tree/main#file-uploads)r   ÚRuntimeErrorÚtype)r   r    Úprefixr   r   r   Úassert_is_file_content!   s     ÿþr&   )Úfilesr   c                 C  s   d S r   r   ©r'   r   r   r   Úto_httpx_files)   s    r)   r   r   c                 C  s   d S r   r   r(   r   r   r   r)   -   s    zRequestFiles | NonezHttpxRequestFiles | Nonec                 C  sX   | d krd S t | ƒr(dd„ |  ¡ D ƒ} n,t| ƒr@dd„ | D ƒ} ntdt| ƒ› d�ƒ‚| S )Nc                 S  s   i | ]\}}|t |ƒ“qS r   ©Ú_transform_file©Ú.0r    Úfiler   r   r   Ú
<dictcomp>6   s      z"to_httpx_files.<locals>.<dictcomp>c                 S  s   g | ]\}}|t |ƒf‘qS r   r*   r,   r   r   r   Ú
<listcomp>8   s     z"to_httpx_files.<locals>.<listcomp>zUnexpected file type input z, expected mapping or sequence)r   Úitemsr   Ú	TypeErrorr$   r(   r   r   r   r)   1   s    r   r	   )r.   r   c                 C  sd   t | ƒr0t| tjƒr,t | ¡}|j| ¡ fS | S t| ƒrX| d t	| d ƒf| dd … ˜S t
dƒ‚d S ©Nr   r   é   zCExpected file types input to be a FileContent type or to be a tuple)r   r   r   r   ÚpathlibÚPathÚnameÚ
read_bytesr   Ú_read_file_contentr2   ©r.   Úpathr   r   r   r+   ?   s    
 r+   r   r   c                 C  s   t | tjƒrt | ¡ ¡ S | S r   )r   r   r   r5   r6   r8   ©r.   r   r   r   r9   M   s    r9   c                 Ã  s   d S r   r   r(   r   r   r   Úasync_to_httpx_filesS   s    r=   c                 Ã  s   d S r   r   r(   r   r   r   r=   W   s    c                 Ã  sX   | d krd S t | ƒr.dd„ |  ¡ D ƒI d H } n&t| ƒrLdd„ | D ƒI d H } ntdƒ‚| S )Nc                 Ó  s    i | ]\}}|t |ƒI d H “qS r   ©Ú_async_transform_filer,   r   r   r   r/   `   s      z(async_to_httpx_files.<locals>.<dictcomp>c                 Ó  s"   g | ]\}}|t |ƒI d H f‘qS r   r>   r,   r   r   r   r0   b   s     z(async_to_httpx_files.<locals>.<listcomp>zFUnexpected file type input {type(files)}, expected mapping or sequence)r   r1   r   r2   r(   r   r   r   r=   [   s    c                 Ã  sp   t | ƒr6t| tjƒr2t | ¡}|j| ¡ I d H fS | S t| ƒrd| d t	| d ƒI d H f| dd … ˜S t
dƒ‚d S r3   )r   r   r   r   Úanyior6   r7   r8   r   Ú_async_read_file_contentr2   r:   r   r   r   r?   i   s    
&r?   c                 Ã  s$   t | tjƒr t | ¡ ¡ I d H S | S r   )r   r   r   r@   r6   r8   r<   r   r   r   rA   w   s    rA   )Ú
__future__r   r   r   r5   Útypingr   Útyping_extensionsr   r@   Ú_typesr   r   r   r	   r
   r   r   Ú_utilsr   r   r   r   r   r&   r)   r+   r9   r=   r?   rA   r   r   r   r   Ú<module>   s2   $	