
    g                         d dl Z d dlmZmZ d dl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  G d de      Z e       Z G d	 d
e      Zy)    N)AnyOptional)ChatCompletionRequestverbose_logger)CustomLogger)AnthropicMessagesRequestAnthropicResponse)AdapterCompletionStreamWrapperModelResponsec                   ^     e Zd Zd	 fdZdee   fdZdedee   fdZ	de
dedz  fdZ xZS )
AnthropicAdapterreturnNc                 "    t         |           y )N)super__init__)self	__class__s    W/var/www/openai/venv/lib/python3.12/site-packages/litellm/adapters/anthropic_adapter.pyr   zAnthropicAdapter.__init__   s        c                 d    t        di |}t        j                         j                  |      }|S )zM
        - translate params, where needed
        - pass rest, as is
        )anthropic_message_request )r   litellm&AnthropicExperimentalPassThroughConfigtranslate_anthropic_to_openai)r   kwargsrequest_bodytranslated_bodys       r   !translate_completion_input_paramsz2AnthropicAdapter.translate_completion_input_params   s;     09&9!HHJhh&2 i 
 r   responsec                 J    t        j                         j                  |      S )Nr    )r   r   &translate_openai_response_to_anthropic)r   r    s     r   "translate_completion_output_paramsz3AnthropicAdapter.translate_completion_output_params    s)     ==?ff g 
 	
r   completion_streamc                     t        |      S )N)r%   )AnthropicStreamWrapper)r   r%   s     r   ,translate_completion_output_params_streamingz=AnthropicAdapter.translate_completion_output_params_streaming(   s     &8IJJr   )r   N)__name__
__module____qualname__r   r   r   r   r   r	   r$   r   r
   r(   __classcell__)r   s   @r   r   r      sS    	'	(
%
	#	$
K!$K	'$	.Kr   r   c                   j    e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	eed<   dZ
ee   ed<   d	 Zd
 Zy)r'   z
    - first chunk return 'message_start'
    - content block must be started and stopped
    - finish_reason must map exactly to anthropic reason, else anthropic client won't be able to parse it.
    Fsent_first_chunksent_content_block_startsent_content_block_finishsent_last_messageNholding_chunkc                    	 | j                   du rd| _         ddddg dd d dd	d
ddS | j                  du rd| _        ddddddS | j                  D ]  }|dk(  s|t        t	        j
                         j                  |      }|d   dk(  r#| j                  du r|| _        d| _        dddc S | j                  | j                  }|| _        |c S |c S  | j                  | j                  }d | _        |S | j                  du rd| _	        ddiS t        # t        $ r" | j                  du rd| _	        ddicY S t        t        $ rA}t        j                  dj                  |t        j                                      Y d }~y d }~ww xY w)NFTmessage_start&msg_1nZdL29xx5MUA1yADyHTEsnR8uuvGzszyYmessage	assistantclaude-3-5-sonnet-20240620      input_tokensoutput_tokensidtyperolecontentmodelstop_reasonstop_sequenceusager@   r6   content_block_startr   text r@   rI   r@   indexcontent_blockNoner"   r@   message_deltacontent_block_stopr@   rM   message_stopzAnthropic Adapter - {}
{})r.   r/   r%   	Exceptionr   r   0translate_streaming_openai_response_to_anthropicr0   r2   r1   StopIterationr   errorformat	traceback
format_exc)r   chunkprocessed_chunkreturn_chunkes        r   __next__zAnthropicStreamWrapper.__next__>   s   ?	$$-(,%+F ) +#%!='+)-24q!I	   ,,504-1.4b%A  //F?em#O")"P"P"R  #D  #D" #D # $F+>66%?)8D&59D2 4!"  ''3#'#5#5L)8D&''**- 0. !!-#11%)"##%%.)-&// 	 %%.)-&// 	  ,33Ay7K7K7MN 	sG   %D D A*D 1"D D !D :D D "F=F7FFc                   K   	 | j                   du rd| _         ddddg dd d dd	d
ddS | j                  du rd| _        ddddddS | j                  2 3 d {   }|dk(  s|t        t	        j
                         j                  |      }|d   dk(  r#| j                  du r|| _        d| _        dddc S | j                  | j                  }|| _        |c S |c S 7 6 | j                  | j                  }d | _        |S | j                  du rd| _	        ddiS t        # t        $ r" | j                  du rd| _	        ddicY S t        w xY ww)NFTr4   r5   r6   r7   r8   r9   r:   r;   r>   rG   rH   r   rI   rJ   rK   rL   rO   r"   r@   rP   rQ   rR   rS   )r.   r/   r%   rT   r   r   rU   r0   r2   r1   rV   StopAsyncIteration)r   r[   r\   r]   s       r   	__anext__z AnthropicStreamWrapper.__anext__   s    9	%$$-(,%+F ) +#%!='+)-24q!I	   ,,504-1.4b%A 
  $55 +eF?em#O")"P"P"R  #D  #D" #D # $F+>66%?)8D&59D2 4!"  ''3#'#5#5L)8D&''**++5, !!-#11%)"##%%.)-&// 	%%%.)-&//$$		%s   E%D# ED# ED# C"C C"AD# 8E9"D# ED# E C""!D# ED# ED# #"EEEE)r)   r*   r+   __doc__r.   bool__annotations__r/   r0   r1   r2   r   r   r_   rb   r   r   r   r'   r'   1   sO     #d"%*d*&+t+#t##'M8C='@D:%r   r'   )rY   typingr   r   r   r   r   "litellm.integrations.custom_loggerr   litellm.types.llms.anthropicr   r	   litellm.types.utilsr
   r   r   anthropic_adapterr'   r   r   r   <module>rk      sE        9 ; T MK| KB %& I%; I%r   