
    h6                         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mZ dd	l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"y)    N)TYPE_CHECKINGAnyAsyncIteratorIteratorListOptionalUnion)cohere_messages_pt_v2)
BaseConfigBaseLLMException)CohereV2ChatResponse)AllMessageValuesChatCompletionToolCallChunk)ModelResponseUsage   CohereError)ModelResponseIterator)validate_environment)Loggingc            '          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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CohereV2ChatConfiga  
    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 )Nself)localsitemssetattr	__class__)r.   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   locals_keyvalues                         f/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/llms/cohere/chat/v2_transformation.py__init__zCohereV2ChatConfig.__init__E   s;    * (!--/JCf}!2U3 *    headersmodelmessagesoptional_paramslitellm_paramsapi_keyapi_basec                 "    t        |||||      S )N)r9   r:   r;   r<   r>   )cohere_validate_environment)r.   r9   r:   r;   r<   r=   r>   r?   s           r6   r   z'CohereV2ChatConfig.validate_environment_   s      ++
 	
r8   c                 
    g dS )N)streamr#   r$   top_pr'   r(   stopnr)   tool_choicer+   extra_headers )r.   r:   s     r6   get_supported_openai_paramsz.CohereV2ChatConfig.get_supported_openai_paramsq   s    
 	
r8   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 )NrC   r#   r$   rF   num_generationsrD   r&   r'   r(   rE   stop_sequencesr)   r+   )r0   )r.   rK   r<   r:   rL   paramr5   s          r6   map_openai_paramsz$CohereV2ChatConfig.map_openai_params   s     /446LE5 ,1)%16.$05-|5: 12',$++7< 34**6; 2349 01+0(*/') 7* r8   c                 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)r;   r:   llm_providerr)   T)r)   r*   messager   roleUSERforce_single_step)
litellmCohereChatConfig
get_configr0   r
   _construct_cohere_tool
isinstancedictstrlen)r.   r:   r;   r<   r=   r9   r%   vmost_recent_messager   _is_function_callcohere_toolss               r6   transform_requestz$CohereV2ChatConfig.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r8   raw_responsemodel_responselogging_objrequest_dataencoding	json_modec           	         	 |j                         }	 t        di |}|d   j                  dd       }|Mdj                  |D cg c]  }||j                  dd       c}      |j                  d   j                  _
        d|v rt        |d|d          |d   j                  d	g       }|^|g k7  rYg }t        |      D ]  \  }}i |d
|i}|j                  |         t        j                  |d       }||j                  d   _	        |d   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# t        $ r t        |j                  d      w xY wc c}w )N)rU   status_codei  rU   content textr   	citations
tool_callsindex)rs   ro   usagetokensinput_tokensoutput_tokens)prompt_tokenscompletion_tokenstotal_tokensrI   )json	Exceptionr   rq   rn   r   getjoinchoicesrU   ro   r1   	enumerateappendrZ   Messageinttimecreatedr:   r   )r.   r:   rg   rh   ri   rj   r;   r<   r=   rk   r>   rl   raw_response_jsoncohere_v2_chat_responsecohere_contentro   cohere_tools_responsers   rt   tool	tool_call_messagetoken_usagery   rz   ru   s                             r6   transform_responsez%CohereV2ChatConfig.transform_response   s$   	 , 1 1 3	J&:&O=N&O# 1;??	4P%8: $2* KK+9N""1%--5 11NK1H1UV !8	 B F F|UW X ,1F"1L<>J()>?t::U:	 !!),  @ %H 19N""1%- .g6::8RH#:'OOOQ?!$TYY[!1$'/&)::

 	/m  	$))|7O7O 	  	Jl&7&7SII	Js   F F; G+F8;!Gc                 d    |g }g }|D ]$  }| j                  |      }|j                  |       & |S N) _translate_openai_tool_to_coherer   )r.   r)   re   r   cohere_tools        r6   r]   z)CohereV2ChatConfig._construct_cohere_tool
  sC     =ED??EK,  r8   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
            }
        }
        }
        functionnamedescription)r   r   parameter_definitions
parameters
propertiesrequiredrp   type)r   r   r   r   )r0   r~   )r.   r   r   
param_name	param_defrequired_paramscohere_param_defs          r6   r   z3CohereV2ChatConfig._translate_openai_tool_to_cohere  s    (	*  
+F3&z2=A%'
 &1%<\%J&

%'&!J	 
B/\2&Z$   )}}]B?!fb1&/9 
 @PK/0<& r8   streaming_responsesync_streamc                     t        |||      S )N)r   r   rl   )CohereModelResponseIterator)r.   r   r   rl   s       r6   get_model_response_iteratorz.CohereV2ChatConfig.get_model_response_iteratorU  s     +1#
 	
r8   error_messagern   c                     t        ||      S )N)rn   rU   r   )r.   r   rn   r9   s       r6   get_error_classz"CohereV2ChatConfig.get_error_classa  s     {MJJr8   )NNNNNNNNNNNNNNNNNN)NNr   )F)3__name__
__module____qualname____doc__r   r   r`   __annotations__r   listr   r   r   r   r    r!   boolr"   r#   r   r$   r%   r&   r'   r(   r)   r*   r+   r7   r_   r   r   r   rJ   rQ   rf   httpxResponser   LiteLLMLoggingObjr   r   r]   r   r	   r   r   r   Headersr   r   rI   r8   r6   r   r      s   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( 
)4B "&"&

 
 '(	

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