Ë
    uPêj€8  ã                   óæ   — d dl Z d dlZd dlmZmZ d dl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Zd dlmZ eZneZ G d„ de«      Z G d„ de«      Z y)é    N)ÚAsyncIteratorÚIterator)ÚTYPE_CHECKINGÚAnyÚFinal)Ú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j                  dz  fˆ fd„Zˆ xZS )ÚCohereErrorNÚ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       €úl/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/llms/cohere/chat/transformation.pyr!   zCohereError.__init__   sX   ø€ ð 'ˆÔØˆŒÜ—}‘}¨FÐ8WÔXˆŒÜŸ™°;ÈÏÉÔUˆŒÜ‰ÑØ#ØØð 	õ 	
ó    ©N)	Ú__name__Ú
__module__Ú__qualname__ÚintÚstrr   ÚHeadersr!   Ú__classcell__)r#   s   @r$   r   r      s7   ø„ ð
 )-ñ	
àð
ð ð
ð —‘ Ñ%÷	
ñ 
r%   r   c            )       ó  — e Zd ZU dZdZedz  ed<   dZedz  ed<   dZ	edz  ed<   dZ
edz  ed<   dZedz  ed<   dZedz  ed<   dZedz  ed	<   dZedz  ed
<   dZedz  ed<   dZedz  ed<   dZedz  ed<   dZedz  ed<   dZedz  ed<   dZedz  ed<   dZedz  ed<   dZedz  ed<   dZedz  ed<   dZedz  ed<   dZedz  ed<   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d6dedz  dedz  dedz  dedz  dedz  dedz  d	edz  d
edz  dedz  dedz  dedz  dedz  dedz  dedz  dedz  dedz  dedz  dedz  dedz  ddf(d„Z	 	 d7dededee   dedededz  dedz  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"	 	 d7ded%e#jH                  d&e%d'e&d(edee   deded)d*dedz  d+edz  de%fd,„Z'	 d8dedz  fd-„Z(d.efd/„Z)	 d9d0e*e   e+e   z  e%z  d1ed+edz  fd2„Z,d3ed4edee#jZ                  z  de.fd5„Z/y):Ú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                 ó¤   — t        «       j                  «       }|j                  «       D ]%  \  }}|dk7  sŒ|€Œt        | j                  ||«       Œ' y )Nr"   )ÚlocalsÚcopyÚitemsÚsetattrr#   )r"   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   Úlocals_ÚkeyÚvalues                          r$   r!   zCohereChatConfig.__init__[   sF   € ô,  ›Ÿ™›ˆØ!Ÿ-™-›/ò 	4‰JˆC�Ø�f‹} Ñ!2Ü˜Ÿ™¨¨UÕ3ñ	4r%   r   ÚmodelÚmessagesÚoptional_paramsÚlitellm_paramsÚapi_keyÚapi_basec                 ó"   — t        |||||¬«      S )N)r   rL   rM   rN   rP   )Úcohere_validate_environment)r"   r   rL   rM   rN   rO   rP   rQ   s           r$   r   z%CohereChatConfig.validate_environmentv   s    € ô +ØØØØ+Øô
ð 	
r%   c                 ó
   — g d¢S )N)Ústreamr9   r:   r;   Útop_pr>   r?   ÚstopÚnr@   Útool_choicerB   Úextra_headers© )r"   rL   s     r$   Úget_supported_openai_paramsz,CohereChatConfig.get_supported_openai_paramsˆ   s   € ò
ð 	
r%   Únon_default_paramsÚdrop_paramsc                 ó  — |j                  «       D ]t  \  }}|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(  r||d<   |dk(  sŒp||d<   Œv |S )NrU   r9   r:   r;   rX   Únum_generationsrV   r=   r>   r?   rW   Ústop_sequencesr@   rB   )rG   )r"   r]   rN   rL   r^   ÚparamrK   s          r$   Úmap_openai_paramsz"CohereChatConfig.map_openai_params™   sò   € ð /×4Ñ4Ó6ò 	0‰LˆE�5Ø˜Ò Ø,1� Ñ)Ø˜Ò%Ø16� Ñ.Ø˜Ò$Ø05� Ñ-ØÐ/Ò/Ø05� Ñ-Ø˜Š|Ø5:�Ð 1Ñ2Ø˜ÒØ',� Ñ$ØÐ+Ò+Ø7<�Ð 3Ñ4ØÐ*Ò*Ø6;�Ð 2Ñ3Ø˜ŠØ49�Ð 0Ñ1Ø˜ÒØ+0� Ñ(Ø˜‹Ø*/� Ò'ð-	0ð. Ðr%   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)rM   rL   Úllm_providerr@   T)r@   rA   r   r   éÿÿÿÿÚroleÚUSERÚforce_single_step)
Úlitellmr/   Ú
get_configrG   r   Ú_construct_cohere_toolÚ
isinstanceÚdictr+   Úlen)r"   rL   rM   rN   rO   r   r<   ÚvÚmost_recent_messager1   Ú_is_function_callÚcohere_toolss               r$   Útransform_requestz"CohereChatConfig.transform_request¹   sì   € ô ×,Ñ,×7Ñ7Ó9×?Ñ?ÓAò 	'‰DˆAˆqà˜Ò(à%&� Ò"ð		'ô -BØ U¸ô-
Ñ)Ð˜\ð
 �oÑ%Ø'+ÐØ"&×"=Ñ"=ÀOÐT[ÑD\Ð"=Ó"]ˆLØ'3ˆO˜GÑ$ÜÐ)¬4Ô0Ø/BÐ.CˆO˜NÒ+ÜÐ+¬SÔ1Ø)<ˆO˜IÑ&ô ˆ|Ó˜qÒ  \°"Ñ%5°fÑ%=ÀÒ%GØ37ˆOÐ/Ñ0àÐr%   Úraw_responseÚmodel_responseÚlogging_objÚrequest_dataÚencodingztiktoken.Encoding | NoneÚ	json_modec                 ón  — 	 |j                  «       }|d   |j                  d   j                  _        d|v rt        |d|d   «       |j                  dd «      }|�Ÿ|g k7  ršg }|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Ú r2   Ú
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   rO   rz   rP   r{   Úraw_response_jsonÚcohere_tools_responser   ÚtoolÚfunction_namer2   r‚   Ú	tool_callÚ_messagerŠ   r�   rŽ   r�   s                            r$   Útransform_responsez#CohereChatConfig.transform_responseÜ   sØ  € ð	_Ø'3×'8Ñ'8Ó':ÐØ8IÈ&Ñ8QˆN×"Ñ" 1Ñ%×-Ñ-Ô5ð
 Ð+Ñ+Ü�N KÐ1BÀ;Ñ1OÔPð (9×'<Ñ'<¸\È4Ó'PÐØ Ð,Ð1FÈ"Ò1Là "ˆJØ-ò -�Ø $§¡¨°Ó 4�Ø $§¡¨¸"Ó =�Ø!ŸX™X l°BÓ7�
à! - Ð1Ø&à -Ü%)§Z¡Z°
Ó%;ñ!ñ�	ð ×!Ñ! )Õ,ð-ô &Ÿo™oØ%ØôˆHð 19ˆN×"Ñ" 1Ñ%Ô-ð 0×3Ñ3°F¸BÓ?×CÑCÀNÐTVÓWˆà+×/Ñ/°ÀÓBˆØ#/×#3Ñ#3°OÀQÓ#GÐä!$¤T§Y¡Y£[Ó!1ˆÔØ$ˆÔÜØ'Ø/Ø&Ð):Ñ:ô
ˆô
 	� ¨Ô/ØÐøô[ ò 	_Ü l×&7Ñ&7À\×E]ÑE]Ô^Ð^ð	_ús   ‚1F	 Æ	+F4c                 ód   — |€g }g }|D ]$  }| j                  |«      }|j                  |«       Œ& |S r&   )Ú _translate_openai_tool_to_coherer–   )r"   r@   rt   rœ   Úcohere_tools        r$   rm   z'CohereChatConfig._construct_cohere_tool  sH   € ð ˆ=ØˆEØ ˆØò 	-ˆDØ×?Ñ?ÀÓEˆKØ×Ñ Õ,ð	-ð Ð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È<Ñ%X×%^Ñ%^Ó%`ò 	PÑ!ˆJ˜	Ø)Ÿo™o¨j¸"Ó=×AÑAÀ,ÐPRÓS×WÑWÐXbÐdfÓgˆOà(Ÿ}™}¨]¸BÓ?Ø!Ÿ™ f¨bÓ1Ø&¨/Ð9ñ Ðð
 @PˆKÐ/Ñ0°Ò<ð	Pð Ð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%   )NNNNNNNNNNNNNNNNNNN)NNr&   )F)0r'   r(   r)   Ú__doc__r0   r+   Ú__annotations__r1   Úlistr2   r3   r4   r5   r6   r7   Úboolr8   r9   r*   r:   r;   r<   r=   r>   r?   r@   rA   rB   r!   ro   r   r   r\   rc   ru   r   r   r   ÚLiteLLMLoggingObjr    rm   r¢   r   r   r²   r,   r
   rµ   r[   r%   r$   r/   r/   -   s‘  … ñð2  €Hˆc�D‰jÓØ $€L�$˜‘+Ó$Ø $€M�3˜‘:Ó$Ø"€K��t‘Ó"Ø"&€O�S˜4‘ZÓ&Ø$(Ð�s˜T‘zÓ(Ø"€J��t‘Ó"Ø'+Ð˜ ™Ó+Ø!€Iˆt�d‰{Ó!Ø"€K��t‘Ó"Ø!€J��d‘
Ó!Ø(,Ð˜3 ™:Ó,Ø€A€sˆT�zÓØ€A€sˆT�zÓØ$(Ð�s˜T‘zÓ(Ø#'Ð�c˜D‘jÓ'Ø€Eˆ4�$‰;ÓØ $€L�$˜‘+Ó$Ø€Dˆ#�‰*Óð  $Ø$(Ø$(Ø"&Ø&*Ø(,Ø"&Ø+/Ø!%Ø"&Ø!%Ø,0ØØØ(,Ø'+Ø!Ø$(Øñ)4à˜‘*ð4ð ˜T‘kð4ð ˜T‘zð	4ð
 ˜4‘Zð4ð ˜t™ð4ð  ™:ð4ð ˜4‘Kð4ð " D™[ð4ð ˜$‘;ð4ð ˜4‘Zð4ð ˜$‘Jð4ð  # T™zð4ð �‰:ð4ð �‰:ð4ð   ™:ð!4ð"  ™*ð#4ð$ �d‰{ð%4ð& ˜T‘kð'4ð( �D‰jð)4ð* 
ó+4ðD #Ø#ñ
àð
ð ð
ð Ð'Ñ(ð	
ð
 ð
ð ð
ð �t‘ð
ð ˜‘*ð
ð 
ó
ð$
°ð 
¸¸c¹ó 
ð"à ðð ðð ð	ð
 ðð 
óð@!àð!ð Ð'Ñ(ð!ð ð	!ð
 ð!ð ð!ð 
ó!ð\ #Ø!%ñ>àð>ð —n‘nð>ð &ð	>ð
 'ð>ð ð>ð Ð'Ñ(ð>ð ð>ð ð>ð -ð>ð �t‘ð>ð ˜$‘;ð>ð 
ó>ðD "ñ
à�d‰{ó
ð7àó7ðz "'ñ	

à$ S™M¨M¸#Ñ,>Ñ>ÀÑNð

ð ð

ð ˜$‘;ó	

ðK¨Sð K¸sð KÈTÐTY×TaÑTaÑMað KÐfvô Kr%   r/   )!r‘   r˜   Úcollections.abcr   r   Útypingr   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   rS   ÚtiktokenÚ*litellm.litellm_core_utils.litellm_loggingr   Ú_LiteLLMLoggingObjrº   r   r/   r[   r%   r$   ú<module>rÅ      s^   ðÛ Û ß 3ß ,Ñ ,ã ã Ý Uß RÝ 6ß 4å OÝ NáÛåXà*ÑàÐô
Ð"ô 
ô$AK�zõ AKr%   