U
    ·ÜŸjƒ0  ã                   @  sœ  d dl m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mZmZmZ d dlmZ d dlmZmZ 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 ed
ƒZ ede	e!df d�Z"ede
e#e!f d�Z$edee! d�Z%ededef d�Z&e�rddl'm(Z(m)Z) dddœdd„Z*ddddœdd„Z+ddd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d0d&œd1d2„Z0d3d4d&œd5d6„Z1dd7d&œd8d9„Z2d:d;d&œd<d=„Z3dd>d&œd?d@„Z4ddAd&œdBdC„Z5ddDd&œdEdF„Z6d
d
dGœdHdI„Z7dJdKdLœddMdMdMdNœdOdP„Z8dMdMdQœdRdS„Z9ddTdUœdVdW„Z:edXƒZ;edYƒZ<edZdZd&œd[d\„ƒZ=ed]d^d&œd_d\„ƒZ=eddd&œd`d\„ƒZ=dadd&œdbd\„Z=dMddcœddde„Z>dMdfdcœdgdh„Z?dMdidcœdjdk„Z@d dldcœdmdn„ZAd dodcœdpdq„ZBd drdcœdsdt„ZCdMdMdMduœdvdw„ZDdMdMdMdxœdydz„ZEdMd{d|œd}d~„ZFddMdMd€œd�d‚„ZGdMdƒœd„d…„ZHd†d‡œdldTdˆœd‰dŠ„ZIddd‹œdŒd�„ZJddŽd�œd�d‘„ZKdd’d�œd“d”„ZLdS )•é    )ÚannotationsN)
ÚTYPE_CHECKINGÚAnyÚTupleÚMappingÚTypeVarÚCallableÚIterableÚSequenceÚcastÚoverload)ÚPath)ÚdateÚdatetime)Ú	TypeGuardé   )ÚNotGivenÚ	FileTypesÚ
NotGivenOrÚHeadersLike)Ú
parse_dateÚparse_datetimeÚ_TÚ_TupleT.)ÚboundÚ	_MappingTÚ
_SequenceTÚ	CallableT)ÚAzureOpenAIÚAsyncAzureOpenAIzIterable[Iterable[_T]]zlist[_T])ÚtÚreturnc                 C  s   dd„ | D ƒS )Nc                 S  s   g | ]}|D ]}|‘qqS © r"   )Ú.0ZsublistÚitemr"   r"   ú8/tmp/pip-unpacked-wheel-2qj3ofc_/openai/_utils/_utils.pyÚ
<listcomp>'   s       zflatten.<locals>.<listcomp>r"   )r    r"   r"   r%   Úflatten&   s    r'   zMapping[str, object]zSequence[Sequence[str]]zlist[tuple[str, FileTypes]])ÚqueryÚpathsr!   c             	   C  s(   g }|D ]}|  t| |ddd�¡ q|S )zÄRecursively extract files from the given dictionary based on specified paths.

    A path may look like this ['foo', 'files', '<array>', 'data'].

    Note: this mutates the given dictionary.
    r   N©ÚindexÚflattened_key)ÚextendÚ_extract_items)r(   r)   ÚfilesÚpathr"   r"   r%   Úextract_files*   s    r1   ÚobjectzSequence[str]Úintz
str | None)Úobjr0   r+   r,   r!   c                  s  zˆˆ }W nZ t k
rf   t| tƒr.g  Y S ddlm} || ˆ d� ˆ d k	sRt‚ˆ tt| ƒfg Y S X ˆd7 ‰t| ƒräz$t	ˆƒˆkr’|  
|¡}n| | }W n tk
r´   g  Y S X ˆ d krÄ|‰ nˆ d|› d�7 ‰ t|ˆˆˆ d�S t| ƒ�r|dk� rüg S t‡ ‡‡fd	d
„| D ƒƒS g S )Nr   )Úassert_is_file_content)Úkeyé   ú[ú]r*   z<array>c                   s,   g | ]$}t |ˆˆˆ d k	r ˆ d ndd�‘qS )Nz[]r*   )r.   ©r#   r$   ©r,   r+   r0   r"   r%   r&   o   s   úüz"_extract_items.<locals>.<listcomp>)Ú
IndexErrorÚ
isinstancer   Ú_filesr5   ÚAssertionErrorr   r   Úis_dictÚlenÚpopÚKeyErrorr.   Úis_listr'   )r4   r0   r+   r,   r6   r5   r$   r"   r;   r%   r.   =   sF    

ü

ùÿr.   zNotGivenOr[_T]zTypeGuard[_T])r4   r!   c                 C  s   t | tƒ S ©N©r=   r   ©r4   r"   r"   r%   Úis_given~   s    rH   zTypeGuard[tuple[object, ...]]c                 C  s
   t | tƒS rE   ©r=   ÚtuplerG   r"   r"   r%   Úis_tupleŒ   s    rK   z_TupleT | objectzTypeGuard[_TupleT]c                 C  s
   t | tƒS rE   rI   rG   r"   r"   r%   Ú
is_tuple_t�   s    rL   zTypeGuard[Sequence[object]]c                 C  s
   t | tƒS rE   ©r=   r
   rG   r"   r"   r%   Úis_sequence”   s    rN   z_SequenceT | objectzTypeGuard[_SequenceT]c                 C  s
   t | tƒS rE   rM   rG   r"   r"   r%   Úis_sequence_t˜   s    rO   zTypeGuard[Mapping[str, object]]c                 C  s
   t | tƒS rE   ©r=   r   rG   r"   r"   r%   Ú
is_mappingœ   s    rQ   z_MappingT | objectzTypeGuard[_MappingT]c                 C  s
   t | tƒS rE   rP   rG   r"   r"   r%   Úis_mapping_t    s    rR   zTypeGuard[dict[object, object]]c                 C  s
   t | tƒS rE   )r=   ÚdictrG   r"   r"   r%   r@   ¤   s    r@   zTypeGuard[list[object]]c                 C  s
   t | tƒS rE   )r=   ÚlistrG   r"   r"   r%   rD   ¨   s    rD   zTypeGuard[Iterable[object]]c                 C  s
   t | tƒS rE   )r=   r	   rG   r"   r"   r%   Úis_iterable¬   s    rU   )r$   r!   c                 C  s@   t | ƒr ttdd„ |  ¡ D ƒƒS t| ƒr<ttdd„ | D ƒƒS | S )z­Minimal reimplementation of copy.deepcopy() that will only copy certain object types:

    - mappings, e.g. `dict`
    - list

    This is done for performance reasons.
    c                 S  s   i | ]\}}|t |ƒ“qS r"   ©Údeepcopy_minimal)r#   ÚkÚvr"   r"   r%   Ú
<dictcomp>¹   s      z$deepcopy_minimal.<locals>.<dictcomp>c                 S  s   g | ]}t |ƒ‘qS r"   rV   )r#   Úentryr"   r"   r%   r&   »   s     z$deepcopy_minimal.<locals>.<listcomp>)rQ   r   r   ÚitemsrD   )r$   r"   r"   r%   rW   °   s
    rW   z, Úor)ÚdelimÚfinalÚstr)Úseqr^   r_   r!   c                C  sn   t | ƒ}|dkrdS |dkr$| d S |dkrH| d › d|› d| d › �S | | d d… ¡d|› d| d › � S )Nr   Ú r7   r   ú éÿÿÿÿ)rA   Újoin)ra   r^   r_   Úsizer"   r"   r%   Ú
human_joinÀ   s    rg   )Ústringr!   c                 C  s   d| › d�S )zOAdd single quotation marks around the given string. Does *not* do any escaping.ú'r"   )rh   r"   r"   r%   ÚquoteÎ   s    rj   z Callable[[CallableT], CallableT])Úvariantsr!   c                    s   dddœ‡ fdd„}|S )a4  Decorator to enforce a given set of arguments or variants of arguments are passed to the decorated function.

    Useful for enforcing runtime validation of overloaded functions.

    Example usage:
    ```py
    @overload
    def foo(*, a: str) -> str: ...


    @overload
    def foo(*, b: bool) -> str: ...


    # This enforces the same constraints that a static type checker would
    # i.e. that either a or b must be passed to the function
    @required_args(["a"], ["b"])
    def foo(*, a: str | None = None, b: bool | None = None) -> str: ...
    ```
    r   )Úfuncr!   c                   sF   t  ˆ ¡j}dd„ | ¡ D ƒ‰t ˆ ¡ddddœ‡ ‡‡fdd„ƒ}|S )Nc                 S  s&   g | ]\}}|j |j|jhkr|‘qS r"   )ÚkindÚPOSITIONAL_ONLYÚPOSITIONAL_OR_KEYWORD)r#   ÚnameÚparamr"   r"   r%   r&   ë   s   þÿþz0required_args.<locals>.inner.<locals>.<listcomp>r2   )ÚargsÚkwargsr!   c            
        sD  t ƒ ‰ t| ƒD ]V\}}zˆ  ˆ| ¡ W q tk
rb   tˆj› dtˆƒ› dt| ƒ› d�ƒd ‚Y qX q| ¡ D ]}ˆ  |¡ qnˆD ]$}t‡ fdd„|D ƒƒ}|r‚ �q:q‚tˆƒdkrÔt	dd„ ˆD ƒƒ}d	|› d
�}n^tˆƒdksät
‚tt ˆd ƒˆ  ƒ}	t|	ƒdk�r dt	dd„ |	D ƒƒ› �}ndt|	d ƒ› �}t|ƒ‚ˆ| |ŽS )Nz	() takes z argument(s) but z were givenc                 3  s   | ]}|ˆ kV  qd S rE   r"   )r#   rq   ©Zgiven_paramsr"   r%   Ú	<genexpr>  s     z@required_args.<locals>.inner.<locals>.wrapper.<locals>.<genexpr>r7   c                 S  s*   g | ]"}d t dd„ |D ƒdd� d ‘qS )ú(c                 S  s   g | ]}t |ƒ‘qS r"   ©rj   ©r#   Úargr"   r"   r%   r&   
  s     zLrequired_args.<locals>.inner.<locals>.wrapper.<locals>.<listcomp>.<listcomp>Úand)r_   ú))rg   )r#   Úvariantr"   r"   r%   r&   
  s     zArequired_args.<locals>.inner.<locals>.wrapper.<locals>.<listcomp>z,Missing required arguments; Expected either z arguments to be givenr   zMissing required arguments: c                 S  s   g | ]}t |ƒ‘qS r"   rw   rx   r"   r"   r%   r&     s     zMissing required argument: )ÚsetÚ	enumerateÚaddr<   Ú	TypeErrorÚ__name__rA   ÚkeysÚallrg   r?   rT   rj   )
rr   rs   ÚiÚ_r6   r|   ÚmatchesZ
variationsÚmsgÚmissing)rl   Ú
positionalrk   rt   r%   Úwrapperõ   s8    ÿþ
ÿz-required_args.<locals>.inner.<locals>.wrapper)ÚinspectÚ	signatureÚ
parametersr\   Ú	functoolsÚwraps)rl   ÚparamsrŠ   ©rk   )rl   r‰   r%   Úinneré   s    þ
#zrequired_args.<locals>.innerr"   )rk   r’   r"   r‘   r%   Úrequired_argsÓ   s    2r“   Ú_KÚ_VÚNonec                 C  s   d S rE   r"   rG   r"   r"   r%   Ústrip_not_given"  s    r—   zMapping[_K, _V | NotGiven]zdict[_K, _V]c                 C  s   d S rE   r"   rG   r"   r"   r%   r—   &  s    c                 C  s   d S rE   r"   rG   r"   r"   r%   r—   *  s    zobject | Nonec                 C  s*   | dkrdS t | ƒs| S dd„ |  ¡ D ƒS )zHRemove all top-level keys where their values are instances of `NotGiven`Nc                 S  s    i | ]\}}t |tƒs||“qS r"   rF   ©r#   r6   Úvaluer"   r"   r%   rZ   6  s     
  z#strip_not_given.<locals>.<dictcomp>)rQ   r\   rG   r"   r"   r%   r—   .  s
    )Úvalr!   c                 C  s   t | dd�S )Né
   )Úbase)r3   ©rš   r"   r"   r%   Úcoerce_integer9  s    rž   Úfloatc                 C  s   t | ƒS rE   )rŸ   r�   r"   r"   r%   Úcoerce_float=  s    r    Úboolc                 C  s   | dkp| dkp| dkS )NÚtrueÚ1Úonr"   r�   r"   r"   r%   Úcoerce_booleanA  s    r¥   z
int | Nonec                 C  s   | d krd S t | ƒS rE   )rž   r�   r"   r"   r%   Úmaybe_coerce_integerE  s    r¦   zfloat | Nonec                 C  s   | d krd S t | ƒS rE   )r    r�   r"   r"   r%   Úmaybe_coerce_floatK  s    r§   zbool | Nonec                 C  s   | d krd S t | ƒS rE   )r¥   r�   r"   r"   r%   Úmaybe_coerce_booleanQ  s    r¨   )rh   Úprefixr!   c                 C  s   |   |¡r| t|ƒd… S | S )zXRemove a prefix from a string.

    Backport of `str.removeprefix` for Python < 3.9
    N)Ú
startswithrA   )rh   r©   r"   r"   r%   ÚremoveprefixW  s    
r«   )rh   Úsuffixr!   c                 C  s    |   |¡r| dt|ƒ … S | S )zXRemove a suffix from a string.

    Backport of `str.removesuffix` for Python < 3.9
    N)ÚendswithrA   )rh   r¬   r"   r"   r%   Úremovesuffixa  s    
r®   r   )r0   r!   c                 C  s    t | ƒ ¡ }tj | ¡}||fS rE   )r   Ú
read_bytesÚosr0   Úbasename)r0   ÚcontentsÚ	file_namer"   r"   r%   Úfile_from_pathk  s    r´   r   )ÚheadersÚheaderr!   c                 C  s–   |  ¡ }t| ƒr@|  ¡ D ]&\}}|  ¡ |krt|tƒr|  S qt ddd„ | ¡ ¡}||| ¡ |fD ]}|  	|¡}|rf|  S qft
d|› d�ƒ‚d S )Nz([^\w])(\w)c                 S  s   |   d¡|   d¡ ¡  S )Nr7   r   )ÚgroupÚupper)Úpatr"   r"   r%   Ú<lambda>z  ó    z%get_required_header.<locals>.<lambda>zCould not find z header)ÚlowerrR   r\   r=   r`   ÚreÚsubÚ
capitalizer¸   ÚgetÚ
ValueError)rµ   r¶   Zlower_headerrX   rY   Zintercaps_headerZnormalized_headerr™   r"   r"   r%   Úget_required_headerq  s    


rÂ   )r!   c                   C  s&   z
t  ¡ W S  tk
r    Y dS X d S )NÚfalse)ÚsniffioZcurrent_async_libraryÚ	Exceptionr"   r"   r"   r%   Úget_async_library„  s    
rÆ   é€   ©Úmaxsize)rÉ   r!   c                 C  s   t j| d�}tt|ƒS )zmA version of functools.lru_cache that retains the type signature
    for the wrapped function arguments.
    rÈ   )rŽ   Ú	lru_cacher   r   )rÉ   rŠ   r"   r"   r%   rÊ   ‹  s    ÿrÊ   )Údatar!   c                 C  sZ   t | ƒrdd„ |  ¡ D ƒS t| ƒr@t| tttfƒs@dd„ | D ƒS t| ttfƒrV|  	¡ S | S )zvTranslates a mapping / sequence recursively in the same fashion
    as `pydantic` v2's `model_dump(mode="json")`.
    c                 S  s   i | ]\}}t |ƒt |ƒ“qS r"   ©Ú	json_safer˜   r"   r"   r%   rZ   š  s      zjson_safe.<locals>.<dictcomp>c                 S  s   g | ]}t |ƒ‘qS r"   rÌ   r:   r"   r"   r%   r&   �  s     zjson_safe.<locals>.<listcomp>)
rQ   r\   rU   r=   r`   ÚbytesÚ	bytearrayr   r   Ú	isoformat)rË   r"   r"   r%   rÍ   •  s    rÍ   zTypeGuard[AzureOpenAI])Úclientr!   c                 C  s   ddl m} t| |ƒS )Nr   )r   )Ú	lib.azurer   r=   )rÑ   r   r"   r"   r%   Úis_azure_client¥  s    rÓ   zTypeGuard[AsyncAzureOpenAI]c                 C  s   ddl m} t| |ƒS )Nr   )r   )rÒ   r   r=   )rÑ   r   r"   r"   r%   Úis_async_azure_client«  s    rÔ   )MÚ
__future__r   r°   r½   r‹   rŽ   Útypingr   r   r   r   r   r   r	   r
   r   r   Úpathlibr   r   r   Útyping_extensionsr   rÄ   Ú_typesr   r   r   r   Ú_compatr   r   r   r2   r   r`   r   r   r   rÒ   r   r   r'   r1   r.   rH   rK   rL   rN   rO   rQ   rR   r@   rD   rU   rW   rg   rj   r“   r”   r•   r—   rž   r    r¥   r¦   r§   r¨   r«   r®   r´   rÂ   rÆ   rÊ   rÍ   rÓ   rÔ   r"   r"   r"   r%   Ú<module>   sv   0AK


