§
    šŠtjÍ  ã                  óÆ   — 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 )z¾Remove 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     úf/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/langchain_community/chains/graph_qa/kuzu.pyÚremove_prefixr      s2   € ð ‡‚�vÑÔð #Ø•C˜‘K”K�M�MÔ"Ð"Ø€Kó    c                ó^   — d}t          j        || t           j        ¦  «        }|r|d         n| S )z›Extract 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Ô2€Gà Ð*ˆ7�1Œ:ˆ: 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 )zReturn the input keys.)r)   )r5   s    r   Ú
input_keyszKuzuQAChain.input_keysk   s   € ð ”ÐÐr   c                ó   — | j         g}|S )zReturn the output keys.)r+   )r5   Ú_output_keyss     r   Úoutput_keyszKuzuQAChain.output_keysp   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_llmv   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×;Ò;Ø!¨T¬ZÔ-BÐCÐCÈyð <ñ 
ô 
Ðõ
 )­Ð8HÑ)IÔ)IÈ8ÑTÔTÐà×ÒÐ0°dÀDÄLÐÑQÔQÐQØ×ÒØ G°¸t¼|ð 	ñ 	
ô 	
ð 	
ð ”*×"Ò"Ð#3Ñ4Ô4ˆà×Ò˜_°$ÀÄÐÑMÔMÐMØ×ÒÝ�‰LŒL ¨T¸4¼<ð 	ñ 	
ô 	
ð 	
ð —’Ø!¨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  ø€ € € € € € ðð ð �u TÐ*Ñ*Ô*€EÐ*Ð*Ð*Ñ*Ø%Ð%Ð%Ñ%ØÐÐÑØ€IÐÐÐÑØ€JÐÐÐÑà%*ÐÐ*Ð*Ð*Ñ*ððð ð ð ð ð ð  ð ð  ð  ñ „Xð ð ðð ð ñ „Xðð
 ð ,0ð%
ð )9Ø,BØ26Ø.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_classic.chains.baser   Úlangchain_classic.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  ðØ &Ð &à "Ð "Ð "Ð "Ð "Ð "à 	€	€	€	Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,à /Ð /Ð /Ð /Ð /Ð /Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø ?Ð ?Ð ?Ð ?Ð ?Ð ?Ø <Ð <Ð <Ð <Ð <Ð <Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø Ð Ð Ð Ð Ð ðð ð ð ð ð ð ð ð <Ð ;Ð ;Ð ;Ð ;Ð ;ðð ð ð ð+ð +ð +ð +ð$GCð GCð GCð GCð GC�%ñ GCô GCð GCð GCð GCr   