o
    ưid;                     @   sb  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	 d dl
Z
d dl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 d dlmZ d d	lmZmZmZ d
dl m!Z!m"Z" erkd dl#m$Z% e%Z&neZ&dZ'G dd deZ(G dd deZ)dddddddddddddddZ*dee fddZ+dee fddZ,dee- fddZ.dS )     N)TYPE_CHECKINGAnyDictListOptionalUnion)exception_type)track_llm_api_timing)
BaseConfigBaseLLMException)AsyncHTTPHandlerHTTPHandler_get_httpx_clientget_async_httpx_clientversion)AllMessageValues)LlmProviders)CustomStreamWrapperModelResponseUsage   )API_BASE
BytezError)Loggingi,  c                   @   s  e Zd ZdZ		d/ddZdedee fddZd	ed
edede	def
ddZ
		d0dededee d
ededee dee defddZ	d1dee dee ded
ededee	 defddZdedee d
edededefddZ		d0dedejdedededee d
edededee dee	 defd d!Ze 			d2ded"edededed#eded$eeeef  dee	 d%ee dd&fd'd(Ze 			d2ded"edededed#eded$eeeef  dee	 d%ee dd&fd)d*Zd+ed,edeeejf de fd-d.Z!dS )3BytezChatConfigz8
    Configuration class for Bytez's API interface.
    returnNc              
   C   s   t   }| D ]\}}|dkr|d urt| j|| q	t| jdd i 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dddddddddddddddddd| _d S )NselfZhas_custom_stream_wrapperTstreamZ
max_tokensZmax_new_tokensZmax_completion_tokensZtemperatureZtop_pnZnum_return_sequencesmax_retriesseedFstopZ
logit_biasZlogprobsZfrequency_penaltyZpresence_penaltyZtop_logprobsZ
modalitiesZ
predictionZstream_options)ZtoolsZtool_choiceZfunction_callZ	functionsextra_headersZparallel_tool_callsaudioZweb_search_options)localscopyitemssetattr	__class__openai_to_bytez_param_map)r   Zlocals_keyvalue r,   ]/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/bytez/chat/transformation.py__init__)   sd   
	
zBytezChatConfig.__init__modelc                 C   s*   g }| j  D ]\}}|r|| q|S N)r)   r&   append)r   r/   Zsupported_paramsr*   r+   r,   r,   r-   get_supported_openai_paramsO   s   
z+BytezChatConfig.get_supported_openai_paramsnon_default_paramsoptional_paramsdrop_paramsc           
      C   sj   i }i ||}|  D ]&\}}| j|}	|	du r%|rqtd| d|	d u r.|||< q|||	< q|S )NFzparam `z` is not supported on Bytez)r&   r)   get	Exception)
r   r3   r4   r/   r5   Zadapted_params
all_paramsr*   r+   aliasr,   r,   r-   map_openai_paramsW   s   
z!BytezChatConfig.map_openai_paramsheadersmessageslitellm_paramsapi_keyapi_basec                 C   s:   | dd| dt d |std|std|S )Nzapplication/jsonzKey zlitellm/)zcontent-typeAuthorizationz
user-agentzRkwarg `messages` must be an array of messages that follow the openai chat standardz3Missing api_key, make sure you pass in your api key)updater   r7   )r   r;   r/   r<   r4   r=   r>   r?   r,   r,   r-   validate_environmentu   s   z$BytezChatConfig.validate_environmentr   c                 C   s   t  d| S )N/)r   )r   r?   r>   r/   r4   r=   r   r,   r,   r-   get_complete_url   s   	z BytezChatConfig.get_complete_urlc                 C   s6   | dd}| dr|d= t|d}|||d}|S )Nr   Fr<   )r<   r   params)r6    adapt_messages_to_bytez_standard)r   r/   r<   r4   r=   r;   r   datar,   r,   r-   transform_request   s   

z!BytezChatConfig.transform_requestraw_responsemodel_responselogging_objrequest_dataencoding	json_modec                 C   s   |  }|d}|d urtt|d |jdtt |_||_|d}|j	d j
}|d d d |_t|d}t|}t|gd}t|}|| }t|||d}||_|j|jd	< |jd
|jd|jd|jd|jdd|_|S )Nerror)messagestatus_codeoutputr   contenttextrE   )prompt_tokenscompletion_tokenstotal_tokensZadditional_headersratelimit-limitratelimit-remainingratelimit-resetinference-meterinference-time)rY   rZ   r[   r\   r]   )jsonr6   r   strrR   inttimecreatedr/   choicesrQ   rT   rG   get_tokens_from_messagesr   usager;   Z_hidden_paramsZprovider_specific_fields)r   r/   rJ   rK   rL   rM   r<   r4   r=   rN   r>   rO   r^   rP   rS   rQ   rV   Zoutput_messagesrW   rX   re   r,   r,   r-   transform_response   s>   








z"BytezChatConfig.transform_responsecustom_llm_providerrH   clientsigned_json_bodyBytezCustomStreamWrapperc              
   C   s   |d u s	t |trti d}z|j||t|d|td}W n tjy5 } z
t	|j
j|j
jdd }~ww |jdkrCt	|j|jd| }t||||d}|S )N)rF   Tr;   rH   r   rL   timeoutrR   rQ      completion_streamr/   rg   rL   )
isinstancer   r   postr^   dumpsSTREAMING_TIMEOUThttpxHTTPStatusErrorr   responserR   rU   	iter_textrj   r   r/   rg   rL   r?   r;   rH   r<   rh   rO   ri   rw   erp   Zstreaming_responser,   r,   r-   get_sync_custom_stream_wrapper   s6   


z.BytezChatConfig.get_sync_custom_stream_wrapperc              
      s   |d u s
t |trttji d}z|j||t|d|tdI d H }W n t	j
y; } z
t|jj|jjdd }~ww |jdkrIt|j|jd| }t||||d}|S )N)Zllm_providerrF   Trk   rm   rn   ro   )rq   r   r   r   ZBYTEZrr   r^   rs   rt   ru   rv   r   rw   rR   rU   Z
aiter_textrj   ry   r,   r,   r-   get_async_custom_stream_wrapper(  s8   
z/BytezChatConfig.get_async_custom_stream_wrappererror_messagerR   c                 C   s   t ||dS )Nrm   )r   )r   r}   rR   r;   r,   r,   r-   get_error_classT  s   zBytezChatConfig.get_error_class)r   N)NNr0   )NNN)"__name__
__module____qualname____doc__r.   r_   r   r2   dictboolr:   r   r   rB   rD   rI   ru   Responser   LiteLLMLoggingObjr   rf   r	   listr   r   r   bytesr{   r|   r`   ZHeadersr   r~   r,   r,   r,   r-   r   $   s6   
&
%	
%

#	

F
	
+
	
+r   c                   @   s   e Zd ZdefddZdS )rj   chunkc              
   C   s   z|   }i }|ddd}d|i}| j|||dW S  ty"   t tyB } zt  t|dt| t| j	| j
|dd }~ww )NF )rU   is_finishedZfinish_reasonrT   )completion_objrK   response_objrQ   )r/   rg   Zoriginal_exception)Zmodel_response_creatorZreturn_processed_chunk_logicStopIterationr7   	traceback
format_excr'   r_   r   r/   rg   )r   r   rK   r   r   rz   r,   r,   r-   chunk_creator[  s2   z&BytezCustomStreamWrapper.chunk_creatorN)r   r   r   r   r   r,   r,   r,   r-   rj   Z  s    rj   rU   )type
value_nameimageurlr#   Zvideo)rU   Z	image_urlZinput_audioZ	video_urlZdocumentfiler<   c                 C   s   t | } g }| D ]R}|d }|d }g }|D ];}|d}|s#tdt| }|s1td| d|d }	|d }
||
}|sHtd|
 d|d|	|
|i q|||d	 q|S )
NrolerT   r   zProp `type` is not a stringzProp `z` is not supportedr   z` is not a stringr   rT   )#_adapt_string_only_content_to_listsr6   r7   !open_ai_to_bytez_content_item_mapr1   )r<   new_messagesrQ   r   rT   new_contentcontent_itemr   Zcontent_item_mapnew_typer   r+   r,   r,   r-   rG     s*   

rG   c                 C   s   g }| D ]`}| d}| d}g }t|tr |d|d n<t|tr+|| n1t|trXg }|D ]}t|trD|d|d q4t|trO|| q4td||7 }ntd|||d q|S )Nr   rT   rU   )r   rU   z:`content` can only contain strings or openai content dictszContent must be a stringr   )r6   rq   r_   r1   r   r   r7   )r<   r   rQ   r   rT   r   Znew_content_itemsr   r,   r,   r-   r     s.   







r   c                 C   s\   d}| D ]'}|d }|D ]}|d }|dkr&|d }| d}|t|7 }q|d7 }qq|S )Nr   rT   r   rU       )splitlen)r<   totalrQ   rT   r   r   r+   wordsr,   r,   r-   rd     s   


rd   )/r^   ra   r   typingr   r   r   r   r   r   ru   Z2litellm.litellm_core_utils.exception_mapping_utilsr   Z(litellm.litellm_core_utils.logging_utilsr	   Z)litellm.llms.base_llm.chat.transformationr
   r   Z&litellm.llms.custom_httpx.http_handlerr   r   r   r   r   Zlitellm.types.llms.openair   Zlitellm.types.utilsr   Zlitellm.utilsr   r   r   Zcommon_utilsr   r   Z*litellm.litellm_core_utils.litellm_loggingr   Z_LiteLLMLoggingObjr   rt   r   rj   r   rG   r   r   rd   r,   r,   r,   r-   <module>   s@       8"
+'