
    +#h&                        d dl Z d dlZd dlZd dlmZmZmZmZ d dlZd dl	Z	d dl
mZmZmZ d dlmZ 	  ee	j"                  j$                  d          d k(  Z	 d dlZ	d dlmZ e	j0                  j2                  e	j0                  j4                  e	j0                  j6                  fZd Z G d d	e      Zej@                  d
        Z! e jD                  erdnd       ej@                  d               Z#ej@                  defd       Z$ e jD                  erdnd       ej@                  defd              Z%d Z&d Z'd Z(d Z)y# e$ r dZY w xY w# e$ r e	j2                  e	j6                  fZeZY w xY w)    N)AnyLiteralOptionalcast)CacheMemoryNotebookCacheMemorycache_turn_on)LMT)OpenAIObjectc                 :    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    U/var/www/html/sandstorm/venv/lib/python3.12/site-packages/dsp/modules/azure_openai.pybackoff_hdlrr      s&    		 	6	%#	%    c                       e Zd ZdZ	 	 	 	 ddedededee   ded   dee   f fd	Zd
 Zd Z	defdZ
 ej                  ej                  ede      defd       Zdeeef   defdZ	 	 ddedededeeeef      fdZd Z xZS )AzureOpenAIa  Wrapper around Azure's API for OpenAI.

    Args:
        api_base (str): Azure URL endpoint for model calling, often called 'azure_endpoint'.
        api_version (str): Version identifier for API.
        model (str, optional): OpenAI or Azure supported LLM model to use. Defaults to "gpt-3.5-turbo-instruct".
        api_key (Optional[str], optional): API provider Authentication token. use Defaults to None.
        model_type (Literal["chat", "text"], optional): The type of model that was specified. Mainly to decide the optimal prompting strategy. Defaults to "chat".
        **kwargs: Additional arguments to pass to the API provider.
    api_baseapi_versionmodelapi_key
model_type)chattextsystem_promptc                    t         	|   |       d| _        || _        t        rCd|v sd|v sJ d       |t
        _        |t
        _        dt
        _        |t
        _	        d | _
        nt        j                  |||      }|| _
        || _        t        sd|vrd|v r|d   |d<   |d= d|v r|d= d|vr||d<   d	d
ddddd|| _        || _        || _	        || _        g | _        y )Nopenaienginedeployment_idzDMust specify engine or deployment_id for Azure API instead of model.azure)azure_endpointr   r   r   r   g              r   )temperature
max_tokenstop_pfrequency_penaltypresence_penaltyn)super__init__providerr   OPENAI_LEGACYr   r   r   api_typer   clientr   r   kwargshistory)
selfr   r   r   r   r   r   r2   r1   	__class__s
            r   r-   zAzureOpenAI.__init__4   s)    	 *  F"o&?VUV? 'FO$FN%FO!,FDK ''''F !DK$!6&("("9w?+&=)& #F7O !" !
 
 !&-/r   c                 2    t         rt        S | j                  S )N)r/   r   r1   )r4   s    r   _openai_clientzAzureOpenAI._openai_clientw   s    M{{r   c                 ~    |j                  d      }|r*|j                  d      }t        j                  d|        yy)z8Log the total tokens from the Azure OpenAI API response.usagetotal_tokensz Azure OpenAI Total Token Usage: N)getloggingdebug)r4   response
usage_datar:   s       r   	log_usagezAzureOpenAI.log_usage}   s:    \\'*
%>>.9LMM<\NKL r   promptc                    |}i | j                   |}| j                  dk(  rdd|dg}| j                  r|j                  dd| j                  d       ||d<   dt	        j
                  |      i}t        | j                  fi |}n||d<   t        | j                  fi |}||||d	}| j                  j                  |       |S )
Nr   user)rolecontentr   systemmessagesstringify_requestrA   )rA   r>   r2   
raw_kwargs)r2   r   r   insertjsondumpschat_requestr1   completions_requestr3   append)r4   rA   r2   rI   rG   r>   r3   s          r   basic_requestzAzureOpenAI.basic_request   s    
*DKK*6*??f$!'F;<H!!HASAS#TU!)F:)4::f+=>F#DKK:6:H  &F8*4;;A&AH  $	
 	G$r   i  )max_time
on_backoffc                 6    d|v r|d=  | j                   |fi |S )zQHandles retrieval of GPT-3 completions whilst handling rate limiting and caching.r   )rP   )r4   rA   r2   s      r   requestzAzureOpenAI.request   s,     6!|$!t!!&3F33r   choicereturnc                 :    | j                   dk(  r|d   d   S |d   S )Nr   messagerE   r   )r   )r4   rU   s     r   _get_choice_textzAzureOpenAI._get_choice_text   s(    ??f$)$Y//f~r   only_completedreturn_sortedc                    |sJ d       |du sJ d        | j                   |fi |}| j                  |       |d   }|D cg c]  }|d   dk7  s| }}|rt        |      r|}|D cg c]  }| j                  |       }	}|r|j	                  dd      dkD  rg }
|D ]m  }|d   d	   |d   d
   }}d|v r|j                  d      dz   }|d| |d| }}t        |      t        |      z  }|
j                  || j                  |      f       o t        |
d      }
|
D cg c]  \  }}|	 }	}}|	S c c}w c c}w c c}}w )a  Retrieves completions from OpenAI Model.

        Args:
            prompt (str): prompt to send to GPT-3
            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[dict[str, Any]]: list of completion choices
        zfor nowFchoicesfinish_reasonlengthr+   r%   logprobstokenstoken_logprobsz<|endoftext|>NT)reverse)	rT   r@   lenrY   r;   indexsumrO   sorted)r4   rA   rZ   r[   r2   r>   r]   ccompleted_choicescompletionsscored_completionsra   r`   re   avglog_s                   r   __call__zAzureOpenAI.__call__   s   $ (y(~%0y0%4<<1&1x 9%(/R11_3E3QQRRc"34'G9@AAt,,Q/AAVZZQ/!3!# NjM(+jM"23 !
 #f,"LL9A=E'-fu~x7GHFXX6"))643H3H3K*LMN "((:D!I);<A1<K<3 S
 B$ =s   D1D1)D6 D;c                     i | j                   |}|j                  d      } | j                  d|| j                  | j                  | j
                  d|S )z>Returns a copy of the language model with the same parameters.r   )r   r   r   r   r   )r2   popr5   r   r   r   )r4   r2   r   s      r   copyzAzureOpenAI.copy   s_    *DKK*6*

7#t~~ 
LL((]]	

 
 	
r   )zgpt-3.5-turbo-instructNr   N)TF)__name__
__module____qualname____doc__strr   r   r-   r7   r@   rP   backoffon_exceptionexpoERRORSr   rT   dictr   rY   boollistrn   rq   __classcell__)r5   s   @r   r   r   (   s   	 .!%.4'+A0A0 A0 	A0
 #A0 N+A0  }A0FMC 8 W	4c 44tCH~ #   $#	44 4 	4 
d38n	4l
r   r   c                  @    t        j                  j                  di | S Nr   )r   
Completioncreater2   s    r   cached_gpt3_request_v2r      s    ##-f--r   maxsizec                      t        di | S r   )r   r   s    r   cached_gpt3_request_v2_wrappedr      s     "+F++r   rV   c                      d| v rt        j                  | d         } t        t        t	        j
                  j                  di |       S NrH   r   )rK   loadsr   r   r   ChatCompletionr   r   s    r   _cached_gpt3_turbo_request_v2r     s?    f$F#678f33::DVDEEr   c                      t        di | S r   )r   r   s    r   %_cached_gpt3_turbo_request_v2_wrappedr     s     )2622r   c                      t        j                  t        rd nd      t        j                   fd              } |di |j                         S )Nr   r   c                  D    t         j                  fd       } |di | S )Nc                      d| v rt        j                  | d         }  j                  j                  j                  di | S r   )rK   r   r   rj   r   r2   r1   s    r   v1_cached_gpt3_turbo_request_v2ziv1_chat_request.<locals>.v1_cached_gpt3_turbo_request_v2_wrapped.<locals>.v1_cached_gpt3_turbo_request_v2  s?    "f,F+>$?@16;;**11;F;;r   r   r   cache)r2   r   r1   s     r   'v1_cached_gpt3_turbo_request_v2_wrappedz@v1_chat_request.<locals>.v1_cached_gpt3_turbo_request_v2_wrapped  s-     
			< 
	<
 /888r   r   	functools	lru_cacher	   r   r   
model_dump)r1   r2   r   s   `  r   v1_chat_requestr     sK    A>9  ?9 3<V<GGIIr   c                      t        j                  t        rd nd      t        j                   fd              } |di |j                         S )Nr   r   c                  D    t         j                  fd       } |di | S )Nc                  <     j                   j                  di | S r   )rj   r   r   s    r   v1_cached_gpt3_request_v2zdv1_completions_request.<locals>.v1_cached_gpt3_request_v2_wrapped.<locals>.v1_cached_gpt3_request_v2!  s    ,6%%,,6v66r   r   r   )r2   r   r1   s     r   !v1_cached_gpt3_request_v2_wrappedzAv1_completions_request.<locals>.v1_cached_gpt3_request_v2_wrapped  s-     
			7 
	7 )2622r   r   r   )r1   r2   r   s   `  r   v1_completions_requestr     sK    A>3  ?3 -6v6AACCr   c                 <    t         rt        di |S t        | fi |S r   )r/   r   r   r1   r2   s     r   rM   rM   *  s#    4>v>>6,V,,r   c                 <    t         rt        di |S t        | fi |S r   )r/   r   r   r   s     r   rN   rN   1  s#    -777!&3F33r   )*r   rK   r<   typingr   r   r   r   rw   r   dsp.modules.cache_utilsr   r   r	   dsp.modules.lmr
   intversion__version__r/   	Exceptionopenai.erroropenai.openai_objectr   errorRateLimitErrorServiceUnavailableErrorAPIErrorrz   r{   r   r   r   r   r   r   r   r   r   r   rM   rN   r   r   r   <module>r      s      / /   S S 22156!;M1 	##,,FK
" K
\ . . ]T:,  ;, F| F F ]T:3| 3  ;3J
D-4G	  M  ##V__5FLs$   "D= AE
 =EE
E,+E,