o
    ưi                     @   s~  d Z ddlZddlmZ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 ddlmZ ddlmZ dd	lmZ d
efddZG dd deZ	d dedeeef dedejjdeeeejf  dedededee d
ededee fddZddddeddfd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ee  deeeejf  deeeef  fddZ!dS )!z3
Legacy /v1/embedding handler for Bedrock Cohere. 
    N)AnyCallableOptionalUnion)Logging)AsyncHTTPHandlerHTTPHandlerget_async_httpx_client)CohereEmbeddingRequest)EmbeddingResponse   )CohereEmbeddingConfigheadersc                 C   s`   dd |  D }d|vrd|d< d|vrd|d< d|vr!d|d< | r.d	|vr.d
|  |d< |S )Nc                 S   s   i | ]}|  |qS  )lower).0kr   r   X/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/cohere/embed/handler.py
<dictcomp>   s    z(validate_environment.<locals>.<dictcomp>zrequest-sourcezunspecified:litellmzRequest-Sourceacceptzapplication/jsonzcontent-typeauthorizationzBearer Authorization)keys)api_keyr   Zexisting_keys_lowerr   r   r   validate_environment   s   r   c                       s   e Zd Z fddZ  ZS )CohereErrorc                    s@   || _ || _tjddd| _tj|| jd| _t | j d S )NPOSTz!https://api.cohere.ai/v1/generate)methodurl)status_coderequest)	r   messagehttpxRequestr    Responseresponsesuper__init__)selfr   r!   	__class__r   r   r'   )   s   zCohereError.__init__)__name__
__module____qualname__r'   __classcell__r   r   r)   r   r   (   s    r   modeldatainputmodel_responsetimeoutlogging_objoptional_paramsapi_baser   encodingclientc              
      s   |j ||||	|dd |d u rttjjd|id}z|j||	t|dI d H }W n6 tj	yG } z|j
||d|i|jjd |d }~w tya } z|j
||d|it|d |d }~ww t j|||||| |
|dS )	N)complete_input_dictr   r6   r1   r   additional_argsr3   )Zllm_providerparamsr   r0   r9   )r1   r   r;   original_responser%   r   r4   r0   r2   r/   r7   r1   )pre_callr	   litellmZLlmProvidersZCOHEREpostjsondumpsr"   ZHTTPStatusErrorZ	post_callr%   text	Exceptionstrr   _transform_response)r/   r0   r1   r2   r3   r4   r5   r6   r   r   r7   r8   r%   er   r   r   async_embedding5   sZ    rJ   complete_api_base
aembeddingc                 C   s   t |	|d}|p	d}| } |pt j| ||d}|
du r8t| ||||||||	|||d ur4t|tr4|dS d dS |j||	d|id |d u sKt|tsPtdd	}|j||t	
|d
}t j||	|||| ||dS )N)r   zhttps://api.cohere.ai/v1/embed)r/   r1   Zinference_paramsT)r/   r0   r1   r2   r3   r4   r5   r6   r   r   r7   r8   r9   r:   r   )Zconcurrent_limitr=   r?   )r   r   Z_transform_requestrJ   
isinstancer   r@   r   rB   rC   rD   rH   )r/   r1   r2   r4   r5   r   r7   r0   rK   r   rL   r3   r8   Z	embed_urlr%   r   r   r   	embeddingw   sT   

rN   )N)"__doc__rC   typingr   r   r   r   r"   rA   Z*litellm.litellm_core_utils.litellm_loggingr   ZLiteLLMLoggingObjZ&litellm.llms.custom_httpx.http_handlerr   r   r	   Zlitellm.types.llms.bedrockr
   Zlitellm.types.utilsr   Zv1_transformationr   dictr   rF   r   rG   listutilsfloatTimeoutrJ   boolrN   r   r   r   r   <module>   s    
	

J	
