
    gj7                         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r	d d
lmZ eZneZ G d de      Z G d de      Z y)    N)TYPE_CHECKINGAnyAsyncIteratorIteratorListOptionalUnion)cohere_messages_pt_v2)
BaseConfigBaseLLMException)AllMessageValues)ModelResponseUsage   )ModelResponseIterator)validate_environment)Loggingc                   J     e Zd Z	 ddededeej                     f fdZ xZ	S )CohereErrorstatus_codemessageheadersc                     || _         || _        t        j                  dd      | _        t        j
                  || j                        | _        t        | !  |||       y )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__s       \/var/www/openai/venv/lib/python3.12/site-packages/litellm/llms/cohere/chat/transformation.pyr#   zCohereError.__init__   sX     '}}F8WX;U# 	 	
    N)
__name__
__module____qualname__intstrr   r   Headersr#   __classcell__)r%   s   @r&   r   r      s7    
 ,0	

 
 %--(	
 
r'   r   c            '          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<   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d4d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Z	 	 d5dededee   dedee   dee   defdZ dedee   fdZ!dededed edef
d!Z"dedee   ded"ededefd#Z#	 	 d5ded$e$jJ                  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*Z)	 d6dee	   fd+Z*d,efd-Z+	 d7d.e,e-e   e.e   e&f   d/ed)ee   fd0Z/d1ed2ede,ee$j`                  f   de1fd3Z2y)8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 (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kpfrequency_penaltypresence_penaltytoolstool_resultsseedreturnc                     t               }|j                         D ]%  \  }}|dk7  s|t        | j                  ||       ' y )Nr$   )localsitemssetattrr%   )r$   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   locals_keyvalues                         r&   r#   zCohereChatConfig.__init__V   s;    * (!--/JCf}!2U3 *r'   r   modelmessagesoptional_paramsapi_keyapi_basec                 "    t        |||||      S )N)r   rL   rM   rN   rO   )cohere_validate_environment)r$   r   rL   rM   rN   rO   rP   s          r&   r   z%CohereChatConfig.validate_environmentp   s      ++
 	
r'   c                 
    g dS )N)streamr;   r<   top_pr?   r@   stopnrA   tool_choicerC   extra_headers )r$   rL   s     r&   get_supported_openai_paramsz,CohereChatConfig.get_supported_openai_params   s    
 	
r'   non_default_paramsdrop_paramsc                     |j                         D ]j  \  }}|dk(  r||d<   |dk(  r||d<   |dk(  r||d<   |dk(  r||d<   |dk(  r||d<   |dk(  r||d<   |d	k(  r||d	<   |d
k(  r||d<   |dk(  r||d<   |dk(  sf||d<   l |S )NrT   r;   r<   rW   num_generationsrU   r>   r?   r@   rV   stop_sequencesrA   rC   )rG   )r$   r\   rN   rL   r]   paramrK   s          r&   map_openai_paramsz"CohereChatConfig.map_openai_params   s     /446LE5 ,1)%16.$05-|5: 12',$++7< 34**6; 2349 01+0(*/') 7* r'   litellm_paramsc                 x   t         j                  j                         j                         D ]  \  }}||vs|||<    t	        ||d      \  }}	d|v rd}
| j                  |d         }||d<   t        |t              r|g|d<   nt        |t              r||d<   t        |	      dkD  r|	d	   d
   dk(  rd|d<   |S )Ncohere_chat)rM   rL   llm_providerrA   T)rA   rB   r   r   roleUSERforce_single_step)
litellmr1   
get_configrG   r
   _construct_cohere_tool
isinstancedictr-   len)r$   rL   rM   rN   rc   r   r=   vmost_recent_messager3   _is_function_callcohere_toolss               r&   transform_requestz"CohereChatConfig.transform_request   s     ,,779??ADAq(%&"	 B -BU-
)\
 o% $66_W=U6VL'3OG$)40/B.CON++S1)<OI& |q \"%5f%=%G37O/0r'   raw_responsemodel_responselogging_objrequest_dataencoding	json_modec                 n   	 |j                         }|d   |j                  d   j                  _        d|v rt        |d|d          |j                  dd       }||g k7  rg }|D ]h  }|j                  dd      }|j                  dd      }|j                  d	i       }d
| d|t        j                  |      dd}|j                  |       j t        j                  |d       }||j                  d   _        |j                  di       j                  di       }|j                  dd      }|j                  dd      }t        t        j                               |_        ||_        t%        ||||z         }t        |d|       |S # t        $ r" t        |j                  |j                        w xY w)Ntextr   )r   r   	citations
tool_callsname r4   
parameterscall_function)r   	arguments)idtyper   )r   contentmetabilled_unitsinput_tokensoutput_tokens)prompt_tokenscompletion_tokenstotal_tokensusage)jsonchoicesr   r   	Exceptionr   r}   r   rH   getdumpsappendrk   Messager,   timecreatedrL   r   )r$   rL   rv   rw   rx   ry   rM   rN   rc   rz   rO   r{   raw_response_jsoncohere_tools_responser   toolfunction_namer4   r   	tool_call_messager   r   r   r   s                            r&   transform_responsez#CohereChatConfig.transform_response   s   	 , 1 1 38I&8QN""1%--5 ++NK1B;1OP !2 5 5lD I ,1F"1LJ- $ 4 $" =!XXlB7
!-1& -%)ZZ
%;!	 !!), . %H 19N""1%- ),,VR8<<^RP$((;(,,_a@!$TYY[!1$'/&)::

 	/_  	$))|7O7O 	s   1F	 	+F4c                 d    |g }g }|D ]$  }| j                  |      }|j                  |       & |S r(   ) _translate_openai_tool_to_coherer   )r$   rA   rt   r   cohere_tools        r&   rm   z'CohereChatConfig._construct_cohere_tool  sC     =ED??EK,  r'   openai_toolc                 2   	 |d   d   |d   d   i d}|d   d   d   j                         D ]g  \  }}|j                  di       j                  di       j                  dg       }|j                  dd      |j                  d	d      ||v d
}||d   |<   i |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
            }
        }
        }
        r   r   description)r   r   parameter_definitionsr   
propertiesrequiredr   r   )r   r   r   r   )rG   r   )r$   r   r   
param_name	param_defrequired_paramscohere_param_defs          r&   r   z1CohereChatConfig._translate_openai_tool_to_cohere"  s    (	*  
+F3&z2=A%'
 &1%<\%J&

%'&!J	 
B/\2&Z$   )}}]B?!fb1&/9 
 @PK/0<& r'   streaming_responsesync_streamc                     t        |||      S )N)r   r   r{   )CohereModelResponseIterator)r$   r   r   r{   s       r&   get_model_response_iteratorz,CohereChatConfig.get_model_response_iteratora  s     +1#
 	
r'   error_messager   c                     t        ||      S )N)r   r   )r   )r$   r   r   r   s       r&   get_error_classz CohereChatConfig.get_error_classm  s     {MJJr'   )NNNNNNNNNNNNNNNNNN)NNr(   )F)3r)   r*   r+   __doc__r2   r   r-   __annotations__r3   listr4   r5   r6   r7   r8   r9   boolr:   r;   r,   r<   r=   r>   r?   r@   rA   rB   rC   r#   ro   r   r   r   r[   rb   ru   r   r    r   LiteLLMLoggingObjr   r   rm   r   r	   r   r   r   r.   r   r   rZ   r'   r&   r1   r1   *   sz   0 #Hhsm"#'L(4.'#'M8C='!%K#%%)OXc])'+x}+!%J%*.$. $Ix~$!%K#% $J$Ax}Ax}'+x}+&*hsm* E8D> #'L(4.'D(3- #''+'+%))-+/%).2$(%)$(+/*. $'+"'43-4 tn4  }	4
 c]4 "#4 $C=4 TN4 &d^4 D>4 c]4 SM4 C=4 C=4 $C=4  #3-!4" ~#4$ tn%4& sm'4( 
)4@ "&"&

 
 '(	

 
 #
 3-
 

"
 
c 
    	
  
<"" '(" 	"
 " " 
"^ "&$(AA nnA &	A
 'A A '(A A A A #A D>A 
AJ !%
~
==F %*	

!(3-s1C]"RS

 

 D>	

K K/2K=B4CV=WK	Kr'   r1   )!r   r   typingr   r   r   r   r   r   r	   r   rk   3litellm.litellm_core_utils.prompt_templates.factoryr
   )litellm.llms.base_llm.chat.transformationr   r   litellm.types.llms.openair   litellm.types.utilsr   r   common_utilsr   r   r   rR   *litellm.litellm_core_utils.litellm_loggingr   _LiteLLMLoggingObjr   r   r1   rZ   r'   r&   <module>r      s[      U U U   U R 6 4 O NX*
" 
$FKz FKr'   