
    ^h"                     8   % S r SSKrSSKJr  SSKJrJrJrJrJ	r	J
r
Jr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KJr  SSKJr  \(       a  SSKJr  SSKJ r J!r!  \r"\\#S'   \\!\ 4   r$O\r$\r"\" SS9    SS\"4S jj5       r% " S S\5      r&g)zF
Call Hook for LiteLLM Proxy which allows Langfuse prompt management.
    N)	lru_cache)TYPE_CHECKINGAnyListLiteralOptionalTupleUnioncast)Version)	TypeAlias)verbose_proxy_logger)	DualCache)CustomLogger)UserAPIKeyAuth)AllMessageValues)StandardCallbackDynamicParamsLangfuse)ChatPromptClientTextPromptClientLangfuseClass
   )maxsizereturnc                     SSK nSSK Jn  U=(       d    [        R                  " S5      nU =(       d    [        R                  " S5      nU=(       d    [        R                  " SS	5      nUR                  S
5      (       d  UR                  S5      (       d  S
U-   n[        R                  " S5      n	[        R                  " S5      n
[        R                  " S5      =(       d    UnUUUU	U
US.n[        UR                  R                  5      [        S5      :  a  SUS'   U" S0 UD6nU$ ! [         a  n[        SU S35      eSnAff = f)a  
Initialize Langfuse client with caching to prevent multiple initializations.

Args:
    langfuse_public_key (str, optional): Public key for Langfuse. Defaults to None.
    langfuse_secret (str, optional): Secret key for Langfuse. Defaults to None.
    langfuse_host (str, optional): Host URL for Langfuse. Defaults to None.
    flush_interval (int, optional): Flush interval in seconds. Defaults to 1.

Returns:
    Langfuse: Initialized Langfuse client instance

Raises:
    Exception: If langfuse package is not installed
r   Nr   zS[91mLangfuse not installed, try running 'pip install langfuse' to fix this error: z
[0mLANGFUSE_SECRET_KEYLANGFUSE_PUBLIC_KEYLANGFUSE_HOSTzhttps://cloud.langfuse.comzhttp://zhttps://LANGFUSE_RELEASELANGFUSE_DEBUGLANGFUSE_FLUSH_INTERVAL)
public_key
secret_keyhostreleasedebugflush_intervalz2.6.0litellmsdk_integration )	langfuser   	Exceptionosgetenv
startswithr   version__version__)langfuse_public_keylangfuse_secretlangfuse_hostr(   r,   r   er$   r#   langfuse_releaselangfuse_debuglangfuse_flush_interval
parametersclients                 r/home/james-whalen/.local/lib/python3.13/site-packages/litellm/integrations/langfuse/langfuse_prompt_management.pylangfuse_client_initr=      s<   ,
% !DBII.C$DJ$H		2G(HJ! RYY5&M
 	  ++}/G/G
/S/S "M1yy!34YY/0N ii(ABTn ! #1J x++,0@@(1
$%#
#FMI  
defdggpq
 	

s   
D* *
E4EEc                   `  ^  \ rS rSr    S"S jrS\S\S\4S jrS\S	\	\
   S
\\S   \S   4   S\	\\\4      4S jrS\S\S\4S jrS\S\S\
S
\\S   \S   \S   \S   \S   \S   \S   \S   4   S\\\\
S4   4
U 4S jjrS\S\\   S\
S\
S\S\	\
   S\S\\\\   \
4   4S  jrS!rU =r$ )#LangfusePromptManagement_   Nc                 &    [        UUUUS9U l        g )N)r3   r4   r5   r(   )r=   r   )selfr3   r4   r5   r(   s        r<   __init__!LangfusePromptManagement.__init__`   s     - 3+')	
    langfuse_prompt_idlangfuse_clientr   c                 $    UR                  U5      $ )N)
get_prompt)rB   rF   rG   s      r<   _get_prompt_from_id,LangfusePromptManagement._get_prompt_from_idn   s     ))*<==rE   langfuse_prompt_clientlangfuse_prompt_variables	call_type
completiontext_completionc                 8    S nUc  0 nUR                   " S0 UD6nU$ )Nr+   )compile)rB   rL   rM   rN   compiled_prompts        r<   _compile_prompt(LangfusePromptManagement._compile_prompts   s/     7;$,(*%088U;TUrE   modelc                 T    UR                   nSU;   a  US   $ UR                  SS5      $ )NrV   z	langfuse/ )configreplace)rB   rL   rV   rY   s       r<   _get_model_from_prompt/LangfusePromptManagement._get_model_from_prompt   s2     (..f'?"==b11rE   user_api_key_dictcachedata
embeddingsimage_generation
moderationaudio_transcriptionpass_through_endpointrerankc                   >#    UR                  S5      =(       d    0 n[        U[        5      (       aV  [        [        [
           UR                  S5      5      n[        [        [           UR                  S5      =(       d    0 5      nOg Uc  g U R                  X`R                  S9nS n	US:X  d  US:X  a  U R                  UUUS9n	U	c  [        T
U ])  XX45      I S h  vN $ US:X  a0  [        U	[        5      (       a  XS   -   US'   O9S	U	S
./US   -   US'   O)US:X  a#  [        U	[
        5      (       a  U	S-   US   -   US'   [        R                  " SU	 S[        U	5       35        [        T
U ])  XX45      I S h  vN $  N N7f)NmetadatarF   rM   rF   rG   rO   rP   rL   rM   rN   messagessystemrolecontent
promptz>LangfusePromptManagement.async_pre_call_hook compiled_prompt: z, type: )get
isinstancedictr   r   strrJ   r   rT   superasync_pre_call_hooklistr   r'   type)rB   r]   r^   r_   rN   rg   rF   rM   prompt_clientrS   	__class__s             r<   rv   ,LangfusePromptManagement.async_pre_call_hook   s    " 88J'-2h%%!%hsmX\\BV5W!X(,-H I OR)% %001== 1 
 7;$	5F(F"22'4*C# 3 O
 "4!$   $/400#2*5E#EZ  &/B$$$%Z  ++
?C0P0P,t3d8nDDN""L_L]]efjkzf{e|}	
 W0d
 
 	
#"
s%   CFE=BF8E?9F?Frj   non_default_paramsheaders	prompt_idprompt_variablesdynamic_callback_paramsc                    Uc  [        S5      e[        UR                  S5      UR                  S5      UR                  S5      S9nU R                  XXS9n	U R	                  U	USS9n
U
c  [        S	U 35      e[        U
[        5      (       a  U
nO6[        U
[        5      (       a  S
U
S./nO[        SU S[        U
5       35      eU R                  X5      nXU4$ )NzILangfuse prompt id is required. Pass in as parameter 'langfuse_prompt_id'r3   r4   r5   )r3   r4   r5   rh   rO   ri   z%Langfuse prompt not found. Prompt id=userrl   z3Langfuse prompt is not a list or string. Prompt id=z, compiled_prompt type=)

ValueErrorr=   rq   rJ   rT   rr   rw   rt   rx   r[   )rB   rV   rj   r|   r}   r~   r   r   rG   rL   rS   s              r<   get_chat_completion_prompt3LangfusePromptManagement.get_chat_completion_prompt   s    [  / 7 ; ;<Q R3778IJ155oF

 "&!9!9( ": "

 ..#9&6" / 
 "DYKPQQot,,&H--!'ODEHEi[Pghlm|h}g~ 
 ++,BJ 222rE   r   NNN   )__name__
__module____qualname____firstlineno__rC   rt   r   PROMPT_CLIENTrJ   r   rs   r
   r   rw   rT   r[   r   r   r-   rv   r   r   r   r	   r   __static_attributes____classcell__)rz   s   @r<   r?   r?   _   s    !
>"%>8E>	>
 - $,D> .8I0JJK	
 
%T	"	#2&32<?2	2=
)=
 =
 	=

 L!%&L!&'L!)*+,H	
=
 
y#tT)	*=
~0303 '(03 !	03
 03 03 #4.03 "?03 
	
03 03rE   r?   r   )'__doc__r.   	functoolsr   typingr   r   r   r   r   r	   r
   r   packaging.versionr   typing_extensionsr   litellm._loggingr   litellm.caching.dual_cacher   "litellm.integrations.custom_loggerr   litellm.proxy._typesr   litellm.types.llms.openair   litellm.types.utilsr   r,   r   langfuse.clientr   r   r   __annotations__r   r=   r?   r+   rE   r<   <module>r      s    
  R R R % ' 1 0 ; / 6 =!B'M9'*,<<=MMM 2	<
 < <~[3| [3rE   