HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //usr/local/lib/python3.10/dist-packages/openai/__pycache__/_legacy_response.cpython-310.pyc
o

���gn?�@s�UddlmZddlZddlZddlZddlZddlZddlmZm	Z	m
Z
mZmZm
Z
mZmZmZmZddlmZmZmZmZmZddlZddlZddlZddlmZddlmZmZm Z m!Z!ddl"m#Z#m$Z$m%Z%dd	l&m'Z'dd
l(m)Z)m*Z*m+Z+m,Z,ddl-m.Z.er�ddl"m/Z/dd
l0m1Z1ed�Z2ed�Z3ed�Z4e�5e6�Z7de8d<Gdd�dee3�Z9Gdd�de:�Z;d#dd�Z<d$dd �Z=Gd!d"�d"�Z>dS)%�)�annotationsN)
�
TYPE_CHECKING�Any�Union�Generic�TypeVar�Callable�Iterator�
AsyncIterator�cast�overload)�	Awaitable�	ParamSpec�override�
deprecated�
get_origin�)�NoneType)�is_given�extract_type_arg�is_annotated_type�is_type_alias_type)�	BaseModel�is_basemodel�add_request_id)�RAW_RESPONSE_HEADER)�Stream�AsyncStream�is_stream_class_type�extract_stream_chunk_type)�APIResponseValidationError)�FinalRequestOptions)�
BaseClient�P�R�_Tzlogging.Logger�logc@sHeZdZUdZded<ded<ded<ded	<d
ed<ded
<ded<ded<	dd�dKdd�ZedLdd ��ZedMd$d%��Z	edNd'd%��Z	d(d)�dOd,d%�Z	edPd.d/��Z
edQd1d2��ZedRd3d4��ZedSd6d7��Z
edTd9d:��ZedUd<d=��ZedTd>d?��ZedTd@dA��ZedVdBdC��ZedWdEdF��Zd(d)�dOdGdH�ZedTdIdJ��Zd(S)X�LegacyAPIResponsea�This is a legacy class as it will be replaced by `APIResponse`
    and `AsyncAPIResponse` in the `_response.py` file in the next major
    release.

    For the sync client this will mostly be the same with the exception
    of `content` & `text` will be methods instead of properties. In the
    async client, all methods will be async.

    A migration script will be provided & the migration in general should
    be smooth.
    �type[R]�_cast_to�BaseClient[Any, Any]�_clientzdict[type[Any], Any]�_parsed_by_type�bool�_stream�1type[Stream[Any]] | type[AsyncStream[Any]] | None�_stream_clsr!�_options�httpx.Response�
http_response�int�
retries_takenr)r5�raw�cast_to�client�stream�
stream_cls�options�return�NonecCs4||_||_i|_||_||_||_||_||_dS�N)r)r+r,r.r0r1r3r5)�selfr6r7r8r9r:r;r5�r@�B/usr/local/lib/python3.10/dist-packages/openai/_legacy_response.py�__init__Fs
zLegacyAPIResponse.__init__�
str | NonecCs|jj�d�S)Nzx-request-id)r3�headers�get�r?r@r@rA�
request_idZszLegacyAPIResponse.request_id�to�type[_T]r%cC�dSr>r@)r?rHr@r@rA�parse^�zLegacyAPIResponse.parser$cCrJr>r@rFr@r@rArKarLN�rH�type[_T] | None�R | _TcCsx|dur|n|j}|j�|�}|dur|S|j|d�}t|jj�r'|j�|�}t|t�r2t	||j
�||j|<tt|�S)a�Returns the rich python representation of this response's data.

        NOTE: For the async client: this will become a coroutine in the next major version.

        For lower-level control, see `.read()`, `.json()`, `.iter_bytes()`.

        You can customise the type that the response is parsed into through
        the `to` argument, e.g.

        ```py
        from openai import BaseModel


        class MyModel(BaseModel):
            foo: str


        obj = response.parse(to=MyModel)
        print(obj.foo)
        ```

        We support parsing:
          - `BaseModel`
          - `dict`
          - `list`
          - `Union`
          - `str`
          - `int`
          - `float`
          - `httpx.Response`
        NrM)
r)r,rE�_parserr1�post_parser�
isinstancerrrGrr$)r?rH�	cache_key�cached�parsedr@r@rArKds 


�
httpx.HeaderscC�|jjSr>)r3rDrFr@r@rArD��zLegacyAPIResponse.headers�
httpx.RequestcCrWr>)r3�requestrFr@r@rA�http_request�rXzLegacyAPIResponse.http_requestcCrWr>)r3�status_coderFr@r@rAr\�rXzLegacyAPIResponse.status_code�	httpx.URLcCrWr>)r3�urlrFr@r@rAr^�rXzLegacyAPIResponse.url�strcCrWr>)r[�methodrFr@r@rAr`�rXzLegacyAPIResponse.method�bytescCrW)z�Return the binary response content.

        NOTE: this will be removed in favour of `.read()` in the
        next major version.
        )r3�contentrFr@r@rArb�szLegacyAPIResponse.contentcCrW)zyReturn the decoded response content.

        NOTE: this will be turned into a method in the next major version.
        )r3�textrFr@r@rArc�szLegacyAPIResponse.textcCrWr>)r3�http_versionrFr@r@rArd�rXzLegacyAPIResponse.http_versioncCrWr>)r3�	is_closedrFr@r@rAre�rXzLegacyAPIResponse.is_closed�datetime.timedeltacCrW)zCThe time taken for the complete request/response cycle to complete.)r3�elapsedrFr@r@rArg�szLegacyAPIResponse.elapsedc
Cs.|dur|n|j}t|�r|j}|rt|�rt|d�}t|�p |}|jrz|rGt|�s4tdt	�dt
����tt|t
|dd�|jtt|j�d��S|jr]tt|jt
|j�|jtt|j�d��Std|jj�}|durkt��tt|||jtt|j�d��S|tur�ttd�S|j}|tkr�tt|j�S|tkr�ttt|j��S|tkr�ttt|j��S|tkr�tt|j��dk�St�|�r�t|t�r�tt||��S|t kr�t!d	��t�|�r�t|t"j#�r�|t"j#kr�t$d
��tt|�St�|�r�t|t%�s�t|t&j%�r�td��|t'u�r4|t(u�r4|t)u�r4|t*u�r4t|t%��s4t!d|�d
t%�dt)�dt(�dt*�dt�dt�dt"j#�d���|j+�,dd��-d�^}}|dk�r�t.|��rvz|�/�}Wnt0�yl}	zt1�2dt3|	�|	�WYd}	~	nd}	~	ww|jj4|||d�S|jj5�r�t6|d|�d�|jd��|jS|�/�}|jj4|||d�S)Nrz/Expected custom parse type to be a subclass of z or zUExpected custom stream type to be passed with a type argument, e.g. Stream[ChunkType])�failure_message)r7�responser8r/�truez+Unexpected state - cast_to is `APIResponse`z:Subclasses of httpx.Response cannot be passed to `cast_to`zVPydantic models must subclass our base model type, e.g. `from openai import BaseModel`zUnsupported type, expected z to be a subclass of z, �.zcontent-type�*�;zapplication/jsonz5Could not read JSON from response data due to %s - %s)�datar7rizMExpected Content-Type response header to be `application/json` but received `z
` instead.)ri�message�body)7r)r�	__value__rrrr.r�	TypeErrorrrrr%rr3rr+r0r$�_default_stream_cls�MissingStreamClassErrorrr_rcr4�floatr-�lower�inspect�isclass�
issubclass�HttpxBinaryResponseContentr'�RuntimeError�httpx�Response�
ValueErrorr�pydantic�object�list�dictrrDrE�splitr�json�	Exceptionr&�debug�type�_process_response_data�_strict_response_validationr )
r?rHr7�originr:ri�content_type�_rn�excr@r@rArP�s�
�
��
��	
��	
�
�

���
�



�6�

���

�	�zLegacyAPIResponse._parsecCs d|j�d|jj�d|j�d�S)Nz<APIResponse [� z] type=�>)r\r3�
reason_phraser)rFr@r@rA�__repr__Ts zLegacyAPIResponse.__repr__)r6r2r7r(r8r*r9r-r:r/r;r!r5r4r<r=�r<rC)rHrIr<r%)r<r$)rHrNr<rO)r<rV)r<rY)r<r4)r<r]�r<r_�r<ra)r<r-)r<rf)�__name__�
__module__�__qualname__�__doc__�__annotations__rB�propertyrGrrKrDr[r\r^r`rbrcrdrergrPrr�r@r@r@rAr'-sX
�/r'cseZdZd�fdd�Z�ZS)rtr<r=cst��d�dS)NzzThe `stream` argument was set to `True` but the `stream_cls` argument was not given. See `openai._streaming` for reference)�superrBrF��	__class__r@rArBZs�z MissingStreamClassError.__init__�r<r=)r�r�r�rB�
__classcell__r@r@r�rArtYsrt�func�Callable[P, R]r<�!Callable[P, LegacyAPIResponse[R]]c�t���d
�fdd��}|S)��Higher order function that takes one of our bound API methods and wraps it
    to support returning the raw `APIResponse` object directly.
    �args�P.args�kwargs�P.kwargsr<�LegacyAPIResponse[R]cs@itt|�d��p
i�}d|t<||d<ttt�|i|���S�N�
extra_headersrj�rrrErr'r$�r�r�r��r�r@rA�wrappedesz(to_raw_response_wrapper.<locals>.wrappedN�r�r�r�r�r<r���	functools�wraps�r�r�r@r�rA�to_raw_response_wrapper`�r��Callable[P, Awaitable[R]]�,Callable[P, Awaitable[LegacyAPIResponse[R]]]cr�)r�r�r�r�r�r<r�c�sH�itt|�d��pi�}d|t<||d<ttt�|i|��IdH�Sr�r�r�r�r@rAr�vs
�z.async_to_raw_response_wrapper.<locals>.wrappedNr�r�r�r@r�rA�async_to_raw_response_wrapperqr�r�c@s eZdZUded<d?dd�Zed@dd	��ZedAdd��ZedBdd��ZedBdd��Z	dCdd�Z
d@dd�ZdDdEdd�ZdDdFdd �Z
dGd!d"�ZdDdEd#d$�ZdHd'd(�Zed)�dd*�dId+d,��ZdJd-d.�Zd@d/d0�ZdDdKd2d3�ZdDdLd5d6�ZdMd7d8�ZdDdKd9d:�Zed)�dd*�dId;d<��ZdJd=d>�ZdS)Nrzr2rir<r=cCs
||_dSr>)ri)r?rir@r@rArB��
z#HttpxBinaryResponseContent.__init__racCrWr>)rirbrFr@r@rArb�rXz"HttpxBinaryResponseContent.contentr_cCrWr>)rircrFr@r@rArc�rXzHttpxBinaryResponseContent.textrCcCrWr>)ri�encodingrFr@r@rAr��rXz#HttpxBinaryResponseContent.encodingcCrWr>)ri�charset_encodingrFr@r@rAr��rXz+HttpxBinaryResponseContent.charset_encodingr�rcKs|jjdi|��S)Nr@)rir�)r?r�r@r@rAr��szHttpxBinaryResponseContent.jsoncC�
|j��Sr>)ri�readrFr@r@rAr��r�zHttpxBinaryResponseContent.readN�
chunk_size�
int | None�Iterator[bytes]cC�|j�|�Sr>)ri�
iter_bytes�r?r�r@r@rAr���z%HttpxBinaryResponseContent.iter_bytes�
Iterator[str]cCr�r>)ri�	iter_textr�r@r@rAr��r�z$HttpxBinaryResponseContent.iter_textcCr�r>)ri�
iter_linesrFr@r@rAr��r�z%HttpxBinaryResponseContent.iter_linescCr�r>)ri�iter_rawr�r@r@rAr��r�z#HttpxBinaryResponseContent.iter_raw�file�str | os.PathLike[str]cCsLt|dd��}|j��D]}|�|�qWd�dS1swYdS)avWrite the output to the given file.

        Accepts a filename or any path-like object, e.g. pathlib.Path

        Note: if you want to stream the data to the file instead of writing
        all at once then you should use `.with_streaming_response` when making
        the API request, e.g. `client.with_streaming_response.foo().stream_to_file('my_filename.txt')`
        �wb��modeN��openrir��write)r?r��frnr@r@rA�
write_to_file�s
�"�z(HttpxBinaryResponseContent.write_to_filez�Due to a bug, this method doesn't actually stream the response content, `.with_streaming_response.method()` should be used instead)r�cCsNt|dd��}|j�|�D]}|�|�q
Wd�dS1s wYdS�Nr�r�r�)r?r�r�r�rnr@r@rA�stream_to_file�s
	�"�z)HttpxBinaryResponseContent.stream_to_filecCr�r>)ri�closerFr@r@rAr��r�z HttpxBinaryResponseContent.closec���|j��IdHSr>)ri�areadrFr@r@rAr����z HttpxBinaryResponseContent.aread�AsyncIterator[bytes]c���|j�|�Sr>)ri�aiter_bytesr�r@r@rAr����z&HttpxBinaryResponseContent.aiter_bytes�AsyncIterator[str]c�r�r>)ri�
aiter_textr�r@r@rAr��r�z%HttpxBinaryResponseContent.aiter_textc�s�|j��Sr>)ri�aiter_linesrFr@r@rAr��s�
z&HttpxBinaryResponseContent.aiter_linesc�r�r>)ri�	aiter_rawr�r@r@rAr��r�z$HttpxBinaryResponseContent.aiter_rawc	�s��t�|�}|jdd�IdH4IdH�"}|j�|�2z3dHW}|�|�IdHq6Wd�IdHdS1IdHs;wYdSr�)�anyio�Pathr�rir�r�)r?r�r��pathr�rnr@r@rA�astream_to_file�s�
	�.�z*HttpxBinaryResponseContent.astream_to_filec�r�r>)ri�acloserFr@r@rAr��r�z!HttpxBinaryResponseContent.aclose)rir2r<r=r�r�r�)r�rr<rr>)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�rBr�rbrcr�r�r�r�r�r�r�r�r�rr�r�r�r�r�r�r�r�r�r@r@r@rArz�sF





��



��rz)r�r�r<r�)r�r�r<r�)?�
__future__r�osrw�logging�datetimer��typingrrrrrrr	r
rr�typing_extensionsr
rrrrr�r|r�_typesr�_utilsrrrr�_modelsrrr�
_constantsr�
_streamingrrrr�_exceptionsr r!�_base_clientr"r#r$r%�	getLoggerr�r&r�r'rrrtr�r�rzr@r@r@rA�<module>s>0.