
    
@gv>                       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rddl!m.Z. ddl/m0Z0  ed          Z1 ed          Z2 ed          Z3 ej4        e5          Z6de7d<    G d dee2                   Z8 G d de9          Z:d!dZ;d"dZ< G 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                     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d<dZed=d            Zed>d"            Z	ed?d$            Z	d%d&d@d)Z	edAd+            Z
edBd-            ZedCd.            ZedDd0            ZedEd2            ZedFd4            ZedEd5            ZedEd6            ZedGd7            ZedHd9            Zd%d&d@d:ZedEd;            Zd%S )I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   )r6   rawcast_toclientstream
stream_clsoptionsreturnNonec               v    || _         || _        i | _        || _        || _        || _        || _        || _        d S N)r*   r,   r-   r/   r1   r2   r4   r6   )selfr7   r8   r9   r:   r;   r<   r6   s           K/var/www/piapp/venv/lib/python3.11/site-packages/openai/_legacy_response.py__init__zLegacyAPIResponse.__init__F   sF      !% *    
str | Nonec                @    | j         j                            d          S )Nzx-request-id)r4   headersgetrA   s    rB   
request_idzLegacyAPIResponse.request_idZ   s    !)--n===rD   totype[_T]r%   c                   d S r@    )rA   rK   s     rB   parsezLegacyAPIResponse.parse^   s    ,/CrD   r$   c                    d S r@   rN   rI   s    rB   rO   zLegacyAPIResponse.parsea   s    #rD   NrK   type[_T] | NoneR | _Tc               x   ||n| j         }| j                            |          }||S |                     |          }t	          | j        j                  r| j                            |          }t          |t                    rt          || 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`
        NrQ   )r*   r-   rH   _parser   r2   post_parser
isinstancer   r   rJ   r   r$   )rA   rK   	cache_keycachedparseds        rB   rO   zLegacyAPIResponse.parsed   s    @ .BBdm	%)))44M##DM-.. 	7]..v66Ffi(( 	464?333*0Y'AvrD   httpx.Headersc                    | j         j        S r@   )r4   rG   rI   s    rB   rG   zLegacyAPIResponse.headers       !))rD   httpx.Requestc                    | j         j        S r@   )r4   requestrI   s    rB   http_requestzLegacyAPIResponse.http_request   r]   rD   c                    | j         j        S r@   )r4   status_coderI   s    rB   rc   zLegacyAPIResponse.status_code   s    !--rD   	httpx.URLc                    | j         j        S r@   )r4   urlrI   s    rB   rf   zLegacyAPIResponse.url   s    !%%rD   strc                    | j         j        S r@   )ra   methodrI   s    rB   ri   zLegacyAPIResponse.method   s     ''rD   bytesc                    | j         j        S )zReturn the binary response content.

        NOTE: this will be removed in favour of `.read()` in the
        next major version.
        )r4   contentrI   s    rB   rl   zLegacyAPIResponse.content   s     !))rD   c                    | j         j        S )zyReturn the decoded response content.

        NOTE: this will be turned into a method in the next major version.
        )r4   textrI   s    rB   rn   zLegacyAPIResponse.text   s     !&&rD   c                    | j         j        S r@   )r4   http_versionrI   s    rB   rp   zLegacyAPIResponse.http_version   s    !..rD   c                    | j         j        S r@   )r4   	is_closedrI   s    rB   rr   zLegacyAPIResponse.is_closed   s    !++rD   datetime.timedeltac                    | j         j        S )zCThe time taken for the complete request/response cycle to complete.)r4   elapsedrI   s    rB   ru   zLegacyAPIResponse.elapsed   s     !))rD   c               
   |rt          |          rt          |d          }| j        rK|r{t          |          st	          dt
           dt                     t          t           |t          |d          | j
        t          t          | j                                      S | j        rZt          t          |                     t          | j                  | j
        t          t          | j                                      S t          d| j        j                  }|t!                      t          t           || j        | j
        t          t          | j                                      S ||n| j        }t          |          rt          |d          }|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    r0t          t          |j                                        dk              S t3          |          p|}t5          j        |          r3t9          |t:                    rt          t           ||                    S |t<          k    rt?          d	          t5          j        |          rNt9          |t@          j!                  r4|t@          j!        k    rtE          d
          t          t          |          S t5          j        |          r>t9          |tF                    s)t9          |tH          j#                  rt	          d          |tJ          ur|tL          urw|tN          urn|tP          uret9          |tF                    sPt?          d| dtF           dtN           dtL           dtP           dt$           dt&           dt@          j!         d          |j)        *                    dd          +                    d          ^}}|dk    rtY          |          rr	 |-                                }| j        .                    |||          S # t^          $ r3}	t`          1                    dte          |	          |	           Y d }	~	nd }	~	ww xY w| j        j3        rti          |d| d|j                  |j        S |-                                }| j        .                    |||          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)r8   responser9   r0   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/json)datar8   rx   z5Could not read JSON from response data due to %s - %szMExpected Content-Type response header to be `application/json` but received `z
` instead.)rx   messagebody)5r   r   r/   r   	TypeErrorr   r   r   r%   r   r4   r   r,   r1   r$   _default_stream_clsMissingStreamClassErrorr*   r   rg   rn   r5   floatr.   lowerr   inspectisclass
issubclassHttpxBinaryResponseContentr(   RuntimeErrorhttpxResponse
ValueErrorr   pydanticobjectlistdictr   rG   rH   splitr   json_process_response_data	Exceptionr&   debugtype_strict_response_validationr    )
rA   rK   r;   r8   rx   origincontent_type_r}   excs
             rB   rU   zLegacyAPIResponse._parse   s    	)#B'' 	)!"a((B< &	 +B// q#$oV\$o$obm$o$opppB 9 -D! ! ! "&!3#C66  
 
 
  $$ 9$:J K K!%!3#C66 %     QSWS_SsttJ!-///
 M!/T\22     ""DM W%% 	3&w22Gh4== %c>>8=)))c>>3x}--...e5//000d??8=..00F:;;;G$$/?6"" 	.z&:T'U'U 	.778,,---&&&LMMM?6"" 	%z&%.'I'I 	% %.(( !^___8$$$?6"" 	v:fi+H+H 	vZX^`h`rMsMs 	vtuuu 6!!dNNdNNeOOvy11 $  Yg  Y  YI  Y  YY]  Y  Yae  Y  Yin  Y  Yrz  Y  Y  B  Y  Y  HM  HV  Y  Y  Y   $+//DDJJ3OOq---G$$ 
	#==??D  <>>! '!) ?    ! g g gIIUW[\_W`W`beffffffffg |7 0% Elx  E  E  E!    = }}|22 3 
 
 	
s   =R. .
S+8)S&&S+c                B    d| j          d| j        j         d| j         dS )Nz<APIResponse [ z] type=>)rc   r4   reason_phraser*   rI   s    rB   __repr__zLegacyAPIResponse.__repr__L  s0    l 0ll43E3Sll\`\illllrD   )r7   r3   r8   r)   r9   r+   r:   r.   r;   r0   r<   r!   r6   r5   r=   r>   r=   rE   )rK   rL   r=   r%   )r=   r$   )rK   rR   r=   rS   )r=   r[   )r=   r^   )r=   r5   )r=   rd   r=   rg   r=   rj   )r=   r.   )r=   rs   )__name__
__module____qualname____doc____annotations__rC   propertyrJ   r   rO   rG   ra   rc   rf   ri   rl   rn   rp   rr   ru   rU   r   r   rN   rD   rB   r(   r(   -   s        
 
 !!!!))))MMMBBBB!!!!!!!!M + + + + + +( > > > X> /// X/ X-1 - - - - - -^ * * * X* * * * X* . . . X. & & & X& ( ( ( X( * * * X* ' ' ' X' / / / X/ , , , X, * * * X* /3 E
 E
 E
 E
 E
 E
N m m m Xm m mrD   r(   c                        e Zd Zd fdZ xZS )r   r=   r>   c                J    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   )rA   	__class__s    rB   rC   z MissingStreamClassError.__init__R  s/     I	
 	
 	
 	
 	
rD   r=   r>   )r   r   r   rC   __classcell__)r   s   @rB   r   r   Q  s=        
 
 
 
 
 
 
 
 
 
rD   r   funcCallable[P, R]r=   !Callable[P, LegacyAPIResponse[R]]c                H     t          j                   d f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                     i t          t          |                    d                    pi }d|t          <   ||d<   t          t          t
                    | i |          S Nextra_headersry   r   r   rH   r   r(   r$   r   r   r   r   s      rB   wrappedz(to_raw_response_wrapper.<locals>.wrapped]  sc    (ZDfjj6Q6Q,R,R,XVX(Z-3)*"/%a($$*?*?*?@@@rD   r   r   r   r   r=   r   	functoolswrapsr   r   s   ` rB   to_raw_response_wrapperr   X  sE    
 _TA A A A A A NrD   Callable[P, Awaitable[R]],Callable[P, Awaitable[LegacyAPIResponse[R]]]c                H     t          j                   d fd            }|S )	r   r   r   r   r   r=   r   c                    K   i t          t          |                    d                    pi }d|t          <   ||d<   t          t          t
                    | i | d {V           S r   r   r   s      rB   r   z.async_to_raw_response_wrapper.<locals>.wrappedn  sy      (ZDfjj6Q6Q,R,R,XVX(Z-3)*"/%a(d0Ef0E0E*E*E*E*E*E*EFFFrD   r   r   r   s   ` rB   async_to_raw_response_wrapperr   i  sE    
 _TG G G G G G NrD   c                  ^   e Zd ZU ded<   d*dZed+d            Zed,d	            Zed-d            Zed-d            Z	d.dZ
d+dZd/d0dZd/d1dZd2dZd/d0dZd3dZ ed          ddd4d            Zd5d Zd+d!Zd/d6d#Zd/d7d%Zd8d&Zd/d6d'Z ed          ddd4d(            Zd5d)ZdS )9r   r3   rx   r=   r>   c                    || _         d S r@   )rx   )rA   rx   s     rB   rC   z#HttpxBinaryResponseContent.__init__}  s     rD   rj   c                    | j         j        S r@   )rx   rl   rI   s    rB   rl   z"HttpxBinaryResponseContent.content  s    }$$rD   rg   c                    | j         j        S r@   )rx   rn   rI   s    rB   rn   zHttpxBinaryResponseContent.text  s    }!!rD   rE   c                    | j         j        S r@   )rx   encodingrI   s    rB   r   z#HttpxBinaryResponseContent.encoding  s    }%%rD   c                    | j         j        S r@   )rx   charset_encodingrI   s    rB   r   z+HttpxBinaryResponseContent.charset_encoding  s    }--rD   r   r   c                &     | j         j        di |S )NrN   )rx   r   )rA   r   s     rB   r   zHttpxBinaryResponseContent.json  s    !t}!++F+++rD   c                4    | j                                         S r@   )rx   readrI   s    rB   r   zHttpxBinaryResponseContent.read  s    }!!###rD   N
chunk_size
int | NoneIterator[bytes]c                6    | j                             |          S r@   )rx   
iter_bytesrA   r   s     rB   r   z%HttpxBinaryResponseContent.iter_bytes  s    }''
333rD   Iterator[str]c                6    | j                             |          S r@   )rx   	iter_textr   s     rB   r   z$HttpxBinaryResponseContent.iter_text  s    }&&z222rD   c                4    | j                                         S r@   )rx   
iter_linesrI   s    rB   r   z%HttpxBinaryResponseContent.iter_lines  s    }'')))rD   c                6    | j                             |          S r@   )rx   iter_rawr   s     rB   r   z#HttpxBinaryResponseContent.iter_raw  s    }%%j111rD   filestr | os.PathLike[str]c                    t          |d          5 }| j                                        D ]}|                    |           	 ddd           dS # 1 swxY 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openrx   r   write)rA   r   fr}   s       rB   write_to_filez(HttpxBinaryResponseContent.write_to_file  s     $T""" 	a0022  	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   2AAAzDue to a bug, this method doesn't actually stream the response content, `.with_streaming_response.method()` should be used instead)r   c                   t          |d          5 }| j                            |          D ]}|                    |           	 d d d            d S # 1 swxY w Y   d S Nr   r   r   )rA   r   r   r   r}   s        rB   stream_to_filez)HttpxBinaryResponseContent.stream_to_file  s     $T""" 	a00<<  	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   3AAAc                4    | j                                         S r@   )rx   closerI   s    rB   r   z HttpxBinaryResponseContent.close  s    }""$$$rD   c                D   K   | j                                          d {V S r@   )rx   areadrI   s    rB   r   z HttpxBinaryResponseContent.aread  s,      ]((*********rD   AsyncIterator[bytes]c                :   K   | j                             |          S r@   )rx   aiter_bytesr   s     rB   r   z&HttpxBinaryResponseContent.aiter_bytes  s      }((444rD   AsyncIterator[str]c                :   K   | j                             |          S r@   )rx   
aiter_textr   s     rB   r   z%HttpxBinaryResponseContent.aiter_text  s      }''
333rD   c                8   K   | j                                         S r@   )rx   aiter_linesrI   s    rB   r   z&HttpxBinaryResponseContent.aiter_lines  s      }((***rD   c                :   K   | j                             |          S r@   )rx   	aiter_rawr   s     rB   r   z$HttpxBinaryResponseContent.aiter_raw  s      }&&z222rD   c               >  K   t          j        |          }|                    d           d {V 4 d {V }| j                            |          2 3 d {V }|                    |           d {V  #6 	 d d d           d {V  d S # 1 d {V swxY w Y   d S r   )anyioPathr   rx   r   r   )rA   r   r   pathr   r}   s         rB   astream_to_filez*HttpxBinaryResponseContent.astream_to_file  s^      z$-------- 	$ 	$ 	$ 	$ 	$ 	$ 	$"m77
CC $ $ $ $ $ $ $dggdmm######## DC	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$s   BA7B
BBc                D   K   | j                                          d {V S r@   )rx   acloserI   s    rB   r  z!HttpxBinaryResponseContent.aclose  s,      ]))+++++++++rD   )rx   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   rl   rn   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  rN   rD   rB   r   r   z  st        ! ! ! ! % % % X% " " " X" & & & X& . . . X., , , ,$ $ $ $4 4 4 4 43 3 3 3 3* * * *2 2 2 2 2     Z 	M  "&	     % % % %+ + + +5 5 5 5 54 4 4 4 4+ + + +3 3 3 3 3 Z 	M  "&		$ 	$ 	$ 	$ 	$ 	$, , , , , ,rD   r   )r   r   r=   r   )r   r   r=   r   )>
__future__r   osr   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   _modelsr   r   r   
_constantsr   
_streamingr   r   r   r   _exceptionsr    r!   _base_clientr"   r#   r$   r%   	getLoggerr   r&   r   r(   r   r   r   r   r   rN   rD   rB   <module>r     s   " " " " " " " 				                               U T T T T T T T T T T T T T          A A A A A A A A A A < < < < < < < < < < + + + + + + \ \ \ \ \ \ \ \ \ \ \ \ 3 3 3 3 3 3 ),,,,,,(((((( IcNNGCLLWT]]'g'11 1 1 1 1am am am am am
 am am amH	
 
 
 
 
i 
 
 
   "   "f, f, f, f, f, f, f, f, f, f,rD   