o
    ưiS                     @   s   d dl Z d dlZd dlmZ d dlmZmZmZ d dlm	Z	 d dl
mZ d dlmZmZmZ i Zdd Ze ZG d	d
 d
Ze ZdS )    N)Path)CallableDictOptional)verbose_proxy_logger)CustomPromptManagement)
PromptInfoPromptLiteLLMParams
PromptSpecc            
      C   s  i } zt tjjj}tj|d}tj|std | W S t	|D ]}tj||}tj
|r8|dr9q$tj|d}tj|sGq$d| }z2td|  t|}t|dr}t|d}t|tr}| | td| d	t|   W q$ ty }	 ztd
| d	|	  W Y d}	~	q$d}	~	w ty }	 ztd| d	|	  W Y d}	~	q$d}	~	ww tdt|  dt|    W | S  ty }	 ztd|	  W Y d}	~	| S d}	~	ww )ax  
    Get prompt initializers by discovering them from the prompt_integrations directory structure.

    Scans the integrations directory for subdirectories containing __init__.py files
    with either prompt_initializer_registry or initialize_prompt functions.

    Returns:
        Dict[str, Callable]: A dictionary mapping guardrail types to their initializer functions
    Zintegrationsz integrations directory not found__z__init__.pyzlitellm.integrations.z$Discovering prompt integrations in: prompt_initializer_registryz%Found prompt_initializer_registry in z: zCould not import NzError processing zDiscovered z prompt initializers: z'Error discovering prompt initializers: )r   __file__parentospathjoinexistsr   debuglistdirisdir
startswith	importlibimport_modulehasattrgetattr
isinstancedictupdatelistkeysImportErrorerror	Exceptionlen)
Zdiscovered_initializersZcurrent_dirZintegrations_diritemZ	item_pathZ	init_filemodule_pathmoduleregistrye r)   \/home/app/Keep/.python/lib/python3.10/site-packages/litellm/proxy/prompts/prompt_registry.py(get_prompt_initializer_from_integrations   s\   







r+   c                   @   s|   e Zd ZdZdd Z	ddedee dee fdd	Zd
edee fddZ	d
edee
 fddZdedee fddZdS )InMemoryPromptRegistryzM
    Class that handles adding prompt callbacks to the CallbacksManager.
    c                 C   s   i | _ 	 i | _d S N)IN_MEMORY_PROMPTSprompt_id_to_custom_prompt)selfr)   r)   r*   __init__^   s
   zInMemoryPromptRegistry.__init__Npromptconfig_file_pathreturnc                 C   s   ddl }|j}|| jv rtd | j| S d}|j}td| t|tr.tdi |}n|}|j	}|du r;t
dt|}	|	r\|	||}t|tsUt
dt| |j| nt
d| t|||jpmtdd	|j|jd
}
|
| j|< || j|< |
S )z
        Initialize a guardrail from a dictionary and add it to the litellm callback manager

        Returns a Guardrail object if the guardrail is initialized successfully
        r   Nz-prompt_id already exists in IN_MEMORY_PROMPTSzlitellm_params= %szprompt_integration is requiredz(CustomPromptManagement is required, got zUnsupported prompt: config)Zprompt_type)	prompt_idlitellm_paramsprompt_info
created_at
updated_atr)   )litellmr6   r.   r   r   r7   r   r   r	   prompt_integration
ValueErrorr   getr   typeZlogging_callback_managerZadd_litellm_callbackr
   r8   r   r9   r:   r/   )r0   r2   r3   r;   r6   Zcustom_prompt_callbackZlitellm_params_datar7   r<   ZinitializerZparsed_promptr)   r)   r*   initialize_promptk   sB   








	
z(InMemoryPromptRegistry.initialize_promptr6   c                 C      | j |S )z4
        Get a prompt by its ID from memory
        )r.   r>   r0   r6   r)   r)   r*   get_prompt_by_id   s   z'InMemoryPromptRegistry.get_prompt_by_idc                 C   rA   )z=
        Get a prompt callback by its ID from memory
        )r/   r>   rB   r)   r)   r*   get_prompt_callback_by_id   s   z0InMemoryPromptRegistry.get_prompt_callback_by_idbase_prompt_idc                    sN   ddl m  fdd| j D }|D ]}| j|= || jv r$| j|= q|S )z
        Delete all prompts matching the given base prompt ID from memory.

        Args:
            base_prompt_id: The base prompt ID (without version suffix)

        Returns:
            List of prompt IDs that were deleted
        r   )get_base_prompt_idc                    s   g | ]}|d  kr|qS ))r6   r)   ).0pidrE   rF   r)   r*   
<listcomp>   s
    zDInMemoryPromptRegistry.delete_prompts_by_base_id.<locals>.<listcomp>)Z&litellm.proxy.prompts.prompt_endpointsrF   r.   r   r/   )r0   rE   Zprompts_to_deleterH   r)   rI   r*   delete_prompts_by_base_id   s   

z0InMemoryPromptRegistry.delete_prompts_by_base_idr-   )__name__
__module____qualname____doc__r1   r
   r   strr@   rC   r   rD   r   rK   r)   r)   r)   r*   r,   Y   s$    
8
r,   )r   r   pathlibr   typingr   r   r   Zlitellm._loggingr   Z-litellm.integrations.custom_prompt_managementr   Z"litellm.types.prompts.init_promptsr   r	   r
   r   r+   r,   ZIN_MEMORY_PROMPT_REGISTRYr)   r)   r)   r*   <module>   s    E
r