
    j*                         d Z ddlZddlmZmZmZmZmZ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mZ  G d
 de
      Zy)z
Dotprompt manager that integrates with LiteLLM's prompt management system.
Builds on top of PromptManagementBase to provide .prompt file support.
    N)AnyDictListOptionalTupleUnion)CustomPromptManagement)PromptManagementClient)AllMessageValues)StandardCallbackDynamicParams   )PromptManagerPromptTemplatec                      e Zd ZdZ	 	 	 	 d%dee   dee   deeeef      dee   fdZe	defd	       Z
e	defd
       ZdededefdZ	 	 d&dedee   dedee   dee   def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ee   ef   fdZdedee   fdZdededefdZdedefdZdeddfdZd'dZdedeeef   ddfdZd eeeeef   f   ddfd!Z deeeeef   f   fd"Z!d#edeeef   fd$Z"y)(DotpromptManageran  
    Dotprompt manager that integrates with LiteLLM's prompt management system.

    This class enables using .prompt files with the litellm completion() function
    by implementing the PromptManagementBase interface.

    Usage:
        # Set global prompt directory
        litellm.prompt_directory = "path/to/prompts"

        # Use with completion
        response = litellm.completion(
            model="dotprompt/gpt-4",
            prompt_id="my_prompt",
            prompt_variables={"variable": "value"},
            messages=[{"role": "user", "content": "This will be combined with the prompt"}]
        )
    Nprompt_directoryprompt_fileprompt_data	prompt_idc                     dd l }|xs |j                  | _        t        |t              rt        j                  |      | _        n|xs i | _        d | _        || _	        || _
        y )Nr   )litellmglobal_prompt_directoryr   
isinstancestrjsonloadsr   _prompt_managerr   r   )selfr   r   r   r   r   s         /Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/litellm/integrations/dotprompt/dotprompt_manager.py__init__zDotpromptManager.__init__%   sY     	 0 SG4S4Sk3'#zz+6D*0bD8<&"    returnc                      y)z<Integration name used in model names like 'dotprompt/gpt-4'.	dotprompt r   s    r   integration_namez!DotpromptManager.integration_name9   s     r!   c                    | j                   k| j                  #| j                  s| j                  st	        d      t        | j                  | j                  | j                  | j                        | _         | j                   S )zLazy-load the prompt manager.zEither prompt_directory or prompt_data must be set before using dotprompt manager. Set litellm.global_prompt_directory, initialize with prompt_directory parameter, or provide prompt_data.)r   r   r   r   )r   r   r   r   
ValueErrorr   r   r&   s    r   prompt_managerzDotpromptManager.prompt_manager>   s     '%%-((((   $1!%!6!6 ,, ,,..	$D  ###r!   dynamic_callback_paramsc                 Z    	 || j                   j                         v S # t        $ r Y yw xY w)z
        Determine if prompt management should run based on the prompt_id.

        Returns True if the prompt_id exists in our prompt manager.
        F)r*   list_prompts	Exception)r   r   r+   s      r   should_run_prompt_managementz-DotpromptManager.should_run_prompt_managementS   s3    	 3 3 @ @ BBB 		s    	**prompt_variablesprompt_labelprompt_versionc                 T   	 | j                   j                  |      }|t        d| d      | j                   j                  ||      }| j	                  |      }|j
                  }	| j                  |      }
t        |||	|
d      S # t        $ r}t        d| d|       d}~ww xY w)aM  
        Compile a .prompt file into a PromptManagementClient structure.

        This method:
        1. Loads the prompt template from the .prompt file
        2. Renders it with the provided variables
        3. Converts the rendered text into chat messages
        4. Extracts model and optional parameters from metadata
        NzPrompt 'z' not found in prompt directory)r   prompt_templateprompt_template_modelprompt_template_optional_paramscompleted_messageszError compiling prompt 'z': )	r*   
get_promptr)   render_convert_to_messagesmodel_extract_optional_paramsr
   r.   )r   r   r0   r+   r1   r2   templaterendered_contentmessagestemplate_modeloptional_paramses               r   _compile_prompt_helperz'DotpromptManager._compile_prompt_helperc   s    $	K **55i@H 8I;6U!VWW  $2299)EUV 001ABH &^^N #;;HEO)# (&40?#'   	K7	{#aSIJJ	Ks   BB 	B'B""B'r;   r?   non_default_paramsc	                 @    ddl m}	 |	j                  | ||||||||	      S )Nr   )PromptManagementBase)+litellm.integrations.prompt_management_baserF   get_chat_completion_prompt)
r   r;   r?   rD   r   r0   r+   r1   r2   rF   s
             r   rH   z+DotpromptManager.get_chat_completion_prompt   s6     	U#>>#

 
	
r!   r>   c           	         |j                         }g }d}g }|j                  d      }|D ]l  }|j                         }|j                  d      rY|r@|r>|j                  | j	                  |dj                  |      j                                      d}|dd j                         g}~|j                  d      rY|r@|r>|j                  | j	                  |dj                  |      j                                      d}|dd j                         g}|j                  d	      rZ|r@|r>|j                  | j	                  |dj                  |      j                                      d
}|dd j                         g}S|r|j                  |       hd}|g}o |rD|rBdj                  |      j                         }|r!|j                  | j	                  ||             |s#|r!|j                  | j	                  d|             |S )a9  
        Convert rendered prompt content to chat messages.

        This method supports multiple formats:
        1. Simple text -> converted to user message
        2. Text with role prefixes (System:, User:, Assistant:) -> parsed into separate messages
        3. Already formatted as a single message
        N
zSystem:system   zUser:user   z
Assistant:	assistant
   )stripsplit
startswithappend_create_messagejoin)	r   r>   contentr?   current_rolecurrent_contentlineslinecontent_texts	            r   r:   z%DotpromptManager._convert_to_messages   s    #((* d#D::<D y)OOO,,($))O*D*J*J*L
  (#'8>>#3"4)OOO,,($))O*D*J*J*L
  &#'8>>#3"4.OOO,,($))O*D*J*J*L
  +#'9??#4"5  #**40 $*L'+fOM R O99_5;;=L 4 4\< PQ GOOD00ABr!   rolerW   c                     ||dS )z5Create a message with the specified role and content.)r]   rW   r%   )r   r]   rW   s      r   rU   z DotpromptManager._create_message   s     
 	
r!   r=   c                 X    i }|j                   |j                  |j                          |S )z
        Extract optional parameters from the prompt template metadata.

        Includes parameters like temperature, max_tokens, etc.
        )rA   update)r   r=   rA   s      r   r<   z)DotpromptManager._extract_optional_params   s1      ##/""8#;#;<r!   c                      || _         d| _        y)z,Set the prompt directory and reload prompts.N)r   r   )r   r   s     r   set_prompt_directoryz%DotpromptManager.set_prompt_directory	  s     0#r!   c                 R    | j                   r| j                   j                          yy)z&Reload all prompts from the directory.N)r   reload_promptsr&   s    r   rd   zDotpromptManager.reload_prompts  s"      //1  r!   	json_datac                     |j                  dd      }|j                  di       }| j                  j                  |||       y)zAdd a prompt from JSON data.rW    metadataN)getr*   
add_prompt)r   r   re   rW   rh   s        r   add_prompt_from_jsonz%DotpromptManager.add_prompt_from_json  s:    --	2.==R0&&y'8Dr!   prompts_datac                 :    | j                   j                  |       y)z%Load multiple prompts from JSON data.N)r*   load_prompts_from_json_data)r   rl   s     r   load_prompts_from_jsonz'DotpromptManager.load_prompts_from_json  s    77Er!   c                 6    | j                   j                         S )zGet all prompts in JSON format.)r*   get_all_prompts_as_jsonr&   s    r   get_prompts_as_jsonz$DotpromptManager.get_prompts_as_json  s    ""::<<r!   	file_pathc                 8    | j                   j                  |      S )z&Convert a .prompt file to JSON format.)r*   prompt_file_to_json)r   rs   s     r   convert_prompt_file_to_jsonz,DotpromptManager.convert_prompt_file_to_json!  s    ""66yAAr!   )NNNN)NN)r"   N)#__name__
__module____qualname____doc__r   r   r   dictr    propertyr'   r   r*   r   boolr/   intr
   rC   r   r   r   rH   r:   rU   r   r<   rb   rd   r   r   rk   ro   rr   rv   r%   r!   r   r   r      s   * +/%)26#'#"3-# c]# eD#I./	#
 C=#( #   $ $ $( "? 
	* '+(,.K.K #4..K "?	.K
 sm.K !.K 
 .Kp '+(,

 '(
 !	

 C=
 #4.
 "?
 sm
 !
 
sD)*D0	1
4ES ETBR=S EN
C 
# 
:J 
 D $S $T $
2
Ec Ed38n EQU EF4T#s(^8K3L FQU F=T#tCH~*=%> =BS BT#s(^ Br!   r   )rz   r   typingr   r   r   r   r   r   -litellm.integrations.custom_prompt_managementr	   rG   r
   litellm.types.llms.openair   litellm.types.utilsr   r*   r   r   r   r%   r!   r   <module>r      s5   
  : : P N 6 = 9RB- RBr!   