
    +#hW                         d dl Z d dlZd dlmZmZ d dlZd dlmZ 	 d dlZej                  Z
 e j                  e      ZdZd Zd Z G d de      Zy# e$ r eZ
Y /w xY w)	    N)AnyOptional)LMz%https://api.anthropic.com/v1/messagesc                 :    t         dj                  di |        y)z/Handler from https://pypi.org/project/backoff/.zbBacking off {wait:0.1f} seconds after {tries} tries calling function {target} with kwargs {kwargs}N )printformatdetailss    R/var/www/html/sandstorm/venv/lib/python3.12/site-packages/dsp/modules/anthropic.pybackoff_hdlrr      s&    		 	6	%#	%    c                 "    d| j                   v ryy)z7Wrapper function that decides when to give up on retry.zrate limitsFT)messager
   s    r   giveup_hdlrr      s    'r   c                        e Zd ZdZ	 	 	 ddedee   dee   f fdZd ZdefdZ e	j                  e	j                  ed	d
ee      defd       ZddZ xZS )ClaudezKWrapper around anthropic's API. Supports both the Anthropic and Azure APIs.modelapi_keyapi_basec                 &   t         |   |       	 ddlm} d| _        |t        j                  j                  d      n|x| _	        }|t        n|| _        |j                  dd      t        |j                  dd	      d	      |j                  d
d      |j                  dd      |j                  d|j                  dd            d|| _        || j                  d<   g | _         ||      | _        y # t        $ r}t	        d      |d }~ww xY w)Nr   )	Anthropicz(Claude requires `pip install anthropic`.	anthropicANTHROPIC_API_KEYtemperatureg        
max_tokensi   top_pg      ?top_k   nnum_generations)r   r   r   r   r    r   )r   )super__init__r   r   ImportErrorproviderosenvirongetr   BASE_URLr   minpopkwargshistoryclient)selfr   r   r   r,   r   err	__class__s          r   r#   zClaude.__init__'   s    		S+ $HO0C!D]ddw$,$4(!::mS9fjjt<dCZZ-ZZ+C,=q!AB
 
  %G-/0!  	SHIsR	Ss   C6 6	D?DDc                     |j                   }|r2|j                  |j                  z   }t        j	                  d|        yy)z5Log the total tokens from the Anthropic API response.z&Anthropic Total Token Response Usage: N)usageinput_tokensoutput_tokensloggerdebug)r/   response
usage_datatotal_tokenss       r   	log_usagezClaude.log_usageC   s=    ^^
%22Z5M5MMLLLA,PQ r   promptc                     |}i | j                   |}d|dg|d<   |j                  d        | j                  j                  j                  di |}||||d}| j
                  j                  |       |S )Nuser)rolecontentmessagesr    )r<   r8   r,   
raw_kwargsr   )r,   r+   r.   rA   creater-   append)r/   r<   r,   rB   r8   r-   s         r   basic_requestzClaude.basic_requestJ   s    
*DKK*6*'-&ABz

3.4;;''..88 $	
 	G$r   i     )max_time	max_tries
on_backoffgiveupc                 (     | j                   |fi |S )zKHandles retrieval of completions from Anthropic whilst handling API errors.)rE   )r/   r<   r,   s      r   requestzClaude.requestZ   s     "t!!&3F33r   c                 2   |sJ d       |du sJ d       |j                  dd      }g }t        |      D ][  } | j                  |fi |}| j                  |       |r|j                  dk(  r9|j
                  D 	cg c]  }	|	j                   }}	] |S c c}	w )a  Retrieves completions from Anthropic.

        Args:
            prompt (str): prompt to send to Anthropic
            only_completed (bool, optional): return only completed responses and ignores completion due to length. Defaults to True.
            return_sorted (bool, optional): sort the completion choices using the returned probabilities. Defaults to False.

        Returns:
            list[str]: list of completion choices
        zfor nowFr    r   r   )r+   rangerL   r;   stop_reasonr@   text)
r/   r<   only_completedreturn_sortedr,   r    completions_r8   cs
             r   __call__zClaude.__call__f   s     (y(~%0y0% JJsAq 	=A#t||F5f5HNN8$("6"6,"F+3+;+;<a166<K<	=  =s   ;B)zclaude-3-opus-20240229NN)TF)__name__
__module____qualname____doc__strr   r#   r;   rE   backoffon_exceptionexpoanthropic_rate_limitr   r   rL   rV   __classcell__)r1   s   @r   r   r   $   s    U .!%"&	11 #1 3-	18RC   W	4c 44r   r   )loggingr&   typingr   r   r\   dsp.modules.lmr   r   RateLimitErrorr_   r$   	Exception	getLoggerrW   r6   r)   r   r   r   r   r   r   <module>rg      sp     	    %$33 
		8	$2ZR Z/  %$%s   A AA