o
    ưi)                     @   s   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 deZG dd deZ G dd deZ!dS )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                       s   e Zd Z fddZ  ZS )AnthropicTextErrorc                    sN   || _ || _tjddd| _tj|| jd| _t j| j| j | j| jd d S )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__ g/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/anthropic/completion/transformation.pyr!   $   s   
zAnthropicTextError.__init__)__name__
__module____qualname__r!   __classcell__r%   r%   r#   r&   r   #   s    r   c                   @   s   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
dZ		d2dededee dededee dee d	efddZdedee dededed	efddZdefddZdedededed	ef
ddZ		d2dedej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#d$Zd%ed&ede e!ej"f d	e#fd'd(Z$e%ded	efd)d*Z&dee ded	efd+d,Z'	-d3d.e e(e e)e ef d/ed"ee fd0d1Z*dS )4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           
      C   s>   t   }| D ]\}}	|dkr|	d urt| j||	 q	d S )Nr"   )localscopyitemssetattrr$   )
r"   r,   r-   r.   r/   r0   r1   Zlocals_keyvaluer%   r%   r&   r!   C   s   
zAnthropicTextConfig.__init__headersmodelmessagesoptional_paramslitellm_paramsapi_keyapi_basec           	      C   s,   |d u rt dddd|d}|| |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"   r9   r:   r;   r<   r=   r>   r?   _headersr%   r%   r&   validate_environmentT   s   

z(AnthropicTextConfig.validate_environmentc                 C   sL   | j ||d}tj }| D ]\}}	||vr|	||< q||d|}
|
S )Nr;   r:   )r:   prompt)(_get_anthropic_text_prompt_from_messageslitellmr+   Z
get_configr5   )r"   r:   r;   r<   r=   r9   rF   configkvdatar%   r%   r&   transform_requestk   s   
z%AnthropicTextConfig.transform_requestc                 C   s   g dS )zY
        Anthropic /complete API Ref: https://docs.anthropic.com/en/api/complete
        )stream
max_tokensmax_completion_tokensstopr.   r/   extra_headersuserr%   )r"   r:   r%   r%   r&   get_supported_openai_params   s   z/AnthropicTextConfig.get_supported_openai_paramsnon_default_paramsdrop_paramsc                 C   s   |  D ]W\}}|dkr||d< |dkr||d< |dkr$|du r$||d< |dkrAt|ts2t|trAt |}|durA||d< |d	krI||d	< |d
krQ||d
< |dkr[d|i|d< q|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
        rO   r,   rP   rN   TrQ   Nr-   r.   r/   rS   Zuser_idr1   )r5   
isinstancestrlistrH   ZAnthropicConfigZ_map_stop_sequences)r"   rU   r<   r:   rV   paramr8   _valuer%   r%   r&   map_openai_params   s&   z%AnthropicTextConfig.map_openai_paramsraw_responsemodel_responselogging_objrequest_dataencoding	json_modec                 C   s   z|  }W n ty   t|j|jdw | j||d}d|v r,tt|d |jdt|d dkr=|d |jd j	_
|d |jd _t|	|}t|	|d d d d	d
}tt |_||_t|||| d}t|d| |S )N)r   r   rE   error
completionr   stop_reasonchoicesr   content )prompt_tokenscompletion_tokensZtotal_tokensusage)json	Exceptionr   textr   rG   rX   lenrf   r   rg   finish_reasonencodegetinttimecreatedr:   r   r6   )r"   r:   r]   r^   r_   r`   r;   r<   r=   ra   r>   rb   Zcompletion_responserF   ri   rj   rk   r%   r%   r&   transform_response   sF   
z&AnthropicTextConfig.transform_responseerror_messager   c                 C   s   t ||dS )N)r   r   )r   )r"   rw   r   r9   r%   r%   r&   get_error_class   s   z#AnthropicTextConfig.get_error_classc                 C   s   | dkp| dkS )Nzclaude-2zclaude-instant-1r%   )r:   r%   r%   r&   _is_anthropic_text_model   s   z,AnthropicTextConfig._is_anthropic_text_modelc                 C   sP   t j}||v r|| }t|d |d |d |d}t|S t||dd}t|S )NZrolesinitial_prompt_valuefinal_prompt_value)Z	role_dictrz   r{   r;   Z	anthropic)r:   r;   Zcustom_llm_provider)rH   custom_prompt_dictr	   r
   rX   )r"   r;   r:   r|   Zmodel_prompt_detailsrF   r%   r%   r&   rG      s   z<AnthropicTextConfig._get_anthropic_text_prompt_from_messagesFstreaming_responsesync_streamc                 C   s   t |||dS )N)r}   r~   rb   )'AnthropicTextCompletionResponseIterator)r"   r}   r~   rb   r%   r%   r&   get_model_response_iterator  s
   z/AnthropicTextConfig.get_model_response_iterator)NN)F)+r'   r(   r)   __doc__rH   rO   r,   r   rs   __annotations__r-   rY   r.   r/   r0   r1   dictr   r!   rX   r   r   rD   rM   rT   boolr\   r   r   r   r   rv   r   r   ZHeadersr   rx   staticmethodry   rG   r   r   r   r%   r%   r%   r&   r+   3   s  
 	

	


,	

6

r+   c                   @   s   e Zd ZdedefddZdS )r   chunkr2   c              	   C   s   z@d}d }d}d}d }d }t |dd}|dd }	|	d ur&t|	tr&|	}|dp,d}|d ur3d}t|||||||d}
|
W S  tjyO   td	| w )
Nrh   Findexr   rd   re   T)rn   tool_useis_finishedrp   rk   r   provider_specific_fieldsz"Failed to decode JSON from chunk: )rs   rr   rW   rX   r   rl   JSONDecodeErrorrA   )r"   r   rn   r   r   rp   rk   r   r   Z_chunk_textreturned_chunkr%   r%   r&   chunk_parser  s6   
z4AnthropicTextCompletionResponseIterator.chunk_parserN)r'   r(   r)   r   r   r   r%   r%   r%   r&   r     s    r   )"r   rl   rt   typingr   r   r   r   r   r   r   rH   Zlitellm.constantsr   Z3litellm.litellm_core_utils.prompt_templates.factoryr	   r
   Z)litellm.llms.base_llm.base_model_iteratorr   Z)litellm.llms.base_llm.chat.transformationr   r   r   Zlitellm.types.llms.openair   Zlitellm.types.utilsr   r   r   r   r   r   r+   r   r%   r%   r%   r&   <module>   s      	 g