
    Ah*6                       S r SSKJr  SSKrSSKJr  SSK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  SSK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(J)r)J*r*J+r+J,r,J-r-J.r.J/r/J0r0J1r1  SSK2J3r3   " S S\\"   5      r4SSSS.         S#S jjr5S$S jr6S%S jr7S\8" \5      \8" \!5      SSS.                 S&S jjr9\" SSSS9S\8" \5      \8" \!5      SS4                   S'S  jj5       r:S\8" \5      \8" \!5      SSSS!.                     S(S" jjr;g))z@LLM Chain for turning a user text query into a structured query.    )annotationsN)Sequence)AnyCallableOptionalUnioncast)
deprecated)OutputParserException)BaseLanguageModel)BaseOutputParser)parse_and_check_json_markdown)BasePromptTemplate)FewShotPromptTemplate)Runnable)
Comparator
ComparisonFilterDirective	OperationOperatorStructuredQuery)LLMChain)
get_parser)
DEFAULT_EXAMPLESDEFAULT_PREFIXDEFAULT_SCHEMA_PROMPTDEFAULT_SUFFIXEXAMPLE_PROMPTEXAMPLES_WITH_LIMITPREFIX_WITH_DATA_SOURCESCHEMA_WITH_LIMIT_PROMPTSUFFIX_WITHOUT_DATA_SOURCEUSER_SPECIFIED_EXAMPLE_PROMPT)AttributeInfoc                  b    \ rS rSr% SrS\S'    S	S jr\    S
         SS jj5       rSr	g)StructuredQueryOutputParser+   z-Output parser that parses a structured query.r   	ast_parsec           
         SS/n/ SQn[        X5      nUS   b  [        US   5      S:X  a  SUS'   US   S:X  d
  US   (       d  S US'   OU R                  US   5      US'   UR                  S5      (       d  UR	                  SS 5        [        S
0 UR                  5        VVs0 sH  u  pVXS;   d  M  XV_M     snnD6$ s  snnf ! [         a  nSU S	U 3n[        U5      UeS nAff = f)Nqueryfilter)r*   r+   limitr    	NO_FILTERr,   zParsing text
z
 raised following error:
 )	r   lenr(   getpopr   items	Exceptionr   )	selftextexpected_keysallowed_keysparsedkvemsgs	            _/var/www/html/shao/venv/lib/python3.13/site-packages/langchain/chains/query_constructor/base.pyparse!StructuredQueryOutputParser.parse1   s    	4$h/M7L24GFg&#fWo*>!*C"%wh;.fX6F#'x #'>>&2B#Cx ::g&&

7D)" $*LLNHNDAa6G414NH H 	4"4&(DQCHC',!3	4s0   B"C $
B>2B>8C >C 
C'C""C'Nc                b   ^^^ U(       a  SUUU4S jjnO[        TTTS9R                  nU " US9$ )z
Create a structured query output parser from components.

Args:
    allowed_comparators: allowed comparators
    allowed_operators: allowed operators

Returns:
    a structured query output parser
c                x   > [        [        [           [        5       R	                  U 5      5      n[        UTTTS9$ )Nallowed_comparatorsallowed_operatorsallowed_attributes)r	   r   r   r   r?   fix_filter_directive)
raw_filterfilter_directiverF   rD   rE   s     r>   r(   >StructuredQueryOutputParser.from_components.<locals>.ast_parseZ   sA    #'_-L&&z2$  ,$(;&7'9	     rC   )r(   )rH   strreturnOptional[FilterDirective])r   r?   )clsrD   rE   rF   fix_invalidr(   s    ```  r>   from_components+StructuredQueryOutputParser.from_componentsE   s>    & 
 
 #$7"3#5 e	 
 Y''rK   r/   )r6   rL   rM   r   )NNNF)
rD   Optional[Sequence[Comparator]]rE   Optional[Sequence[Operator]]rF   Optional[Sequence[str]]rP   boolrM   r&   )
__name__
__module____qualname____firstlineno____doc____annotations__r?   classmethodrQ   __static_attributes__r/   rK   r>   r&   r&   +   sf    7S4(  ?C:>6:!&(;&( 8&( 4	&(
 &( 
%&( &(rK   r&   rC   c               `   U(       d  U(       d  U(       a  U (       d  U $ [        U [        5      (       a2  U(       a  U R                  U;  a  gU(       a  U R                  U;  a  gU $ [        U [        5      (       a  U(       a  U R
                  U;  a  gU R                   Vs/ sH"  nUc  M  [        [        [        UUUUS95      PM$     nnU(       d  g[        U5      S:X  a3  U R
                  [        R                  [        R                  4;   a  US   $ [	        U R
                  US9$ U $ s  snf )aH  Fix invalid filter directive.

Args:
    filter: Filter directive to fix.
    allowed_comparators: allowed comparators. Defaults to all comparators.
    allowed_operators: allowed operators. Defaults to all operators.
    allowed_attributes: allowed attributes. Defaults to all attributes.

Returns:
    Fixed filter directive.
NrC      r   )operator	arguments)
isinstancer   
comparator	attributer   ra   rb   r	   r   rG   r0   r   ANDOR)r+   rD   rE   rF   argargss         r>   rG   rG   o   s   & !$59K&*%%6#4#4<O#O&"2"2:L"L&)$$8I!I ''
 (D$(;&7'9	 ( 	 
 t9>foo(,,1LL7N__
 	
 M+
s   !D++D+c                    0 nU  H!  n[        U5      nX1UR                  S5      '   M#     [        R                  " USS9R	                  SS5      R	                  SS5      $ )Nname   indent{{{}}})dictr2   jsondumpsreplace)info
info_dictsii_dicts       r>   _format_attribute_infor{      sX    Ja)/6::f%&  ::j+33C>FFsDQQrK   c                    / n[        U 5       HW  u  nu  p4[        R                  " USS9R                  SS5      R                  SS5      nUS-   UUS.nUR	                  U5        MY     U$ )	zConstruct examples from input-output pairs.

Args:
    input_output_pairs: Sequence of input-output pairs.

Returns:
    List of examples.
rl   rm   ro   rp   rq   rr   r`   )ry   
user_querystructured_request)	enumeratert   ru   rv   append)input_output_pairsexamplesry   _inputoutputr~   examples          r>   construct_examplesr      sy     H();<FJJva(00d;CCCN 	 Q "4

 	   = OrK   Fr   rD   rE   enable_limitschema_promptc          	     d   U(       a  [         O[        nU=(       d    Un[        U5      n	UR                  SR	                  U5      SR	                  U5      S9n
U(       a`  [        US   [        5      (       aH  [        U5      n[        n[        R                  " U
U U	S9n[        R                  " [        U5      S-   S9nOXU=(       d    U(       a  [        O[        n[        n[        R                  " U
S9n[         R                  " [        U5      S-   U U	S9n[#        S[%        U5      US	/UUS
.UD6$ )a  Create query construction prompt.

Args:
    document_contents: The contents of the document to be queried.
    attribute_info: A list of AttributeInfo objects describing
        the attributes of the document.
    examples: Optional list of examples to use for the chain.
    allowed_comparators: Sequence of allowed comparators.
    allowed_operators: Sequence of allowed operators.
    enable_limit: Whether to enable the limit operator. Defaults to False.
    schema_prompt: Prompt for describing query schema. Should have string input
        variables allowed_comparators and allowed_operators.
    kwargs: Additional named params to pass to FewShotPromptTemplate init.

Returns:
    A prompt template that can be used to construct queries.
z | )rD   rE   r   )schemacontent
attributesr`   )ry   )r   )ry   r   r   r*   )r   example_promptinput_variablessuffixprefixr/   )r!   r   r{   formatjoinrc   tupler   r#   r    r"   r0   r   r   r   r   r   r   list)document_contentsattribute_infor   rD   rE   r   r   kwargsdefault_schema_promptattribute_strr   r   r   r   s                 r>   get_query_constructor_promptr      s2   : %1 6K  ":%:M*>:M!!!JJ':;**%67 " F Jx{E22%h/6(//%$

 ,22S]Q5FG 
#/5E 	 (&&f5&&(ma%$

 ! h% 	  rK   z0.2.13load_query_constructor_runnablez1.0)sincealternativeremovalc           
         [        UUUUUUUS9n	U V
s/ sH)  n
[        U
[        5      (       a  U
R                  OU
S   PM+     nn
[        R                  UUUS9nXl        [        SX	US.UD6$ s  sn
f )a  Load a query constructor chain.

Args:
    llm: BaseLanguageModel to use for the chain.
    document_contents: The contents of the document to be queried.
    attribute_info: Sequence of attributes in the document.
    examples: Optional list of examples to use for the chain.
    allowed_comparators: Sequence of allowed comparators. Defaults to all
        Comparators.
    allowed_operators: Sequence of allowed operators. Defaults to all Operators.
    enable_limit: Whether to enable the limit operator. Defaults to False.
    schema_prompt: Prompt for describing query schema. Should have string input
        variables allowed_comparators and allowed_operators.
    **kwargs: Arbitrary named params to pass to LLMChain.

Returns:
    A LLMChain that can be used to construct queries.
r   rk   rC   )llmpromptoutput_parserr/   )r   rc   r$   rk   r&   rQ   r   r   )r   r   r   r   rD   rE   r   r   r   r   ainforF   r   s                r>   load_query_constructor_chainr   	  s    D */+!#F $#E !66

E&MI#   0??/+- @ M )R-R6RRs   /A.)r   rD   rE   r   r   rP   c          	         [        UU4UUUUUS.U	D6n
U Vs/ sH)  n[        U[        5      (       a  UR                  OUS   PM+     nn[        R                  UUUUS9nX-  U-  $ s  snf )a  Load a query constructor runnable chain.

Args:
    llm: BaseLanguageModel to use for the chain.
    document_contents: Description of the page contents of the document to be
        queried.
    attribute_info: Sequence of attributes in the document.
    examples: Optional list of examples to use for the chain.
    allowed_comparators: Sequence of allowed comparators. Defaults to all
        Comparators.
    allowed_operators: Sequence of allowed operators. Defaults to all Operators.
    enable_limit: Whether to enable the limit operator. Defaults to False.
    schema_prompt: Prompt for describing query schema. Should have string input
        variables allowed_comparators and allowed_operators.
    fix_invalid: Whether to fix invalid filter directives by ignoring invalid
        operators, comparators and attributes.
    kwargs: Additional named params to pass to FewShotPromptTemplate init.

Returns:
    A Runnable that can be used to construct queries.
r   rk   )rD   rE   rF   rP   )r   rc   r$   rk   r&   rQ   )r   r   r   r   rD   rE   r   r   rP   r   r   r   rF   r   s                 r>   r   r   B  s    D *	 /+!#	 	F $#E !66

E&MI#   0??/+-	 @ M <-''s   /A&)
r+   rN   rD   rS   rE   rT   rF   rU   rM   rN   )rw   $Sequence[Union[AttributeInfo, dict]]rM   rL   )r   zSequence[tuple[str, dict]]rM   z
list[dict])r   rL   r   r   r   Optional[Sequence]rD   Sequence[Comparator]rE   Sequence[Operator]r   rV   r   Optional[BasePromptTemplate]r   r   rM   r   )r   r   r   rL   r   r   r   zOptional[list]rD   r   rE   r   r   rV   r   r   r   r   rM   r   )r   r   r   rL   r   r   r   r   rD   r   rE   r   r   rV   r   r   rP   rV   r   r   rM   r   )<r[   
__future__r   rt   collections.abcr   typingr   r   r   r   r	   langchain_core._apir
   langchain_core.exceptionsr   langchain_core.language_modelsr   langchain_core.output_parsersr   "langchain_core.output_parsers.jsonr   langchain_core.promptsr   langchain_core.prompts.few_shotr   langchain_core.runnablesr   langchain_core.structured_queryr   r   r   r   r   r   langchain.chains.llmr   )langchain.chains.query_constructor.parserr   )langchain.chains.query_constructor.promptr   r   r   r   r   r   r    r!   r"   r#   )langchain.chains.query_constructor.schemar$   r&   rG   r{   r   r   r   r   r   r/   rK   r>   <module>r      sl   F "  $ 7 7 * ; < : L 5 A -  * @   DA("2?"C A(N ;?6:265%5 85 4	5
 05 5pR6 $(05j0A,1(O26@@8@ !	@
 .@ *@ @ 0@ @ @F 
1  $05j0A,1(O261S	1S1S 91S 	1S
 .1S *1S 1S 01S 1S 1S
1Sr $(05j0A,1(O266(	6(6( 96(
 !6( .6( *6( 6( 06( 6( 6( 6(rK   