Ë
    ´Œj*6  ã                  ó€  — d Z ddlmZ ddlZddlmZ ddl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 ddlmZ ddlmZ ddlm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(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1 ddl2m3Z3  G d„ dee"   «      Z4ddddœ	 	 	 	 	 	 	 	 	 d#d„Z5d$d„Z6d%d„Z7d e8e«       e8e!«      dddœ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d&d„Z9 eddd¬«      d e8e«       e8e!«      ddf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d'd „«       Z:d e8e«       e8e!«      dddd!œ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d(d"„Z;y))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                  óT   — e Zd ZU dZded<   	 dd„Ze	 	 	 	 d	 	 	 	 	 	 	 	 	 d	d„«       Zy)
ÚStructuredQueryOutputParserz-Output parser that parses a structured query.r   Ú	ast_parsec           
     ó¶  — 	 ddg}g d¢}t        ||«      }|d   �t        |d   «      dk(  rd|d<   |d   dk(  s|d   sd |d<   n| j                  |d   «      |d<   |j                  d«      s|j	                  dd «       t        d
i |j                  «       D ��ci c]  \  }}||v sŒ||“Œ c}}¤ŽS c c}}w # t        $ r}d|› d	|› �}t        |«      |‚d }~ww xY w)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	            úq/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/langchain/chains/query_constructor/base.pyÚparsez!StructuredQueryOutputParser.parse1   s  € ð	4Ø$ hÐ/ˆMÚ7ˆLÜ2°4¸ÓGˆFØ�g‰Ð&¬#¨f°W©oÓ*>À!Ò*CØ"%��w‘Ø�hÑ ;Ò.°f¸XÒ6FØ#'��xÒ à#'§>¡>°&¸Ñ2BÓ#C��xÑ Ø—:‘:˜gÔ&Ø—
‘
˜7 DÔ)Ü"ñ Ø$*§L¡L¤NÔH¡N™D˜A˜q°a¸<Ò6G�1�a‘4 NÒHñð ùÛHøäò 	4Ø" 4 &Ð(DÀQÀCÐHˆCÜ'¨Ó,°!Ð3ûð	4ús0   ‚BB6 ÂB0Â%B0Â*B6 Â0B6 Â6	CÂ?CÃCNc                ó`   ‡‡‡— |r	dˆˆˆfd„}nt        ‰‰‰¬«      j                  } | |¬«      S )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                ó~   •— t        t        t           t        «       j	                  | «      «      }t        |‰‰‰¬«      S )N©Úallowed_comparatorsÚallowed_operatorsÚallowed_attributes)r	   r   r   r   r>   Úfix_filter_directive)Ú
raw_filterÚfilter_directiverD   rB   rC   s     €€€r=   r'   z>StructuredQueryOutputParser.from_components.<locals>.ast_parseZ   sA   ø€ Ü#'Üœ_Ñ-Ü“L×&Ñ& zÓ2ó$Ð ô ,Ø$Ø(;Ø&7Ø'9ô	ð ó    rA   )r'   )rF   ÚstrÚreturnúOptional[FilterDirective])r   r>   )ÚclsrB   rC   rD   Úfix_invalidr'   s    ```  r=   Úfrom_componentsz+StructuredQueryOutputParser.from_componentsE   s>   ú€ ñ& ÷
ð 
ô #Ø$7Ø"3Ø#5ô÷ ‰eð	 ñ
 ˜YÔ'Ð'rH   )r5   rI   rJ   r   )NNNF)
rB   úOptional[Sequence[Comparator]]rC   úOptional[Sequence[Operator]]rD   úOptional[Sequence[str]]rM   ÚboolrJ   r&   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r>   ÚclassmethodrN   r.   rH   r=   r&   r&   +   sf   … Ù7àÓØSó4ð( ð ?CØ:>Ø6:Ø!ð&(à;ð&(ð 8ð&(ð 4ð	&(ð
 ð&(ð 
%ò&(ó ñ&(rH   r&   rA   c               óì  — |s|s|r| s| S t        | t        «      r$|r| j                  |vry|r| j                  |vry| S t        | t        «      r |r| j
                  |vry| j                  D �cg c]!  }|�t        t        t        ||||¬«      «      ‘Œ# }}|syt        |«      dk(  r1| j
                  t        j                  t        j                  fv r|d   S t	        | j
                  |¬«      S | S c c}w )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.
    NrA   é   r   )ÚoperatorÚ	arguments)Ú
isinstancer   Ú
comparatorÚ	attributer   r[   r\   r	   r   rE   r/   r   ÚANDÚOR)r*   rB   rC   rD   ÚargÚargss         r=   rE   rE   o   s  € ñ& !Ñ$5Ñ9KÙØˆä�&œ*Ô%Ù 6×#4Ñ#4Ð<OÑ#OØÙ &×"2Ñ"2Ð:LÑ"LØØˆÜ�&œ)Ô$Ù §¡Ð8IÑ!IØð ×'Ò'ó
ñ (�Øˆô ÜÜ$ØØ(;Ø&7Ø'9ô	õð (ð 	ð 
ñ ØÜˆt‹9˜Š>˜fŸo™o´(·,±,ÄÇÁÐ1LÑLØ˜‘7ˆNÜØ—_‘_Øô
ð 	
ð €Mùò+
s   Á.&C1c                óÀ   — i }| D ]!  }t        |«      }|||j                  d«      <   Œ# t        j                  |d¬«      j	                  dd«      j	                  dd«      S )NÚnameé   ©ÚindentÚ{ú{{Ú}ú}})Údictr1   ÚjsonÚdumpsÚreplace)ÚinfoÚ
info_dictsÚiÚi_dicts       r=   Ú_format_attribute_inforu   §   sZ   € Ø€JÛˆÜ�a“ˆØ)/ˆ
�6—:‘:˜fÓ%Ò&ð ô �:‰:�j¨Ô+×3Ñ3°C¸Ó>×FÑFÀsÈDÓQÐQrH   c                óØ   — g }t        | «      D ]Y  \  }\  }}t        j                  |d¬«      j                  dd«      j                  dd«      }|dz   ||dœ}|j	                  |«       Œ[ |S )	zŸConstruct examples from input-output pairs.

    Args:
        input_output_pairs: Sequence of input-output pairs.

    Returns:
        List of examples.
    rf   rg   ri   rj   rk   rl   rZ   )rs   Ú
user_queryÚstructured_request)Ú	enumeratern   ro   rp   Úappend)Úinput_output_pairsÚexamplesrs   Ú_inputÚoutputrx   Úexamples          r=   Úconstruct_examplesr€   ¯   s{   € ð €HÜ(Ð);Ö<ÑˆÑˆF�Fä�J‰J�v aÔ(×0Ñ0°°dÓ;×CÑCÀCÈÓNð 	ð �Q‘Ø Ø"4ñ
ˆð
 	�‰˜Õ ð  =ð €OrH   F©r|   rB   rC   Úenable_limitÚschema_promptc          	     ó>  — |rt         nt        }|xs |}t        |«      }	|j                  dj	                  |«      dj	                  |«      ¬«      }
|r_t        |d   t        «      rLt        |«      }t        }t        j                  |
| |	¬«      }t        j                  t        |«      dz   ¬«      }nR|xs |rt        nt        }t        }t        j                  |
¬«      }t!        j                  t        |«      dz   | |	¬«      }t#        dt%        |«      |d	g||d
œ|¤ŽS )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 | )rB   rC   r   )ÚschemaÚcontentÚ
attributesrZ   )rs   )r…   )rs   r†   r‡   r)   )r|   Úexample_promptÚinput_variablesÚsuffixÚprefixr.   )r!   r   ru   ÚformatÚjoinr]   Útupler€   r#   r    r"   r/   r   r   r   r   r   r   Úlist)Údocument_contentsÚattribute_infor|   rB   rC   r‚   rƒ   ÚkwargsÚdefault_schema_promptÚattribute_strr…   rˆ   r‹   rŠ   s                 r=   Úget_query_constructor_promptr•   Æ   s/  € ñ: %1Õ Ô6Kð ð "Ò:Ð%:€MÜ*¨>Ó:€MØ×!Ñ!Ø!ŸJ™JÐ':Ó;ØŸ*™*Ð%6Ó7ð "ó €Fñ ”J˜x¨™{¬EÔ2Ü% hÓ/ˆÜ6ˆÜ(×/Ñ/ØØ%Ø$ô
ˆô
 ,×2Ñ2´S¸³]ÀQÑ5FÔG‰àò 
Ù#/ÕÔ5Eð 	ô (ˆÜ×&Ñ&¨fÔ5ˆÜ×&Ñ&Ü�(‹m˜aÑØ%Ø$ô
ˆô
 !ð Ü�h“Ø%Ø ˜	ØØñð ñð rH   z0.2.13Úload_query_constructor_runnablez1.0)ÚsinceÚalternativeÚremovalc           	     óè   — t        |||||||¬«      }	|D �
cg c]#  }
t        |
t        «      r|
j                  n|
d   ‘Œ% }}
t        j                  |||¬«      }||	_        t        d| |	|dœ|¤ŽS c c}
w )aS  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�   re   rA   )ÚllmÚpromptÚoutput_parserr.   )r•   r]   r$   re   r&   rN   r�   r   )r›   r�   r‘   r|   rB   rC   r‚   rƒ   r’   rœ   ÚainforD   r�   s                r=   Úload_query_constructor_chainrŸ   	  s£   € ôD *ØØØØ/Ø+Ø!Ø#ô€Fñ $óá#ˆEô ! ¬Ô6ˆ�
Š
¸EÀ&¹MÑIØ#ð ð ô 0×?Ñ?Ø/Ø+Ø-ð @ó €Mð )€FÔÜÐR˜ F¸-ÑRÈ6ÑRÐRùòs   —(A/)r|   rB   rC   r‚   rƒ   rM   c          	     óÐ   — t        ||f|||||dœ|	¤Ž}
|D �cg c]#  }t        |t        «      r|j                  n|d   ‘Œ% }}t        j                  ||||¬«      }|
| z  |z  S c c}w )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�   re   )rB   rC   rD   rM   )r•   r]   r$   re   r&   rN   )r›   r�   r‘   r|   rB   rC   r‚   rƒ   rM   r’   rœ   rž   rD   r�   s                 r=   r–   r–   B  s§   € ôD *ØØð	ð Ø/Ø+Ø!Ø#ñ	ð ñ	€Fñ $óá#ˆEô ! ¬Ô6ˆ�
Š
¸EÀ&¹MÑIØ#ð ð ô 0×?Ñ?Ø/Ø+Ø-Øð	 @ó €Mð �C‰<˜-Ñ'Ð'ùòs   ˜(A#)
r*   rK   rB   rO   rC   rP   rD   rQ   rJ   rK   )rq   ú$Sequence[Union[AttributeInfo, dict]]rJ   rI   )r{   zSequence[tuple[str, dict]]rJ   z
list[dict])r�   rI   r‘   r¡   r|   úOptional[Sequence]rB   úSequence[Comparator]rC   úSequence[Operator]r‚   rR   rƒ   úOptional[BasePromptTemplate]r’   r   rJ   r   )r›   r   r�   rI   r‘   r¡   r|   zOptional[list]rB   r£   rC   r¤   r‚   rR   rƒ   r¥   r’   r   rJ   r   )r›   r   r�   rI   r‘   r¡   r|   r¢   rB   r£   rC   r¤   r‚   rR   rƒ   r¥   rM   rR   r’   r   rJ   r   )<rV   Ú
__future__r   rn   Ú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&   rE   ru   r€   rŽ   r•   rŸ   r–   r.   rH   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 $(Ù05°jÓ0AÙ,1°(«OØØ26ñ@Øð@à8ð@ð !ð	@ð
 .ð@ð *ð@ð ð@ð 0ð@ð ð@ð ó@ñF Ø
Ø1Øôð  $Ù05°jÓ0AÙ,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 $(Ù05°jÓ0AÙ,1°(«OØØ26Øñ6(Ø	ð6(àð6(ð 9ð6(ð
 !ð6(ð .ð6(ð *ð6(ð ð6(ð 0ð6(ð ð6(ð ð6(ð ô6(rH   