
    hU@                         d dl Z d dlZd dlmZmZm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 ddlmZmZ dd	lmZ d
dlmZmZmZ 	 	 ddee   dededededede
dee    de fdZ! G d de      Z"y)    N)AnyOptionalUnion)Logging)AsyncHTTPHandlerHTTPHandler_get_httpx_clientget_async_httpx_client)ModelResponse)CustomStreamWrapper   )
BaseAWSLLMCredentials)BedrockError   )AWSEventStreamDecoderMockResponseIterator	make_callclientapi_baseheadersdatamodelmessageslogging_obj	json_modefake_streamc	                    | 
t               } | j                  |||| |      }	|	j                  dk7  r.t        |	j                  t	        |	j                                     |r[t        j                         j                  ||	t        j                         d|i d||t        j                  
      }
t        |
|      }n-t        |      }|j                  |	j                  d	
            }|j                  |ddd|i       |S )N)r   r   streamr      status_codemessageT )
r   responsemodel_responser   r   optional_paramsapi_keyr   r   encoding)r&   r   )r   i   )
chunk_sizezfirst stream response receivedcomplete_input_dict)inputr(   original_responseadditional_args)r	   postr"   r   strreadlitellmAmazonConverseConfig_transform_responser   r)   r   r   
iter_bytes	post_call)r   r   r   r   r   r   r   r   r   r%   r&   completion_streamdecoders                f/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/llms/bedrock/chat/converse_handler.pymake_sync_callr:      s(    ~"${{  H s" ,,c(--/6J
 	
  ((*>>"002#%% ? 
 	 "6)Y"
 (e4#..x/B/Bd/B/ST :.5	       c                       e Zd Zd fdZdedefdZdi ddddfdeded	ed
edee	e
ej                  f      dedededee   dedee   dee   defdZdi ddfdeded	ed
edee	e
ej                  f      dedededededee   dee   de	eef   fdZ	 	 	 	 ddeded	ee   ded
ededededee	e
ej                  f      dedee   dee	eef      dee   fdZ xZS )BedrockConverseLLMreturnNc                 "    t         |           y )N)super__init__)self	__class__s    r9   rA   zBedrockConverseLLM.__init__T   s    r;   model_idc                 D    t         j                  j                  |d      S )z
        Double encode the model ID to ensure it matches the expected double-encoded format.
        Args:
            model_id (str): The model ID to encode.
        Returns:
            str: The double-encoded model ID.
        r$   )safe)urllibparsequote)rB   rD   s     r9   encode_model_idz"BedrockConverseLLM.encode_model_idW   s     ||!!(!44r;   Fr   r   r   r&   timeoutr'   litellm_paramscredentialsr   r   r   r(   c                   K   t        j                         j                  |||	|
       d {   }t        j                  |      }| j                  ||
j                  d      xs d|||||      }|j                  |d||t        |j                        d       t        ||t        |j                        ||||||	       d {   }t        ||d	|
      }|S 7 7 w)Nr   r   r'   rL   aws_region_name	us-west-2rM   rP   extra_headersendpoint_urlr   r   r(   r$   r+   r   r   r,   r(   r.   )	r   r   r   r   r   r   r   r   r   bedrockr7   r   custom_llm_providerr   )r2   r3   _async_transform_requestjsondumpsget_request_headersgetpre_calldictr   r   r   )rB   r   r   r   r&   rK   r)   r   r   r'   rL   rM   	logger_fnr   r   r   r   r(   request_datar   preppedr7   streaming_responses                          r9   async_streamingz"BedrockConverseLLM.async_streaminga   s    ( %99;TT+)	 U 
 
 zz,'**#*../@AP[!! + 
 	'+$0 	 	
 #,)##
#
 

 1/ )#	
 "!]
:

s"   +C!CBC!CC!C!r   r   c                   K   t        j                         j                  |||	|
       d {   }t        j                  |      }| j                  ||
j                  d      xs d|||||      }|j                  |d|||j                  d       t        |j                        }|t        |t              sdi }|:t        |t              st        |t              rt        j                  |      }||d<   t!        |t         j"                  j$                  	      }n|}	 |j'                  ||||
       d {   }|j)                          t        j                         j7                  |||t        |t8              r|nd|d|||	|
      S 7 a7 V# t        j*                  $ r<}|j,                  j.                  }t1        ||j,                  j2                        d }~wt        j4                  $ r t1        dd      w xY ww)NrO   rP   rQ   rR   r$   rU   rV   rK   )paramsllm_providerurlr   r   r   r!     Timeout error occurred.F
r   r%   r&   r   r   r(   r   r   r'   r)   )r2   r3   rZ   r[   r\   r]   r^   r_   r   r`   
isinstancer   floatinthttpxTimeoutr
   LlmProvidersBEDROCKr/   raise_for_statusHTTPStatusErrorr%   r"   r   textTimeoutExceptionr4   bool)rB   r   r   r   r&   rK   r)   r   r   r'   rL   rM   ra   r   r   r(   rb   r   rc   _paramsr%   err
error_codes                          r9   async_completionz#BedrockConverseLLM.async_completion   s    $ %99;TT+)	 U 
 
 zz,'**#*../@AP[!! + 
 	'+$"?? 	 	
 w'>F4D!EG"gu-GS1I#mmG4G%,	"+W-A-A-I-IF F	S#[['	 )  H %%' ++-AA)'565#+ B 
 	
o
T $$ 	R11J:s||?P?PQQ%% 	S38QRR	SsG   +HFC3H"F :F;F A HF H&7G$HHcustom_prompt_dictacompletionrS   c                    |j                  dd       }|j                  dd       }|j                  dd      }|j                  dd      }|| j                  |      }n| j                  |      }|du rd|v rd}| j                  |||	      }|j                  d
d       }|j                  dd       }|j                  dd       }|j                  dd       }|j                  dd       }|j                  dd       }|j                  dd       }|j                  dd       }|j                  dd       }|j                  dd        ||d<   | j	                  |||||||||	      }| j                  |||      \  } }!||du r|s|  d| d} |! d| d}!n|  d| d} |! d| d}!ddi}"|ddi|}"|	rXt        |t              rd }|du r"| j                  |||!||||d|||"|
|||||      S | j                  |||!||||||||"|
|||      S t        j                         j                  ||||      }#t        j                  |#      }$| j                  ||||!|$|"|      }%|j!                  |d|$|!|%j"                  d !       |t        |t$              rJi }&|
:t        |
t&              st        |
t(              rt+        j,                  |
      }
|
|&d"<   t/        |&      }n|}|G|du rCt1        |t        |t              r|nd |!|%j"                  |$|||||#	      }'t3        |'|d$|%      }(|(S 	 |j5                  |!|%j"                  |$|&      })|)j7                          t        j                         jE                  ||)|t        |tF              r|nd|d|$|||*
      S # t*        j8                  $ r<}*|*j:                  j<                  }+t?        |+|*j:                  j@                  '      d }*~*wt*        jB                  $ r t?        d(d)'      w xY w)+Nr   rD   r   Fr   )rD   Tai21)r'   r   rD   aws_secret_access_keyaws_access_key_idaws_session_tokenaws_role_nameaws_session_nameaws_profile_nameaws_bedrock_runtime_endpointaws_web_identity_tokenaws_sts_endpointrP   )	r   r   r   rP   r   r   r   r   r   )r   r   rP   z/model/z/converse-streamz	/conversezContent-Typezapplication/json)r   r   r   r&   r)   r   r'   r   rL   ra   r   rK   r   r   r   rM   r(   )r   r   r   r&   r)   r   r'   r   rL   ra   r   rK   r   rM   r(   rO   rR   r$   rU   rV   rK   )	r   r   r   r   r   r   r   r   r   rW   rX   ri   r!   rk   rl   rm   )$popr^   rJ   _get_aws_region_nameget_credentialsget_runtime_endpointrn   r   re   r}   r2   r3   _transform_requestr[   r\   r]   r_   r   r   ro   rp   rq   rr   r	   r:   r   r/   ru   rv   r%   r"   r   rw   rx   r4   ry   ),rB   r   r   r   r~   r&   r)   r   r'   r   rK   rL   ra   rS   r   r(   r   unencoded_model_idr   r   modelIdrP   r   r   r   r   r   r   r   r   r   rM   rT   proxy_endpoint_urlr   _datar   rc   rz   r7   rd   r%   r{   r|   s,                                               r9   
completionzBedrockConverseLLM.completion   sG   & !$$Xt4,00TB%))-?#''U;	)**4F*GG**E*:GT>f/K 33+' 4 
 !0 3 34KT R+//0CTJ+//0CTJ'++OTB*../A4H*../A4H'6':':*D(
$ "1!4!45Mt!T*../A4H-t4  		
 $(#7#7/"7/+--'#9- $8 
$
 ,0+D+D)E+ ,E ,
((
 6T>;*^77);KLL$6#7wwiGW!X*^77)9EL$6#7wwiy!Q "#56$%'9K]KG &+.~++%/#1% +$3#1'##!' + +## ,  ( ((!+-!' /-#' )  ( ,,.AA+)	 B 
 zz% **#+'+ + 
 	'+."?? 	 	
 >Z0@AG"gu-GS1I#mmG4G%,	"&w/FF&D. . )j.M +!'#'! "5"3$-'	" &%	S{{&'	 # H %%' ++-AA)'565#+ B 
 	
 $$ 	R11J:s||?P?PQQ%% 	S38QRR	Ss   %/N P&7O$P)r>   N)NNNN)__name__
__module____qualname__rA   r0   rJ   listr   r   r   ro   rq   rr   r`   r   r   ry   r   re   LiteLLMLoggingObjectr}   r   r   __classcell__)rC   s   @r9   r=   r=   S   s~   5 5 5. -1!$)!%%B"B" B" 	B"
 &B" %u}} 456B" B" B" !B" )*B"  !B"" D>#B"$ #%B"& 
'B"b -1!%!T
T
 T
 	T

 &T
 %u}} 456T
 *T
 T
 T
 !T
 T
 )*T
  #!T
" 
}11	2#T
F (,AE!%!]
]
 ]
 3-	]

 !]
 &]
 *]
 ]
 ]
 %u}} 456]
 ]
  ~]
 /<=>]
  #!]
r;   r=   )FF)#r[   rG   typingr   r   r   rq   r2   *litellm.litellm_core_utils.litellm_loggingr   r   &litellm.llms.custom_httpx.http_handlerr   r   r	   r
   litellm.types.utilsr   litellm.utilsr   base_aws_llmr   r   common_utilsr   invoke_handlerr   r   r   r0   r`   r   ry   r:   r=    r;   r9   <module>r      s      ' '   V  . - 2 ' R R !&9[!99 9 	9
 9 9 &9 ~9 9xE
 E
r;   