o
    ưi                     @   s   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 deZG dd deZe ZG dd deZdS )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                   @   sB   e Zd ZU eed< ee ed< ee ed< eeee	f  ed< dS )PromptManagementClient	prompt_idprompt_templatemodeloptional_paramsN)
__name__
__module____qualname__str__annotations__r   r   r   r   r    r   r   U/home/app/Keep/.python/lib/python3.10/site-packages/litellm/integrations/humanloop.pyr      s
   
 r   c                   @   s   e Zd Zedd Zdedee fddZde	e
 deeef de	e
 fd	d
ZdededefddZdededefddZde	e
 dee de	e
 fddZdededefddZdS )HumanLoopPromptManagerc                 C   s   dS )NZ	humanloopr   )selfr   r   r   integration_name   s   z'HumanLoopPromptManager.integration_namehumanloop_prompt_idreturnc                 C   s   t tt | j|dS )N)key)r   r   r   Z	get_cache)r   r!   r   r   r   _get_prompt_from_id_cache#   s   z0HumanLoopPromptManager._get_prompt_from_id_cacher   prompt_variablesc                 C   s`   g }|D ])}| d}|r(t|tr(|dddd}|jdi |}||d< || 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}}}Nr   )get
isinstancer   replaceformatappend)r   r   r%   Zcompiled_promptstemplateZtcZformatted_templateZcompiled_contentr   r   r   _compile_prompt_helper*   s   
z-HumanLoopPromptManager._compile_prompt_helperhumanloop_api_keyc              
   C   s   t  }d|}|j||ddd}z|  W n tjy. } z	td|jj d }~ww |	 }|d }t
|tr@|g}	nt
|trH|}	n	tdt| |d }
i }| D ]\}}|tjv rh|||< q[t|ttt |	|
|d	S )
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,   r)   raise_for_statushttpxZHTTPStatusError	Exceptionresponsetextjsonr*   dictlist
ValueErrortypeitemslitellmZOPENAI_CHAT_COMPLETION_PARAMSr   r   r   r   )r   r!   r0   clientbase_urlr6   eZjson_responseZtemplate_messageZtemplate_messagesZtemplate_modelr   kvr   r   r   _get_prompt_from_id_apiC   sB   



z.HumanLoopPromptManager._get_prompt_from_id_apic                 C   s4   |  |}|d u r| ||}| j||tjd |S )N)r#   valuettl)r$   rD   Z	set_cacher>   Z"HUMANLOOP_PROMPT_CACHE_TTL_SECONDS)r   r!   r0   promptr   r   r   _get_prompt_from_idk   s   
z*HumanLoopPromptManager._get_prompt_from_idc                 C   s"   d }|d u ri }| j ||d}|S )Nr   r%   )r/   )r   r   r%   Zcompiled_promptr   r   r   compile_promptz   s   z%HumanLoopPromptManager.compile_promptprompt_management_clientr   c                 C   s(   |d d ur
|d S | d| jdS )Nr   z{}/ )r+   r,   r    )r   rK   r   r   r   r   _get_model_from_prompt   s   z-HumanLoopPromptManager._get_model_from_promptN)r   r   r   propertyr    r   r   r   r$   r   r   r   r   r/   rD   rH   r9   rJ   rM   r   r   r   r   r      sV    




(

r   c                       s   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dZ  ZS )HumanloopLoggerNFr   messagesnon_default_paramsr   r%   dynamic_callback_paramsprompt_specprompt_labelprompt_versionignore_prompt_manager_model%ignore_prompt_manager_optional_paramsr"   c              	      s   | dptd}|d u rtd|d u r"t j|||||||dS tj||d}tj|d |d}|d p7i }i ||}tj||d	}|||fS )
Nr0   ZHUMANLOOP_API_KEYz/prompt_id is required for Humanloop integration)r   rP   rQ   r   r%   rR   rS   )r!   r0   r   rI   r   )rK   r   )	r)   r   r;   superget_chat_completion_promptprompt_managerrH   rJ   rM   )r   r   rP   rQ   r   r%   rR   rS   rT   rU   rV   rW   r0   r   Zupdated_messagesZprompt_template_optional_paramsZupdated_non_default_params	__class__r   r   rY      sD   

z*HumanloopLogger.get_chat_completion_prompt)NNNFF)r   r   r   r   r   r   r9   r   r   r   intboolr   rY   __classcell__r   r   r[   r   rO      sH    		
rO   )__doc__typingr   r   r   r   r   r   r   r4   Ztyping_extensionsr	   r>   Zlitellm.cachingr
   Z&litellm.llms.custom_httpx.http_handlerr   Zlitellm.secret_managers.mainr   Zlitellm.types.llms.openair   Z"litellm.types.prompts.init_promptsr   Zlitellm.types.utilsr   Zcustom_loggerr   r   r   rZ   rO   r   r   r   r   <module>   s     $v