
    Cj                         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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)
PromptSpec)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       p/home/rurouni/.local/share/pipx/venvs/strix-agent/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   r3   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;   r8   raise_for_statushttpxHTTPStatusError	Exceptionresponsetextjsonr9   dictlist
ValueErrortypeitemslitellmOPENAI_CHAT_COMPLETION_PARAMSr   r	   r   r   )r)   r,   rD   clientbase_urlrL   ejson_responsetemplate_messagetemplate_messagestemplate_modelr   kvs                 r#   _get_prompt_from_id_api.HumanLoopPromptManager._get_prompt_from_id_apiC   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)r1   r_   	set_cacherT   "HUMANLOOP_PROMPT_CACHE_TTL_SECONDS)r)   r,   rD   prompts       r#   _get_prompt_from_id*HumanLoopPromptManager._get_prompt_from_idk   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   r3   )rB   )r)   r   r3   compiled_prompts       r#   compile_prompt%HumanLoopPromptManager.compile_promptz   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{}/ )r:   r;   r*   )r)   rn   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   r1   r   r   r   r   rB   r_   rg   rO   rl   rq   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
\\   S\\   S\\   S\\\\   \4   4U 4S jjjrSrU =r$ )HumanloopLogger   r   messagesnon_default_paramsr   r3   dynamic_callback_paramsprompt_specprompt_labelprompt_versionignore_prompt_manager_model%ignore_prompt_manager_optional_paramsr-   c           
      <  > UR                  S5      =(       d    [        S5      nUc  [        S5      eUc  [        TU ]  UUUUUUUS9$ [
        R                  XLS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$ )
NrD   HUMANLOOP_API_KEYz/prompt_id is required for Humanloop integration)r   rw   rx   r   r3   ry   rz   )r,   rD   r   rj   r   )rn   r   )	r8   r   rQ   superget_chat_completion_promptprompt_managerrg   rl   rq   )r)   r   rw   rx   r   r3   ry   rz   r{   r|   r}   r~   rD   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   )NNNFF)r   r   r   r   r   r   r   rO   r   r   r   intboolr   r   r!   __classcell__)r   s   @r#   ru   ru      s     -1&*(,6;@E8C8C '(8C !	8C
 C=8C #4.8C "?8C j)8C sm8C !8C &.d^8C 08~8C 
	
8C 8Cr"   ru   )__doc__typingr   r   r   r   r   r   r	   rI   typing_extensionsr
   rT   litellm.cachingr   &litellm.llms.custom_httpx.http_handlerr   litellm.secret_managers.mainr   litellm.types.llms.openair   "litellm.types.prompts.init_promptsr   litellm.types.utilsr   custom_loggerr   r   r%   r   ru   r   r"   r#   <module>r      si    A @ @  '  % D 7 6 9 = '.Y .sJY sJl ()9Cl 9Cr"   