o
    ưi8                     @   s   d dl Z d dlmZmZmZm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 G dd de	ZG d	d
 d
eZ	ddededee dedee defddZG dd dZG dd dZdS )    N)ListOptionalLiteralTuple)BaseLLMModelInfo)BaseLLMException)AllMessageValues)ChatCompletionToolCallChunkChatCompletionUsageBlockGenericStreamingChunkProviderSpecificModelInfoc                       s   e Zd Z fddZ  ZS )CohereErrorc                    s   t  j||d d S )N)status_codemessage)super__init__)selfr   r   	__class__ W/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/cohere/common_utils.pyr      s   zCohereError.__init__)__name__
__module____qualname__r   __classcell__r   r   r   r   r      s    r   c                   @   s   e Zd Zdedee fddZ	ddee dee dee fdd	Ze	ddee dee fd
dZ
e		ddee dee fddZ		ddededee dededee dee defddZe	dedee fddZe	deded fddZdS )CohereModelInfomodelreturnc                 C      dS )zE
        Default values all models of this provider support.
        Nr   )r   r   r   r   r   get_provider_info   s   z!CohereModelInfo.get_provider_infoNapi_keyapi_basec                 C   s   g S )zF
        Returns a list of models supported by this provider.
        r   )r   r    r!   r   r   r   
get_models   s   zCohereModelInfo.get_modelsc                 C      | S Nr   )r    r   r   r   get_api_key&   s   zCohereModelInfo.get_api_keyc                 C   r#   r$   r   )r!   r   r   r   get_api_base*   s   zCohereModelInfo.get_api_baseheadersmessagesoptional_paramslitellm_paramsc                 C   s   i S r$   r   )r   r'   r   r(   r)   r*   r    r!   r   r   r   validate_environment0   s   
z$CohereModelInfo.validate_environmentc                 C   r   )a2  
        Returns the base model name from the given model name.

        Some providers like bedrock - can receive model=`invoke/anthropic.claude-3-opus-20240229-v1:0` or `converse/anthropic.claude-3-opus-20240229-v1:0`
            This function will return `anthropic.claude-3-opus-20240229-v1:0`
        Nr   r   r   r   r   get_base_model<   s   zCohereModelInfo.get_base_model)v1v2c                 C   s   d| v rdS dS )a  
        Get the Cohere route for the given model.
        
        Args:
            model: The model name (e.g., "cohere_chat/v2/command-r-plus", "command-r-plus")
            
        Returns:
            "v2" for standard Cohere v2 API (default), "v1" for Cohere v1 API
        zv1/r.   r/   r   r,   r   r   r   get_cohere_routeF   s   z CohereModelInfo.get_cohere_route)NNr$   )r   r   r   strr   r   r   r   r"   staticmethodr%   r&   dictr   r+   r-   r   r0   r   r   r   r   r      s\    


	
	r   r'   r   r(   r)   r    r   c                 C   s(   |  dddd |rd| | d< | S )aQ  
    Return headers to use for cohere chat completion request

    Cohere API Ref: https://docs.cohere.com/reference/chat
    Expected headers:
    {
        "Request-Source": "unspecified:litellm",
        "accept": "application/json",
        "content-type": "application/json",
        "Authorization": "Bearer $CO_API_KEY"
    }
    zunspecified:litellmzapplication/json)zRequest-Sourceacceptzcontent-typezBearer Authorization)update)r'   r   r(   r)   r    r   r   r   r+   X   s   r+   c                   @   sj   e Zd Z	ddedee fddZdedefdd	Zd
d Z	dd Z
dedefddZdd Zdd ZdS )ModelResponseIteratorFsync_stream	json_modec                 C   $   || _ | j | _g | _d| _|| _d S Nstreaming_responseresponse_iteratorZcontent_blocksZ
tool_indexr9   r   r>   r8   r9   r   r   r   r   x   
   
zModelResponseIterator.__init__chunkr   c           
   	   C   s   zGd}d }d}d}d }d }t |dd}d|v r|d }nd|v r0|d du r0|d }|d }d	|v r:d	|d	 i}t|||||||d
}	|	W S  tjyV   td| w )N Findexr   textis_finishedTfinish_reason	citationsrE   tool_userF   rG   usagerD   provider_specific_fieldsz"Failed to decode JSON from chunk: )intgetr   jsonJSONDecodeError
ValueError)
r   rB   rE   rJ   rF   rG   rK   rL   rD   returned_chunkr   r   r   chunk_parser   s8   

z"ModelResponseIterator.chunk_parserc                 C   r#   r$   r   r   r   r   r   __iter__      zModelResponseIterator.__iter__c              
   C      z| j  }W n ty   t ty" } ztd| d }~ww z| j|dW S  ty3   t tyH } z
td| d| d }~ww Nz#Error receiving chunk from stream: rB   zError parsing chunk: z,
Received chunk: r?   __next__StopIterationrQ   RuntimeError"convert_str_chunk_to_generic_chunkr   rB   er   r   r   r[          zModelResponseIterator.__next__c                 C   L   |}t |tr|d}|d}|dkr||d }t|}| j|dS )z
        Convert a string chunk to a GenericStreamingChunk

        Note: This is used for Cohere pass through streaming logging
        utf-8data:r<   NrY   
isinstancebytesdecodefindrO   loadsrS   r   rB   Zstr_linerD   Z	data_jsonr   r   r   r^         



z8ModelResponseIterator.convert_str_chunk_to_generic_chunkc                 C      | j  | _| S r$   r>   	__aiter__async_response_iteratorrT   r   r   r   ro         zModelResponseIterator.__aiter__c              
         z
| j  I d H }W n ty   t ty& } ztd| d }~ww z| j|dW S  ty7   t tyL } z
td| d| d }~ww rX   rp   	__anext__StopAsyncIterationrQ   r]   r^   r_   r   r   r   rt      "   zModelResponseIterator.__anext__NF)r   r   r   boolr   r   r3   r   rS   rU   r[   r1   r^   ro   rt   r   r   r   r   r7   w   s    
	$r7   c                   @   s   e Zd ZdZ	d dedee fddZdedefd	d
Z	dedee
 fddZdedee fddZdedee fddZdedeeeee f fddZdedefddZdd Zdd ZdedefddZdd Zdd ZdS )!CohereV2ModelResponseIteratorz2V2-specific response iterator for Cohere streamingFr8   r9   c                 C   r:   r;   r=   r@   r   r   r   r      rA   z&CohereV2ModelResponseIterator.__init__rB   r   c                 C   sP   | di }| di }| di }t|trd|v r|d S t|tr&|S dS )z+Parse content-delta chunks to extract text.deltar   contentrE   rC   )rN   rf   r3   r1   )r   rB   rz   r   r{   r   r   r   _parse_content_delta   s   
z2CohereV2ModelResponseIterator._parse_content_deltac                 C   sV   | di }| dg }|r)|d  ddd|d  dd|d  ddd	d
S dS )z3Parse tool-call-delta chunks to extract tool calls.rz   
tool_callsr   idrC   functionname	arguments)r   r   )r~   typer   NrN   )r   rB   rz   r}   r   r   r   _parse_tool_call_delta   s   z4CohereV2ModelResponseIterator._parse_tool_call_deltac                 C   s@   | di }| di }| di }| dd}|rd|iS dS )z2Parse tool-plan-delta events to extract tool plan.datarz   r   	tool_planrC   Nr   )r   rB   r   rz   r   r   r   r   r   _parse_tool_plan_delta  s   z4CohereV2ModelResponseIterator._parse_tool_plan_deltac                 C   sz   | di }| di }| di }| di }|r;| dd| dd| dd	| d
g | ddd}d|giS dS )z1Parse citation-start events to extract citations.r   rz   r   rH   startr   endrE   rC   sourcesr   ZTEXT_CONTENT)r   r   rE   r   r   Nr   )r   rB   r   rz   r   rH   Zcitation_datar   r   r   _parse_citation_start  s   





z3CohereV2ModelResponseIterator._parse_citation_startc           	      C   s   | di }| di }d}| dd}d}| di }|r=| di }t| d	d
| dd
| d	d
| dd
 d}|||fS )z:Parse message-end events to extract finish info and usage.r   rz   TrG   stopNrK   tokensZinput_tokensr   Zoutput_tokens)Zprompt_tokensZcompletion_tokensZtotal_tokens)rN   r
   )	r   rB   r   rz   rF   rG   rK   Z
usage_dataZtokens_datar   r   r   _parse_message_end  s   


z0CohereV2ModelResponseIterator._parse_message_endc              
   C   s  zpd}d}d}d}d}d}t |dd}|dd}	|dd}
|	dkr+| |}n*|	d	kr5| |}n |
d
kr?| |}n|
dkrI| |}n|
dkrU| |\}}}d|v re|du r_i }|d |d< t|||||||dW S  ty } z
t	d| d| d}~ww )a  
        Parse Cohere v2 streaming chunks.
        
        v2 format:
        - Content: chunk.type == "content-delta" -> chunk.delta.message.content.text
        - Tool calls: chunk.type == "tool-call-delta" -> chunk.delta.tool_calls
        - Tool plan: chunk.event == "tool-plan-delta" -> chunk.data.delta.message.tool_plan
        - Citations: chunk.event == "citation-start" -> chunk.data.delta.message.citations
        - Finish: chunk.event == "message-end" -> chunk.data.delta.finish_reason
        rC   NFrD   r   r   eventzcontent-deltaztool-call-deltaztool-plan-deltazcitation-startzmessage-endrH   rI   zFailed to parse v2 chunk: z	, chunk: )
rM   rN   r|   r   r   r   r   r   	ExceptionrQ   )r   rB   rE   rJ   rF   rG   rK   rL   rD   Z
chunk_typeZ
event_typer`   r   r   r   rS   /  sJ   
z*CohereV2ModelResponseIterator.chunk_parserc                 C   r#   r$   r   rT   r   r   r   rU   f  rV   z&CohereV2ModelResponseIterator.__iter__c              
   C   rW   rX   rZ   r_   r   r   r   r[   i  ra   z&CohereV2ModelResponseIterator.__next__c                 C   rb   )z
        Convert a string chunk to a GenericStreamingChunk for v2

        Note: This is used for Cohere v2 pass through streaming logging
        rc   rd   r<   NrY   re   rk   r   r   r   r^   x  rl   z@CohereV2ModelResponseIterator.convert_str_chunk_to_generic_chunkc                 C   rm   r$   rn   rT   r   r   r   ro     rq   z'CohereV2ModelResponseIterator.__aiter__c              
      rr   rX   rs   r_   r   r   r   rt     rv   z'CohereV2ModelResponseIterator.__anext__Nrw   )r   r   r   __doc__rx   r   r   r3   r1   r|   r	   r   r   r   r   r
   r   r   rS   rU   r[   r^   ro   rt   r   r   r   r   ry      s&    
	
 7ry   r$   )rO   typingr   r   r   r   Z litellm.llms.base_llm.base_utilsr   Z)litellm.llms.base_llm.chat.transformationr   Zlitellm.types.llms.openair   Zlitellm.types.utilsr	   r
   r   r   r   r   r3   r1   r+   r7   ry   r   r   r   r   <module>   s0    I
d