
    D_i                        % S r SSKrSSKrSSKJr  SSKJr  SSK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
r\R&                  " \5      rS\S\4S jrS\S\S\4S jrS\S\4S jrS\S\4S jrS\S\4S jrS\S\4S jrSS\\-  S\S-  S\4S jjr SS\\-  S\S-  S\4S jjrS\S\4S jr \\\ S.r!\\\\/\4   4   \"S'   g)zLoad prompts.    N)Callable)Path)StrOutputParser)BasePromptTemplate)ChatPromptTemplate)FewShotPromptTemplate)PromptTemplatezIhttps://raw.githubusercontent.com/hwchase17/langchain-hub/master/prompts/configreturnc                     SU ;  a  [         R                  S5        U R                  SS5      nU[        ;  a  SU S3n[	        U5      e[        U   nU" U 5      $ )zLoad prompt from Config Dict.

Args:
    config: Dict containing the prompt configuration.

Returns:
    A PromptTemplate object.

Raises:
    ValueError: If the prompt type is not supported.
_typez-No `_type` key found, defaulting to `prompt`.promptzLoading z prompt not supported)loggerwarningpoptype_to_loader_dict
ValueError)r
   config_typemsgprompt_loaders       X/home/james-whalen/.local/lib/python3.13/site-packages/langchain_core/prompts/loading.pyload_prompt_from_configr      s`     fFG**Wh/K--%:;o'4M      var_namec                     U  S3U;   a`  X;   a  SU  SU  S3n[        U5      e[        UR                  U  S35      5      nUR                  S:X  a  UR	                  SS9nO[         eXAU '   U$ )z*Load template from the path if applicable._pathzBoth `z_path` and `z` cannot be provided.z.txtutf-8encoding)r   r   r   suffix	read_text)r   r
   r   template_pathtemplates        r   _load_templater$   ,   s     5V#8*L
:OPCS/!VZZ8*E(:;<6)$...@H#xMr   c                    [        U S   [        5      (       a   U $ [        U S   [        5      (       a  [        U S   5      nUR	                  SS9 nUR
                  S:X  a  [        R                  " U5      nO4UR
                  S;   a  [        R                  " U5      nOSn[        U5      eSSS5        WU S'   U $ Sn[        U5      e! , (       d  f       N"= f)	zLoad examples if necessary.examplesr   r   .json>   .yml.yamlz=Invalid file format. Only json or yaml formats are supported.Nz;Invalid examples format. Only list or string are supported.)
isinstanceliststrr   openr    jsonloadyaml	safe_loadr   )r
   pathfr&   r   s        r   _load_examplesr4   @   s    &$d++ M 
F:&	,	,F:&'YYY(A{{g%99Q< 11>>!,U o% ) &z M Lo )(s   AC		
Cc                     U R                  S5      =n(       a9  UR                  S5      S:g  =n(       a  SU 3n[        U5      e[        S0 UD6U S'   U $ )zLoad output parser.output_parserr   defaultzUnsupported output parser  )getr   r   )r
   _configoutput_parser_typer   s       r   _load_output_parserr<   U   s_    **_--w-!(W!5!BBB./A.BCCS/!"1"<G"<Mr   c                    [        SU 5      n [        SU 5      n SU ;   a1  SU ;   a  Sn[        U5      e[        U R                  S5      5      U S'   O[	        U S   5      U S'   [        U 5      n [        U 5      n [        S0 U D6$ )z+Load the "few shot" prompt from the config.r    prefixexample_prompt_pathexample_promptzGOnly one of example_prompt and example_prompt_path should be specified.r8   )r$   r   load_promptr   r   r4   r<   r   )r
   r   s     r   _load_few_shot_promptrB   _   s     Hf-FHf-F&v%   S/!#.vzz:O/P#Q #:6BR;S#T F#F (F *6**r   c                     [        SU 5      n [        U 5      n U R                  SS5      nUS:X  a  SU S3n[        U5      e[	        S0 U D6$ )z%Load the prompt template from config.r#   template_formatzf-stringjinja2zLoading templates with 'z' format is no longer supported since it can lead to arbitrary code execution. Please migrate to using the 'f-string' template format, which does not suffer from this issue.r8   )r$   r<   r9   r   r	   )r
   rD   r   s      r   _load_promptrF   u   sh     J/F (Fjj!2J?O(" '&7 8U V 	
 o#F##r   r2   r   c                     [        U [        5      (       a#  U R                  S5      (       a  Sn[        U5      e[	        X5      $ )zUnified method for loading a prompt from LangChainHub or local fs.

Args:
    path: Path to the prompt file.
    encoding: Encoding of the file.

Returns:
    A PromptTemplate object.

Raises:
    RuntimeError: If the path is a Lang Chain Hub path.
zlc://zLoading from the deprecated github-based Hub is no longer supported. Please use the new LangChain Hub at https://smith.langchain.com/hub instead.)r*   r,   
startswithRuntimeError_load_prompt_from_file)r2   r   r   s      r   rA   rA      sA     $!9!9 	
 3!$11r   filec                    [        U 5      nUR                  S:X  a/  UR                  US9 n[        R                  " U5      nSSS5        OiUR                  R                  S5      (       a/  UR                  US9 n[        R                  " U5      nSSS5        OSUR                   3n[        U5      e[        W5      $ ! , (       d  f       N= f! , (       d  f       N*= f)zLoad prompt from file.r'   r   N)r)   r(   zGot unsupported file type )
r   r    r-   r.   r/   endswithr0   r1   r   r   )rK   r   	file_pathr3   r
   r   s         r   rJ   rJ      s    
 T
I7"^^X^.!YYq\F /.				"	"#4	5	5^^X^.!^^A&F /. +9+;+;*<=o"6** /. /.s   B?:C?
C
Cc                     U R                  S5      nU(       a  US   S   R                  S5      OSnU R                  S5        U(       d  Sn[        U5      e[        R                  " SSU0U D6$ )	zLoad chat prompt from config.messagesr   r   r#   Ninput_variablesz'Can't load chat prompt without templater8   )r   r   r   from_template)r
   rP   r#   r   s       r   _load_chat_promptrS      se    zz*%H8@x{8$((4dH
JJ !7o++HXHHHr   )r   few_shotchatr   )N)#__doc__r.   loggingcollections.abcr   pathlibr   r0   $langchain_core.output_parsers.stringr   langchain_core.prompts.baser   langchain_core.prompts.chatr   langchain_core.prompts.few_shotr   langchain_core.prompts.promptr	   URL_BASE	getLogger__name__r   dictr   r,   r$   r4   r<   rB   rF   rA   rJ   rS   r   __annotations__r8   r   r   <module>rd      sL      $   @ : : A 8V			8	$!D !-? !0S $ 4 (4 D *  +$ ++@ +,$ $. $(2cDj 2C$J 2BT 20 .2+
*+ #d
++(
Id 
I'9 
I %H T#x0B(BCCD r   