o
    ¨Ýji@  ã                   @  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# 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/m0Z0 er•ddl$m1Z1 ddl2m3Z3 edƒZ4edƒZ5edƒZ6e 7e8¡Z9de:d< G dd„ dee5 ƒZ;G 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)Úhttp_response_types)Ú	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Ú_streamú1type[Stream[Any]] | type[AsyncStream[Any]] | NoneÚ_stream_clsr"   Ú_optionsúhttpx2.ResponseÚhttp_responseÚintÚretries_takenr   )r6   ÚrawÚcast_toÚclientÚstreamÚ
stream_clsÚoptionsÚreturnÚNonec                C  s4   || _ || _i | _|| _|| _|| _|| _|| _d S ©N)r*   r,   r-   r/   r1   r2   r4   r6   )Úselfr7   r8   r9   r:   r;   r<   r6   © rA   úR/var/www/html/chatgpt/venv/lib/python3.10/site-packages/openai/_legacy_response.pyÚ__init__G   s   
zLegacyAPIResponse.__init__ú
str | Nonec                 C  s   | j j d¡S )Nzx-request-id)r4   ÚheadersÚget©r@   rA   rA   rB   Ú
request_id[   s   zLegacyAPIResponse.request_idÚtoútype[_T]r&   c                C  ó   d S r?   rA   )r@   rI   rA   rA   rB   Úparse_   ó   zLegacyAPIResponse.parser%   c                 C  rK   r?   rA   rG   rA   rA   rB   rL   b   rM   N©rI   ú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`
          - `httpx2.Response`
        NrN   )r*   r-   rF   Ú_parser   r2   Zpost_parserÚ
isinstancer   r   rH   r   r%   )r@   rI   Ú	cache_keyÚcachedÚparsedrA   rA   rB   rL   e   s    


úhttpx2.Headersc                 C  ó   | j jS r?   )r4   rE   rG   rA   rA   rB   rE   ”   ó   zLegacyAPIResponse.headersúhttpx2.Requestc                 C  rW   r?   )r4   ÚrequestrG   rA   rA   rB   Úhttp_request˜   rX   zLegacyAPIResponse.http_requestc                 C  rW   r?   )r4   Ústatus_coderG   rA   rA   rB   r\   œ   rX   zLegacyAPIResponse.status_codeú
httpx2.URLc                 C  rW   r?   )r4   ÚurlrG   rA   rA   rB   r^       rX   zLegacyAPIResponse.urlÚstrc                 C  rW   r?   )r[   ÚmethodrG   rA   rA   rB   r`   ¤   rX   zLegacyAPIResponse.methodÚbytesc                 C  rW   )zŠReturn the binary response content.

        NOTE: this will be removed in favour of `.read()` in the
        next major version.
        )r4   ÚcontentrG   rA   rA   rB   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.
        )r4   ÚtextrG   rA   rA   rB   rc   ±   s   zLegacyAPIResponse.textc                 C  rW   r?   )r4   Úhttp_versionrG   rA   rA   rB   rd   ¹   rX   zLegacyAPIResponse.http_versionc                 C  rW   r?   )r4   Ú	is_closedrG   rA   rA   rB   re   ½   rX   zLegacyAPIResponse.is_closedúdatetime.timedeltac                 C  rW   )zCThe time taken for the complete request/response cycle to complete.)r4   ÚelapsedrG   rA   rA   rB   rg   Á   s   zLegacyAPIResponse.elapsedc                C  sH  |d ur|n| j }t|ƒr|j}|rt|ƒrt|dƒ}t|ƒp |}| jr€|rIt|ƒs4tdt	› dt
› �ƒ‚tt|t|dd�| jtt| jƒ| jd�ƒS | jratt| jt| jƒ| jtt| jƒ| jd�ƒS td| jjƒ}|d u rotƒ ‚tt||| jtt| jƒ| jd�ƒS |tu r‰ttd ƒS | j}|tkr–tt|jƒS |tkr¢ttt|jƒƒS |tkr®ttt|jƒƒS |tkr¾tt|j ¡  ¡ dkƒS t |¡rÏt |t!ƒrÏtt||ƒƒS |t"kr×t#d	ƒ‚t$ƒ }t |¡rñt ||ƒrñ||vrìt%d
ƒ‚tt|ƒS t |¡�rt |t&ƒ�st |t'j&ƒ�rt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¡ 0d¡^}}| 1d¡�s—t2|ƒ�rƒz| 3¡ }	W n t4�yy }
 zt5 6dt7|
ƒj8¡ W Y d }
~
nd }
~
ww | jj9|	||d�S | jj:�r”t;|d|› d�|jd�‚|jS | 3¡ }	| jj9|	||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])Zfailure_message)r8   Úresponser9   r<   r0   Útruez+Unexpected state - cast_to is `APIResponse`zASubclasses of HTTP response classes 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Ú*ú;Újsonz0Could not read JSON from response data due to %s)Údatar8   rh   zMExpected Content-Type response header to be `application/json` but received `z
` instead.)rh   ÚmessageÚbody)<r*   r   Z	__value__r   r   r   r/   r   Ú	TypeErrorr   r   r   r&   r    r4   r   r,   r2   r1   r%   Z_default_stream_clsÚMissingStreamClassErrorr   r_   rc   r5   Úfloatr.   ÚstripÚlowerÚinspectÚisclassÚ
issubclassÚHttpxBinaryResponseContentr(   ÚRuntimeErrorr   Ú
ValueErrorr   ÚpydanticÚobjectÚlistÚdictr   Úhttpx2ÚResponserE   rF   ÚsplitÚendswithr   rm   Ú	Exceptionr'   ÚdebugÚtypeÚ__name__Z_process_response_dataZ_strict_response_validationr!   )r@   rI   r8   Úoriginr;   rh   Zresponse_typesÚcontent_typeÚ_rn   ÚexcrA   rA   rB   rQ   Æ   sâ   
þ
ùþ
üþ

üþ

ÿþ
ÿÿü
û



ÿ6ÿ
€ÿý

ý	ýzLegacyAPIResponse._parsec                 C  s    d| j › d| jj› d| j› d�S )Nz<APIResponse [ú z] type=ú>)r\   r4   Zreason_phraser*   rG   rA   rA   rB   Ú__repr__Y  s    zLegacyAPIResponse.__repr__)r7   r3   r8   r)   r9   r+   r:   r.   r;   r0   r<   r"   r6   r5   r=   r>   ©r=   rD   )rI   rJ   r=   r&   )r=   r%   )rI   rO   r=   rP   )r=   rV   )r=   rY   )r=   r5   )r=   r]   ©r=   r_   ©r=   ra   )r=   r.   )r=   rf   )r‡   Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__rC   ÚpropertyrH   r   rL   rE   r[   r\   r^   r`   rb   rc   rd   re   rg   rQ   r   rŽ   rA   rA   rA   rB   r(   .   sX   
 ÷/ r(   c                      s   e Zd Zd‡ fdd„Z‡  ZS )rr   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)ÚsuperrC   rG   ©Ú	__class__rA   rB   rC   _  s   ÿz MissingStreamClassError.__init__©r=   r>   )r‡   r’   r“   rC   Ú__classcell__rA   rA   r˜   rB   rr   ^  s    rr   Ú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_headersri   ©r   r   rF   r   r(   r%   ©r¡   r£   r§   ©rœ   rA   rB   Úwrappedj  s   z(to_raw_response_wrapper.<locals>.wrappedN©r¡   r¢   r£   r¤   r=   r¥   ©Ú	functoolsÚwraps©rœ   r«   rA   rª   rB   Úto_raw_response_wrappere  ó   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ª   rA   rB   r«   {  s
   €z.async_to_raw_response_wrapper.<locals>.wrappedNr¬   r­   r°   rA   rª   rB   Úasync_to_raw_response_wrapperv  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 )Nry   r3   rh   r=   r>   c                 C  s
   || _ d S r?   )rh   )r@   rh   rA   rA   rB   rC   Š  ó   
z#HttpxBinaryResponseContent.__init__ra   c                 C  rW   r?   )rh   rb   rG   rA   rA   rB   rb   �  rX   z"HttpxBinaryResponseContent.contentr_   c                 C  rW   r?   )rh   rc   rG   rA   rA   rB   rc   ‘  rX   zHttpxBinaryResponseContent.textrD   c                 C  rW   r?   )rh   ÚencodingrG   rA   rA   rB   r·   •  rX   z#HttpxBinaryResponseContent.encodingc                 C  rW   r?   )rh   Úcharset_encodingrG   rA   rA   rB   r¸   ™  rX   z+HttpxBinaryResponseContent.charset_encodingr£   r   c                 K  s   | j jdi |¤ŽS )NrA   )rh   rm   )r@   r£   rA   rA   rB   rm   �  s   zHttpxBinaryResponseContent.jsonc                 C  ó
   | j  ¡ S r?   )rh   ÚreadrG   rA   rA   rB   rº      r¶   zHttpxBinaryResponseContent.readNÚ
chunk_sizeú
int | NoneúIterator[bytes]c                 C  ó   | j  |¡S r?   )rh   Ú
iter_bytes©r@   r»   rA   rA   rB   r¿   £  ó   z%HttpxBinaryResponseContent.iter_bytesúIterator[str]c                 C  r¾   r?   )rh   Ú	iter_textrÀ   rA   rA   rB   rÃ   ¦  rÁ   z$HttpxBinaryResponseContent.iter_textc                 C  r¹   r?   )rh   Ú
iter_linesrG   rA   rA   rB   rÄ   ©  r¶   z%HttpxBinaryResponseContent.iter_linesc                 C  r¾   r?   )rh   Úiter_rawrÀ   rA   rA   rB   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Æ   Úfrn   rA   rA   rB   Ú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»   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   rA   rA   rB   Ústream_to_file¿  s
   	ÿ"ÿz)HttpxBinaryResponseContent.stream_to_filec                 C  r¹   r?   )rh   ÚcloserG   rA   rA   rB   rÒ   Ì  r¶   z HttpxBinaryResponseContent.closec                 Ã  ó   �| j  ¡ I d H S r?   )rh   ÚareadrG   rA   rA   rB   rÔ   Ï  ó   €z HttpxBinaryResponseContent.areadúAsyncIterator[bytes]c                 Ã  ó   �| j  |¡S r?   )rh   Úaiter_bytesrÀ   rA   rA   rB   rØ   Ò  ó   €z&HttpxBinaryResponseContent.aiter_bytesúAsyncIterator[str]c                 Ã  r×   r?   )rh   Ú
aiter_textrÀ   rA   rA   rB   rÛ   Õ  rÙ   z%HttpxBinaryResponseContent.aiter_textc                 Ã  s   �| j  ¡ S r?   )rh   Úaiter_linesrG   rA   rA   rB   rÜ   Ø  s   €
z&HttpxBinaryResponseContent.aiter_linesc                 Ã  r×   r?   )rh   Ú	aiter_rawrÀ   rA   rA   rB   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Î   rn   rA   rA   rB   Úastream_to_fileÞ  s   €
	ÿ.ÿz*HttpxBinaryResponseContent.astream_to_filec                 Ã  rÓ   r?   )rh   ÚacloserG   rA   rA   rB   râ   ì  rÕ   z!HttpxBinaryResponseContent.aclose)rh   r3   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•   rC   r–   rb   rc   r·   r¸   rm   rº   r¿   rÃ   rÄ   rÅ   rÏ   r   rÑ   rÒ   rÔ   rØ   rÛ   rÜ   rÝ   rá   râ   rA   rA   rA   rB   ry   ‡  sF   
 




ÿü



ÿüry   )rœ   r�   r=   rž   )rœ   r³   r=   r´   )AÚ
__future__r   Úosrv   ÚloggingÚdatetimer®   Útypingr   r   r   r   r   r   r	   r
   r   r   Ztyping_extensionsr   r   r   r   r   rÞ   r€   r|   Ú_typesr   Z_utilsr   r   r   r   Z_httpx2r   Z_modelsr   r   r   Ú
_constantsr   Z
_streamingr   r   r   r    Ú_exceptionsr!   r"   Z_base_clientr#   r$   r%   r&   Ú	getLoggerr‡   r'   r•   r(   rq   rr   r±   rµ   ry   rA   rA   rA   rB   Ú<module>   s@    0  2

