o
    ưi$9                     @   s   d dl Z d dlZd dlmZmZmZ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 d dlmZ d dlmZmZ ddlmZ dd	lmZ erUd d
lmZ eZneZG dd deZG dd deZ dS )    N)TYPE_CHECKINGAnyAsyncIteratorIteratorListOptionalUnion)cohere_messages_pt_v2)
BaseConfigBaseLLMException)AllMessageValues)ModelResponseUsage   )ModelResponseIterator)validate_environment)Loggingc                       s4   e Zd Z	ddededeej f fddZ  Z	S )CohereErrorNstatus_codemessageheadersc                    sD   || _ || _tjddd| _tj|| jd| _t j|||d d S )NPOSTzhttps://api.cohere.ai/v1/chat)methodurl)r   request)r   r   r   )	r   r   httpxRequestr   Responseresponsesuper__init__)selfr   r   r   	__class__ ^/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/cohere/chat/transformation.pyr       s   
zCohereError.__init__N)
__name__
__module____qualname__intstrr   r   Headersr    __classcell__r$   r$   r"   r%   r      s    r   c                )   @   sl  e Zd ZU dZdZee ed< dZee	 ed< dZ
ee ed< dZee ed< dZee ed< dZee ed< dZee	 ed	< dZee ed
< dZee	 ed< dZee ed< dZee ed< dZee ed< dZee ed< dZee ed< dZee ed< dZee ed< dZee	 ed< dZee	 ed< dZee ed< 																			d@dee dee	 dee dee dee dee d	ee	 d
ee dee	 dee dee dee dee dee dee dee dee	 dee	 dee ddf(ddZ		dAdededee  dededee dee def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#dedee  dedededefd(d)Z$		dAd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d0d1Z*	dBdee	 fd2d3Z+d4efd5d6Z,	7dCd8e-e.e e/e e'f d9ed/ee fd:d;Z0d<ed=ede-ee%j1f de2fd>d?Z3dS )DCohereChatConfiga  
    Configuration class for Cohere's API interface.

    Args:
        preamble (str, optional): When specified, the default Cohere preamble will be replaced with the provided one.
        chat_history (List[Dict[str, str]], optional): A list of previous messages between the user and the model.
        generation_id (str, optional): Unique identifier for the generated reply.
        response_id (str, optional): Unique identifier for the response.
        conversation_id (str, optional): An alternative to chat_history, creates or resumes a persisted conversation.
        prompt_truncation (str, optional): Dictates how the prompt will be constructed. Options: 'AUTO', 'AUTO_PRESERVE_ORDER', 'OFF'.
        connectors (List[Dict[str, str]], optional): List of connectors (e.g., web-search) to enrich the model's reply.
        search_queries_only (bool, optional): When true, the response will only contain a list of generated search queries.
        documents (List[Dict[str, str]], optional): A list of relevant documents that the model can cite.
        temperature (float, optional): A non-negative float that tunes the degree of randomness in generation.
        max_tokens [DEPRECATED - use max_completion_tokens] (int, optional): The maximum number of tokens the model will generate as part of the response.
        max_completion_tokens (int, optional): The maximum number of tokens the model will generate as part of the response.
        k (int, optional): Ensures only the top k most likely tokens are considered for generation at each step.
        p (float, optional): Ensures that only the most likely tokens, with total probability mass of p, are considered for generation.
        frequency_penalty (float, optional): Used to reduce repetitiveness of generated tokens.
        presence_penalty (float, optional): Used to reduce repetitiveness of generated tokens.
        tools (List[Dict[str, str]], optional): A list of available tools (functions) that the model may suggest invoking.
        tool_results (List[Dict[str, Any]], optional): A list of results from invoking tools.
        seed (int, optional): A seed to assist reproducibility of the model's response.
    Npreamblechat_historygeneration_idresponse_idconversation_idprompt_truncation
connectorssearch_queries_only	documentstemperature
max_tokensmax_completion_tokenskpfrequency_penaltypresence_penaltytoolstool_resultsseedreturnc                 C   s>   t   }| D ]\}}|dkr|d urt| j|| q	d S )Nr!   )localscopyitemssetattrr#   )r!   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   Zlocals_keyvaluer$   r$   r%   r    X   s   
zCohereChatConfig.__init__r   modelmessagesoptional_paramslitellm_paramsapi_keyapi_basec                 C   s   t |||||dS )N)r   rI   rJ   rK   rM   )cohere_validate_environment)r!   r   rI   rJ   rK   rL   rM   rN   r$   r$   r%   r   s   s   
z%CohereChatConfig.validate_environmentc                 C   s   g dS )N)streamr8   r9   r:   top_pr=   r>   stopnr?   Ztool_choicerA   extra_headersr$   )r!   rI   r$   r$   r%   get_supported_openai_params   s   z,CohereChatConfig.get_supported_openai_paramsnon_default_paramsdrop_paramsc                 C   s   |  D ]\\}}|dkr||d< |dkr||d< |dkr ||d< |dkr(||d< |dkr0||d< |dkr8||d< |d	kr@||d	< |d
krH||d
< |dkrP||d< |dkrX||d< |dkr`||d< q|S )NrP   r8   r9   r:   rS   Znum_generationsrQ   r<   r=   r>   rR   Zstop_sequencesr?   rA   )rE   )r!   rV   rK   rI   rW   paramrH   r$   r$   r%   map_openai_params   s2   z"CohereChatConfig.map_openai_paramsc                 C   s   t j  D ]\}}||vr|||< qt||dd\}}	d|v r/d}
| j|d d}||d< t|tr:|g|d< n	t|trC||d< t	|	dkrU|	d	 d
 dkrUd|d< |S )NZcohere_chat)rJ   rI   Zllm_providerr?   T)r?   r@   r   r   ZroleUSERZforce_single_step)
litellmr.   Z
get_configrE   r	   _construct_cohere_tool
isinstancedictr+   len)r!   rI   rJ   rK   rL   r   r;   vZmost_recent_messager0   Z_is_function_callcohere_toolsr$   r$   r%   transform_request   s$   	


z"CohereChatConfig.transform_requestraw_responsemodel_responselogging_objrequest_dataencoding	json_modec                 C   sH  z|  }|d |jd j_W n ty   t|j|jdw d|v r+t|d|d  |	dd }|d urs|g krsg }|D ](}|	dd}|	dd}|	d	i }d
| d|t 
|dd}|| q=tj|d d}||jd _|	di 	di }|	dd}|	dd}tt |_||_t|||| d}t|d| |S )Ntextr   )r   r   Z	citations
tool_callsname r1   
parametersZcall_function)rl   	arguments)idtypero   )rk   contentmetabilled_unitsZinput_tokensZoutput_tokens)prompt_tokenscompletion_tokensZtotal_tokensusage)jsonchoicesr   rs   	Exceptionr   rj   r   rF   getdumpsappendr\   Messager*   timecreatedrI   r   )r!   rI   rd   re   rf   rg   rJ   rK   rL   rh   rM   ri   Zraw_response_jsonZcohere_tools_responserk   toolZfunction_namer1   rn   Z	tool_call_messageru   rv   rw   rx   r$   r$   r%   transform_response   sR   z#CohereChatConfig.transform_responsec                 C   s2   |d u rg }g }|D ]}|  |}|| q
|S r&   ) _translate_openai_tool_to_coherer~   )r!   r?   rb   r   cohere_toolr$   r$   r%   r]     s   
z'CohereChatConfig._construct_cohere_toolopenai_toolc                 C   s   	 |d d |d d i d}|d d d   D ](\}}|di di dg }|dd|d	d||v d
}||d |< q|S )a  
        {
        "name": "query_daily_sales_report",
        "description": "Connects to a database to retrieve overall sales volumes and sales information for a given day.",
        "parameter_definitions": {
            "day": {
                "description": "Retrieves sales data for this day, formatted as YYYY-MM-DD.",
                "type": "str",
                "required": True
            }
        }
        }
        ro   rl   description)rl   r   parameter_definitionsrn   
propertiesrequiredrm   rr   )r   rr   r   r   )rE   r|   )r!   r   r   
param_nameZ	param_defZrequired_paramsZcohere_param_defr$   r$   r%   r   '  s(   






z1CohereChatConfig._translate_openai_tool_to_cohereFstreaming_responsesync_streamc                 C   s   t |||dS )N)r   r   ri   )CohereModelResponseIterator)r!   r   r   ri   r$   r$   r%   get_model_response_iteratorf  s
   z,CohereChatConfig.get_model_response_iteratorerror_messager   c                 C   s   t ||dS )N)r   r   )r   )r!   r   r   r   r$   r$   r%   get_error_classr  s   z CohereChatConfig.get_error_class)NNNNNNNNNNNNNNNNNNN)NNr&   )F)4r'   r(   r)   __doc__r/   r   r+   __annotations__r0   listr1   r2   r3   r4   r5   r6   boolr7   r8   r*   r9   r:   r;   r<   r=   r>   r?   r@   rA   r    r_   r   r   r   rU   rY   rc   r   r   r   LiteLLMLoggingObjr   r   r]   r   r   r   r   r   r,   r   r   r$   r$   r$   r%   r.   *   s`  
 	

"	

 
.	

D

C
r.   )!ry   r   typingr   r   r   r   r   r   r   r   r\   Z3litellm.litellm_core_utils.prompt_templates.factoryr	   Z)litellm.llms.base_llm.chat.transformationr
   r   Zlitellm.types.llms.openair   Zlitellm.types.utilsr   r   Zcommon_utilsr   r   r   rO   Z*litellm.litellm_core_utils.litellm_loggingr   Z_LiteLLMLoggingObjr   r   r.   r$   r$   r$   r%   <module>   s"    $