o
    5ti2                     @   s  d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	 d dl
m
Z
 d dlmZ d dlmZ d dlmZ d dlmZmZ e eZd	ed
edededee dedefddZd	ed
edededee dedefddZedG dd deZeddG dd deZdS )    N)cached_property)AnyDictListTupleUnion)tqdm)LM)register_model)LocalCompletionsAPI)handle_stop_sequencesretry_on_specific_exceptionsmodelpromptmax_tokens_to_sampletemperaturestopkwargsreturnc           
   
      sz   zddl  W n ty } zt|dd}~ww dtdtddfdd}t jgd|d	 fd
d}	|	 S )a  Wrapper function around the Anthropic completion API client with exponential back-off
    in case of RateLimitError.

    params:
        client: anthropic.Anthropic
            Anthropic API client
        model: str
            Anthropic model e.g. 'claude-instant-v1', 'claude-2'
        prompt: str
            Prompt to feed to the model
        max_tokens_to_sample: int
            Maximum number of tokens to sample from the model
        temperature: float
            Sampling temperature
        stop: List[str]
            List of stop sequences
        kwargs: Any
            Additional model_args to pass to the API client
    r   Nattempted to use 'anthropic' LM type, but package `anthropic` is not installed. please install anthropic via `pip install 'lm-eval[anthropic]'` or `pip install -e '.[anthropic]'`e
sleep_timer   c                 S      t d| j d| d d S NzRateLimitError occurred: z
 Retrying in z secondseval_loggerwarning	__cause__r   r    r   Q/home/ubuntu/.local/lib/python3.10/site-packages/lm_eval/models/anthropic_llms.py_exception_callback6      z1anthropic_completion.<locals>._exception_callbackon_exceptionsmax_retrieson_exception_callbackc                     s>   j jd j d  j  jg d} | jS )N )r   r   stop_sequencesr   r   r   )completionscreateHUMAN_PROMPT	AI_PROMPT
completionresponse	anthropicclientr   r   r   r   r   r   r   r    r-   ;   s   

z(anthropic_completion.<locals>.completion)r1   ModuleNotFoundErrortype	Exceptionfloatr   RateLimitError)
r2   r   r   r   r   r   r   	exceptionr!   r-   r   r0   r    anthropic_completion   s    r9   
max_tokensc              
      s~   zddl }W n ty } zt|dd}~ww dtdtddfdd}	t|j|j|jgd|	d	 fd
d}
|
 S )a  Wrapper function around the Anthropic completion API client with exponential back-off
    in case of RateLimitError.

    params:
        client: anthropic.Anthropic
            Anthropic API client
        model: str
            Anthropic model e.g. 'claude-3-opus-20240229', 'claude-3-sonnet-20240229'
        prompt: str
            Prompt to feed to the model
        max_tokens: int
            Maximum number of tokens to sample from the model
        temperature: float
            Sampling temperature
        stop: List[str]
            List of stop sequences
        kwargs: Any
            Additional model_args to pass to the API client
    r   Nr   r   r   r   c                 S   r   r   r   r   r   r   r    r!   u   r"   z+anthropic_chat.<locals>._exception_callbackr#   c                     s2    j jdd dgd} | jd jS )Nuserrolecontent)r   r:   r   messagesr   r   )r?   r*   r>   textr.   r2   r   r:   r   r   r   r   r    r?   z   s   
z anthropic_chat.<locals>.messages)	r1   r3   r4   r5   r6   r   r7   APIConnectionErrorAPIStatusError)r2   r   r   r:   r   r   r   r1   r8   r!   r?   r   rA   r    anthropic_chatP   s&   	
rD   zanthropic-completionsc                       s  e Zd ZdZ				d,dededed	ed
df
 fddZedd Z	ed
efddZ
ed
efddZedd Zedd Zded
ee fddZdee d
efddZd-defd d!Zd-ded
ee fd"d#Zd$d% Zd&d' Zd-defd(d)Zd-defd*d+Z  ZS ).AnthropicLM      
claude-2.0   r   
batch_sizer   r   r   r   Nc              
      sn   t    zddl}W n ty } zt|dd}~ww || _| | _|| _|| _	| j
 | _|| _dS )a|  Anthropic API wrapper.

        :param model: str
            Anthropic model e.g. 'claude-instant-v1', 'claude-2'
        :param max_tokens_to_sample: int
            Maximum number of tokens to sample from the model
        :param temperature: float
            Sampling temperature
        :param kwargs: Any
            Additional model_args to pass to the API client
        r   Nr   )super__init__r1   r3   r4   r   	Anthropicr2   r   r   get_tokenizer	tokenizerr   )selfrJ   r   r   r   r   r1   r8   	__class__r   r    rL      s   


zAnthropicLM.__init__c                 C      t d)Nz%No idea about anthropic tokenization.NotImplementedErrorrP   r   r   r    eot_token_id      zAnthropicLM.eot_token_idc                 C   s   dS )Ni   r   rV   r   r   r    
max_length   s   zAnthropicLM.max_lengthc                 C   s   | j S N)r   rV   r   r   r    max_gen_toks      zAnthropicLM.max_gen_toksc                 C   rS   NzNo support for logits.rT   rV   r   r   r    rJ      rX   zAnthropicLM.batch_sizec                 C   rS   r]   rT   rV   r   r   r    device   rX   zAnthropicLM.devicestringc                 C   s   | j |jS rZ   )rO   encodeids)rP   r_   r   r   r    
tok_encode   s   zAnthropicLM.tok_encodetokensc                 C   s   | j |S rZ   )rO   decode)rP   rc   r   r   r    
tok_decode   s   zAnthropicLM.tok_decodeFdisable_tqdmc                 C   rS   r]   rT   rP   requestsrf   r   r   r    _loglikelihood_tokens      z!AnthropicLM._loglikelihood_tokensc                 C   sV  zdd l }W n ty } zt|dd }~ww |sg S dd |D }g }t||dD ]|}z;|d }|d }	|	d}
|	d| j}|	d	| j}td| j| j	||||
d
| j
}|| | jd|| W q, |jy } ztd|j  W Y d }~ |S d }~w |jy } ztd|j d|j  W Y d }~ |S d }~ww |S )Nr   r   c                 S   s   g | ]}|j qS r   )args).0reqr   r   r    
<listcomp>   s    z.AnthropicLM.generate_until.<locals>.<listcomp>)disablerG   untilr[   r   )r2   r   r   r   r   r   generate_untilzServer unreachable: z
API error z: r   )r1   r3   r4   r   getrY   r   r9   r2   r   r   append
cache_hookadd_partialrB   r   criticalr   rC   status_codemessage)rP   rh   rf   r1   r8   	_requestsresrequestinprequest_argsrp   r[   r   r/   r   r   r   r    rq      sV   

	zAnthropicLM.generate_untilc                 C      t  rZ   rT   )rP   inpsr   r   r    _model_call  r\   zAnthropicLM._model_callc                 C   r~   rZ   rT   )rP   contextrY   eos_token_idr   r   r    _model_generate	  r\   zAnthropicLM._model_generatec                 C   rS   r]   rT   rg   r   r   r    loglikelihood  rj   zAnthropicLM.loglikelihoodc                 C   rS   r]   rT   rg   r   r   r    loglikelihood_rolling  rj   z!AnthropicLM.loglikelihood_rolling)rG   rH   rI   r   )F)__name__
__module____qualname__REQ_CHUNK_SIZEintstrr6   rL   propertyrW   rY   r[   rJ   r^   r   rb   re   boolri   rq   r   r   r   r   __classcell__r   r   rQ   r    rE      sF    %


,rE   zanthropic-chatzanthropic-chat-completionsc                       s   e Zd Z		d fdd	Zedd Zedd Z				
ddee de	de	fddZ
deeee f dee fddZ		ddedee fddZdd Z  ZS )AnthropicChat%https://api.anthropic.com/v1/messagesNc                    sF   t  jd||d| td d| _d| _td| j d d S )N)base_urltokenizer_backendzGChat completions does not support batching. Defaulting to batch size 1.rG   z
2023-06-01zUsing Anthropic Version: zP. Confirm the current version here: https://docs.anthropic.com/en/api/versioningr   )rK   rL   r   r   _batch_sizeanthropic_version)rP   r   r   r   rQ   r   r    rL     s   zAnthropicChat.__init__c                 C   s"   t jdd}|du rtd|S )zAOverride this property to return the API key for the API request.ANTHROPIC_API_KEYNzIAPI key not found. Please set the ANTHROPIC_API_KEY environment variable.)osenvironrr   
ValueError)rP   keyr   r   r    api_key(  s   zAnthropicChat.api_keyc                 C   s   | j  | jdS )N)z	x-api-keyzanthropic-version)r   r   rV   r   r   r    header2  s   zAnthropicChat.headerT

Human:r?   
gen_kwargsr   c                 K   s   |d  ddkr|d  dnd }|r|dd  }g }|D ]}|d d|d |d |d igd}	||	 q|dd	 |d
| j}
|dd}t|ddg|d}t|ts^|g}dd |D }|| j|
||d|}|rv||d< |S )Nr   r=   systemr>   rG   r4   r<   	do_sampleFr[   r   rp   r   )eosc                 S   s   g | ]
}|r|  r|qS r   )strip)rl   sr   r   r    rn   Y  s    z1AnthropicChat._create_payload.<locals>.<listcomp>)r?   r   r:   r   r(   )rr   rs   pop_max_gen_toksr   
isinstancelistr   )rP   r?   generater   r   r   r   cleaned_messagesmsgcleaned_msgr:   r   r   outr   r   r    _create_payload9  s:   "	
zAnthropicChat._create_payloadoutputsc                 K   s>   g }t |ts
|g}|D ]}|d D ]	}||d  qq|S )Nr>   r@   )r   r   rs   )rP   r   r   rz   r   choicesr   r   r    parse_generationsg  s   
zAnthropicChat.parse_generationsr_   c                 K   s   |gS rZ   r   )rP   r_   left_truncate_lenadd_special_tokensr   r   r   r    rb   r  s   zAnthropicChat.tok_encodec                 K   rS   )NzKAnthropic Chat Completions API does not support the return of loglikelihoodrT   )rP   rh   r   r   r   r    r   {  s   zAnthropicChat.loglikelihood)r   N)TNr   )NN)r   r   r   rL   r   r   r   r   r   dictr   r   r   r   rb   r   r   r   r   rQ   r    r     s>    
	
	
.

	r   )loggingr   	functoolsr   typingr   r   r   r   r   r   lm_eval.api.modelr	   lm_eval.api.registryr
   !lm_eval.models.openai_completionsr   lm_eval.models.utilsr   r   	getLoggerr   r   r   r   r6   r9   rD   rE   r   r   r   r   r    <module>   sZ    

?
@ 