
    ^hB7                         S SK r S SKrS SKJrJrJrJrJrJrJ	r	  S SK
r
S SKrS SKJr  S SKJrJr  S SKJr  S SKJrJr  SSKJr  SS	KJr  \(       a	  S S
KJr  \rO\r " S S\5      r " S S\5      r g)    N)TYPE_CHECKINGAnyAsyncIteratorIteratorListOptionalUnion)cohere_messages_pt_v2)
BaseConfigBaseLLMException)AllMessageValues)ModelResponseUsage   )ModelResponseIterator)validate_environment)Loggingc                   X   ^  \ rS rSr SS\S\S\\R                     4U 4S jjjr	Sr
U =r$ )CohereError   status_codemessageheadersc                    > Xl         X l        [        R                  " SSS9U l        [        R
                  " XR                  S9U l        [        TU ]!  UUUS9  g )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       a/home/james-whalen/.local/lib/python3.13/site-packages/litellm/llms/cohere/chat/transformation.pyr$   CohereError.__init__   sR     '}}F8WX;U# 	 	
    )r   r   r"   r   N)__name__
__module____qualname____firstlineno__intstrr   r   Headersr$   __static_attributes____classcell__)r&   s   @r'   r   r      s7    
 ,0	

 
 %--(	
 
r)   r   c            '           \ rS rSr% SrSr\\   \S'   Sr	\\
   \S'   Sr\\   \S'   Sr\\   \S'   Sr\\   \S'   Sr\\   \S	'   Sr\\
   \S
'   Sr\\   \S'   Sr\\
   \S'   Sr\\   \S'   Sr\\   \S'   Sr\\   \S'   Sr\\   \S'   Sr\\   \S'   Sr\\   \S'   Sr\\
   \S'   Sr\\
   \S'   Sr\\   \S'                     S5S\\   S\\
   S\\   S\\   S\\   S	\\   S
\\
   S\\   S\\
   S\\   S\\   S\\   S\\   S\\   S\\   S\\
   S\\
   S\\   SS4&S jjr S6S\S\S\\    S\S\\   S\4S jjr!S\S\\   4S jr"S\S\S\S \S\4
S! jr#S\S\\    S\S"\S\S\4S# jr$  S7S\S$\%RL                  S%\'S&\(S'\S\\    S\S"\S(\)S\\   S)\\   S\'4S* jjr* S6S\\
   4S+ jjr+S,\4S- jr, S8S.\-\.\   \/\   \'4   S/\S)\\   4S0 jjr0S1\S2\S\-\\%Rb                  4   S\24S3 jr3S4r4g)9CohereChatConfig*   a  
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                     [        5       nUR                  5        H*  u  nnUS:w  d  M  Uc  M  [        U R                  UU5        M,     g )Nr%   )localsitemssetattrr&   )r%   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   locals_keyvalues                         r'   r$   CohereChatConfig.__init__V   s;    * (!--/JCf}!2U3 *r)   r   modelmessagesoptional_paramsapi_keyc                     [        UUUUUS9$ )N)r   rR   rS   rT   rU   )cohere_validate_environment)r%   r   rR   rS   rT   rU   s         r'   r   %CohereChatConfig.validate_environmentp   s      ++
 	
r)   c                 
    / SQ$ )N)streamr@   rA   top_prD   rE   stopnrF   tool_choicerH   extra_headers )r%   rR   s     r'   get_supported_openai_params,CohereChatConfig.get_supported_openai_params   s    
 	
r)   non_default_paramsdrop_paramsc                    UR                  5        Hk  u  pVUS:X  a  XbS'   US:X  a  XbS'   US:X  a  XbS'   US:X  a  XbS'   US:X  a  XbS'   US:X  a  XbS'   US	:X  a  XbS	'   US
:X  a  XbS'   US:X  a  XbS'   US:X  d  Mg  XbS'   Mm     U$ )NrZ   r@   rA   r]   num_generationsr[   rC   rD   rE   r\   stop_sequencesrF   rH   )rL   )r%   rc   rT   rR   rd   paramrP   s          r'   map_openai_params"CohereChatConfig.map_openai_params   s     /446LE ,1)%16.$05-|5: 12',$++7< 34**6; 2349 01+0(*/') 7* r)   litellm_paramsc                    [         R                  R                  5       R                  5        H  u  pgXc;  d  M  XsU'   M     [	        X!SS9u  pSU;   a  Sn
U R                  US   S9nXS'   [        U[        5      (       a  U/US'   O[        U[        5      (       a  XS'   [        U	5      S:  a  U	S	   S
   S:X  a  SUS'   U$ )Ncohere_chat)rS   rR   llm_providerrF   T)rF   rG   r   r   roleUSERforce_single_step)
litellmr5   
get_configrL   r
   _construct_cohere_tool
isinstancedictr0   len)r%   rR   rS   rT   rk   r   rB   vmost_recent_messager8   _is_function_callcohere_toolss               r'   transform_request"CohereChatConfig.transform_request   s     ,,779??ADA(%&"	 B -B-
)
 o% $66_W=U6VL'3G$)400/B.CON++S11)<I& |q \"%5f%=%G37O/0r)   raw_responsemodel_responselogging_objrequest_dataencoding	json_modec                 n    UR                  5       nUS   UR                  S   R                  l        SU;   a  [        USUS   5        UR                  SS 5      nUb  U/ :w  a  / nU Hj  nUR                  SS5      nUR                  SS5      nUR                  S	0 5      nS
U 3SU[         R                  " U5      S.S.nUR                  U5        Ml     [        R                  " US S9nUUR                  S   l        UR                  S0 5      R                  S0 5      nUR                  SS5      nUR                  SS5      n[        [        R                  " 5       5      Ul        Xl        [%        UUUU-   S9n[        USU5        U$ ! [         a    [        UR                  UR                  S9ef = f)Ntextr   )r   r   	citations
tool_callsname r9   
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   rM   getdumpsappendrs   Messager/   timecreatedrR   r   )r%   rR   r   r   r   r   rS   rT   rk   r   rU   r   raw_response_jsoncohere_tools_responser   toolfunction_namer9   r   	tool_call_messager   r   r   r   s                            r'   transform_response#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                 j    Uc  / n/ nU H%  nU R                  U5      nUR                  U5        M'     U$ r*   ) _translate_openai_tool_to_coherer   )r%   rF   r|   r   cohere_tools        r'   ru   'CohereChatConfig._construct_cohere_tool  sC     =ED??EK,  r)   openai_toolc                 2    US   S   US   S   0 S.nUS   S   S   R                  5        Hf  u  p4UR                  S0 5      R                  S0 5      R                  S/ 5      nUR                  SS5      UR                  S	S5      X5;   S
.nXbS   U'   Mh     U$ )aQ  
{
"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   )rL   r   )r%   r   r   
param_name	param_defrequired_paramscohere_param_defs          r'   r   1CohereChatConfig._translate_openai_tool_to_cohere!  s    (	*  
+F3&z2=A%'
 &1%<\%J&

%'&!J 
B/\2&Z$   )}}]B?!fb1&9 
 @P/0<& r)   streaming_responsesync_streamc                     [        UUUS9$ )N)r   r   r   )CohereModelResponseIterator)r%   r   r   r   s       r'   get_model_response_iterator,CohereChatConfig.get_model_response_iterator`  s     +1#
 	
r)   error_messager   c                     [        X!S9$ )N)r   r   )r   )r%   r   r   r   s       r'   get_error_class CohereChatConfig.get_error_classl  s     {JJr)   r`   )NNNNNNNNNNNNNNNNNNr*   )NN)F)5r+   r,   r-   r.   __doc__r7   r   r0   __annotations__r8   listr9   r:   r;   r<   r=   r>   boolr?   r@   r/   rA   rB   rC   rD   rE   rF   rG   rH   r$   rw   r   r   r   ra   ri   r}   r   r!   r   LiteLLMLoggingObjr   r   ru   r   r	   r   r   r   r1   r   r   r2   r`   r)   r'   r5   r5   *   si   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@ "&

 
 '(	

 
 #
 

 
 
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)   r5   )!r   r   typingr   r   r   r   r   r   r	   r   rs   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   rW   *litellm.litellm_core_utils.litellm_loggingr   _LiteLLMLoggingObjr   r   r5   r`   r)   r'   <module>r      s[      U U U   U R 6 4 O NX*
" 
$EKz EKr)   