
    gh                        U d Z 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	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
Z ej        e          ZdedefdZdededefdZdedefdZdedefdZdedefdZdedefdZ	 ddeeef         dee         defdZ 	 ddeeef         dee         defdZ!dedefdZ"eee"dZ#eeeegef         f         e$d<   dS )zLoad prompts.    N)Path)CallableOptionalUnion)StrOutputParser)BasePromptTemplate)ChatPromptTemplate)FewShotPromptTemplate)PromptTemplatezIhttps://raw.githubusercontent.com/hwchase17/langchain-hub/master/prompts/configreturnc                     d| vrt                               d           |                     dd          }|t          vrd| d}t	          |          t          |         } ||           S )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       [/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/langchain_core/prompts/loading.pyload_prompt_from_configr      sw     fFGGG**Wh//K---;;;;oo'4M=       var_namec                     |  d|v rm| |v rd|  d|  d}t          |          t          |                    |  d                    }|j        dk    r|                                }nt           ||| <   |S )z*Load template from the path if applicable._pathzBoth `z_path` and `z` cannot be provided.z.txt)r   r   r   suffix	read_text)r   r   r   template_pathtemplates        r   _load_templater#   ,   s     V##vP8PPPPPCS//!VZZ8(:(:(:;;<<6))$..00HH#xMr   c                    t          | d         t                    rnt          | d         t                    rt          | d                   }|                                5 }|j        dk    rt          j        |          }n/|j        dv rt          j	        |          }nd}t          |          ddd           n# 1 swxY w Y   || d<   nd}t          |          | S )zLoad examples if necessary.examples.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_examplesr3   @   s   &$d++ 	F:&	,	, F:&''YY[[ 	&A{g%%9Q<< 111>!,,U oo%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& &zKooMs   !AB==CCc                     d| v r^| d         rV|                      d          }|                     d          }|dk    rt          di |}nd| }t          |          || d<   | S )zLoad output parser.output_parserr   defaultzUnsupported output parser  )r   r   r   )r   _configoutput_parser_typer5   r   s        r   _load_output_parserr:   U   s    &  VO%< **_--$[[11**+66g66MMC/ACCCS//!"/Mr   c                 D   t          d|           } t          d|           } d| v r;d| v rd}t          |          t          |                     d                    | d<   nt	          | d                   | d<   t          |           } t          |           } t          di | S )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.r7   )r#   r   load_promptr   r   r3   r:   r
   )r   r   s     r   _load_few_shot_promptr@   c   s     Hf--FHf--F&&v%%   S//!#.vzz:O/P/P#Q#Q  #:6BR;S#T#T F##F ((F **6***r   c                     t          d|           } t          |           } |                     dd          }|dk    rd| d}t          |          t	          di | S )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.r7   )r#   r:   getr   r   )r   rB   r   s      r   _load_promptrE   y   s     J//F ((Fjj!2J??O(""V V V V 	
 oo##F###r   r1   encodingc                     t          | t                    r&|                     d          rd}t          |          t	          | |          S )a+  Unified method for loading a prompt from LangChainHub or local fs.

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

    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)r1   rF   r   s      r   r?   r?      sS     $  !9!9   	
 3!$111r   filec                    t          |           }|j        dk    rC|                    |          5 }t          j        |          }ddd           n# 1 swxY w Y   nv|j                            d          rC|                    |          5 }t          j        |          }ddd           n# 1 swxY w Y   nd|j         }t          |          t          |          S )zLoad prompt from file.r&   )rF   N)r(   r'   zGot unsupported file type )
r   r   r,   r-   r.   endswithr/   r0   r   r   )rK   rF   	file_pathr2   r   r   s         r   rJ   rJ      sM   
 T

I7""^^X^.. 	"!Yq\\F	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"			"	"#4	5	5 ^^X^.. 	'!^A&&F	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' >9+;==oo"6***s#   AAAB//B36B3c                     |                      d          }|r!|d         d                              d          nd}|                      d           |sd}t          |          t          j        dd|i| S )	zLoad chat prompt from config.messagesr   r   r"   Ninput_variablesz'Can't load chat prompt without templater7   )r   r   r	   from_template)r   rP   r"   r   s       r   _load_chat_promptrS      s    zz*%%H8@Jx{8$((444dH
JJ !!! 7oo+HHXHHHHr   )r   few_shotchatr   )N)%__doc__r-   loggingpathlibr   typingr   r   r   r/   $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#   r3   r:   r@   rE   r?   rJ   rS   r   __annotations__r7   r   r   <module>rd      s              , , , , , , , , , ,  @ @ @ @ @ @ : : : : : : : : : : : : A A A A A A 8 8 8 8 8 8V		8	$	$!D !-? ! ! ! !0S $ 4    (4 D    *     +$ ++@ + + + +,$ $. $ $ $ $* 7;2 2
T	
2&.sm22 2 2 24 7;+ +
T	
+&.sm++ + + +(
Id 
I'9 
I 
I 
I 
I %H H T#x0B(BCCD     r   