o
    Jjg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    	NO_FILTERr-   c                   s   i | ]\}}| v r||qS  r0   ).0kvallowed_keysr0   _/var/www/html/zoom/venv/lib/python3.10/site-packages/langchain/chains/query_constructor/base.py
<dictcomp>>   s    z5StructuredQueryOutputParser.parse.<locals>.<dictcomp>zParsing text
z
 raised following error:
r0   )	r   lenr'   getpopr   items	Exceptionr   )selfr(   expected_keysparseder0   r4   r6   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rB   rD   rF   )r
   r   r   r   rA   fix_filter_directive)rJ   r,   fixedrF   rB   rD   r0   r6   r'   Z   s   z>StructuredQueryOutputParser.from_components.<locals>.ast_parserM   )r'   N)rJ   r)   r*   rK   )r   rA   )clsrB   rD   rF   rH   r'   r0   rP   r6   from_componentsE   s   
z+StructuredQueryOutputParser.from_components)r(   r)   r*   r   )NNNF)
rB   rC   rD   rE   rF   rG   rH   rI   r*   r&   )__name__
__module____qualname____doc____annotations__rA   classmethodrR   r0   r0   r0   r6   r&   *   s   
 
r&   rM   r,   rK   rB   rC   rD   rE   rF   rG   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 rL   )r
   r   rN   )r1   argrP   r0   r6   
<listcomp>   s    

z(fix_filter_directive.<locals>.<listcomp>   r   )operator	arguments)
isinstancer   
comparator	attributer   r\   r]   r8   r   ANDOR)r,   rB   rD   rF   argsr0   rP   r6   rN   m   s<   


rN   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{{{}}})dictr:   jsondumpsreplace)rd   
info_dictsii_dictr0   r0   r6   _format_attribute_info   s
   ru   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.
    rg   rh   rj   rk   rl   rm   r[   )rs   
user_querystructured_request)	enumeratero   rp   rq   append)rv   examplesrs   _inputoutputrz   exampler0   r0   r6   construct_examples   s   	r   Fr}   rB   rD   enable_limitschema_promptdocument_contentsattribute_infor}   Optional[Sequence]Sequence[Comparator]Sequence[Operator]r   rI   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 | )rB   rD   r   )schemacontent
attributesr[   )rs   )r   )rs   r   r   r+   )r}   example_promptinput_variablessuffixprefixNr0   )r"   r   ru   formatjoinr^   tupler   r$   r!   r#   r8   r    r   r   r   r   r   list)r   r   r}   rB   rD   r   r   r   default_schema_promptattribute_strr   r   r   r   r0   r0   r6   get_query_constructor_prompt   s@   

r   z0.2.13load_query_constructor_runnablez1.0)sincealternative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.
    r   rf   rM   )r   promptoutput_parserNr0   )	r   r|   r^   r%   rf   r&   rR   r   r   )r   r   r   r}   rB   rD   r   r   r   r   rF   ainfor   r0   r0   r6   load_query_constructor_chain  s*   "	r   )r}   rB   rD   r   r   rH   rH   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.
    r   rf   )rB   rD   rF   rH   )r   r|   r^   r%   rf   r&   rR   )r   r   r   r}   rB   rD   r   r   rH   r   r   rF   r   r   r0   r0   r6   r   ?  s0   "
)
r,   rK   rB   rC   rD   rE   rF   rG   r*   rK   )rd   re   r*   r)   )rv   rw   r*   rx   )r   r)   r   re   r}   r   rB   r   rD   r   r   rI   r   r   r   r   r*   r   )r   r   r   r)   r   re   r}   r   rB   r   rD   r   r   rI   r   r   r   r   r*   r   )r   r   r   r)   r   re   r}   r   rB   r   rD   r   r   rI   r   r   rH   rI   r   r   r*   r   )=rV   
__future__r   ro   typingr   r   r   r   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&   rN   ru   r   r   r   r   r   r0   r0   r0   r6   <module>   s`    ( 	0F
:
?	: