
    g                         d Z ddlmZmZ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 ddlmZ ddlmZ ddlmZ d	d
lmZ  G d de      Z G d de      Z e       Z G d de      Zy)z/
Humanloop integration

https://humanloop.com/
    )AnyDictListOptionalTuple	TypedDictUnioncastN)	DualCache)_get_httpx_client)get_secret_str)AllMessageValues)StandardCallbackDynamicParams   )CustomLoggerc                   R    e Zd ZU eed<   ee   ed<   ee   ed<   eeee	f      ed<   y)PromptManagementClient	prompt_idprompt_templatemodeloptional_paramsN)
__name__
__module____qualname__str__annotations__r   r   r   r   r        S/var/www/openai/venv/lib/python3.12/site-packages/litellm/integrations/humanloop.pyr   r      s0    N*++C=d38n--r   r   c                       e Zd Zed        Zdedee   fdZde	e
   deeef   de	e
   fdZdededefd	Zdededefd
Zde	e
   dee   de	e
   fdZdededefdZy)HumanLoopPromptManagerc                      y)N	humanloopr   )selfs    r   integration_namez'HumanLoopPromptManager.integration_name   s    r   humanloop_prompt_idreturnc                 P    t        t        t           | j                  |            S )N)key)r
   r   r   	get_cache)r$   r&   s     r   _get_prompt_from_id_cachez0HumanLoopPromptManager._get_prompt_from_id_cache!   s(     +,dnnATn.U
 	
r   r   prompt_variablesc                     g }|D ]o  }|j                  d      }|rIt        |t              r9|j                  dd      j                  dd      } |j                  di |}||d<   |j                  |       q |S )ac  
        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   r,   compiled_promptstemplatetcformatted_templatecompiled_contents           r   _compile_prompt_helperz-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                 \   t               }dj                  |      }|j                  ||dd      }	 |j                          |j                         }|d   }t        |t              r|g}	n*t        |t              r|}	nt        dt        |             |d   }
i }|j                         D ]  \  }}|t         j"                  v s|||<    t%        |t'        t(        t*           |	      |
|	      S # t        j
                  $ r'}t        d|j                  j                         d }~ww xY w)
Nz'https://api.humanloop.com/v5/prompts/{}zapplication/json)z	X-Api-KeyzContent-Type)urlheadersz%Error getting prompt from Humanloop: r7   zInvalid template message type: r   )r   r   r   r   )r   r4   r1   raise_for_statushttpxHTTPStatusError	Exceptionresponsetextjsonr2   dictlist
ValueErrortypeitemslitellmOPENAI_CHAT_COMPLETION_PARAMSr   r
   r   r   )r$   r&   r<   clientbase_urlrD   ejson_responsetemplate_messagetemplate_messagestemplate_modelr   kvs                 r   _get_prompt_from_id_apiz.HumanLoopPromptManager._get_prompt_from_id_apiA   sD    #$<CCDWX::. 2  
	W%%' !(4&-!1 2($/ 0>tDT?U>VWXX&w/!'')DAqG999%&" * &) &6!79JK +	
 	
! $$ 	WCAJJOOCTUVV	Ws   C1 1D+"D&&D+c                     | j                  |      }|4| j                  ||      }| j                  ||t        j                         |S )N)r)   valuettl)r+   rW   	set_cacherL   "HUMANLOOP_PROMPT_CACHE_TTL_SECONDS)r$   r&   r<   prompts       r   _get_prompt_from_idz*HumanLoopPromptManager._get_prompt_from_idi   sY     //0CD>11#%6F NN'>>  
 r   c                 8    d }|i }| j                  ||      }|S )Nr   r,   )r;   )r$   r   r,   compiled_prompts       r   compile_promptz%HumanLoopPromptManager.compile_promptx   s8    
 7;#!55+- 6 

 r   prompt_management_clientr   c                 l    |d   |d   S |j                  dj                  | j                        d      S )Nr   z{}/ )r3   r4   r%   )r$   rc   r   s      r   _get_model_from_promptz-HumanLoopPromptManager._get_model_from_prompt   s:     $G,8+G44==d.C.C!DbIIr   N)r   r   r   propertyr%   r   r   r   r+   r   r   r   r   r;   rW   r^   rG   rb   rf   r   r   r   r!   r!      s     
#&
	(	)
 #$45 IMcSVh 		 2&
#&&
;>&
	&
P#&;>	./ #4. 
		"J(>JGJJ	Jr   r!   c                   Z     e Zd Zdedee   dededee   dede	eee   ef   f fdZ
 xZS )	HumanloopLoggerr   messagesnon_default_paramsr   r,   dynamic_callback_paramsr'   c                 &   |j                  d      xs t        d      }|t        |   ||||||      S t        j                  ||      }t        j                  |d   |      }	|d   xs i }
i ||
}t        j                  ||      }||	|fS )	Nr<   HUMANLOOP_API_KEY)r   rj   rk   r   r,   rl   )r&   r<   r   r`   r   )rc   r   )r1   r   superget_chat_completion_promptprompt_managerr^   rb   rf   )r$   r   rj   rk   r   r,   rl   r<   r   updated_messagesprompt_template_optional_paramsupdated_non_default_params	__class__s               r   rp   z*HumanloopLogger.get_chat_completion_prompt   s     477
 1/0 	 $75!#5#!1(? 6   )<< )=N = 
 *88+,=>- 9 

 +::K*L*RPR'&
 &
-&
"
 55%4E 6 
 &(BBBr   )r   r   r   r   r   r   rG   r   r   r   rp   __classcell__)ru   s   @r   ri   ri      s}    /C/C '(/C !	/C
 /C #4./C "?/C 
	
/C /Cr   ri   )__doc__typingr   r   r   r   r   r   r	   r
   rA   rL   litellm.cachingr   &litellm.llms.custom_httpx.http_handlerr   litellm.secret_managers.mainr   litellm.types.llms.openair   litellm.types.utilsr   custom_loggerr   r   r!   rq   ri   r   r   r   <module>r      sc    L K K   % D 7 6 = '.Y .sJY sJl ()0Cl 0Cr   