
    ^hO)                         S r SSKrSSKrSSKJrJrJrJrJrJ	r	  SSK
r
SSKrSSKJrJr  SSKJr  SSKJrJrJr  SSKJr  SSKJrJrJrJrJr   " S	 S
\5      r " S S\5      r " S S\5      rg)zq
Translation logic for anthropic's `/v1/complete` endpoint

Litellm provider slug: `anthropic_text/<model_name>`
    N)AsyncIteratorDictIteratorListOptionalUnion)custom_promptprompt_factory)BaseModelResponseIterator)
BaseConfigBaseLLMExceptionLiteLLMLoggingObj)AllMessageValues)ChatCompletionToolCallChunkChatCompletionUsageBlockGenericStreamingChunkModelResponseUsagec                   (   ^  \ rS rSrU 4S jrSrU =r$ )AnthropicTextError"   c                   > Xl         X l        [        R                  " SSS9U l        [        R
                  " XR                  S9U l        [        TU ]!  U R                  U R                   U R                  U R                  S9  g )NPOSTz%https://api.anthropic.com/v1/complete)methodurl)status_coderequest)messager   r   response)	r   r   httpxRequestr   Responser   super__init__)selfr   r   	__class__s      j/home/james-whalen/.local/lib/python3.13/site-packages/litellm/llms/anthropic/completion/transformation.pyr$   AnthropicTextError.__init__#   sj    &}}F
 ;ULL((LL]]	 	 	
    )r   r   r   r   )__name__
__module____qualname____firstlineno__r$   __static_attributes____classcell__)r&   s   @r'   r   r   "   s    
 
r)   r   c                      \ rS rSr% Sr\R                  r\\	   \
S'   Sr\\   \
S'   Sr\\	   \
S'   Sr\\	   \
S'   Sr\\	   \
S'   Sr\\   \
S	'         S(S\\	   S\\   S\\	   S\\	   S\\	   S	\\   S
S4S jjr S)S\S\S\\   S\S\\   S
\4S jjrS\S\\   S\S\S\S
\4S jrS\4S jrS\S\S\S\S
\4
S jr  S*S\S\R8                  S\S\S\S\\   S\S\S\S\\   S\\   S
\4S jjrS\S \	S\ \!\RD                  4   S
\#4S! jr$\%S\S
\4S" j5       r&S\\   S\S
\4S# jr' S+S$\ \(\   \)\   \4   S%\S\\   4S& jjr*S'r+g),AnthropicTextConfig2   z
Reference: https://docs.anthropic.com/claude/reference/complete_post

to pass metadata to anthropic, it's {"user_id": "any-relevant-information"}
max_tokens_to_sampleNstop_sequencestemperaturetop_ptop_kmetadatareturnc                     [        5       nUR                  5        H(  u  pUS:w  d  M  U	c  M  [        U R                  X5        M*     g )Nr%   )localsitemssetattrr&   )
r%   r3   r4   r5   r6   r7   r8   locals_keyvalues
             r'   r$   AnthropicTextConfig.__init__B   s7     (!--/JCf}!23 *r)   headersmodelmessagesoptional_paramsapi_keyc                 R    Uc  [        S5      eSSSUS.nUR                  U5        U$ )NzMissing Anthropic API Key - A call is being made to anthropic but no key is set either in the environment variables or via paramszapplication/jsonz
2023-06-01)acceptzanthropic-versionzcontent-typez	x-api-key)
ValueErrorupdate)r%   rB   rC   rD   rE   rF   _headerss          r'   validate_environment(AnthropicTextConfig.validate_environmentQ   sD     ? T  )!-. 	
 	x r)   litellm_paramsc                     U R                  X!S9n[        R                  R                  5       nUR	                  5        H  u  pX;  d  M  XU'   M     UUS.UEn
U
$ )NrD   rC   )rC   prompt)(_get_anthropic_text_prompt_from_messageslitellmr1   
get_configr<   )r%   rC   rD   rE   rN   rB   rQ   configkvdatas              r'   transform_request%AnthropicTextConfig.transform_requestf   sr     >> ? 
 ,,779LLNDA(%&"	 # 
 
 r)   c                 
    / SQ$ )zI
Anthropic /complete API Ref: https://docs.anthropic.com/en/api/complete
)stream
max_tokensmax_completion_tokensstopr5   r6   extra_headersuser )r%   rC   s     r'   get_supported_openai_params/AnthropicTextConfig.get_supported_openai_params   s    	
 		
r)   non_default_paramsdrop_paramsc                 z   UR                  5        H  u  pVUS:X  a  XbS'   US:X  a  XbS'   US:X  a	  USL a  XbS'   US:X  aU  [        U[        5      (       d  [        U[        5      (       a+  [        R
                  " 5       R                  U5      nUb  XrS'   US:X  a  XbS'   US	:X  a  XbS	'   US
:X  d  M  SU0US'   M     U$ )a  
Follows the same logic as the AnthropicConfig.map_openai_params method (which is the Anthropic /messages API)

Note: the only difference is in the get supported openai params method between the AnthropicConfig and AnthropicTextConfig
API Ref: https://docs.anthropic.com/en/api/complete
r]   r3   r^   r\   Tr_   r4   r5   r6   ra   user_idr8   )r<   
isinstancestrlistrS   AnthropicConfig_map_stop_sequences)r%   re   rE   rC   rf   paramr@   _values           r'   map_openai_params%AnthropicTextConfig.map_openai_params   s     /446LE$:? 67//:? 67 Ud],1)Juc$:$:jPT>U>U 002FFuM%8>$45%16.+0(/8%.@
+! 7$ r)   raw_responsemodel_responselogging_objrequest_dataencoding	json_modec                     UR                  5       nU R                  XaS9nSU;   a   [        [        US   5      UR                  S9e[        US   5      S:  a!  US   UR                  S   R                  l
        US   UR                  S   l        [        U	R                  U5      5      n[        U	R                  US   S   S   R                  S	S
5      5      5      n[        [        R                  " 5       5      Ul        Xl        [%        UUX-   S9n['        USU5        U$ ! [         a    [        UR                  UR                  S9ef = f)N)r   r   rP   error
completionr   stop_reasonchoicesr   content )prompt_tokenscompletion_tokenstotal_tokensusage)json	Exceptionr   textr   rR   rj   lenr|   r   r}   finish_reasonencodegetinttimecreatedrC   r   r=   )r%   rC   rr   rs   rt   ru   rD   rE   rN   rv   rF   rw   completion_responserQ   r   r   r   s                    r'   transform_response&AnthropicTextConfig.transform_response   ss   	"."3"3"5
 >> ? 
 ))$/89(44 
 &|459<O =&&q)119 7J-6XN""1%3 OOF#
  OON95a8CGG	SUVW
 "%TYY[!1$'/&:
 	/I  	$$))|7O7O 	s   D# #)Eerror_messager   c                     [        UUS9$ )N)r   r   )r   )r%   r   r   rB   s       r'   get_error_class#AnthropicTextConfig.get_error_class   s     "#!
 	
r)   c                 $    U S:H  =(       d    U S:H  $ )Nzclaude-2zclaude-instant-1rb   )rC   s    r'   _is_anthropic_text_model,AnthropicTextConfig._is_anthropic_text_model   s    
"Ae/A&AAr)   c                     [         R                  nX#;   a  X2   n[        US   US   US   US9nO
[        X!SS9n[	        U5      $ )Nrolesinitial_prompt_valuefinal_prompt_value)	role_dictr   r   rD   	anthropic)rC   rD   custom_llm_provider)rS   custom_prompt_dictr	   r
   rj   )r%   rD   rC   r   model_prompt_detailsrQ   s         r'   rR   <AnthropicTextConfig._get_anthropic_text_prompt_from_messages   sc     %77&#5#< ".w7%9:P%Q#78L#M!	F $KF 6{r)   streaming_responsesync_streamc                     [        UUUS9$ )N)r   r   rw   )'AnthropicTextCompletionResponseIterator)r%   r   r   rw   s       r'   get_model_response_iterator/AnthropicTextConfig.get_model_response_iterator  s     71#
 	
r)   rb   )   NNNNN)N)NN)F),r*   r+   r,   r-   __doc__rS   r]   r3   r   r   __annotations__r4   rk   r5   r6   r7   r8   dictr$   rj   r   r   rL   rY   rc   boolrp   r    r"   r   r   r   r   r   Headersr   r   staticmethodr   rR   r   r   r   r.   rb   r)   r'   r1   r1   2   s
    	 (3-  &*NHTN)!%K#%E8C=E8C=#Hhtn# /2)-%)###'4&sm4 !4 c]	4
 }4 }4 4.4 
4* "&  '(	
  # 
* '( 	
   
6
 
   	
  
X "&$(44 nn4 &	4
 '4 4 '(4 4 4 4 #4 D>4 
4l
 
/2
=B4CV=W
	
 B B B B-.7:	0 %*	

!(3-s1C]"RS

 

 D>	

 

r)   r1   c                   &    \ rS rSrS\S\4S jrSrg)r   i  chunkr9   c           
      F    SnS nSnSnS nS n[        UR                  SS5      5      nUR                  SS 5      n	U	b  [        U	[        5      (       a  U	nUR                  SS 5      nUb  Sn[	        UUUUUUUS9n
U
$ ! [
        R                   a    [        S	U 35      ef = f)
Nr~   Findexr   rz   r{   T)r   tool_useis_finishedr   r   r   provider_specific_fieldsz"Failed to decode JSON from chunk: )r   r   ri   rj   r   r   JSONDecodeErrorrI   )r%   r   r   r   r   r   r   r   r   _chunk_textreturned_chunks              r'   chunk_parser4AnthropicTextCompletionResponseIterator.chunk_parser  s    	KD>BHKM8<E'+$		'1-.E))L$7K&:k3+G+G"!IImT:M("2!'+)AN "!## 	KA%IJJ	Ks   A:A= =#B rb   N)r*   r+   r,   r-   r   r   r   r.   rb   r)   r'   r   r     s    K$ K+@ Kr)   r   ) r   r   r   typingr   r   r   r   r   r   r    rS   3litellm.litellm_core_utils.prompt_templates.factoryr	   r
   )litellm.llms.base_llm.base_model_iteratorr   )litellm.llms.base_llm.chat.transformationr   r   r   litellm.types.llms.openair   litellm.types.utilsr   r   r   r   r   r   r1   r   rb   r)   r'   <module>r      sm      G G   P 
 7 
) 
 _
* _
DK.G Kr)   