o
    #`^h>                     @  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  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l/m0Z0 edZ1edZ2edZ3e4e5Z6de7d< G dd dee2 Z8G dd de9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)	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   )r4   rawcast_toclientstream
stream_clsoptionsreturnNonec                C  s4   || _ || _i | _|| _|| _|| _|| _|| _d S N)r(   r*   r+   r-   r/   r0   r2   r4   )selfr5   r6   r7   r8   r9   r:   r4    r?   U/home/air/shanriGPT/back/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)r2   headersgetr>   r?   r?   r@   
request_idZ   s   zLegacyAPIResponse.request_idtotype[_T]r$   c                C     d S r=   r?   )r>   rG   r?   r?   r@   parse^      zLegacyAPIResponse.parser#   c                 C  rI   r=   r?   rE   r?   r?   r@   rJ   a   rK   NrG   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`
        NrL   )r(   r+   rD   _parser   r0   post_parser
isinstancer   r   rF   r   r#   )r>   rG   	cache_keycachedparsedr?   r?   r@   rJ   d   s    


httpx.Headersc                 C     | j jS r=   )r2   rC   rE   r?   r?   r@   rC         zLegacyAPIResponse.headershttpx.Requestc                 C  rV   r=   )r2   requestrE   r?   r?   r@   http_request   rW   zLegacyAPIResponse.http_requestc                 C  rV   r=   )r2   status_coderE   r?   r?   r@   r[      rW   zLegacyAPIResponse.status_code	httpx.URLc                 C  rV   r=   )r2   urlrE   r?   r?   r@   r]      rW   zLegacyAPIResponse.urlstrc                 C  rV   r=   )rZ   methodrE   r?   r?   r@   r_      rW   zLegacyAPIResponse.methodbytesc                 C  rV   )zReturn the binary response content.

        NOTE: this will be removed in favour of `.read()` in the
        next major version.
        )r2   contentrE   r?   r?   r@   ra      s   zLegacyAPIResponse.contentc                 C  rV   )zyReturn the decoded response content.

        NOTE: this will be turned into a method in the next major version.
        )r2   textrE   r?   r?   r@   rb      s   zLegacyAPIResponse.textc                 C  rV   r=   )r2   http_versionrE   r?   r?   r@   rc      rW   zLegacyAPIResponse.http_versionc                 C  rV   r=   )r2   	is_closedrE   r?   r?   r@   rd      rW   zLegacyAPIResponse.is_closeddatetime.timedeltac                 C  rV   )zCThe time taken for the complete request/response cycle to complete.)r2   elapsedrE   r?   r?   r@   rf      s   zLegacyAPIResponse.elapsedc          
      C  s  |rt |rt|d}| jre|r1t|stdt dt tt|t	|dd| j
tt| jdS | jrGtt| jt	| j| j
tt| jdS td| jj}|d u rUt tt|| j| j
tt| jdS |d urk|n| j}t |rwt|d}|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|p|}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r)|t$ur)|t%ur)|t&ur)t|t!s)td| dt! dt% dt$ dt& dt dt dtj d|j'(dd)d^}}|dkrt*|rkz|+ }W n t,ya }	 zt-.dt/|	|	 W Y d }	~	nd }	~	ww | jj0|||dS | jj1r|t2|d| d|jd|jS |+ }| jj0|||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)r6   responser7   r.   z+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)datar6   rh   zMExpected Content-Type response header to be `application/json` but received `z
` instead.)rh   messagebody)3r   r   r-   r   	TypeErrorr   r   r   r$   r   r2   r   r*   r/   r#   _default_stream_clsMissingStreamClassErrorr(   r   r^   rb   r3   floatr   inspectisclass
issubclassHttpxBinaryResponseContentr&   RuntimeErrorhttpxResponse
ValueErrorr   pydanticobjectlistdictr   rC   rD   splitr   json	Exceptionr%   debugtype_process_response_data_strict_response_validationr   )
r>   rG   r9   r6   rh   origincontent_type_rl   excr?   r?   r@   rO      s   


	
	



 



6



	zLegacyAPIResponse._parsec                 C  s    d| j  d| jj d| j dS )Nz<APIResponse [ z] type=>)r[   r2   reason_phraser(   rE   r?   r?   r@   __repr__I  s    zLegacyAPIResponse.__repr__)r5   r1   r6   r'   r7   r)   r8   r,   r9   r.   r:   r    r4   r3   r;   r<   r;   rB   )rG   rH   r;   r$   )r;   r#   )rG   rM   r;   rN   )r;   rU   )r;   rX   )r;   r3   )r;   r\   r;   r^   r;   r`   )r;   r,   )r;   re   )__name__
__module____qualname____doc____annotations__rA   propertyrF   r   rJ   rC   rZ   r[   r]   r_   ra   rb   rc   rd   rf   rO   r   r   r?   r?   r?   r@   r&   -   sX   
 / r&   c                      s   e Zd Zd fddZ  ZS )rq   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)superrA   rE   	__class__r?   r@   rA   O  s   z MissingStreamClassError.__init__r;   r<   )r   r   r   rA   __classcell__r?   r?   r   r@   rq   N  s    rq   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_headerstruer   r   rD   r   r&   r#   r   r   r   r   r?   r@   wrappedZ  s   z(to_raw_response_wrapper.<locals>.wrappedNr   r   r   r   r;   r   	functoolswrapsr   r   r?   r   r@   to_raw_response_wrapperU     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?   r@   r   k  s
   z.async_to_raw_response_wrapper.<locals>.wrappedNr   r   r   r?   r   r@   async_to_raw_response_wrapperf  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 )Nrv   r1   rh   r;   r<   c                 C  s
   || _ d S r=   )rh   )r>   rh   r?   r?   r@   rA   z     
z#HttpxBinaryResponseContent.__init__r`   c                 C  rV   r=   )rh   ra   rE   r?   r?   r@   ra   }  rW   z"HttpxBinaryResponseContent.contentr^   c                 C  rV   r=   )rh   rb   rE   r?   r?   r@   rb     rW   zHttpxBinaryResponseContent.textrB   c                 C  rV   r=   )rh   encodingrE   r?   r?   r@   r     rW   z#HttpxBinaryResponseContent.encodingc                 C  rV   r=   )rh   charset_encodingrE   r?   r?   r@   r     rW   z+HttpxBinaryResponseContent.charset_encodingr   r   c                 K  s   | j jdi |S )Nr?   )rh   r   )r>   r   r?   r?   r@   r     s   zHttpxBinaryResponseContent.jsonc                 C  
   | j  S r=   )rh   readrE   r?   r?   r@   r     r   zHttpxBinaryResponseContent.readN
chunk_size
int | NoneIterator[bytes]c                 C     | j |S r=   )rh   
iter_bytesr>   r   r?   r?   r@   r        z%HttpxBinaryResponseContent.iter_bytesIterator[str]c                 C  r   r=   )rh   	iter_textr   r?   r?   r@   r     r   z$HttpxBinaryResponseContent.iter_textc                 C  r   r=   )rh   
iter_linesrE   r?   r?   r@   r     r   z%HttpxBinaryResponseContent.iter_linesc                 C  r   r=   )rh   iter_rawr   r?   r?   r@   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openrh   r   write)r>   r   frl   r?   r?   r@   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   rl   r?   r?   r@   stream_to_file  s
   	"z)HttpxBinaryResponseContent.stream_to_filec                 C  r   r=   )rh   closerE   r?   r?   r@   r     r   z HttpxBinaryResponseContent.closec                      | j  I d H S r=   )rh   areadrE   r?   r?   r@   r        z HttpxBinaryResponseContent.areadAsyncIterator[bytes]c                      | j |S r=   )rh   aiter_bytesr   r?   r?   r@   r        z&HttpxBinaryResponseContent.aiter_bytesAsyncIterator[str]c                   r   r=   )rh   
aiter_textr   r?   r?   r@   r     r   z%HttpxBinaryResponseContent.aiter_textc                   s   | j  S r=   )rh   aiter_linesrE   r?   r?   r@   r     s   
z&HttpxBinaryResponseContent.aiter_linesc                   r   r=   )rh   	aiter_rawr   r?   r?   r@   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   rh   r   r   )r>   r   r   pathr   rl   r?   r?   r@   astream_to_file  s   
	.z*HttpxBinaryResponseContent.astream_to_filec                   r   r=   )rh   acloserE   r?   r?   r@   r     r   z!HttpxBinaryResponseContent.aclose)rh   r1   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   rA   r   ra   rb   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r?   r?   r?   r@   rv   w  sF   
 








rv   )r   r   r;   r   )r   r   r;   r   )>
__future__r   osrs   loggingdatetimer   typingr   r   r   r   r   r   r	   r
   r   r   typing_extensionsr   r   r   r   r   r   rx   r{   _typesr   _utilsr   r   r   _modelsr   r   r   
_constantsr   
_streamingr   r   r   r   _exceptionsr   r    _base_clientr!   r"   r#   r$   	getLoggerr   r%   r   r&   ro   rq   r   r   rv   r?   r?   r?   r@   <module>   s>    0  #

