
    Mi                         S r SSKJrJrJrJrJrJrJr  SSK	r	SSK
Jr  SSKrSSKJr  SSKJr  SSKJr  SSKJr  SS	KJr  S
SKJr   " S S\5      r " S S\5      r\" 5       r " S S\5      rg)z/
Humanloop integration

https://humanloop.com/
    )AnyDictListOptionalTupleUnioncastN)	TypedDict)	DualCache)_get_httpx_client)get_secret_str)AllMessageValues)StandardCallbackDynamicParams   )CustomLoggerc                   Z    \ rS rSr% \\S'   \\   \S'   \\   \S'   \\	\\
4      \S'   Srg)PromptManagementClient   	prompt_idprompt_templatemodeloptional_params N)__name__
__module____qualname____firstlineno__str__annotations__r   r   r   r   r   __static_attributes__r       x/var/www/eduai.edurigo.com/storigo/production/storigo_env/lib/python3.13/site-packages/litellm/integrations/humanloop.pyr   r      s0    N*++C=d38n--r!   r   c                       \ rS rSr\S 5       rS\S\\   4S jr	S\
\   S\\\4   S\
\   4S jrS\S	\S\4S
 jrS\S	\S\4S jrS\
\   S\\   S\
\   4S jrS\S\S\4S jrSrg)HumanLoopPromptManager   c                     g)N	humanloopr   )selfs    r"   integration_name'HumanLoopPromptManager.integration_name   s    r!   humanloop_prompt_idreturnc                 J    [        [        [           U R                  US95      $ )N)key)r	   r   r   	get_cache)r(   r+   s     r"   _get_prompt_from_id_cache0HumanLoopPromptManager._get_prompt_from_id_cache"   s(     +,dnnATn.U
 	
r!   r   prompt_variablesc                    / nU Hy  nUR                  S5      nU(       aM  [        U[        5      (       a8  UR                  SS5      R                  SS5      nUR                  " S0 UD6nXtS'   UR                  U5        M{     U$ )a+  
Helper function to compile the prompt by substituting variables in the template.

Args:
    prompt_template: List[AllMessageValues]
    prompt_variables (dict): A dictionary of variables to substitute into the prompt template.

Returns:
    list: A list of dictionaries with variables substituted.
contentz{{{z}}}r   )get
isinstancer   replaceformatappend)r(   r   r2   compiled_promptstemplatetcformatted_templatecompiled_contents           r"   _compile_prompt_helper-HumanLoopPromptManager._compile_prompt_helper)   s     46'Hi(BjS))%'ZZc%:%B%B4%M"#5#<#<#P?O#P &6###H- (  r!   humanloop_api_keyc                 l   [        5       nSR                  U5      nUR                  UUSS.S9n UR                  5         UR                  5       nUS   n[        U[        5      (       a  U/n	O/[        U[        5      (       a  Un	O[        S[        U5       35      eUS   n
0 nUR                  5        H  u  pU[         R"                  ;   d  M  XU'   M!     [%        U['        [(        [*           U	5      U
US	9$ ! [        R
                   a'  n[        SUR                  R                   35      eS nAff = f)
Nz'https://api.humanloop.com/v5/prompts/{}zapplication/json)z	X-Api-KeyzContent-Type)urlheadersz%Error getting prompt from Humanloop: r=   zInvalid template message type: r   )r   r   r   r   )r   r:   r7   raise_for_statushttpxHTTPStatusError	Exceptionresponsetextjsonr8   dictlist
ValueErrortypeitemslitellmOPENAI_CHAT_COMPLETION_PARAMSr   r	   r   r   )r(   r+   rC   clientbase_urlrK   ejson_responsetemplate_messagetemplate_messagestemplate_modelr   kvs                 r"   _get_prompt_from_id_api.HumanLoopPromptManager._get_prompt_from_id_apiB   sF    #$<CCDWX::. 2  
	W%%' !(4&--!1 2($// 0>tDT?U>VWXX&w/!'')DAG999%&" * &) &6!79JK +	
 	
! $$ 	WCAJJOOCTUVV	Ws   C8 8D3"D..D3c                     U R                  U5      nUc0  U R                  X5      nU R                  UU[        R                  S9  U$ )N)r.   valuettl)r0   r^   	set_cacherS   "HUMANLOOP_PROMPT_CACHE_TTL_SECONDS)r(   r+   rC   prompts       r"   _get_prompt_from_id*HumanLoopPromptManager._get_prompt_from_idj   sV     //0CD>11#F NN'>>  
 r!   c                 4    S nUc  0 nU R                  UUS9nU$ )Nr   r2   )rA   )r(   r   r2   compiled_prompts       r"   compile_prompt%HumanLoopPromptManager.compile_prompty   s8    
 7;#!55+- 6 

 r!   prompt_management_clientr   c                 n    US   b  US   $ UR                  SR                  U R                  5      S5      $ )Nr   z{}/ )r9   r:   r)   )r(   rm   r   s      r"   _get_model_from_prompt-HumanLoopPromptManager._get_model_from_prompt   s:     $G,8+G44==d.C.C!DbIIr!   r   N)r   r   r   r   propertyr)   r   r   r   r0   r   r   r   r   rA   r^   rf   rN   rk   rp   r    r   r!   r"   r$   r$      s     
#&
	(	)
 #$45 IMcSVh 		 2&
#&&
;>&
	&
P#&;>	./ #4. 
		"J(>JGJJ	Jr!   r$   c                      ^  \ rS rSr  SS\S\\   S\S\\   S\\   S\	S\\   S	\\
   S
\\\\   \4   4U 4S jjjrSrU =r$ )HumanloopLogger   r   messagesnon_default_paramsr   r2   dynamic_callback_paramsprompt_labelprompt_versionr,   c	           	      :  > UR                  S5      =(       d    [        S5      n	Uc  [        S5      eU	c  [        TU ]  UUUUUUS9$ [
        R                  XIS9n
[
        R                  U
S   US9nU
S   =(       d    0 n0 UEUEn[
        R                  XS	9nXU4$ )
NrC   HUMANLOOP_API_KEYz/prompt_id is required for Humanloop integration)r   rv   rw   r   r2   rx   )r+   rC   r   ri   r   )rm   r   )	r7   r   rP   superget_chat_completion_promptprompt_managerrf   rk   rp   )r(   r   rv   rw   r   r2   rx   ry   rz   rC   r   updated_messagesprompt_template_optional_paramsupdated_non_default_params	__class__s                 r"   r~   *HumanloopLogger.get_chat_completion_prompt   s     477
 1/0 	 NOO$75!#5#!1(? 6   )<< ) = 
 *88+,=>- 9 

 +::K*L*RPR'&
 &
-&
"
 55%4 6 
 (BBBr!   r   )NN)r   r   r   r   r   r   r   rN   r   r   intr   r~   r    __classcell__)r   s   @r"   rt   rt      s     '+(,4C4C '(4C !	4C
 C=4C #4.4C "?4C sm4C !4C 
	
4C 4Cr!   rt   )__doc__typingr   r   r   r   r   r   r	   rH   typing_extensionsr
   rS   litellm.cachingr   &litellm.llms.custom_httpx.http_handlerr   litellm.secret_managers.mainr   litellm.types.llms.openair   litellm.types.utilsr   custom_loggerr   r   r$   r   rt   r   r!   r"   <module>r      sf    A @ @  '  % D 7 6 = '.Y .sJY sJl ()5Cl 5Cr!   