o
    ͩZh0                     @  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rddl'm(Z(m)Z) dddZ*dddZ+dd'd(Z,dd+d,Z-dd.d/Z.dd2d3Z/dd5d6Z0dd9d:Z1dd<d=Z2dd@dAZ3ddCdDZ4ddFdGZ5ddIdJZ6ddLdMZ7dNdOdPddUdVZ8ddXdYZ9dd\d]Z:ed^Z;ed_Z<eddadbZ=eddedbZ=eddfdbZ=ddhdbZ=ddjdkZ>ddmdnZ?ddpdqZ@ddsdtZAddvdwZBddydzZCdd|d}ZDdddZEdddZFdddZGdddZHdddddZIdddZJdddZKddd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AsyncAzureOpenAItIterable[Iterable[_T]]returnlist[_T]c                 C  s   dd | D S )Nc                 S  s   g | ]	}|D ]}|qqS  r$   ).0Zsublistitemr$   r$   K/var/www/html/lang_env/lib/python3.10/site-packages/openai/_utils/_utils.py
<listcomp>'       zflatten.<locals>.<listcomp>r$   )r    r$   r$   r'   flatten&   s   r*   queryMapping[str, object]pathsSequence[Sequence[str]]list[tuple[str, FileTypes]]c             	   C  s(   g }|D ]}| t| |ddd q|S )zRecursively 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   r7   objobjectr6   Sequence[str]r1   intr2   
str | Nonec                  s  z }W n, t y2   t| trg  Y S ddlm} ||  d  d us(J  tt| fg Y S w d7 t| rpztkrH| 	|}n| | }W n t
yX   g  Y S w  d u r`| n d| d7  t| dS t| r|dkrzg S t fd	d
| D S g S )Nr   )assert_is_file_content)key   []r0   z<array>c                   s,   g | ]}t | d ur d nddqS )Nz[]r0   )r4   r%   r&   r2   r1   r6   r$   r'   r(   o   s    z"_extract_items.<locals>.<listcomp>)
IndexError
isinstancer   _filesr=   r   r   is_dictlenpopKeyErrorr4   is_listr*   )r8   r6   r1   r2   r>   r=   r&   r$   rC   r'   r4   =   sL   
r4   NotGivenOr[_T]TypeGuard[_T]c                 C  s   t | t S NrE   r   r8   r$   r$   r'   is_given~      rQ   TypeGuard[tuple[object, ...]]c                 C  
   t | tS rN   rE   tuplerP   r$   r$   r'   is_tuple      
rW   _TupleT | objectTypeGuard[_TupleT]c                 C  rT   rN   rU   rP   r$   r$   r'   
is_tuple_t   rX   r[   TypeGuard[Sequence[object]]c                 C  rT   rN   rE   r
   rP   r$   r$   r'   is_sequence   rX   r^   _SequenceT | objectTypeGuard[_SequenceT]c                 C  rT   rN   r]   rP   r$   r$   r'   is_sequence_t   rX   ra   TypeGuard[Mapping[str, object]]c                 C  rT   rN   rE   r   rP   r$   r$   r'   
is_mapping   rX   rd   _MappingT | objectTypeGuard[_MappingT]c                 C  rT   rN   rc   rP   r$   r$   r'   is_mapping_t   rX   rg   TypeGuard[dict[object, object]]c                 C  rT   rN   )rE   dictrP   r$   r$   r'   rG      rX   rG   TypeGuard[list[object]]c                 C  rT   rN   )rE   listrP   r$   r$   r'   rK      rX   rK   TypeGuard[Iterable[object]]c                 C  rT   rN   )rE   r	   rP   r$   r$   r'   is_iterable   rX   rm   r&   c                 C  s@   t | rttdd |  D S t| rttdd | D S | S )zMinimal 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>   r)   z$deepcopy_minimal.<locals>.<dictcomp>c                 S     g | ]}t |qS r$   rn   )r%   entryr$   r$   r'   r(          z$deepcopy_minimal.<locals>.<listcomp>)rd   r   r   itemsrK   )r&   r$   r$   r'   ro      s
   ro   z, or)delimfinalseqrx   strry   c                C  sn   t | }|dkr
dS |dkr| d S |dkr$| d  d| d| d  S || d d d| d| d   S )Nr    r?   r    )rH   join)rz   rx   ry   sizer$   r$   r'   
human_join   s   &r   stringc                 C  s   d|  dS )zOAdd single quotation marks around the given string. Does *not* do any escaping.'r$   )r   r$   r$   r'   quote   s   r   variants Callable[[CallableT], CallableT]c                    s   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: ...
    ```
    funcr   r"   c                   s>   t  j}dd | D t d	 fdd}|S )
Nc                 S  s&   g | ]\}}|j |j|jhv r|qS r$   )kindPOSITIONAL_ONLYPOSITIONAL_OR_KEYWORD)r%   nameparamr$   r$   r'   r(      s    z0required_args.<locals>.inner.<locals>.<listcomp>argsr9   kwargsr"   c            
        sH  t   t| D ]'\}}z	 |  W q ty.   tj dt dt|  dd w | D ]} | q3D ]}t fdd|D }|rN nOq=tdkrht	dd D }d	| d
}t|tdkspJ t
t d   }	t|	dkrdt	dd |	D  }t|dt|	d  }t|| i |S )Nz	() takes z argument(s) but z were givenc                 3  s    | ]}| v V  qd S rN   r$   )r%   r   Zgiven_paramsr$   r'   	<genexpr>  s    z@required_args.<locals>.inner.<locals>.wrapper.<locals>.<genexpr>r?   c                 S  s*   g | ]}d t dd |D dd d qS )(c                 S  rs   r$   r   r%   argr$   r$   r'   r(   
  ru   zLrequired_args.<locals>.inner.<locals>.wrapper.<locals>.<listcomp>.<listcomp>and)ry   ))r   )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  rs   r$   r   r   r$   r$   r'   r(     ru   zMissing required argument: )set	enumerateaddrD   	TypeError__name__rH   keysallr   rk   r   )
r   r   i_r>   r   matchesZ
variationsmsgmissing)r   
positionalr   r   r'   wrapper   s@   
z-required_args.<locals>.inner.<locals>.wrapper)r   r9   r   r9   r"   r9   )inspect	signature
parametersrv   	functoolswraps)r   paramsr   r   )r   r   r'   inner   s   
#zrequired_args.<locals>.innerN)r   r   r"   r   r$   )r   r   r$   r   r'   required_args   s   2r   _K_VNonec                 C     d S rN   r$   rP   r$   r$   r'   strip_not_given"     r   Mapping[_K, _V | NotGiven]dict[_K, _V]c                 C  r   rN   r$   rP   r$   r$   r'   r   &  r   c                 C  r   rN   r$   rP   r$   r$   r'   r   *  r   object | Nonec                 C  s*   | du 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$   rO   r%   r>   valuer$   r$   r'   rr   6  s     z#strip_not_given.<locals>.<dictcomp>)rd   rv   rP   r$   r$   r'   r   .  s
   valc                 C  s   t | ddS )N
   )base)r;   r   r$   r$   r'   coerce_integer9  rR   r   floatc                 C  s   t | S rN   )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   
int | Nonec                 C     | d u rd S t | S rN   )r   r   r$   r$   r'   maybe_coerce_integerE     r   float | Nonec                 C  r   rN   )r   r   r$   r$   r'   maybe_coerce_floatK  r   r   bool | Nonec                 C  r   rN   )r   r   r$   r$   r'   maybe_coerce_booleanQ  r   r   prefixc                 C  s   |  |r| t|d S | S )zXRemove a prefix from a string.

    Backport of `str.removeprefix` for Python < 3.9
    N)
startswithrH   )r   r   r$   r$   r'   removeprefixW  s   
r   suffixc                 C  s    |  |r| dt|  S | S )zXRemove a suffix from a string.

    Backport of `str.removesuffix` for Python < 3.9
    N)endswithrH   )r   r   r$   r$   r'   removesuffixa  s   
r   r   c                 C  s    t |  }tj| }||fS rN   )r   
read_bytesosr6   basename)r6   contents	file_namer$   r$   r'   file_from_pathk  s   r   headersr   headerc                 C  s   |  }t| r |  D ]\}}|  |krt|tr|  S qtddd | }||| |fD ]}| 	|}|r@|  S q3t
d| d)Nz([^\w])(\w)c                 S  s   |  d|  d  S )Nr?   r   )groupupper)patr$   r$   r'   <lambda>z  s    z%get_required_header.<locals>.<lambda>zCould not find z header)lowerrg   rv   rE   r{   resub
capitalizer   get
ValueError)r   r   Zlower_headerrp   rq   Zintercaps_headerZnormalized_headerr   r$   r$   r'   get_required_headerq  s   
r   c                   C  s    zt  W S  ty   Y dS w )Nfalse)sniffioZcurrent_async_library	Exceptionr$   r$   r$   r'   get_async_library  s
   
r      maxsizer   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   datac                 C  sZ   t | rdd |  D S t| r t| tttfs dd | D S t| ttfr+| 	 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'   rr     s    zjson_safe.<locals>.<dictcomp>c                 S  rs   r$   r   rB   r$   r$   r'   r(     ru   zjson_safe.<locals>.<listcomp>)
rd   rv   rm   rE   r{   bytes	bytearrayr   r   	isoformat)r   r$   r$   r'   r     s   r   clientTypeGuard[AzureOpenAI]c                 C     ddl m} t| |S )Nr   )r   )	lib.azurer   rE   )r   r   r$   r$   r'   is_azure_client     
r   TypeGuard[AsyncAzureOpenAI]c                 C  r   )Nr   )r   )r   r   rE   )r   r   r$   r$   r'   is_async_azure_client  r   r   )r    r!   r"   r#   )r+   r,   r-   r.   r"   r/   )
r8   r9   r6   r:   r1   r;   r2   r<   r"   r/   )r8   rL   r"   rM   )r8   r9   r"   rS   )r8   rY   r"   rZ   )r8   r9   r"   r\   )r8   r_   r"   r`   )r8   r9   r"   rb   )r8   re   r"   rf   )r8   r9   r"   rh   )r8   r9   r"   rj   )r8   r9   r"   rl   )r&   r   r"   r   )rz   r:   rx   r{   ry   r{   r"   r{   )r   r{   r"   r{   )r   r:   r"   r   )r8   r   r"   r   )r8   r   r"   r   )r8   r9   r"   r9   )r8   r   r"   r9   )r   r{   r"   r;   )r   r{   r"   r   )r   r{   r"   r   )r   r<   r"   r   )r   r<   r"   r   )r   r<   r"   r   )r   r{   r   r{   r"   r{   )r   r{   r   r{   r"   r{   )r6   r{   r"   r   )r   r   r   r{   r"   r{   )r"   r{   )r   r   r"   r   )r   r9   r"   r9   )r   r9   r"   r   )r   r9   r"   r   )M
__future__r   r   r   r   r   typingr   r   r   r   r   r   r	   r
   r   r   pathlibr   r   r   Ztyping_extensionsr   r   _typesr   r   r   r   _compatr   r   r   r9   r   r{   r   r   r   r   r   r   r*   r7   r4   rQ   rW   r[   r^   ra   rd   rg   rG   rK   rm   ro   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r$   r$   r$   r'   <module>   sx    0



A











K
















