o
    Zh6                     @  s  d Z ddlmZ ddlZddlmZmZ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 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/m0Z0m1Z1m2Z2 ddl3m4Z4 G dd dee# Z5dddddIddZ6dJd#d$Z7dKd(d)Z8de9ee9e"d*dd+dLd9d:Z:ed;d<d=d>de9ee9e"d*dfdMdCdDZ;de9ee9e"d*dd*dEdNdHd<Z<dS )Oz@LLM Chain for turning a user text query into a structured query.    )annotationsN)AnyCallableListOptionalSequenceTuple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                   @  s>   e Zd ZU dZded< 	 ddd	Ze	
	
	
	ddddZd
S )StructuredQueryOutputParserz-Output parser that parses a structured query.r   	ast_parsetextstrreturnr   c              
     s   zSddg}g d t ||}|d d u st|d dkr d|d< |d dks*|d s/d |d< n	| |d |d< |dsC|dd  tdi  fdd	| D W S  tyi } z
td
| d| d }~ww )Nqueryfilter)r+   r,   limitr    Z	NO_FILTERr-   c                   s   i | ]\}}| v r||qS  r/   ).0kvZallowed_keysr/   ^/var/www/html/lang_env/lib/python3.10/site-packages/langchain/chains/query_constructor/base.py
<dictcomp>>   s    z5StructuredQueryOutputParser.parse.<locals>.<dictcomp>zParsing text
z
 raised following error:
r/   )	r   lenr'   getpopr   items	Exceptionr   )selfr(   Zexpected_keysparseder/   r3   r4   parse0   s(   


z!StructuredQueryOutputParser.parseNFallowed_comparatorsOptional[Sequence[Comparator]]allowed_operatorsOptional[Sequence[Operator]]allowed_attributesOptional[Sequence[str]]fix_invalidboolc                   s2   |rd
 fdd}nt  dj}| |d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
        
raw_filterr)   r*   Optional[FilterDirective]c                   s*   t tt t | }t| d}|S Nr?   rA   rC   )r
   r   r   r   r>   fix_filter_directive)rG   r,   fixedrC   r?   rA   r/   r4   r'   Z   s   z>StructuredQueryOutputParser.from_components.<locals>.ast_parserJ   )r'   N)rG   r)   r*   rH   )r   r>   )clsr?   rA   rC   rE   r'   r/   rM   r4   from_componentsE   s   
z+StructuredQueryOutputParser.from_components)r(   r)   r*   r   )NNNF)
r?   r@   rA   rB   rC   rD   rE   rF   r*   r&   )__name__
__module____qualname____doc____annotations__r>   classmethodrO   r/   r/   r/   r4   r&   *   s   
 
r&   rJ   r,   rH   r?   r@   rA   rB   rC   rD   r*   c                  s   ss r| s
| S t | tr#r| jvrdS  r!| j vr!dS | S t | tr[r1| jvr1dS  fdd| jD }|sAdS t|dkrT| jtj	tj
fv rT|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 tt| dqS rI   )r
   r   rK   )r0   argrM   r/   r4   
<listcomp>   s    

z(fix_filter_directive.<locals>.<listcomp>   r   )operator	arguments)
isinstancer   Z
comparator	attributer   rY   rZ   r6   r   ANDOR)r,   r?   rA   rC   argsr/   rM   r4   rK   m   s<   


rK   info$Sequence[Union[AttributeInfo, dict]]r)   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/   r4   _format_attribute_info   s
   ro   input_output_pairsSequence[Tuple[str, dict]]
List[dict]c                 C  sV   g }t | D ]"\}\}}tj|dddddd}|d ||d}|| q|S )	zConstruct examples from input-output pairs.

    Args:
        input_output_pairs: Sequence of input-output pairs.

    Returns:
        List of examples.
    rc   rd   rf   rg   rh   ri   rX   )rn   Z
user_querystructured_request)	enumeraterk   rl   rm   append)rp   examplesrn   _inputoutputrs   Zexampler/   r/   r4   construct_examples   s   	ry   Frv   r?   rA   enable_limitschema_promptdocument_contentsattribute_inforv   Optional[Sequence]Sequence[Comparator]Sequence[Operator]r{   rF   r|   Optional[BasePromptTemplate]kwargsr   r   c                K  s   |rt nt}|p	|}t|}	|jd|d|d}
|r=t|d tr=t|}t}t	j|
| |	d}t
jt|d d}n|pD|rCtnt}t}tj|
d}tjt|d | |	d}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 | )r?   rA   r   )schemacontent
attributesrX   )rn   )r   )rn   r   r   r+   )rv   example_promptZinput_variablessuffixprefixNr/   )r"   r   ro   formatjoinr[   tuplery   r$   r!   r#   r6   r    r   r   r   r   r   list)r}   r~   rv   r?   rA   r{   r|   r   Zdefault_schema_promptZattribute_strr   r   r   r   r/   r/   r4   get_query_constructor_prompt   s@   

r   z0.2.13load_query_constructor_runnablez1.0)ZsincealternativeZremovalllmr   Optional[List]r   c              	   K  sn   t |||||||d}	g }
|D ]}|
t|tr|jn|d  qtj|||
d}||	_td| |	|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.
    rz   rb   rJ   )r   promptoutput_parserNr/   )	r   ru   r[   r%   rb   r&   rO   r   r   )r   r}   r~   rv   r?   rA   r{   r|   r   r   rC   ainfor   r/   r/   r4   load_query_constructor_chain  s*   "	r   )rv   r?   rA   r{   r|   rE   rE   r   c                K  sh   t ||f|||||d|	}
g }|D ]}|t|tr|jn|d  q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.
    rz   rb   )r?   rA   rC   rE   )r   ru   r[   r%   rb   r&   rO   )r   r}   r~   rv   r?   rA   r{   r|   rE   r   r   rC   r   r   r/   r/   r4   r   ?  s0   "
)
r,   rH   r?   r@   rA   rB   rC   rD   r*   rH   )r`   ra   r*   r)   )rp   rq   r*   rr   )r}   r)   r~   ra   rv   r   r?   r   rA   r   r{   rF   r|   r   r   r   r*   r   )r   r   r}   r)   r~   ra   rv   r   r?   r   rA   r   r{   rF   r|   r   r   r   r*   r   )r   r   r}   r)   r~   ra   rv   r   r?   r   rA   r   r{   rF   r|   r   rE   rF   r   r   r*   r   )=rS   
__future__r   rk   typingr   r   r   r   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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   ro   ry   r   r   r   r   r/   r/   r/   r4   <module>   s`    ( 	0F
:
?	: