Ë
    uPêjg6  ã                   óà   — 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 d dlmZ d dlmZmZmZmZ d dlmZmZ d	d
lmZ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"y)é    N)ÚAsyncIteratorÚIterator)ÚTYPE_CHECKINGÚAnyÚFinal)ÚBaseLLMException)ÚOpenAIGPTConfig)ÚCohereV2ChatResponse)ÚAllMessageValuesÚChatCompletionAnnotationÚ#ChatCompletionAnnotationURLCitationÚChatCompletionToolCallChunk)ÚModelResponseÚUsageé   )ÚCohereErrorÚCohereV2ModelResponseIterator)Úvalidate_environment)ÚLoggingc            '       ó&  ‡ — 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<   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 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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ˆ fd#„Z!	 	 d7ded$e"jF                  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'e   e(e   z  e$z  d-ed*edz  fd.„Z)	 d9dedz  dedz  dededed/edz  defd0„Z*d1ed2edee"jV                  z  de,fd3„Z-d4ee   dee.   fd5„Z/ˆ xZ0S ):Ú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                         úo/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/llms/cohere/chat/v2_transformation.pyÚ__init__zCohereV2ChatConfig.__init__L   s@   € ô*  ›ˆØ!Ÿ-™-›/ò 	4‰JˆC�Ø�f‹} Ñ!2Ü˜Ÿ™¨¨UÕ3ñ	4ó    ÚheadersÚmodelÚmessagesÚoptional_paramsÚlitellm_paramsÚapi_keyÚapi_basec                 ó"   — t        |||||¬«      S )N)r7   r8   r9   r:   r<   )Úcohere_validate_environment)r,   r7   r8   r9   r:   r;   r<   r=   s           r4   r   z'CohereV2ChatConfig.validate_environmentf   s    € ô +ØØØØ+Øô
ð 	
r6   c                 ó
   — g d¢S )N)Ústreamr!   r"   Úmax_completion_tokensÚtop_pr%   r&   ÚstopÚnr'   Útool_choicer)   Úextra_headers© )r,   r8   s     r4   Úget_supported_openai_paramsz.CohereV2ChatConfig.get_supported_openai_paramsx   s   € ò
ð 	
r6   Únon_default_paramsÚdrop_paramsc                 ó  — |j                  «       D ]x  \  }}|dk(  r||d<   |dk(  r||d<   |dk(  r	d|v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Œt||d<   Œz |S )NrA   r!   r"   rB   rE   Únum_generationsrC   r$   r%   r&   rD   Ústop_sequencesr'   r)   )r.   )r,   rJ   r:   r8   rK   Úparamr3   s          r4   Úmap_openai_paramsz$CohereV2ChatConfig.map_openai_params‰   sû   € ð /×4Ñ4Ó6ò 	0‰LˆE�5Ø˜Ò Ø,1� Ñ)Ø˜Ò%Ø16� Ñ.Ø˜Ò$Ð)@ÐHZÑ)ZØ05� Ñ-ØÐ/Ò/Ø05� Ñ-Ø˜Š|Ø5:�Ð 1Ñ2Ø˜ÒØ',� Ñ$ØÐ+Ò+Ø7<�Ð 3Ñ4ØÐ*Ò*Ø6;�Ð 2Ñ3Ø˜ŠØ49�Ð 0Ñ1Ø˜ÒØ+0� Ñ(Ø˜‹Ø*/� Ò'ð-	0ð. Ðr6   c                 ó.   •— t         ‰| �  |||||«      }|S )z‡
        Cohere v2 chat api is in openai format, so we can use the openai transform request function to transform the request.
        )ÚsuperÚtransform_request)r,   r8   r9   r:   r;   r7   Údatar0   s          €r4   rS   z$CohereV2ChatConfig.transform_request©   s#   ø€ ô ‘gÑ/°°xÀÐR`ÐbiÓjˆàˆr6   Úraw_responseÚmodel_responseÚlogging_objÚrequest_dataÚencodingztiktoken.Encoding | NoneÚ	json_modec           	      ó>  — 	 |j                  «       }	 t        di |¤Ž}|d   j                  dd «      }|�Ndj                  |D �cg c]  }|€Œ|j                  dd«      ‘Œ c}«      |j                  d   j                  _
        d }d }d|v rd|d   v r|d   d   }|r| j                  |«      }|d   j                  d	g «      }|�`|g k7  r[g }t        |«      D ]  \  }}i |¥d
|i¥}|j                  |«       Œ  t        j                  |d |¬«      }||j                  d   _	        n"|r |j                  d   j                  }||_        |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)ÚmessageÚstatus_codei¦  r\   ÚcontentÚ Útextr   Ú	citationsÚ
tool_callsÚindex)rb   r^   ÚannotationsÚusageÚtokensÚinput_tokensÚoutput_tokens)Úprompt_tokensÚcompletion_tokensÚtotal_tokensrH   )ÚjsonÚ	Exceptionr   r`   r]   r
   ÚgetÚjoinÚchoicesr\   r^   Ú*_translate_citations_to_openai_annotationsÚ	enumerateÚappendÚlitellmÚMessagerd   ÚintÚtimeÚcreatedr8   r   r/   )r,   r8   rU   rV   rW   rX   r9   r:   r;   rY   r<   rZ   Úraw_response_jsonÚcohere_v2_chat_responseÚcohere_contentr^   rd   ra   Úcohere_tools_responserb   rc   ÚtoolÚ	tool_callÚ_messageÚcurrent_messageÚtoken_usageri   rj   re   s                                r4   Útransform_responsez%CohereV2ChatConfig.transform_response¸   sj  € ð	_Ø'3×'8Ñ'8Ó':Ðð	JÜ-AÑ-VÐDUÑ-VÐ#ð !8¸	Ñ B× FÑ FÀyÐRVÓ WˆØÐ%Ø8:¿¹Ø8FÖ^¨WÈ'ÑJ]�—‘˜V RÕ(Ò^ó9ˆN×"Ñ" 1Ñ%×-Ñ-Ô5ð
 >BˆØˆ	àÐ/Ñ/°KÐCZÐ[dÑCeÑ4eØ/°	Ñ:¸;ÑGˆIáØ×IÑIÈ)ÓTˆKð (?¸yÑ'I×'MÑ'MÈlÐ\^Ó'_ÐØ Ð,Ð1FÈ"Ò1LàCEˆJÜ(Ð)>Ó?ò -‘��tð:Øð:à˜Uñ:�	ð ×!Ñ! )Õ,ð-ô &Ÿo™oØ%ØØ'ôˆHð
 19ˆN×"Ñ" 1Ñ%Õ-áØ)7×)?Ñ)?ÀÑ)B×)JÑ)J�Ø.9�Ô+ð 5°WÑ=×AÑAÀ(ÈBÓOˆØ*Ÿ™¨~¸qÓAˆØ#.§?¡?°?ÀAÓ#FÐä!$¤T§Y¡Y£[Ó!1ˆÔØ$ˆÔÜØ'Ø/Ø&Ð):Ñ:ô
ˆô
 	� ¨Ô/ØÐøôw ò 	_Ü l×&7Ñ&7À\×E]ÑE]Ô^Ð^ð	_ûô
 ò 	JÜ l×&7Ñ&7ÀSÔIÐIð	Jüò _s"   ‚G “G6 ÁHÁHÇ+G3Ç6!HÚstreaming_responseÚsync_streamc                 ó   — t        |||¬«      S )N)rƒ   r„   rZ   )r   )r,   rƒ   r„   rZ   s       r4   Úget_model_response_iteratorz.CohereV2ChatConfig.get_model_response_iterator  s   € ô -Ø1Ø#Øô
ð 	
r6   rA   c                 ó    — |€t        d«      ‚|S )z€
        Get the complete URL for Cohere v2 chat completion.
        The api_base should already include the full path.
        zapi_base is required)Ú
ValueError)r,   r=   r<   r8   r:   r;   rA   s          r4   Úget_complete_urlz#CohereV2ChatConfig.get_complete_url  s   € ð ÐÜÐ3Ó4Ð4Øˆr6   Úerror_messager]   c                 ó   — t        ||¬«      S )N)r]   r\   )r   )r,   rŠ   r]   r7   s       r4   Úget_error_classz"CohereV2ChatConfig.get_error_class"  s   € Ü {¸MÔJÐJr6   ra   c                 ó„  — g }|D ]¸  }|j                  dd«      }|j                  dd«      }|j                  dg «      }|sŒ<|D ]x  }|j                  d«      dk(  sŒd|v sŒ|d   }|j                  dd«      }	|j                  d	«      xs d
|j                  dd«      › �}
|||	|
dœ}d|dœ}|j                  |«       Œz Œº |S )a½  
        Transform Cohere citations to OpenAI annotations format.

        Creates separate annotations for each source in a citation, allowing multiple
        annotations with the same start/end index if they reference different sources.

        Args:
            citations: List of Cohere citation objects with format:
                {
                    "start": int,
                    "end": int,
                    "text": str,
                    "sources": [
                        {
                            "type": "document",
                            "document": {
                                "title": str,
                                "snippet": str,
                                ...
                            },
                            "id": str
                        }
                    ]
                }

        Returns:
            List of OpenAI ChatCompletionAnnotation objects (one per source)
        Ústartr   ÚendÚsourcesÚtypeÚdocumentÚtitler_   Úurlzsource:ÚidÚunknown)Ústart_indexÚ	end_indexr“   r”   Úurl_citation)r‘   r™   )rn   rs   )r,   ra   rd   Úcitationr—   r˜   r�   Úsourcer’   r“   r”   r™   Ú
annotations                r4   rq   z=CohereV2ChatConfig._translate_citations_to_openai_annotations%  s÷   € ð: >@ˆà!ò 	3ˆHØ"Ÿ,™, w°Ó2ˆKØ Ÿ™ U¨AÓ.ˆIð —l‘l 9¨bÓ1ˆGÙØð "ò 3�Ø—:‘:˜fÓ%¨Ó3¸
ÀfÒ8LØ% jÑ1�HØ$ŸL™L¨°"Ó5�EØ Ÿ*™* UÓ+ÒV°¸¿¹ÀDÈ)Ó9TÐ8UÐ/V�Cð (3Ø%.Ø!&Ø"ñ	I�Lð !/Ø(4ñ<�Jð
  ×&Ñ& zÕ2ñ%3ð	3ð< Ðr6   )NNNNNNNNNNNNNNNNNN)NN)F)N)1Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚstrÚ__annotations__r   Úlistr   r   r   r   r   r   Úboolr    r!   rv   r"   r#   r$   r%   r&   r'   r(   r)   r5   Údictr   r   rI   rP   rS   ÚhttpxÚResponser   ÚLiteLLMLoggingObjr‚   r   r   r†   r‰   ÚHeadersr   rŒ   r   rq   Ú__classcell__)r0   s   @r4   r   r       sÒ  ø… ñð0  €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‘
Ó!Ø€A€sˆT�zÓØ€A€sˆT�zÓØ$(Ð�s˜T‘zÓ(Ø#'Ð�c˜D‘jÓ'Ø€Eˆ4�$‰;ÓØ $€L�$˜‘+Ó$Ø€Dˆ#�‰*Óð  $Ø$(Ø$(Ø"&Ø&*Ø(,Ø"&Ø+/Ø!%Ø"&Ø!%ØØØ(,Ø'+Ø!Ø$(Øñ'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ð �‰:ð4ð �‰:ð4ð  ™:ð4ð   ™*ð!4ð" �d‰{ð#4ð$ ˜T‘kð%4ð& �D‰jð'4ð( 
ó)4ðB #Ø#ñ
àð
ð ð
ð Ð'Ñ(ð	
ð
 ð
ð ð
ð �t‘ð
ð ˜‘*ð
ð 
ó
ð$
°ð 
¸¸c¹ó 
ð"à ðð ðð ð	ð
 ðð 
óð@àðð Ð'Ñ(ðð ð	ð
 ðð ðð 
õð4 #Ø!%ñKàðKð —n‘nðKð &ð	Kð
 'ðKð ðKð Ð'Ñ(ðKð ðKð ðKð -ðKð �t‘ðKð ˜$‘;ðKð 
óKðb "'ñ	

à$ S™M¨M¸#Ñ,>Ñ>ÀÑNð

ð ð

ð ˜$‘;ó	

ð& #ñà˜‘*ðð �t‘ðð ð	ð
 ðð ðð �t‘ðð 
óð"K¨Sð K¸sð KÈTÐTY×TaÑTaÑMað KÐfvó Kð=ÀDÈÁJð =ÐSWÐXpÑSq÷ =r6   r   )#rw   Úcollections.abcr   r   Útypingr   r   r   r¦   rt   Ú)litellm.llms.base_llm.chat.transformationr   Ú+litellm.llms.openai.chat.gpt_transformationr	   Úlitellm.types.llms.coherer
   Úlitellm.types.llms.openair   r   r   r   Úlitellm.types.utilsr   r   Úcommon_utilsr   r   r   r?   ÚtiktokenÚ*litellm.litellm_core_utils.litellm_loggingr   Ú_LiteLLMLoggingObjr¨   r   rH   r6   r4   ú<module>r¶      sX   ðÛ ß 3ß ,Ñ ,ã ã Ý FÝ GÝ :÷ó ÷ 5ç EÝ NáÛåXà*ÑàÐôB˜õ Br6   