a
    •˜ëjÏ6  ã                   @  s(  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( ddl)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3 ddl4m5Z5 G dd„ dee" ƒZ6ddddœddddddœdd„Z7ddd œd!d"„Z8d#d$d%œd&d'„Z9de:eƒe:e!ƒd(dd)œddd*d+d,d-d.d/d0d1œ	d2d3„Z;ed4d5d6d7�de:eƒe:e!ƒd(dfd8ddd9d+d,d-d.d/d:d;œ
d<d=„ƒZ<de:eƒe:e!ƒd(dd(d>œd8ddd*d+d,d-d.d-d/d?d@œdAd5„Z=dS )Bz@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)Úoverride)Ú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                   @  sJ   e Zd ZU dZded< edddœdd„ƒZedddddd dœdd„ƒZd	S )ÚStructuredQueryOutputParserz-Output parser that parses a structured query.r   Ú	ast_parseÚstrr   )ÚtextÚreturnc              
     sê   z¦ddg}g d¢‰ t ||ƒ}|d d u s8t|d ƒdkr@d|d< |d dksT|d s^d |d< n|  |d ¡|d< | d¡s†| dd ¡ tf i ‡ fdd	„| ¡ D ƒ¤ŽW S  tyä } z&d
|› d|› �}t|ƒ|‚W Y d }~n
d }~0 0 d S )NÚqueryÚfilter)r*   r+   Úlimitr   ú Z	NO_FILTERr,   c                   s   i | ]\}}|ˆ v r||“qS © r.   )Ú.0ÚkÚv©Zallowed_keysr.   úf/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/chains/query_constructor/base.pyÚ
<dictcomp>A   ó    z5StructuredQueryOutputParser.parse.<locals>.<dictcomp>zParsing text
z
 raised following error:
)	r   Úlenr&   ÚgetÚpopr   ÚitemsÚ	Exceptionr
   )Úselfr(   Zexpected_keysÚparsedÚeÚmsgr.   r2   r3   Úparse2   s"    


ÿz!StructuredQueryOutputParser.parseNFúOptional[Sequence[Comparator]]úOptional[Sequence[Operator]]úOptional[Sequence[str]]Úbool)Úallowed_comparatorsÚallowed_operatorsÚallowed_attributesÚfix_invalidr)   c                   s8   |rdddœ‡ ‡‡fdd„}nt ˆˆˆ d�j}| |d�S )aj  Create a structured query output parser from components.

        Args:
            allowed_comparators: allowed comparators
            allowed_operators: allowed operators
            allowed_attributes: allowed attributes
            fix_invalid: whether to fix invalid filter directives

        Returns:
            a structured query output parser
        r'   úOptional[FilterDirective])Ú
raw_filterr)   c                   s"   t dtƒ  | ¡ƒ}t|ˆˆˆ d�S )NrH   ©rD   rE   rF   )r   r   r?   Úfix_filter_directive)rI   Zfilter_directive©rF   rD   rE   r.   r3   r&   ]   s    
þüz>StructuredQueryOutputParser.from_components.<locals>.ast_parserJ   )r&   )r   r?   )ÚclsrD   rE   rF   rG   r&   r.   rL   r3   Úfrom_componentsG   s    ýz+StructuredQueryOutputParser.from_components)NNNF)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   r?   ÚclassmethodrN   r.   r.   r.   r3   r%   ,   s   
    ûr%   rJ   rH   r@   rA   rB   )r+   rD   rE   rF   r)   c                  sº   ˆsˆsˆ r| s| S t | tƒrFˆr0| jˆvr0dS ˆ rB| jˆ vrBdS | S t | tƒr¶ˆrb| jˆvrbdS ‡ ‡‡fdd„| jD ƒ}|s‚dS t|ƒdkr¨| jtj	tj
fv r¨|d S t| j|d�S | S )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.
    Nc              
     s*   g | ]"}|d urt dt|ˆˆˆ d�ƒ‘qS )Nr   rJ   )r   rK   )r/   ÚargrL   r.   r3   Ú
<listcomp>’   s   
öüþz(fix_filter_directive.<locals>.<listcomp>é   r   )ÚoperatorÚ	arguments)Ú
isinstancer   Z
comparatorÚ	attributer   rX   rY   r6   r   ÚANDÚOR)r+   rD   rE   rF   Úargsr.   rL   r3   rK   r   s<    ÿÿÿþ


öþrK   z$Sequence[Union[AttributeInfo, dict]]r'   )Úinfor)   c                 C  sB   i }| D ]}t |ƒ}||| d¡< qtj|dd� dd¡ dd¡S )NÚnameé   ©ÚindentÚ{ú{{Ú}ú}})Údictr8   ÚjsonÚdumpsÚreplace)r_   Z
info_dictsÚiZi_dictr.   r.   r3   Ú_format_attribute_infoª   s
    rm   zSequence[tuple[str, dict]]z
list[dict])Úinput_output_pairsr)   c                 C  sV   g }t | ƒD ]D\}\}}tj|dd� dd¡ dd¡}|d ||dœ}| |¡ q|S )	zŸConstruct examples from input-output pairs.

    Args:
        input_output_pairs: Sequence of input-output pairs.

    Returns:
        List of examples.
    ra   rb   rd   re   rf   rg   rW   )rl   Z
user_queryÚstructured_request)Ú	enumerateri   rj   rk   Úappend)rn   Úexamplesrl   Ú_inputÚoutputro   Zexampler.   r.   r3   Úconstruct_examples²   s    	ÿýru   F©rr   rD   rE   Úenable_limitÚschema_promptzOptional[Sequence]zSequence[Comparator]zSequence[Operator]rC   zOptional[BasePromptTemplate]r   r   )	Údocument_contentsÚattribute_inforr   rD   rE   rw   rx   Úkwargsr)   c                K  sÒ   |rt nt}|p|}t|ƒ}	|jd |¡d |¡d�}
|rzt|d tƒrzt|ƒ}t}t	j|
| |	d�}t
jt|ƒd d�}n8|pˆ|r†tnt}t}tj|
d�}tjt|ƒd | |	d�}tf 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 | )rD   rE   r   )ÚschemaÚcontentÚ
attributesrW   )rl   )r|   )rl   r}   r~   r*   )rr   Úexample_promptZinput_variablesÚsuffixÚprefix)r!   r   rm   ÚformatÚjoinrZ   Útupleru   r#   r    r"   r6   r   r   r   r   r   r   Úlist)ry   rz   rr   rD   rE   rw   rx   r{   Zdefault_schema_promptZattribute_strr|   r   r�   r€   r.   r.   r3   Úget_query_constructor_promptÉ   sH    
ÿþý
ÿ
ýûúr†   z0.2.13Úload_query_constructor_runnablez1.0)ZsinceÚalternativeZremovalr   zOptional[list]r   )
Úllmry   rz   rr   rD   rE   rw   rx   r{   r)   c              	   K  sP   t |||||||d�}	dd„ |D ƒ}
tj|||
d�}||	_tf | |	|dœ|¤ŽS )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.
    rv   c                 S  s$   g | ]}t |tƒr|jn|d  ‘qS ©r`   ©rZ   r$   r`   ©r/   Zainfor.   r.   r3   rV   7  s   ÿz0load_query_constructor_chain.<locals>.<listcomp>rJ   )r‰   ÚpromptÚoutput_parser)r†   r%   rN   rŽ   r   )r‰   ry   rz   rr   rD   rE   rw   rx   r{   r�   rF   rŽ   r.   r.   r3   Úload_query_constructor_chain  s&    "ù	þýr�   )rr   rD   rE   rw   rx   rG   r   )r‰   ry   rz   rr   rD   rE   rw   rx   rG   r{   r)   c                K  sJ   t ||f|||||dœ|	¤Ž}
dd„ |D ƒ}tj||||d�}|
| B |B S )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.
    rv   c                 S  s$   g | ]}t |tƒr|jn|d  ‘qS rŠ   r‹   rŒ   r.   r.   r3   rV   q  s   ÿz3load_query_constructor_runnable.<locals>.<listcomp>)rD   rE   rF   rG   )r†   r%   rN   )r‰   ry   rz   rr   rD   rE   rw   rx   rG   r{   r�   rF   rŽ   r.   r.   r3   r‡   E  s,    "þùø
þü)>rR   Ú
__future__r   ri   Úcollections.abcr   Útypingr   r   r   r   r   Zlangchain_core._apir	   Zlangchain_core.exceptionsr
   Zlangchain_core.language_modelsr   Zlangchain_core.output_parsersr   Z"langchain_core.output_parsers.jsonr   Zlangchain_core.promptsr   Zlangchain_core.prompts.few_shotr   Zlangchain_core.runnablesr   Zlangchain_core.structured_queryr   r   r   r   r   r   Ztyping_extensionsr   Zlangchain.chains.llmr   Z)langchain.chains.query_constructor.parserr   Z)langchain.chains.query_constructor.promptr   r   r   r   r   r   r    r!   r"   r#   Z)langchain.chains.query_constructor.schemar$   r%   rK   rm   ru   r„   r†   r�   r‡   r.   r.   r.   r3   Ú<module>   sb    0Iû8ø"Cý	ø$9ö