
    ~iZ2                       d dl mZ d dlZd dlZd dlmZmZ d dlmZ d dlmZm	Z	m
Z
mZmZ d dlmZ d dlZddlmZmZmZ dd	l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 ddl m!Z!m"Z" ddl#m$Z$ ddl%m&Z&m'Z' ddl(m)Z)m*Z*m+Z+m,Z,m-Z- ddl.m/Z/m0Z0 ddgZ1d dZ2d Z3d Z4 G d de!          Z5 G d de"          Z6 G d d          Z7 G d d          Z8dS )!    )annotationsN)	timedeltadatetime)random)DictListUnionIterableOptional)Literal   )ArkAPITimeoutErrorArkAPIConnectionErrorArkAPIStatusError)BodyQueryHeaders)with_sts_tokenasync_with_sts_token   )with_e2e_encryptionasync_with_e2e_encryption)make_request_options)SyncAPIResourceAsyncAPIResource)cached_property)to_raw_response_wrapperasync_to_raw_response_wrapper)ChatCompletionChatCompletionMessageParamcompletion_create_paramsChatCompletionToolParam#ChatCompletionToolChoiceOptionParam)INITIAL_RETRY_DELAYMAX_RETRY_DELAYCompletionsAsyncCompletionsreturnfloatc                    t          | t          t          z            }t          t          t          d|          z  t                    }ddt	                      z  z
  }||z  }|dk    r|ndS )Nr      g      ?r   )minr%   r$   powr   )retry_times	nbRetriessleep_secondsjittertimeouts        /root/.openclaw/workspace/.venvs/ark-sdk/lib/python3.11/site-packages/volcenginesdkarkruntime/resources/batch_chat/completions.py_calculate_retry_timeoutr4   2   sd    K3F!FGGI+c!Y.?.??QQM Ff$Gll77)    c                    | j                             d          }|#|                                rt          |          S d S )NzRetry-After)headersgetisdigitint)responseretry_afters     r3   _get_retry_afterr=   ;   sE    "&&}55K   	${###4r5   c                n    | j         dk    rdS | j         dk    rdS | j         dk    rdS | j         dk    rdS dS )Ni  Ti  i  i  F)status_code)r;   s    r3   _should_retryr@   C   sY    s""t s""t s""t s""t5r5   c                      e Zd Zed5d            Zeedddddddddddddddddddddddddd6d3                        Zd4 ZdS )7r&   r(   CompletionsWithRawResponsec                     t          |           S N)rB   selfs    r3   with_raw_responsezCompletions.with_raw_responseX   s    )$///r5   N)frequency_penaltyfunction_call
logit_biaslogprobs
max_tokenspresence_penaltystoptemperaturetoolstop_logprobstop_prepetition_penaltynparallel_tool_callsservice_tiertool_choiceresponse_formatthinkingmax_completion_tokensuserextra_headersextra_query
extra_bodyr2   messages$Iterable[ChatCompletionMessageParam]modelstrrH   Optional[float] | NonerI   ,completion_create_params.FunctionCall | NonerJ   Optional[Dict[str, int]] | NonerK   Optional[bool] | NonerL   Optional[int] | NonerM   rN   &Union[Optional[str], List[str]] | NonerO   rP   (Iterable[ChatCompletionToolParam] | NonerQ   rR   rS   rT   rU   rV   +Optional[Literal['auto', 'default']] | NonerW   *ChatCompletionToolChoiceOptionParam | NonerX   .completion_create_params.ResponseFormat | NonerY   (completion_create_params.Thinking | NonerZ   r[   
str | Noner\   Headers | Noner]   Query | Noner^   Body | Noner2   float | httpx.Timeout | Noner   c          
     P   d}|                      |          }| j                            |          }	 |                                 t	          j                    |k    rt          d d           	 |                     di d|d|d|d|d|d	|d
|d|d|	d|
d|d|d|d|d|d|d||||||dt          ||||          t                    }n# t          t          f$ rd t          |          }t	          j                    t          |          z   |k    rt          d d           t          j        |           |dz   }Y t          $ rL} t!          | j                  }!|!|                    |!           t'          | j                  rY d } ~ j| d } ~ ww xY w|S Nr   Tz/batch/chat/completionsr_   ra   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   r[   rS   rT   rU   )rV   rW   rX   rY   rZ   )r\   r]   r^   r2   )bodyoptionscast_tosecondsr+   )_get_request_last_time_clientget_model_breakerwaitr   nowr   _post_without_retryr   r   r   r4   r   timesleepr   r=   r;   resetr@   )"rF   r_   ra   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r2   
retryTimes	last_timemodel_breakerrespwaitTimeerrr<   s"                                     r3   createzCompletions.create\   s   @ 
//88	66u==6	   |~~	))(t4441//-"H ,-> (	
 %j #H %j +,<  &{   '    -.@  Q!" ./B#$ )5'2+:$,1F-  0 1&3$/#- '	   +A 0 ! !D '(=>   3J??<>>Ih$?$?$??)KK,T4888
8$$$'!^
$   .s|<<*!''444 .. HHHHI Ks&   /A*C A1F#	F#?FFF#c                   || j         j        }d}t          |t          j                  r|j        }nst          |t                    r|}n[t          | j         j        t                    r|}n9t          d	                    t          | j         j                                      t          j                    t          |          z   S Nr   z timeout type {} is not supportedrx   r{   r2   
isinstancehttpxTimeoutreadr)   r:   	TypeErrorformattyper   r~   r   rF   r2   timeoutSecondss      r3   rz   z"Completions._get_request_last_time       ?l*Ggu}-- 		$\NN'' 	$NN,c22 	$NN299$t|?S:T:TUU   |~~	. A A AAAr5   )r(   rB   )6r_   r`   ra   rb   rH   rc   rI   rd   rJ   re   rK   rf   rL   rg   rM   rc   rN   rh   rO   rc   rP   ri   rQ   rg   rR   rc   rS   rc   rT   rg   rU   rf   rV   rj   rW   rk   rX   rl   rY   rm   rZ   rg   r[   rn   r\   ro   r]   rp   r^   rq   r2   rr   r(   r   )	__name__
__module____qualname__r   rG   r   r   r   rz    r5   r3   r&   r&   W   s        0 0 0 _0  59FJ6:*.+/377;.2:>-1(,59"&59DHBFJN=A6:(,$("&049W W W W W  ^WrB B B B Br5   c                      e Zd Zed5d            Zeedddddddddddddddddddddddddd6d3                        Zd4 ZdS )7r'   r(   AsyncCompletionsWithRawResponsec                     t          |           S rD   )r   rE   s    r3   rG   z"AsyncCompletions.with_raw_response   s    .t444r5   N)rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   r[   rS   rT   rU   rV   rW   rX   rY   rZ   r\   r]   r^   r2   r_   r`   ra   rb   rH   rc   rI   rd   rJ   re   rK   rf   rL   rg   rM   rN   rh   rO   rP   ri   rQ   rR   r[   rn   rS   rT   rU   rV   rj   rW   rk   rX   rl   rY   rm   rZ   r\   ro   r]   rp   r^   rq   r2   rr   r   c          
       K   d}|                      |          }| j                            |           d {V }	 |                                 d {V  t	          j                    |k    rt          d d           	 |                     di d|d|d|d|d|d	|d
|d|d|	d|
d|d|d|d|d|d|d||||||dt          ||||          t                     d {V }n# t          t          f$ rj t          |          }t	          j                    t          |          z   |k    rt          d d           t          j        |           d {V  |dz   }Y .t          $ rL} t!          | j                  }!|!|                    |!           t'          | j                  rY d } ~ || d } ~ ww xY w|S rt   )rz   r{   r|   	asyncwaitr   r~   r   r   r   r   r   r4   r   asyncior   r   r=   r;   r   r@   )"rF   r_   ra   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   r[   rS   rT   rU   rV   rW   rX   rY   rZ   r\   r]   r^   r2   r   r   r   r   r   r   r<   s"                                     r3   r   zAsyncCompletions.create   s     @ 
//88	"l<<UCCCCCCCC6	))+++++++++|~~	))(t4441!55-"H ,-> (	
 %j #H %j +,<  &{   '    -.@  Q!" ./B#$ )5'2+:$,1F-  0 1&3$/#- '	   +A 6 ! ! ! ! ! ! ! !D '(=>   3J??<>>Ih$?$?$??)KK,T4888mH---------'!^
$   .s|<<*!''444 .. HHHHI Ks&   =A0C. .A7F=(	F=1?F86F88F=c                   || j         j        }d}t          |t          j                  r|j        }nst          |t                    r|}n[t          | j         j        t                    r|}n9t          d	                    t          | j         j                                      t          j                    t          |          z   S r   r   r   s      r3   rz   z'AsyncCompletions._get_request_last_time(  r   r5   )r(   r   )6r_   r`   ra   rb   rH   rc   rI   rd   rJ   re   rK   rf   rL   rg   rM   rc   rN   rh   rO   rc   rP   ri   rQ   rg   rR   rc   r[   rn   rS   rc   rT   rg   rU   rf   rV   rj   rW   rk   rX   rl   rY   rm   rZ   rg   r\   ro   r]   rp   r^   rq   r2   rr   r(   r   )	r   r   r   r   rG   r   r   r   rz   r   r5   r3   r'   r'      s        5 5 5 _5  59FJ6:*.+/377;.2:>-1(,59"&59DHBFJN=A6:(,$("&049W W W W W  WrB B B B Br5   c                      e Zd ZddZdS )rB   completionsr&   r(   Nonec                F    || _         t          |j                  | _        d S rD   )_completionsr   r   rF   r   s     r3   __init__z#CompletionsWithRawResponse.__init__:  s%    '-
 
r5   N)r   r&   r(   r   r   r   r   r   r   r5   r3   rB   rB   9  (        
 
 
 
 
 
r5   rB   c                      e Zd ZddZdS )r   r   r'   r(   r   c                F    || _         t          |j                  | _        d S rD   )r   r   r   r   s     r3   r   z(AsyncCompletionsWithRawResponse.__init__C  s%    '3
 
r5   N)r   r'   r(   r   r   r   r5   r3   r   r   B  r   r5   r   )r(   r)   )9
__future__r   r   r   r   r   r   typingr   r   r	   r
   r   typing_extensionsr   r   _exceptionsr   r   r   _typesr   r   r   _utilsr   r   
encryptionr   r   _base_clientr   	_resourcer   r   _compatr   	_responser   r   
types.chatr   r    r!   r"   r#   
_constantsr$   r%   __all__r4   r=   r@   r&   r'   rB   r   r   r5   r3   <module>r      s   # " " " " "   ( ( ( ( ( ( ( (       8 8 8 8 8 8 8 8 8 8 8 8 8 8 % % % % % %  W W W W W W W W W W * * * * * * * * * * : : : : : : : : G G G G G G G G 0 0 0 0 0 0 : : : : : : : : & & & & & &                           
 ,
-* * * *    (nB nB nB nB nB/ nB nB nBbnB nB nB nB nB' nB nB nBb
 
 
 
 
 
 
 

 
 
 
 
 
 
 
 
 
r5   