o
    hip                     @  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 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 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"m#Z# dd	l$m%Z%m&Z& dd
l'm(Z(m)Z) ddl*m+Z+m,Z,m-Z-m.Z. ddl/m0Z0m1Z1 e
rddl$m2Z2 ddl3m4Z4 edZ5edZ6edZ7edddZ8edddZ9e:e;Z<de=d< G dd dee6 Z>G dd de>e6 Z?G dd de>e6 Z@G dd  d e?eA ZBG d!d" d"e@eA ZCG d#d$ d$e?eA ZDG d%d& d&e@eA ZEG d'd( d(eFZGG d)d* d*e0ZHG d+d, d,ee8 ZIG d-d. d.ee9 ZJdUd3d4ZKdVd7d8ZLdWd=d>ZMdXdBdCZNdYdEdFZOdZdHdIZPd[dKdLZQd\dNdOZRd]dSdTZSdS )^    )annotationsN)TracebackType)
TYPE_CHECKINGAnyUnionGenericTypeVarCallableIteratorAsyncIteratorcastoverload)	Awaitable	ParamSpecoverride
get_origin   )NoneType)is_givenextract_type_argis_annotated_typeis_type_alias_typeextract_type_var_from_base)	BaseModelis_basemodel)RAW_RESPONSE_HEADEROVERRIDE_CAST_TO_HEADER)StreamAsyncStreamis_stream_class_typeextract_stream_chunk_type)	GroqErrorAPIResponseValidationError)FinalRequestOptions)
BaseClientPR_T_APIResponseTzAPIResponse[Any])bound_AsyncAPIResponseTzAsyncAPIResponse[Any]zlogging.Loggerlogc                   @  s   e Zd ZU 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dZed<ddZed=d!d"Zed>d#d$Zed?d&d'Z	ed@d)d*Z
ed@d+d,ZedAd.d/ZedBd0d1Zed@d2d3Zd4d5dCd9d:Zd4S )DBaseAPIResponsetype[R]_cast_toBaseClient[Any, Any]_clientzdict[type[Any], Any]_parsed_by_typebool_is_sse_stream1type[Stream[Any]] | type[AsyncStream[Any]] | None_stream_clsr#   _optionshttpx.Responsehttp_responseintretries_takenr   )r:   rawcast_toclientstream
stream_clsoptionsreturnNonec                C  s4   || _ || _i | _|| _|| _|| _|| _|| _d S N)r.   r0   r1   r3   r5   r6   r8   r:   )selfr;   r<   r=   r>   r?   r@   r:    rE   b/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/groq/_response.py__init__=   s   
zBaseAPIResponse.__init__httpx.Headersc                 C     | j jS rC   )r8   headersrD   rE   rE   rF   rJ   Q      zBaseAPIResponse.headershttpx.Requestc                 C  rI   )zHReturns the httpx Request instance associated with the current response.)r8   requestrK   rE   rE   rF   http_requestU      zBaseAPIResponse.http_requestc                 C  rI   rC   )r8   status_coderK   rE   rE   rF   rQ   Z   rL   zBaseAPIResponse.status_code	httpx.URLc                 C  rI   )z/Returns the URL for which the request was made.)r8   urlrK   rE   rE   rF   rS   ^   rP   zBaseAPIResponse.urlstrc                 C  rI   rC   )rO   methodrK   rE   rE   rF   rU   c   rL   zBaseAPIResponse.methodc                 C  rI   rC   )r8   http_versionrK   rE   rE   rF   rV   g   rL   zBaseAPIResponse.http_versiondatetime.timedeltac                 C  rI   )zCThe time taken for the complete request/response cycle to complete.)r8   elapsedrK   rE   rE   rF   rX   k   rP   zBaseAPIResponse.elapsedc                 C  rI   )a  Whether or not the response body has been closed.

        If this is False then there is response data that has not been read yet.
        You must either fully consume the response body or call `.close()`
        before discarding the response to prevent resource leaks.
        )r8   	is_closedrK   rE   rE   rF   rY   p   s   zBaseAPIResponse.is_closedc              	   C  s*   d| j j d| j d| jj d| j d	S )N<z [ z] type=>)	__class____name__rQ   r8   reason_phraser.   rK   rE   rE   rF   __repr__z   s   (zBaseAPIResponse.__repr__Ntorb   type[_T] | NoneR | _Tc          
      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|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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rt/|roz|0 }W n t1ye }	 zt23dt4|	|	 W Y d }	~	nd }	~	ww | jj5|||dS | jj6rt7|d| d|jd|jS |0 }| jj5|||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)r<   responser=   r4   truez+Unexpected state - cast_to is `APIResponse`z:Subclasses of httpx.Response cannot be passed to `cast_to`zTPydantic models must subclass our base model type, e.g. `from groq 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)datar<   rf   zMExpected Content-Type response header to be `application/json` but received `z
` instead.)rf   messagebody)8r.   r   	__value__r   r   r   r3   r   	TypeErrorr   r   r   r'   r    r8   r   r0   r5   r&   _default_stream_clsMissingStreamClassErrorr   rT   textbytescontentr9   floatr2   lowerAPIResponseRuntimeErrorinspectisclass
issubclasshttpxResponse
ValueErrorr   pydanticobjectlistdictr   rJ   getsplitr   json	Exceptionr+   debugtype_process_response_data_strict_response_validationr"   )
rD   rb   r<   originr?   rf   content_type_rk   excrE   rE   rF   _parse   s   


	
	







6



	zBaseAPIResponse._parse)r;   r7   r<   r-   r=   r/   r>   r2   r?   r4   r@   r#   r:   r9   rA   rB   )rA   rH   )rA   rM   )rA   r9   )rA   rR   rA   rT   )rA   rW   )rA   r2   rb   rc   rA   rd   )r^   
__module____qualname____annotations__rG   propertyrJ   rO   rQ   rS   rU   rV   rX   rY   r   r`   r   rE   rE   rE   rF   r,   0   s>   
 	r,   c                   @     e Zd Zed$ddZed%ddZd	d
d&ddZd'ddZd(ddZd)ddZd*ddZd+d,ddZ	d+d-d d!Z
d.d"d#Zd	S )/rw   rb   type[_T]rA   r'   c                C     d S rC   rE   rD   rb   rE   rE   rF   parse     zAPIResponse.parser&   c                 C  r   rC   rE   rK   rE   rE   rF   r     r   Nra   rc   rd   c                C  sj   |dur|n| j }| j|}|dur|S | js|   | j|d}t| jjr.| j|}|| j|< |S )a  Returns the rich python representation of this response's data.

        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 groq 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`
        Nra   	r.   r1   r   r3   readr   r   r6   post_parserrD   rb   	cache_keycachedparsedrE   rE   rF   r     s   
rs   c              
   C  s0   z| j  W S  tjy } zt |d}~ww z,Read and return the binary response content.N)r8   r   r|   StreamConsumedStreamAlreadyConsumedrD   r   rE   rE   rF   r   B  s   zAPIResponse.readrT   c                 C  s   |    | jjS )3Read and decode the response content into a string.r   r8   rr   rK   rE   rE   rF   rr   L  s   zAPIResponse.textr   c                 C  s   |    | j S )*Read and decode the JSON response content.r   r8   r   rK   rE   rE   rF   r   Q  s   
zAPIResponse.jsonrB   c                 C  s   | j   dS zClose the response and release the connection.

        Automatically called if the response body is read to completion.
        N)r8   closerK   rE   rE   rF   r   V  s   zAPIResponse.close
chunk_size
int | NoneIterator[bytes]c                 c      | j |D ]}|V  qdS z
        A byte-iterator over the decoded response content.

        This automatically handles gzip, deflate and brotli encoded responses.
        N)r8   
iter_bytesrD   r   chunkrE   rE   rF   r   ]  s   zAPIResponse.iter_bytesIterator[str]c                 c  r   zA str-iterator over the decoded response content
        that handles both gzip, deflate, etc but also detects the content's
        string encoding.
        N)r8   	iter_textr   rE   rE   rF   r   f  s   zAPIResponse.iter_textc                 c  s    | j  D ]}|V  qdS z;Like `iter_text()` but will only yield chunks for each lineN)r8   
iter_linesrD   r   rE   rE   rF   r   n  s   zAPIResponse.iter_linesrb   r   rA   r'   rA   r&   r   rA   rs   r   rA   r   rA   rB   rC   )r   r   rA   r   )r   r   rA   r   )rA   r   r^   r   r   r   r   r   rr   r   r   r   r   r   rE   rE   rE   rF   rw     s    
-



	rw   c                   @  r   )/AsyncAPIResponserb   r   rA   r'   c                     d S rC   rE   r   rE   rE   rF   r   u     zAsyncAPIResponse.parser&   c                   r   rC   rE   rK   rE   rE   rF   r   x  r   Nra   rc   rd   c                  sr   |dur|n| j }| j|}|dur|S | js |  I dH  | j|d}t| jjr2| j|}|| j|< |S )aY  Returns the rich python representation of this response's data.

        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 groq import BaseModel


        class MyModel(BaseModel):
            foo: str


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

        We support parsing:
          - `BaseModel`
          - `dict`
          - `list`
          - `Union`
          - `str`
          - `httpx.Response`
        Nra   r   r   rE   rE   rF   r   {  s   
rs   c              
     s8   z	| j  I dH W S  tjy } zt |d}~ww r   )r8   areadr|   r   r   r   rE   rE   rF   r     s   zAsyncAPIResponse.readrT   c                   s   |   I dH  | jjS )r   Nr   rK   rE   rE   rF   rr     s   zAsyncAPIResponse.textr   c                   s   |   I dH  | j S )r   Nr   rK   rE   rE   rF   r     s   
zAsyncAPIResponse.jsonrB   c                   s   | j  I dH  dS r   )r8   acloserK   rE   rE   rF   r     s   zAsyncAPIResponse.closer   r   AsyncIterator[bytes]c                 C (   | j |2 z	3 dH W }|V  q6 dS r   )r8   aiter_bytesr   rE   rE   rF   r     s   zAsyncAPIResponse.iter_bytesAsyncIterator[str]c                 C r   r   )r8   
aiter_textr   rE   rE   rF   r     s   zAsyncAPIResponse.iter_textc                 C s&   | j  2 z	3 dH W }|V  q6 dS r   )r8   aiter_linesr   rE   rE   rF   r     s   zAsyncAPIResponse.iter_linesr   r   r   r   r   r   r   rC   )r   r   rA   r   )r   r   rA   r   )rA   r   r   rE   rE   rE   rF   r   t  s    
+



	r   c                   @     e Zd ZdZd	ddZdS )
BinaryAPIResponse3  Subclass of APIResponse providing helpers for dealing with binary data.

    Note: If you want to stream the response data instead of eagerly reading it
    all at once then you should use `.with_streaming_response` when making
    the API request, e.g. `.with_streaming_response.get_binary_response()`
    filestr | os.PathLike[str]rA   rB   c                 C  sJ   t |dd}|  D ]}|| qW d   dS 1 sw   Y  dS a^  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. `.with_streaming_response.get_binary_response()`
        wbmodeNopenr   write)rD   r   frk   rE   rE   rF   write_to_file  s
   "zBinaryAPIResponse.write_to_fileNr   r   rA   rB   r^   r   r   __doc__r   rE   rE   rE   rF   r         r   c                   @  r   )
AsyncBinaryAPIResponser   r   r   rA   rB   c              	     s   t |}|jddI dH 4 I dH  }|  2 z3 dH W }||I dH  q6 W d  I dH  dS 1 I dH s9w   Y  dS r   anyioPathr   r   r   )rD   r   pathr   rk   rE   rE   rF   r     s   
.z$AsyncBinaryAPIResponse.write_to_fileNr   r   rE   rE   rE   rF   r     r   r   c                   @     e Zd Zdddd	d
ZdS )StreamedBinaryAPIResponseNr   r   r   r   r   rA   rB   c                C  sL   t |dd}| |D ]}|| qW d   dS 1 sw   Y  dS zuStreams the output to the given file.

        Accepts a filename or any path-like object, e.g. pathlib.Path
        r   r   Nr   )rD   r   r   r   rk   rE   rE   rF   stream_to_file  s
   
"z(StreamedBinaryAPIResponse.stream_to_filer   r   r   r   rA   rB   r^   r   r   r   rE   rE   rE   rF   r         r   c                   @  r   )AsyncStreamedBinaryAPIResponseNr   r   r   r   r   rA   rB   c             	     s   t |}|jddI dH 4 I dH !}| |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   r   )rD   r   r   r   r   rk   rE   rE   rF   r     s   

.z-AsyncStreamedBinaryAPIResponse.stream_to_filer   r   rE   rE   rE   rF   r     r   r   c                      s   e Zd Zd fddZ  ZS )rq   rA   rB   c                   s   t  d d S )NzxThe `stream` argument was set to `True` but the `stream_cls` argument was not given. See `groq._streaming` for referencesuperrG   rK   r]   rE   rF   rG   -  s   z MissingStreamClassError.__init__r   )r^   r   r   rG   __classcell__rE   rE   r   rF   rq   ,  s    rq   c                      s"   e Zd ZdZd fddZ  ZS )r   aG  
    Attempted to read or stream content, but the content has already
    been streamed.

    This can happen if you use a method like `.iter_lines()` and then attempt
    to read th entire response body afterwards, e.g.

    ```py
    response = await client.post(...)
    async for line in response.iter_lines():
        ...  # do something with `line`

    content = await response.read()
    # ^ error
    ```

    If you want this behaviour you'll need to either manually accumulate the response
    content or call `await response.read()` before iterating over the stream.
    rA   rB   c                   s   d}t  | d S )Na&  Attempted to read or stream some content, but the content has already been streamed. This could be due to attempting to stream the response content more than once.

You can fix this by manually accumulating the response content while streaming or by calling `.read()` before starting to stream.r   )rD   rl   r   rE   rF   rG   H  s   	zStreamAlreadyConsumed.__init__r   )r^   r   r   r   rG   r   rE   rE   r   rF   r   3  s    r   c                   @  .   e Zd ZdZdddZdd	d
ZdddZdS )ResponseContextManagerContext manager for ensuring that a request is not made
    until it is entered and that the response will always be closed
    when the context manager exits
    request_funcCallable[[], _APIResponseT]rA   rB   c                 C     || _ d | _d S rC   _request_func!_ResponseContextManager__response)rD   r   rE   rE   rF   rG   [     
zResponseContextManager.__init__r(   c                 C  s   |   | _| jS rC   r   rK   rE   rE   rF   	__enter___  s   
z ResponseContextManager.__enter__exc_typetype[BaseException] | Noner   BaseException | Noneexc_tbTracebackType | Nonec                 C  s   | j d ur| j   d S d S rC   )r   r   rD   r   r   r   rE   rE   rF   __exit__c  s   
zResponseContextManager.__exit__N)r   r   rA   rB   )rA   r(   r   r   r   r   r   r   rA   rB   )r^   r   r   r   rG   r   r  rE   rE   rE   rF   r   U  
    

r   c                   @  r   )AsyncResponseContextManagerr   api_requestAwaitable[_AsyncAPIResponseT]rA   rB   c                 C  r   rC   _api_request&_AsyncResponseContextManager__response)rD   r  rE   rE   rF   rG   s  r   z$AsyncResponseContextManager.__init__r*   c                   s   | j I d H | _| jS rC   r  rK   rE   rE   rF   
__aenter__w  s   z&AsyncResponseContextManager.__aenter__r   r   r   r   r   r   c                   s$   | j d ur| j  I d H  d S d S rC   )r	  r   r   rE   rE   rF   	__aexit__{  s   
z%AsyncResponseContextManager.__aexit__N)r  r  rA   rB   )rA   r*   r  )r^   r   r   r   rG   r
  r  rE   rE   rE   rF   r  m  r  r  funcCallable[P, R]rA   3Callable[P, ResponseContextManager[APIResponse[R]]]c                      t  d
 fdd}|S )Higher order function that takes one of our bound API methods and wraps it
    to support streaming and returning the raw `APIResponse` object directly.
    argsP.argskwargsP.kwargsrA   &ResponseContextManager[APIResponse[R]]c                    sZ   i t t|dp
i }d|t< ||d< tj g| R i |}tt tg tt	 f |S Nextra_headersr>   )
r   r   r   r   	functoolspartialr   r	   rw   r&   r  r  r  make_requestr  rE   rF   wrapped  s
   z-to_streamed_response_wrapper.<locals>.wrappedN)r  r  r  r  rA   r  r  wrapsr  r  rE   r  rF   to_streamed_response_wrapper  s   
r!  Callable[P, Awaitable[R]]=Callable[P, AsyncResponseContextManager[AsyncAPIResponse[R]]]c                   r  )r  r  r  r  r  rA   0AsyncResponseContextManager[AsyncAPIResponse[R]]c                    sL   i t t|dp
i }d|t< ||d<  | i |}tt ttt  |S r  )r   r   r   r   r  r   r   r&   r  r  rE   rF   r    s
   z3async_to_streamed_response_wrapper.<locals>.wrappedN)r  r  r  r  rA   r$  r  r   rE   r  rF   "async_to_streamed_response_wrapper  s   
r%  Callable[P, object]response_clstype[_APIResponseT]2Callable[P, ResponseContextManager[_APIResponseT]]c                      t  d
 fdd}|S )'  Higher order function that takes one of our bound API methods and an `APIResponse` class
    and wraps the method to support streaming and returning the given response class directly.

    Note: the given `response_cls` *must* be concrete, e.g. `class BinaryAPIResponse(APIResponse[bytes])`
    r  r  r  r  rA   %ResponseContextManager[_APIResponseT]c                    s^   i t t|dp
i }d|t< |t< ||d< tj g| R i |}tt tg t	f |S r  )
r   r   r   r   r   r  r  r   r	   r(   r  r  r'  rE   rF   r    s   z4to_custom_streamed_response_wrapper.<locals>.wrappedN)r  r  r  r  rA   r,  r  r  r'  r  rE   r-  rF   #to_custom_streamed_response_wrapper     
r/  Callable[P, Awaitable[object]]type[_AsyncAPIResponseT]<Callable[P, AsyncResponseContextManager[_AsyncAPIResponseT]]c                   r*  )r+  r  r  r  r  rA   /AsyncResponseContextManager[_AsyncAPIResponseT]c                    sP   i t t|dp
i }d|t< |t< ||d<  | i |}tt tt |S r  )r   r   r   r   r   r  r   r*   r  r-  rE   rF   r    s   z:async_to_custom_streamed_response_wrapper.<locals>.wrappedN)r  r  r  r  rA   r4  r  r.  rE   r-  rF   )async_to_custom_streamed_response_wrapper  r0  r5  Callable[P, APIResponse[R]]c                   r  )Higher order function that takes one of our bound API methods and wraps it
    to support returning the raw `APIResponse` object directly.
    r  r  r  r  rA   APIResponse[R]c                    s@   i t t|dp
i }d|t< ||d< t tt  | i |S Nr  r;   )r   r   r   r   rw   r&   r  r  r  r  rE   rF   r    s   z(to_raw_response_wrapper.<locals>.wrappedN)r  r  r  r  rA   r8  r  r   rE   r  rF   to_raw_response_wrapper     r;  +Callable[P, Awaitable[AsyncAPIResponse[R]]]c                   r  )r7  r  r  r  r  rA   AsyncAPIResponse[R]c                    sH   i t t|dpi }d|t< ||d< t tt  | i |I d H S r9  )r   r   r   r   r   r&   r:  r  rE   rF   r    s
   z.async_to_raw_response_wrapper.<locals>.wrappedN)r  r  r  r  rA   r>  r  r   rE   r  rF   async_to_raw_response_wrapper  r<  r?  Callable[P, _APIResponseT]c                   r*  )  Higher order function that takes one of our bound API methods and an `APIResponse` class
    and wraps the method to support returning the given response class directly.

    Note: the given `response_cls` *must* be concrete, e.g. `class BinaryAPIResponse(APIResponse[bytes])`
    r  r  r  r  rA   r(   c                    sD   i t t|dp
i }d|t< |t< ||d< t t | i |S r9  )r   r   r   r   r   r(   r:  r-  rE   rF   r    s
   z/to_custom_raw_response_wrapper.<locals>.wrappedN)r  r  r  r  rA   r(   r  r.  rE   r-  rF   to_custom_raw_response_wrapper     
	rB  *Callable[P, Awaitable[_AsyncAPIResponseT]]c                   r*  )rA  r  r  r  r  rA   r  c                    sH   i t t|dp
i }d|t< |t< ||d< t tt  | i |S r9  )r   r   r   r   r   r   r*   r:  r-  rE   rF   r  "  s
   z5async_to_custom_raw_response_wrapper.<locals>.wrappedN)r  r  r  r  rA   r  r  r.  rE   r-  rF   $async_to_custom_raw_response_wrapper  rC  rE  typtype[BaseAPIResponse[Any]]r   c                 C  s   t | tdtttfddS )a  Given a type like `APIResponse[T]`, returns the generic type variable `T`.

    This also handles the case where a concrete subclass is given, e.g.
    ```py
    class MyResponse(APIResponse[bytes]):
        ...

    extract_response_type(MyResponse) -> bytes
    ```
    ztuple[type, ...]r   )generic_basesindex)r   r   r,   rw   r   )rF  rE   rE   rF   extract_response_type/  s
   rJ  )r  r  rA   r  )r  r"  rA   r#  )r  r&  r'  r(  rA   r)  )r  r1  r'  r2  rA   r3  )r  r  rA   r6  )r  r"  rA   r=  )r  r&  r'  r(  rA   r@  )r  r1  r'  r2  rA   rD  )rF  rG  rA   r   )T
__future__r   osry   loggingdatetimer  typesr   typingr   r   r   r   r   r	   r
   r   r   r   typing_extensionsr   r   r   r   r   r|   r   _typesr   _utilsr   r   r   r   r   _modelsr   r   
_constantsr   r   
_streamingr   r   r   r    _exceptionsr!   r"   r#   _base_clientr$   r%   r&   r'   r(   r*   	getLoggerr^   r+   r   r,   rw   r   rs   r   r   r   r   ro   rq   r   r   r  r!  r%  r/  r5  r;  r?  rB  rE  rJ  rE   rE   rE   rF   <module>   s`    0 _fd"







