
    Ng%                        d Z ddlmZ ddlZddl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mZ ddlmZ ddZddZ G d de
          ZdS )z Question answering over a graph.    )annotationsN)AnyDictListOptional)Chain)LLMChain)CallbackManagerForChainRun)BaseLanguageModel)BasePromptTemplate)Field)CYPHER_QA_PROMPTKUZU_GENERATION_PROMPT)	KuzuGraphtextstrprefixreturnc                ^    |                      |          r| t          |          d         S | S )zRemove a prefix from a text.

    Args:
        text: Text to remove the prefix from.
        prefix: Prefix to remove from the text.

    Returns:
        Text with the prefix removed.
    N)
startswithlen)r   r   s     d/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/chains/graph_qa/kuzu.pyremove_prefixr      s2     v #CKKMM""K    c                ^    d}t          j        || t           j                  }|r|d         n| S )zExtract Cypher code from a text.

    Args:
        text: Text to extract Cypher code from.

    Returns:
        Cypher code extracted from the text.
    z```(.*?)```r   )refindallDOTALL)r   patternmatchess      r   extract_cypherr!   %   s3     G j$	22G *71::d*r   c                       e Zd ZU dZ ed          Zded<   ded<   ded<   d	Zd
ed<   dZd
ed<   dZ	ded<   	 d) fdZ
ed*d            Zed*d            Ze	 d+eedddd,d"            Z	 d+d-d(Z xZS ).KuzuQAChainu  Question-answering against a graph by generating Cypher statements for Kùzu.

    *Security note*: Make sure that the database connection uses credentials
        that are narrowly-scoped to only include necessary permissions.
        Failure to do so may result in data corruption or loss, since the calling
        code may attempt commands that would result in deletion, mutation
        of data if appropriately prompted or reading sensitive data if such
        data is present in the database.
        The best way to guard against such negative outcomes is to (as appropriate)
        limit the permissions granted to the credentials used with this tool.

        See https://python.langchain.com/docs/security for more information.
    T)excluder   graphr	   cypher_generation_chainqa_chainqueryr   	input_keyresult
output_keyFboolallow_dangerous_requestskwargsr   r   Nonec                j     t                      j        di | | j        durt          d          dS )zInitialize the chain.Ta  In order to use this chain, you must acknowledge that it can make dangerous requests by setting `allow_dangerous_requests` to `True`.You must narrowly scope the permissions of the database connection to only include necessary permissions. Failure to do so may result in data corruption or loss or reading sensitive data if such data is present in the database.Only use this chain if you understand the risks and have taken the necessary precautions. See https://python.langchain.com/docs/security for more information.N )super__init__r-   
ValueError)selfr.   	__class__s     r   r3   zKuzuQAChain.__init__[   sN    ""6"""(44W
 
 
 54r   	List[str]c                    | j         gS )z7Return the input keys.

        :meta private:
        )r)   )r5   s    r   
input_keyszKuzuQAChain.input_keysk   s     r   c                    | j         g}|S )z8Return the output keys.

        :meta private:
        )r+   )r5   _output_keyss     r   output_keyszKuzuQAChain.output_keyss   s     (r   N)	qa_promptcypher_prompt
cypher_llmqa_llmllmOptional[BaseLanguageModel]r=   r   r>   r?   r@   c                   |s|st          d          |s|st          d          |r|r|rt          d          t          |p||          }t          |p||          } | d||d|S )zInitialize from LLM.z8Either `llm` or `cypher_llm` parameters must be providedzLEither `llm` or `qa_llm` parameters must be provided along with `cypher_llm`zaYou can specify up to two of 'cypher_llm', 'qa_llm', and 'llm', but not all three simultaneously.)rA   prompt)r'   r&   r1   )r4   r	   )	clsrA   r=   r>   r?   r@   r.   r'   r&   s	            r   from_llmzKuzuQAChain.from_llm|   s      	Y# 	YWXXX 	c 	     	& 	S 	A  
 #
 
 
 #+!c #
 #
 #

 s 
$;
 
 
 
 	
r   inputsDict[str, Any]run_manager$Optional[CallbackManagerForChainRun]Dict[str, str]c                   |pt          j                    }|                                }|| j                 }| j                            || j        j        d|          }t          t          |          d          }|
                    dd| j                   |
                    |dd| j                   | j                            |          }|
                    d	d| j                   |
                    t          |          dd| j                   |                     ||d
|          }| j        || j        j                 iS )zGGenerate Cypher statement, use it to look up in db and answer question.)questionschema)	callbackscypherzGenerated Cypher:
)endverbosegreen)colorrR   rS   zFull Context:)rM   context)r
   get_noop_manager	get_childr)   r&   runr%   
get_schemar   r!   on_textrS   r(   r   r'   r+   )	r5   rG   rI   _run_managerrO   rM   generated_cypherrV   r*   s	            r   _callzKuzuQAChain._call   sj    #S&@&Q&S&S **,,	$.)7;;!TZ-BCCy < 
 

 )8H)I)I8TT0dDLQQQGt| 	 	
 	
 	
 *""#344_$MMMLLT4< 	 	
 	
 	
 !g66  
 
 (@!ABBr   )r.   r   r   r/   )r   r7   )N)rA   rB   r=   r   r>   r   r?   rB   r@   rB   r.   r   r   r#   )rG   rH   rI   rJ   r   rK   )__name__
__module____qualname____doc__r   r%   __annotations__r)   r+   r-   r3   propertyr9   r<   classmethodr   r   rF   r^   __classcell__)r6   s   @r   r#   r#   7   se          uT***E****%%%%IJ%*****             X     X  ,0%
 )9,B26.2%
 %
 %
 %
 %
 [%
T =A C  C  C  C  C  C  C  C  Cr   r#   )r   r   r   r   r   r   )r   r   r   r   )rb   
__future__r   r   typingr   r   r   r   langchain.chains.baser   langchain.chains.llmr	   langchain_core.callbacksr
   langchain_core.language_modelsr   langchain_core.promptsr   pydanticr   +langchain_community.chains.graph_qa.promptsr   r   %langchain_community.graphs.kuzu_graphr   r   r!   r#   r1   r   r   <module>rq      sb   & & " " " " " " 				 , , , , , , , , , , , , ' ' ' ' ' ' ) ) ) ) ) ) ? ? ? ? ? ? < < < < < < 5 5 5 5 5 5              < ; ; ; ; ;   + + + +$MC MC MC MC MC% MC MC MC MC MCr   