
    h)                         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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mZmZmZ  G d
 de      Z G d de      Z  G d de      Z!y)zq
Translation logic for anthropic's `/v1/complete` endpoint

Litellm provider slug: `anthropic_text/<model_name>`
    N)AsyncIteratorDictIteratorListOptionalUnion)DEFAULT_MAX_TOKENS)custom_promptprompt_factory)BaseModelResponseIterator)
BaseConfigBaseLLMExceptionLiteLLMLoggingObj)AllMessageValues)ChatCompletionToolCallChunkChatCompletionUsageBlockGenericStreamingChunkModelResponseUsagec                        e Zd Z fdZ xZS )AnthropicTextErrorc                    || _         || _        t        j                  dd      | _        t        j
                  || j                        | _        t        | !  | j                  | j                   | j                  | j                         y )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      l/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/llms/anthropic/completion/transformation.pyr$   zAnthropicTextError.__init__$   sp    &}}F
 ;ULL((LL]]	 	 	
    )__name__
__module____qualname__r$   __classcell__)r&   s   @r'   r   r   #   s    
 
r(   r   c                      e Zd ZU dZej
                  Zee   e	d<   dZ
ee   e	d<   dZee   e	d<   dZee   e	d<   dZee   e	d<   dZee   e	d<   edddddfdee   dee   dee   dee   dee   dee   d	dfd
Z	 	 d'dededee   dededee   dee   d	efdZdedee   dededed	efdZdefdZdedededed	ef
dZ	 	 d'dedej8                  dedededee   dedededee   dee   d	efdZded ede e!ejD                  f   d	e#fd!Z$e%ded	efd"       Z&dee   ded	efd#Z'	 d(d$e e(e   e)e   ef   d%edee   fd&Z*y))AnthropicTextConfigz
    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                     t               j                         }|j                         D ]%  \  }}	|dk7  s|	t        | j                  ||	       ' y )Nr%   )localscopyitemssetattrr&   )
r%   r/   r0   r1   r2   r3   r4   locals_keyvalues
             r'   r$   zAnthropicTextConfig.__init__C   sA     (--/!--/JCf}!2U3 *r(   headersmodelmessagesoptional_paramslitellm_paramsapi_keyapi_basec                 P    |t        d      ddd|d}|j                  |       |S )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%   r>   r?   r@   rA   rB   rC   rD   _headerss	            r'   validate_environmentz(AnthropicTextConfig.validate_environmentT   sD     ? T  )!-. 	
 	x r(   c                     | j                  ||      }t        j                  j                         }|j	                         D ]  \  }}	||vs|	||<    ||d|}
|
S )Nr@   r?   )r?   prompt)(_get_anthropic_text_prompt_from_messageslitellmr.   
get_configr9   )r%   r?   r@   rA   rB   r>   rM   configkvdatas              r'   transform_requestz%AnthropicTextConfig.transform_requestk   sz     >>U ? 
 ,,779LLNDAq(%&"	 # 
 
 r(   c                 
    g dS )zY
        Anthropic /complete API Ref: https://docs.anthropic.com/en/api/complete
        )stream
max_tokensmax_completion_tokensstopr1   r2   extra_headersuser )r%   r?   s     r'   get_supported_openai_paramsz/AnthropicTextConfig.get_supported_openai_params   s    	
 		
r(   non_default_paramsdrop_paramsc                 Z   |j                         D ]  \  }}|dk(  r||d<   |dk(  r||d<   |dk(  r	|du r||d<   |dk(  rJt        |t              st        |t              r*t	        j
                         j                  |      }|||d<   |dk(  r||d<   |d	k(  r||d	<   |d
k(  sd|i|d<    |S )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
        rX   r/   rY   rW   TrZ   r0   r1   r2   r\   user_idr4   )r9   
isinstancestrlistrO   AnthropicConfig_map_stop_sequences)r%   r_   rA   r?   r`   paramr=   _values           r'   map_openai_paramsz%AnthropicTextConfig.map_openai_params   s     /446LE5$:? 67//:? 67 Ud],1)Juc$:jPT>U 002FFuM%8>O$45%16.+0(/8%.@
+! 7$ r(   raw_responsemodel_responselogging_objrequest_dataencoding	json_modec                    	 |j                         }| j                  ||      }d|v r#t        t        |d         |j                        t        |d         dkD  r!|d   |j                  d   j                  _
        |d   |j                  d   _        t        |	j                  |            }t        |	j                  |d   d   d   j                  d	d
                  }t        t        j                               |_        ||_        t%        ||||z         }t'        |d|       |S # t        $ r" t        |j                  |j                        w xY w)N)r   r   rL   error
completionr   stop_reasonchoicesr   content )prompt_tokenscompletion_tokenstotal_tokensusage)json	Exceptionr   textr   rN   rd   lenru   r   rv   finish_reasonencodegetinttimecreatedr?   r   r:   )r%   r?   rk   rl   rm   rn   r@   rA   rB   ro   rC   rp   completion_responserM   rx   ry   r{   s                    r'   transform_responsez&AnthropicTextConfig.transform_response   sz   	"."3"3"5
 >>U ? 
 ))$/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                     t        ||      S )N)r   r   )r   )r%   r   r   r>   s       r'   get_error_classz#AnthropicTextConfig.get_error_class   s     "#!
 	
r(   c                     | dk(  xs | dk(  S )Nzclaude-2zclaude-instant-1r]   )r?   s    r'   _is_anthropic_text_modelz,AnthropicTextConfig._is_anthropic_text_model   s    
"Ae/A&AAr(   c                     t         j                  }||v r(||   }t        |d   |d   |d   |      }t	        |      S t        ||d      }t	        |      S )Nrolesinitial_prompt_valuefinal_prompt_value)	role_dictr   r   r@   	anthropic)r?   r@   custom_llm_provider)rO   custom_prompt_dictr
   r   rd   )r%   r@   r?   r   model_prompt_detailsrM   s         r'   rN   z<AnthropicTextConfig._get_anthropic_text_prompt_from_messages   sv     %77&&#5e#< ".w7%9:P%Q#78L#M!	F 6{	 $hKF 6{r(   streaming_responsesync_streamc                     t        |||      S )N)r   r   rp   )'AnthropicTextCompletionResponseIterator)r%   r   r   rp   s       r'   get_model_response_iteratorz/AnthropicTextConfig.get_model_response_iterator  s     71#
 	
r(   )NN)F)+r)   r*   r+   __doc__rO   rX   r/   r   r   __annotations__r0   re   r1   r2   r3   r4   dictr	   r$   rd   r   r   rJ   rU   r^   boolrj   r    r"   r   r   r   r   r   Headersr   r   staticmethodr   rN   r   r   r   r]   r(   r'   r.   r.   3   s(    	 (  &*NHTN)!%K#%E8C=E8C=#Hhtn# )-%)###'4&
4
 !4 c]4 }4 }4 4.4 
40 "&"&  '(	
   # 3- 
. '( 	
   
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(   r.   c                       e Zd ZdedefdZy)r   chunkr5   c           	      <   	 d}d }d}d}d }d }t        |j                  dd            }|j                  dd       }	|	t        |	t              r|	}|j                  dd       }|d}t	        |||||||      }
|
S # t
        j                  $ r t        d	|       w xY w)
Nrw   Findexr   rs   rt   T)r~   tool_useis_finishedr   r{   r   provider_specific_fieldsz"Failed to decode JSON from chunk: )r   r   rc   rd   r   r|   JSONDecodeErrorrG   )r%   r   r~   r   r   r   r{   r   r   _chunk_textreturned_chunks              r'   chunk_parserz4AnthropicTextCompletionResponseIterator.chunk_parser  s    	KD>BHKM8<E'+$		'1-.E))L$7K&:k3+G"!IImT:M("2!'+)AN "!## 	KA%IJJ	Ks   A6A9 9"BN)r)   r*   r+   r   r   r   r]   r(   r'   r   r     s    K$ K+@ Kr(   r   )"r   r|   r   typingr   r   r   r   r   r   r    rO   litellm.constantsr	   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   r.   r   r]   r(   r'   <module>r      sp      G G   0 P 
 7 
) 
 c
* c
LK.G Kr(   