
    gw)                         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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)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      e/var/www/openai/venv/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<   	 	 	 	 	 	 d'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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j6                  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jB                  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                         D ]%  \  }}	|dk7  s|	t        | j                  ||	       ' y )Nr$   )localsitemssetattrr%   )
r$   r.   r/   r0   r1   r2   r3   locals_keyvalues
             r&   r#   zAnthropicTextConfig.__init__B   s;     (!--/JCf}!2U3 *r'   headersmodelmessagesoptional_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>   r?   r@   rA   _headerss           r&   validate_environmentz(AnthropicTextConfig.validate_environmentQ   sD     ? T  )!-. 	
 	x r'   litellm_paramsc                     | j                  ||      }t        j                  j                         }|j	                         D ]  \  }}	||vs|	||<    ||d|}
|
S )Nr>   r=   )r=   prompt)(_get_anthropic_text_prompt_from_messageslitellmr-   
get_configr7   )r$   r=   r>   r?   rH   r<   rK   configkvdatas              r&   transform_requestz%AnthropicTextConfig.transform_requestg   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stopr0   r1   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
        rV   r.   rW   rU   TrX   r/   r0   r1   rZ   user_idr3   )r7   
isinstancestrlistrM   AnthropicConfig_map_stop_sequences)r$   r]   r?   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   rJ   error
completionr   stop_reasonchoicesr   content )prompt_tokenscompletion_tokenstotal_tokensusage)json	Exceptionr   textr   rL   rb   lenrs   r   rt   finish_reasonencodegetinttimecreatedr=   r   r8   )r$   r=   ri   rj   rk   rl   r>   r?   rH   rm   r@   rn   completion_responserK   rv   rw   ry   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)rM   custom_prompt_dictr	   r
   rb   )r$   r>   r=   r   model_prompt_detailsrK   s         r&   rL   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   rn   )'AnthropicTextCompletionResponseIterator)r$   r   r   rn   s       r&   get_model_response_iteratorz/AnthropicTextConfig.get_model_response_iterator  s     71#
 	
r'   )   NNNNN)NN)F)*r(   r)   r*   __doc__rM   rV   r.   r   r   __annotations__r/   rc   r0   r1   r2   r3   dictr#   rb   r   r   rG   rS   r\   boolrh   r   r!   r   r   r   r   r   Headersr   r   staticmethodr   rL   r   r   r   r[   r'   r&   r-   r-   2   s    	 (3-  &*NHTN)!%K#%E8C=E8C=#Hhtn# /2)-%)###'4&sm4 !4 c]	4
 }4 }4 4.4 
4* "&"&  '(	
  # 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   chunkr4   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)
Nru   Findexr   rq   rr   T)r|   tool_useis_finishedr~   ry   r   provider_specific_fieldsz"Failed to decode JSON from chunk: )r   r   ra   rb   r   rz   JSONDecodeErrorrD   )r$   r   r|   r   r   r~   ry   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   rz   r   typingr   r   r   r   r   r   r   rM   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      sm      G G   P 
 7 
) 
 `
* `
FK.G Kr'   