Ë
    µŒj%  ã                  ó¾   — 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y)z Question answering over a graph.é    )ÚannotationsN)ÚAnyÚDictÚListÚOptional)ÚChain)ÚLLMChain)ÚCallbackManagerForChainRun)ÚBaseLanguageModel)ÚBasePromptTemplate)ÚField)ÚCYPHER_QA_PROMPTÚKUZU_GENERATION_PROMPT)Ú	KuzuGraphc                óD   — | j                  |«      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)ÚtextÚprefixs     úr/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/langchain_community/chains/graph_qa/kuzu.pyÚremove_prefixr      s&   € ð ‡��vÔØ”C˜“K�MÐ"Ð"Ø€Kó    c                ób   — d}t        j                  || t         j                  «      }|r|d   S | 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   %   s2   € ð €Gô �j‰j˜ $¬¯	©	Ó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ÚqueryÚstrÚ	input_keyÚresultÚ
output_keyFÚboolÚallow_dangerous_requestsc                óV   •— t        ‰| �  di |¤Ž | j                  durt        d«      ‚y)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)ÚselfÚkwargsÚ	__class__s     €r   r0   zKuzuQAChain.__init__[   s9   ø€ ä‰ÑÑ"˜6Ò"Ø×(Ñ(°Ñ4ÜðWó
ð 
ð 5r   c                ó   — | j                   gS )z7Return the input keys.

        :meta private:
        )r(   )r2   s    r   Ú
input_keyszKuzuQAChain.input_keysk   s   € ð —‘ÐÐr   c                ó    — | j                   g}|S )z8Return the output keys.

        :meta private:
        )r*   )r2   Ú_output_keyss     r   Úoutput_keyszKuzuQAChain.output_keyss   s   € ð Ÿ™Ð(ˆØÐr   N)Ú	qa_promptÚcypher_promptÚ
cypher_llmÚqa_llmc               óº   — |s|st        d«      ‚|s|st        d«      ‚|r|r|rt        d«      ‚t        |xs ||¬«      }t        |xs ||¬«      } | 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.)ÚllmÚprompt)r%   r$   r.   )r1   r	   )	Úclsr?   r:   r;   r<   r=   r3   r%   r$   s	            r   Úfrom_llmzKuzuQAChain.from_llm|   sš   € ñ ¡#ÜÐWÓXÐXÙ™cÜð óð ñ ™&¡SÜðAóð ô
 Ø’˜#Øô
ˆô #+ØÒ!˜cØ ô#
Ðñ
 ð 
ØØ$;ñ
ð ñ
ð 	
r   c                ó´  — |xs t        j                  «       }|j                  «       }|| j                     }| j                  j                  || j                  j                  dœ|¬«      }t        t        |«      d«      }|j                  dd| j                  ¬«       |j                  |dd| j                  ¬«       | j                  j                  |«      }|j                  d	d| j                  ¬«       |j                  t        |«      dd| j                  ¬«       | 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)ÚcolorrI   rJ   zFull Context:)rD   Úcontext)r
   Úget_noop_managerÚ	get_childr(   r$   Úrunr#   Ú
get_schemar   r   Úon_textrJ   r&   r'   r%   r*   )	r2   ÚinputsÚrun_managerÚ_run_managerrF   rD   Úgenerated_cypherrM   r)   s	            r   Ú_callzKuzuQAChain._call¤   sG  € ð #ÒSÔ&@×&QÑ&QÓ&SˆØ ×*Ñ*Ó,ˆ	Ø˜$Ÿ.™.Ñ)ˆà×7Ñ7×;Ñ;Ø!¨T¯Z©Z×-BÑ-BÑCÈyð <ó 
Ðô
 )¬Ð8HÓ)IÈ8ÓTÐà×ÑÐ0°dÀDÇLÁLÐÔQØ×ÑØ G°¸t¿|¹|ð 	ô 	
ð —*‘*×"Ñ"Ð#3Ó4ˆà×Ñ˜_°$ÀÇÁÐÔMØ×ÑÜ�‹L ¨T¸4¿<¹<ð 	ô 	
ð —‘Ø!¨gÑ6Øð ó 
ˆð —‘ ¨¯©×(@Ñ(@Ñ!AÐBÐBr   )r3   r   ÚreturnÚNone)rX   z	List[str])N)r?   úOptional[BaseLanguageModel]r:   r   r;   r   r<   rZ   r=   rZ   r3   r   rX   r!   )rS   zDict[str, Any]rT   z$Optional[CallbackManagerForChainRun]rX   zDict[str, str])Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r#   Ú__annotations__r(   r*   r,   r0   Úpropertyr6   r9   Úclassmethodr   r   rB   rW   Ú__classcell__)r4   s   @r   r!   r!   7   s  ø… ññ  TÔ*€Eˆ9Ó*Ø%Ó%ØÓØ€IˆsÓØ€J�Óà%*Ð˜dÓ*ðõð  ò ó ð ð òó ðð ð ,0ð%
ð )9Ø,BØ26Ø.2ñ%
à(ð%
ð &ð	%
ð
 *ð%
ð 0ð%
ð ,ð%
ð ð%
ð 
ò%
ó ð%
ðT =Að Càð Cð :ð Cð 
÷	 Cr   r!   )r   r'   r   r'   rX   r'   )r   r'   rX   r'   )r^   Ú
__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!   r.   r   r   Ú<module>rm      sJ   ðÙ &å "ã 	ß ,Ó ,å 'Ý )Ý ?Ý <Ý 5Ý ÷õ <óó+ô$MC�%õ MCr   