
    AHj                         d Z ddlmZmZmZmZmZmZmZ ddl	Z	ddl
m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 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UnioncastN)	TypedDict)	DualCache)_get_httpx_client)get_secret_str)AllMessageValues)
PromptSpec)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        V/root/tools/cai/cai_env/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: r8   zInvalid template message type: r   )r   r   r   r   )r   r5   r2   raise_for_statushttpxHTTPStatusError	Exceptionresponsetextjsonr3   dictlist
ValueErrortypeitemslitellmOPENAI_CHAT_COMPLETION_PARAMSr   r	   r   r   )r%   r'   r=   clientbase_urlrE   ejson_responsetemplate_messagetemplate_messagestemplate_modelr   kvs                 r    _get_prompt_from_id_apiz.HumanLoopPromptManager._get_prompt_from_id_apiC   sI    #$<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,   rX   	set_cacherM   "HUMANLOOP_PROMPT_CACHE_TTL_SECONDS)r%   r'   r=   prompts       r    _get_prompt_from_idz*HumanLoopPromptManager._get_prompt_from_idk   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_promptz   s8    
 7;#!55+- 6 

 r   prompt_management_clientr   c                 l    |d   |d   S |j                  dj                  | j                        d      S )Nr   z{}/ )r4   r5   r&   )r%   rd   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<   rX   r_   rH   rc   rg   r   r   r    r"   r"      s     
#&
	(	)
 #$45 IMcSVh 		 2&
#&&
;>&
	&
P#&;>	./ #4. 
		"J(>JGJJ	Jr   r"   c                        e Zd Z	 	 	 	 	 ddedee   dedee   dee   dedee	   dee   d	ee
   d
ee   dee   deeee   ef   f fdZ xZS )HumanloopLoggerr   messagesnon_default_paramsr   r-   dynamic_callback_paramsprompt_specprompt_labelprompt_versionignore_prompt_manager_model%ignore_prompt_manager_optional_paramsr(   c           	      B   |j                  d      xs t        d      }|t        d      |t        |   |||||||      S t
        j                  ||      }t
        j                  |d   |      }|d   xs i }i ||}t
        j                  ||	      }|||fS )
Nr=   HUMANLOOP_API_KEYz/prompt_id is required for Humanloop integration)r   rk   rl   r   r-   rm   rn   )r'   r=   r   ra   r   )rd   r   )	r2   r   rJ   superget_chat_completion_promptprompt_managerr_   rc   rg   )r%   r   rk   rl   r   r-   rm   rn   ro   rp   rq   rr   r=   r   updated_messagesprompt_template_optional_paramsupdated_non_default_params	__class__s                    r    rv   z*HumanloopLogger.get_chat_completion_prompt   s	    477
 1/0 	 NOO$75!#5#!1(?' 6   )<< )=N = 
 *88+,=>- 9 

 +::K*L*RPR'&
 &
-&
"
 55%4E 6 
 &(BBBr   )NNNFF)r   r   r   r   r   r   rH   r   r   r   intboolr   rv   __classcell__)r{   s   @r    rj   rj      s     -1&*(,6;@E4C4C '(4C !	4C
 C=4C #4.4C "?4C j)4C sm4C !4C &.d^4C 08~4C 
sD)*D1	24C 4Cr   rj   )__doc__typingr   r   r   r   r   r   r	   rB   typing_extensionsr
   rM   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"   rw   rj   r   r   r    <module>r      si    A @ @  '  % D 7 6 9 = '.Y .sJY sJl ()5Cl 5Cr   