o
    Rhn?                     @  s  U d dl mZ d dl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mZmZmZmZ d dlZd dlZd dlZddlmZ ddl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*m+Z+m,Z, ddl-m.Z. erddl"m/Z/ ddl0m1Z1 edZ2edZ3edZ4e5e6Z7de8d< G dd dee3 Z9G dd de:Z;d#ddZ<d$dd Z=G d!d" d"Z>dS )%    )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_Tzlogging.Loggerlogc                   @  sH  e Zd ZU dZded< ded< ded< ded	< d
ed< ded< ded< ded< 	 dddKddZedLdd ZedMd$d%Z	edNd'd%Z	d(d)dOd,d%Z	edPd.d/Z
edQd1d2ZedRd3d4ZedSd6d7ZedTd9d:ZedUd<d=ZedTd>d?ZedTd@dAZedVdBdCZedWdEdFZd(d)dOdGdHZedTdIdJZd(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]_clientzdict[type[Any], Any]_parsed_by_typebool_stream1type[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Nonec                C  s4   || _ || _i | _|| _|| _|| _|| _|| _d S N)r)   r+   r,   r.   r0   r1   r3   r5   )selfr6   r7   r8   r9   r:   r;   r5    r@   Y/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/openai/_legacy_response.py__init__F   s   
zLegacyAPIResponse.__init__
str | Nonec                 C  s   | j jdS )Nzx-request-id)r3   headersgetr?   r@   r@   rA   
request_idZ   s   zLegacyAPIResponse.request_idtotype[_T]r%   c                C     d S r>   r@   )r?   rH   r@   r@   rA   parse^      zLegacyAPIResponse.parser$   c                 C  rJ   r>   r@   rF   r@   r@   rA   rK   a   rL   NrH   type[_T] | NoneR | _Tc                C  sx   |dur|n| j }| j|}|dur|S | j|d}t| jjr'| j|}t|tr2t	|| j
 || j|< tt|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   _parser   r1   post_parser
isinstancer   r   rG   r   r$   )r?   rH   	cache_keycachedparsedr@   r@   rA   rK   d   s    


httpx.Headersc                 C     | j jS r>   )r3   rD   rF   r@   r@   rA   rD         zLegacyAPIResponse.headershttpx.Requestc                 C  rW   r>   )r3   requestrF   r@   r@   rA   http_request   rX   zLegacyAPIResponse.http_requestc                 C  rW   r>   )r3   status_coderF   r@   r@   rA   r\      rX   zLegacyAPIResponse.status_code	httpx.URLc                 C  rW   r>   )r3   urlrF   r@   r@   rA   r^      rX   zLegacyAPIResponse.urlstrc                 C  rW   r>   )r[   methodrF   r@   r@   rA   r`      rX   zLegacyAPIResponse.methodbytesc                 C  rW   )zReturn the binary response content.

        NOTE: this will be removed in favour of `.read()` in the
        next major version.
        )r3   contentrF   r@   r@   rA   rb      s   zLegacyAPIResponse.contentc                 C  rW   )zyReturn the decoded response content.

        NOTE: this will be turned into a method in the next major version.
        )r3   textrF   r@   r@   rA   rc      s   zLegacyAPIResponse.textc                 C  rW   r>   )r3   http_versionrF   r@   r@   rA   rd      rX   zLegacyAPIResponse.http_versionc                 C  rW   r>   )r3   	is_closedrF   r@   r@   rA   re      rX   zLegacyAPIResponse.is_closeddatetime.timedeltac                 C  rW   )zCThe time taken for the complete request/response cycle to complete.)r3   elapsedrF   r@   r@   rA   rg      s   zLegacyAPIResponse.elapsedc          
      C  s.  |d ur|n| j }t|r|j}|rt|rt|d}t|p |}| jrz|rGt|s4tdt	 dt
 tt|t|dd| jtt| jdS | jr]tt| jt| j| jtt| jdS td| jj}|d u rkt tt||| jtt| jdS |tu rttd S | j}|tkrtt|jS |tkrttt|jS |tkrttt|jS |tkrtt|j dkS t|rt|trtt||S |t krt!d	t|rt|t"j#r|t"j#krt$d
tt|S t|rt|t%st|t&j%rtd|t'ur4|t(ur4|t)ur4|t*ur4t|t%s4t!d| dt% dt) dt( dt* dt dt dt"j# d|j+,dd-d^}}|dkrt.|rvz|/ }W n t0yl }	 zt12dt3|	|	 W Y d }	~	nd }	~	ww | jj4|||dS | jj5rt6|d| d|jd|jS |/ }| jj4|||dS )Nr   z/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   responser8   r/   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`zUnsupported type, expected z to be a subclass of z, .zcontent-type*;zapplication/jsonz5Could not read JSON from response data due to %s - %s)datar7   ri   zMExpected Content-Type response header to be `application/json` but received `z
` instead.)ri   messagebody)7r)   r   	__value__r   r   r   r.   r   	TypeErrorr   r   r   r%   r   r3   r   r+   r0   r$   _default_stream_clsMissingStreamClassErrorr   r_   rc   r4   floatr-   lowerinspectisclass
issubclassHttpxBinaryResponseContentr'   RuntimeErrorhttpxResponse
ValueErrorr   pydanticobjectlistdictr   rD   rE   splitr   json	Exceptionr&   debugtype_process_response_data_strict_response_validationr    )
r?   rH   r7   originr:   ri   content_type_rn   excr@   r@   rA   rP      s   


	
	








6



	zLegacyAPIResponse._parsec                 C  s    d| j  d| jj d| j dS )Nz<APIResponse [ z] type=>)r\   r3   reason_phraser)   rF   r@   r@   rA   __repr__T  s    zLegacyAPIResponse.__repr__)r6   r2   r7   r(   r8   r*   r9   r-   r:   r/   r;   r!   r5   r4   r<   r=   r<   rC   )rH   rI   r<   r%   )r<   r$   )rH   rN   r<   rO   )r<   rV   )r<   rY   )r<   r4   )r<   r]   r<   r_   r<   ra   )r<   r-   )r<   rf   )__name__
__module____qualname____doc____annotations__rB   propertyrG   r   rK   rD   r[   r\   r^   r`   rb   rc   rd   re   rg   rP   r   r   r@   r@   r@   rA   r'   -   sX   
 / r'   c                      s   e Zd Zd fddZ  ZS )rt   r<   r=   c                   s   t  d d S )NzzThe `stream` argument was set to `True` but the `stream_cls` argument was not given. See `openai._streaming` for reference)superrB   rF   	__class__r@   rA   rB   Z  s   z MissingStreamClassError.__init__r<   r=   )r   r   r   rB   __classcell__r@   r@   r   rA   rt   Y  s    rt   funcCallable[P, R]r<   !Callable[P, LegacyAPIResponse[R]]c                      t  d
 fdd}|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]c                    s@   i t t|dp
i }d|t< ||d< t tt  | i |S Nextra_headersrj   r   r   rE   r   r'   r$   r   r   r   r   r@   rA   wrappede  s   z(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]]]c                   r   )r   r   r   r   r   r<   r   c                    sH   i t t|dpi }d|t< ||d< t tt  | i |I d H S r   r   r   r   r@   rA   r   v  s
   z.async_to_raw_response_wrapper.<locals>.wrappedNr   r   r   r@   r   rA   async_to_raw_response_wrapperq  r   r   c                   @  s   e Zd ZU ded< d?ddZed@dd	ZedAddZedBddZedBddZ	dCddZ
d@ddZdDdEddZdDdFdd ZdGd!d"ZdDdEd#d$ZdHd'd(Zed)dd*dId+d,ZdJd-d.Zd@d/d0ZdDdKd2d3ZdDdLd5d6ZdMd7d8ZdDdKd9d:Zed)dd*dId;d<ZdJd=d>ZdS )Nrz   r2   ri   r<   r=   c                 C  s
   || _ d S r>   )ri   )r?   ri   r@   r@   rA   rB        
z#HttpxBinaryResponseContent.__init__ra   c                 C  rW   r>   )ri   rb   rF   r@   r@   rA   rb     rX   z"HttpxBinaryResponseContent.contentr_   c                 C  rW   r>   )ri   rc   rF   r@   r@   rA   rc     rX   zHttpxBinaryResponseContent.textrC   c                 C  rW   r>   )ri   encodingrF   r@   r@   rA   r     rX   z#HttpxBinaryResponseContent.encodingc                 C  rW   r>   )ri   charset_encodingrF   r@   r@   rA   r     rX   z+HttpxBinaryResponseContent.charset_encodingr   r   c                 K  s   | j jdi |S )Nr@   )ri   r   )r?   r   r@   r@   rA   r     s   zHttpxBinaryResponseContent.jsonc                 C  
   | j  S r>   )ri   readrF   r@   r@   rA   r     r   zHttpxBinaryResponseContent.readN
chunk_size
int | NoneIterator[bytes]c                 C     | j |S r>   )ri   
iter_bytesr?   r   r@   r@   rA   r        z%HttpxBinaryResponseContent.iter_bytesIterator[str]c                 C  r   r>   )ri   	iter_textr   r@   r@   rA   r     r   z$HttpxBinaryResponseContent.iter_textc                 C  r   r>   )ri   
iter_linesrF   r@   r@   rA   r     r   z%HttpxBinaryResponseContent.iter_linesc                 C  r   r>   )ri   iter_rawr   r@   r@   rA   r     r   z#HttpxBinaryResponseContent.iter_rawfilestr | os.PathLike[str]c                 C  sL   t |dd}| j D ]}|| qW d   dS 1 sw   Y  dS )av  Write 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openri   r   write)r?   r   frn   r@   r@   rA   write_to_file  s
   "z(HttpxBinaryResponseContent.write_to_filezDue to a bug, this method doesn't actually stream the response content, `.with_streaming_response.method()` should be used instead)r   c                C  sN   t |dd}| j|D ]}|| qW d    d S 1 s w   Y  d S Nr   r   r   )r?   r   r   r   rn   r@   r@   rA   stream_to_file  s
   	"z)HttpxBinaryResponseContent.stream_to_filec                 C  r   r>   )ri   closerF   r@   r@   rA   r     r   z HttpxBinaryResponseContent.closec                      | j  I d H S r>   )ri   areadrF   r@   r@   rA   r        z HttpxBinaryResponseContent.areadAsyncIterator[bytes]c                      | j |S r>   )ri   aiter_bytesr   r@   r@   rA   r        z&HttpxBinaryResponseContent.aiter_bytesAsyncIterator[str]c                   r   r>   )ri   
aiter_textr   r@   r@   rA   r     r   z%HttpxBinaryResponseContent.aiter_textc                   s   | j  S r>   )ri   aiter_linesrF   r@   r@   rA   r     s   
z&HttpxBinaryResponseContent.aiter_linesc                   r   r>   )ri   	aiter_rawr   r@   r@   rA   r     r   z$HttpxBinaryResponseContent.aiter_rawc             	     s   t |}|jddI d H 4 I d H "}| j|2 z3 d H W }||I d H  q6 W d   I d H  d S 1 I d H s;w   Y  d S r   )anyioPathr   ri   r   r   )r?   r   r   pathr   rn   r@   r@   rA   astream_to_file  s   
	.z*HttpxBinaryResponseContent.astream_to_filec                   r   r>   )ri   acloserF   r@   r@   rA   r     r   z!HttpxBinaryResponseContent.aclose)ri   r2   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   r   r<   r   )r<   r   )r   r   r   r   rB   r   rb   rc   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r@   r@   r@   rA   rz     sF   
 








rz   )r   r   r<   r   )r   r   r<   r   )?
__future__r   osrw   loggingdatetimer   typingr   r   r   r   r   r   r	   r
   r   r   typing_extensionsr   r   r   r   r   r   r|   r   _typesr   _utilsr   r   r   r   _modelsr   r   r   
_constantsr   
_streamingr   r   r   r   _exceptionsr    r!   _base_clientr"   r#   r$   r%   	getLoggerr   r&   r   r'   rr   rt   r   r   rz   r@   r@   r@   rA   <module>   s>    0  .

