
    )#h=m                     &   d Z ddlZddlZddlmZmZmZmZmZm	Z	 ddl
Z
ddlZddl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 ddlmZmZmZmZmZmZmZ dd	l m!Z!m"Z" dd
lm#Z# ddl$m%Z%m&Z&m'Z' ddl(m)Z) ddl*m+Z+m,Z, ddl-m.Z. dee   de/de0de/de/de1dee	e2e
jf                  f      de4deee
jj                  f   fdZ6dee   de/de0de/de/de1dee	e2e
jf                  f      de4deee
jj                  f   fdZ7 G d de)      Z8 G d d       Z9y)!zE
Calling + translation logic for anthropic's `/v1/messages` endpoint
    N)AnyCallableListOptionalTupleUnion)LlmProviders)map_finish_reason)
BaseConfig)AsyncHTTPHandlerHTTPHandlerget_async_httpx_client)!AnthropicChatCompletionUsageBlockContentBlockDeltaContentBlockStartContentBlockStopMessageBlockDeltaMessageStartBlock
UsageDelta)ChatCompletionToolCallChunkChatCompletionUsageBlock)GenericStreamingChunk)CustomStreamWrapperModelResponseProviderConfigManager   )BaseLLM   )AnthropicErrorprocess_anthropic_headers   )AnthropicConfigclientapi_baseheadersdatamodelmessagestimeout	json_modereturnc	                   K   | t         j                  } 	 | j                  |||d|       d {   }	t        |	j                         d|	      }|j!                  |d
|d|i       ||	j"                  fS 7 E# t        j                  $ rs}
t        |
dd       }t        |
dd       }||rt        |dd       }t        |
j                  j                  |
j                  j                          d {  7  |      d }
~
wt        $ r>}
t         j                  D ]  }t        |
|      s|
 t        dt        |
            d }
~
ww xY ww)NTr%   r&   streamr)   r%   responsestatus_codemessager%     r1   r2   Fstreaming_responsesync_streamr*    complete_input_dictinputapi_keyoriginal_responseadditional_args)litellmmodule_level_aclientposthttpxHTTPStatusErrorgetattrr   r/   r1   aread	ExceptionLITELLM_EXCEPTION_TYPES
isinstancestrModelResponseIteratoraiter_lines	post_callr%   )r#   r$   r%   r&   r'   r(   logging_objr)   r*   r/   eerror_headerserror_response	exceptioncompletion_streams                  `/var/www/html/sandstorm/venv/lib/python3.12/site-packages/litellm/llms/anthropic/chat/handler.py	make_callrT   ,   s\     ~-->gDw % 
 
& .#//1 +.5	   h....C
    	
9d3 J5 ^#NItDM

..****,,,!
 	

  > 88 	I!Y'	 c!f==	>sW   EA7 A5A7 AE5A7 7E
A"C8,C/-C88ED=$D==EEc	                    | t         j                  } 	 | j                  |||d|      }	|	j                  dk7  r3t        |	dd       }t        |	j                  |	j                         |      t        |	j                         d|	      }|j!                  |d
dd|i       ||	j"                  fS # t        j                  $ rj}
t        |
dd       }t        |
dd       }||rt        |dd       }t        |
j                  j                  |
j                  j                         |      d }
~
wt        $ r>}
t         j                  D ]  }t        |
|      s|
 t        dt        |
            d }
~
ww xY w)NTr-   r%   r/   r0   r3   r4      r5   r8   zfirst stream response receivedr9   r:   )r?   module_level_clientrA   rB   rC   rD   r   r/   r1   readrF   rG   rH   rI   rJ   
iter_linesrL   r%   )r#   r$   r%   r&   r'   r(   rM   r)   r*   r/   rN   rO   rP   rQ   response_headersrR   s                   rS   make_sync_callr[   _   s    ~,,>;;gDw  
& s""8Y= ,,MMO$
 	
 .#..0di
 :.5	   h....I    	
9d3 J5 ^#NItDM

..JJOO%!
 	

  > 88 	I!Y'	 c!f==	>s*   B- -E/ A%D%%E/1E*E**E/c                   R    e Zd Zd fdZdddi fdededededed	ed
e	e
ej                  f   dee   dedefdZdi dfdededededed	ed
e	e
ej                  f   dedededededee   de	eef   fdZddi dfdedededededed	eded
e	e
ej                  f   defdZd Z xZS )AnthropicChatCompletionr+   Nc                 "    t         |           y N)super__init__)self	__class__s    rS   ra   z AnthropicChatCompletion.__init__   s        r'   r(   r$   custom_prompt_dictmodel_responseprint_verboser)   r#   r&   r*   c                    K   d|d<   t        |||t        j                  |      |||||	       d {   \  }}t        ||d|t	        |            }|S 7 "w)NTr.   	r#   r$   r%   r&   r'   r(   rM   r)   r*   	anthropicrR   r'   custom_llm_providerrM   _response_headers)rT   jsondumpsr   r    )rb   r'   r(   r$   re   rf   rg   r)   r#   encodingr<   rM   r.   _is_function_callr&   r*   optional_paramslitellm_params	logger_fnr%   rR   streamwrappers                         rS   acompletion_stream_functionz3AnthropicChatCompletion.acompletion_stream_function   sw     , X+4D!#
,
 
&
"7 ,/ +#7@
 %
&
s   0AA#Arr   rs   provider_configc                 "  K   |xs$ t        t        j                  j                        }	 |j	                  ||||       d {   }|j                  ||||
|	||||||      S 7  # t
        $ r}|
j                  ||	t        |      d|i       t        |dd      }t        |dd       }t        |dt        |            }t        |d	d       }||rt        |dd       }|rt        |d      rt        |d|      }t        |||
      d }~ww xY ww)N)llm_provider)r%   rn   r)   r9   r:   r1   r3   r%   textr/   r2   r1   r%   r'   raw_responserf   rM   r<   request_datar(   rr   rs   rp   r*   )r   r?   r	   	ANTHROPICrA   rF   rL   rI   rD   hasattrr   transform_response)rb   r'   r(   r$   re   rf   rg   r)   rp   r<   rM   r.   rq   r&   rr   r*   rs   rw   rt   r%   r#   async_handlerr/   rN   r1   rO   
error_textrP   s                               rS   acompletion_functionz,AnthropicChatCompletion.acompletion_function   sN    .  
"8 --77#
	*//'g 0  H2 11!)#+) 2 
 	
3  	!!"%a&!6 =	 "  "!]C8K#Ay$7M FCF3J$Q
D9N$ '	4 H'.&"A$^VZH
 "'% !	s;   )DA' A%A' 	D%A' '	D0BDDDrl   c                 "   t        j                  |      }|j                  dd       }|j                  dd      }|j                  dd      }d}t        j                  |      }t               j	                  |	|||i |d|i      }t        j                  |t        |            }|j                  |||||      }|
j                  ||	|||d	        |d
|        |du rs|du rJ |d       ||d<   | j                  |||||||||	|
|||||||||t        |t              r|      S d       S | j                  |||||||||	||
|||||||||      S |du rH||d<   t        |||t        j                   |      |||
||	      \  }}t#        ||d|
t%        |            S |t        |t&              st'        |      }n|}	 |j)                  ||t        j                   |      |      }|j5                  ||||
|	||||||      S # t*        $ r|}t-        |dd      }t-        |dd       }t-        |dt/        |            }t-        |dd       }||rt-        |dd       }|rt1        |d      rt-        |d|      }t3        |||      d }~ww xY w)Nr.   r*   Fis_vertex_request)r<   r%   r'   r(   rr   )r'   provider)r'   r(   rr   rs   r%   )r9   r$   r%   )r;   r<   r>   z_is_function_call: Tz,makes async anthropic streaming POST request)r'   r(   r&   r$   re   rf   rg   rp   r<   rM   rr   r.   rq   r*   rs   rt   r%   r)   r#   )r'   r(   r&   r$   re   rf   rg   rp   r<   rw   rM   rr   r.   rq   rs   rt   r%   r#   r*   r)   ri   rj   rk   )r)   )r%   r&   r)   r1   r3   r%   rz   r/   r{   r|   )copydeepcopypopr"   validate_environmentr   get_provider_chat_configr	   transform_requestpre_callrv   rH   r   r   r[   rn   ro   r   r    r   rA   rF   rD   rI   r   r   r   )rb   r'   r(   r$   rl   re   rf   rg   rp   r<   rM   rr   r)   rs   acompletionrt   r%   r#   r.   r*   r   rq   configr&   rR   r/   rN   r1   rO   r   rP   s                                  rS   
completionz"AnthropicChatCompletion.completion  s   * --8 $$Xt4)--k5A	"1"5"56I5"Q!==*!#88WW0CEVW 9 
 '??!"56

 ''+) ( 
 	'+$" 	 	
 	+,=+>?@$$LM!'X77%%'9#1"/%# +$3!&7'#1'## "-*VEU2V ) 8  , "- 8  4 00%%'9#1"/%#$* +$3!&7#1'#!'#) 1  2 $!'X-;!%#D)% +#'
.*!7 +&7(3 +&?&H  >FK)H(9F#F%{{  '!ZZ- '	  +  H* ((!)#+) ) 
 	
 ! ")!]C"@K$+Ay$$?M!(FCF!;J%,Q
D%AN$,(/	4(P%'.&*I%,^VZ%P
( *$/ - s   (H	 		JA7J		Jc                      y r_    rb   s    rS   	embeddingz!AnthropicChatCompletion.embedding  s    rd   )r+   N)__name__
__module____qualname__ra   rI   listdictr   r   r   floatrB   Timeoutr   r   boolrv   r   r   r   r   r   __classcell__)rc   s   @rS   r]   r]      s   ( )** * 	*
 !* &*  * uemm+,* )** *  !*~ -1+A
A
 A
 	A

 !A
 &A
  A
 uemm+,A
 A
 A
  !A
" #A
$ $%A
* )*+A
, 
}11	2-A
d %r
r
 r
 	r

 !r
 !r
 &r
  r
 r
 uemm+,r
 r
hrd   r]   c            	           e Zd Z	 ddedee   fdZdefdZdeee	f   de
fdZdedefd	Zd
edee   deeee   f   fdZd Zd Zd Zd ZdedefdZy)rJ   r7   r*   c                 ^    || _         | j                   | _        g | _        d| _        || _        y )N)r6   response_iteratorcontent_blocks
tool_indexr*   )rb   r6   r7   r*   s       rS   ra   zModelResponseIterator.__init__  s0     #5!%!8!879"rd   r+   c                     d}t        | j                        dk(  ry| j                  d   d   d   dk(  ry| j                  D ]&  }|d   d   dk(  s||d   j                  dd      z  }( t        |      dk(  ry	y)
zN
        Check if the tool call block so far has been an empty string
        r8   r   Fdeltatype
text_deltainput_json_deltapartial_jsonT)lenr   get)rb   argsblocks      rS   check_empty_tool_call_argsz0ModelResponseIterator.check_empty_tool_call_args  s     t""#q(q!'*62lB(( 	?EW~f%);;g**>2>>	? t9>rd   anthropic_usage_chunkc           	      H   t        |j                  dd      |j                  dd      |j                  dd      |j                  dd      z         }|j                  d      }|t        |t              r||d<   |j                  d      }|t        |t              r||d<   |S )Ninput_tokensr   output_tokens)prompt_tokenscompletion_tokenstotal_tokenscache_creation_input_tokenscache_read_input_tokens)r   r   rH   int)rb   r   usage_blockr   r   s        rS   _handle_usagez#ModelResponseIterator._handle_usage  s     8/33NAF377K.22>1E#'';<
 '<&?&?)'
# '2z'8
 :UK56"7";";<U"V".:#S4
 6MK12rd   chunkc                    	 |j                  dd      xs d}d}d }d}d}d }t        |j                  dd            }|dk(  r\	 t        d$i |}	| j                  j	                  |	       d|	d   v r
|	d   d   }nd	|	d   v rd d
d |	d   d	   d| j
                  d}nk|dk(  rj	 t        d$i |}
g | _        |
d   d   dk(  r
|
d   d   }n>|
d   d   dk(  r2| xj
                  dz  c_        |
d   d   d
|
d   d   dd| j
                  d}n|dk(  r2t        d$i | | j                         }|rd d
d dd| j
                  d}n|dk(  rG	 t        d$i |}t        |d   j                  dd      xs d      }| j                  |d         }d}ny|dk(  r,	 t        d$i |}d|d   v ra| j                  |d   d         }nH|dk(  rC	 |j                  di       xs i }|j                  dd       xs t        |      }t        |d       | j                  ||!      \  }}t!        ||||||"      }|S # t"        j$                  $ r t'        d#|       w xY w)%Nr   r8   Findexr   content_block_deltarz   r   r   function)name	arguments)idr   r   r   content_block_startcontent_blocktool_user!   r   r   content_block_stopz{}message_deltastop_reasonstop)finish_reasonusage)r   Tmessage_startr2   errorr3   )r2   r1   )rz   r   )rz   r   is_finishedr   r   r   z"Failed to decode JSON from chunk: r   )r   r   r   r   appendr   r   r   r   r   r
   r   r   rI   r   _handle_json_mode_chunkr   rn   JSONDecodeError
ValueError)rb   r   
type_chunkrz   r   r   r   r   r   r   r   is_emptyr   message_start_block_error_dictr2   returned_chunks                    rS   chunk_parserz"ModelResponseIterator.chunk_parser  s#   ~	K62.4"JD>BHKM8<E		'1-.E22 !2 :E :##**=9]733(1&9D#}W'==" *$()6w)?)O% "& H 44 '8&@%&@#&(#&7?6I.?GD(9&AZOOOq(O1/B4H *$7$H$P)+% "& H 33 )5)::<" *$()-% "& H .
 !2 :E : 1"/"8"<"<]F"S #! **wAW*X".& '8&@%&@#1)<< ...A).LW.U / E w& $ii4:%//)T:Hc%j$# # 
 "99th9WND(2!'+N "!## 	KA%IJJ	Ks   H/H2 2"Irz   r   c                     | j                   du r-|+t        j                  |g      }||j                  xs d}d}||fS )a-  
        If JSON mode is enabled, convert the tool call to a message.

        Anthropic returns the JSON schema as part of the tool call
        OpenAI returns the JSON schema as part of the content, this handles placing it in the content

        Args:
            text: str
            tool_use: Optional[ChatCompletionToolCallChunk]
        Returns:
            Tuple[str, Optional[ChatCompletionToolCallChunk]]

            text: The text to use in the content
            tool_use: The ChatCompletionToolCallChunk to use in the chunk response
        TN)
tool_callsr8   )r*   r"   !_convert_tool_response_to_messagecontent)rb   rz   r   r2   s       rS   r   z-ModelResponseIterator._handle_json_mode_chunk~  sO    $ >>T!h&:%GG$:G ","X~rd   c                     | S r_   r   r   s    rS   __iter__zModelResponseIterator.__iter__  s    rd   c                    	 | j                   j                         }	 |}t        |t              r,|j                  d      }|j                  d      }|dk7  r||d  }|j                  d      r*t        j                  |dd        }| j                  |      S t        dddd d	d 
      S # t        $ r t        t        $ r}t	        d|       d }~ww xY w# t        $ r t        t        $ r}t	        d| d|       d }~ww xY wNz#Error receiving chunk from stream: utf-8data:r      r   r8   Fr   rz   r   r   r   r   r   zError parsing chunk: z,
Received chunk: )r   __next__StopIterationr   RuntimeErrorrH   bytesdecodefind
startswithrn   loadsr   r   rb   r   rN   str_liner   	data_jsons         rS   r   zModelResponseIterator.__next__  s%   	J**335E	VH%' <<0 g.B;'/H""7+ JJx|4	((y(99, %"$! #  	  	J!DQCHII	J.  	  	V!6qc9LUGTUU	Vs5   B' A8C C 'C>CCD+C<<Dc                 D    | j                   j                         | _        | S r_   )r6   	__aiter__async_response_iteratorr   s    rS   r   zModelResponseIterator.__aiter__  s    '+'>'>'H'H'J$rd   c                 $  K   	 | j                   j                          d {   }	 |}t        |t              r,|j                  d      }|j                  d      }|dk7  r||d  }|j                  d      r*t        j                  |dd        }| j                  |      S t        dddd d	d 
      S 7 # t        $ r t        t        $ r}t	        d|       d }~ww xY w# t        $ r t        t        $ r}t	        d| d|       d }~ww xY wwr   )r   	__anext__StopAsyncIterationr   r   rH   r   r   r   r   rn   r   r   r   r   s         rS   r   zModelResponseIterator.__anext__  s/    	J66@@BBE	VH%' <<0 g.B;'/H""7+ JJx|4	((y(99, %"$! % C! 	%$$ 	J!DQCHII	J. " 	%$$ 	V!6qc9LUGTUU	Vsb   DB3 B1B3 A8C  D C  0D1B3 3C
CCD D7DDDc                    |}t        |t              r,|j                  d      }|j                  d      }|dk7  r||d }|j	                  d      r*t        j                  |dd       }| j                  |      S t        ddddd	d
      S )a%  
        Convert a string chunk to a GenericStreamingChunk

        Note: This is used for Anthropic pass through streaming logging

        We can move __anext__, and __next__ to use this function since it's common logic.
        Did not migrate them to minmize changes made in 1 PR.
        r   r   r   Nr   r   r8   Fr   r   )	rH   r   r   r   r   rn   r   r   r   )rb   r   r   r   r   s        rS   "convert_str_chunk_to_generic_chunkz8ModelResponseIterator.convert_str_chunk_to_generic_chunk  s     eU#||G,HMM'*E{#EF+w'

8AB<0I$$9$55(!  rd   N)F)r   r   r   r   r   ra   r   r   r   r   r   r   r   r   rI   r   r   r   r   r   r   r   r   r   rd   rS   rJ   rJ     s    QV#/3#@H#D (%*4+;%<	*6K$ K+@ KB#+,G#H	sH899	::VDVB @U rd   rJ   ):__doc__r   rn   typingr   r   r   r   r   r   rB   r?   litellm.litellm_core_utilslitellm.typeslitellm.types.utilsr	   'litellm.litellm_core_utils.core_helpersr
   )litellm.llms.base_llm.chat.transformationr   &litellm.llms.custom_httpx.http_handlerr   r   r   litellm.types.llms.anthropicr   r   r   r   r   r   r   litellm.types.llms.openair   r   r   litellm.utilsr   r   r   baser   common_utilsr   r    transformationr"   rI   r   r   r   r   r   HeadersrT   r[   r]   rJ   r   rd   rS   <module>r	     s     > >   !     E @ 
   6 S S  D +0/%&0/0/ 0/ 	0/
 0/ 0/ eE5==0120/ 0/ 30/f6/[!6/6/ 6/ 	6/
 6/ 6/ eE5==0126/ 6/ 36/rig iX	| |rd   