
    Ng(                        d Z ddlm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  G d de	          ZdS )z Question answering over a graph.    )annotations)AnyDictListOptional)ChainLLMChain)CallbackManagerForChainRun)BaseLanguageModel)BasePromptTemplate)Field)CYPHER_QA_PROMPTGREMLIN_GENERATION_PROMPT)	HugeGraphc                       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eedd)d            Z	 d*d+d&Z xZS ),HugeGraphQAChaina  Chain for question-answering against a graph by generating gremlin statements.

    *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
   gremlin_generation_chainqa_chainquerystr	input_keyresult
output_keyFboolallow_dangerous_requestskwargsr   return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     i/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/chains/graph_qa/hugegraph.pyr%   zHugeGraphQAChain.__init__9   sN    ""6"""(44W
 
 
 54    	List[str]c                    | j         gS )z,Input keys.

        :meta private:
        )r   )r'   s    r)   
input_keyszHugeGraphQAChain.input_keysI   s     r*   c                    | j         g}|S )z-Output keys.

        :meta private:
        )r   )r'   _output_keyss     r)   output_keyszHugeGraphQAChain.output_keysQ   s     (r*   )	qa_promptgremlin_promptllmr   r1   r   r2   c               \    t          ||          }t          ||          } | d||d|S )zInitialize from LLM.)r3   prompt)r   r   r#   r	   )clsr3   r1   r2   r   r   r   s          r)   from_llmzHugeGraphQAChain.from_llmZ   sW     I666#+N#K#K#K s 
%=
 
 
 
 	
r*   NinputsDict[str, Any]run_manager$Optional[CallbackManagerForChainRun]Dict[str, str]c                `   |pt          j                    }|                                }|| j                 }| j                            || j        j        d|          }|                    dd| j	                   |                    |dd| j	                   | j        
                    |          }|                    dd| j	                   |                    t          |          dd| j	                   |                     ||d	|          }| j        || j        j                 iS )
zHGenerate gremlin statement, use it to look up in db and answer question.)questionschema)	callbackszGenerated gremlin:
)endverbosegreen)colorrB   rC   zFull Context:)r>   context)r   get_noop_manager	get_childr   r   runr   
get_schemaon_textrC   r   r   r   r   )	r'   r8   r:   _run_managerr@   r>   generated_gremlinrF   r   s	            r)   _callzHugeGraphQAChain._callm   sQ    #S&@&Q&S&S **,,	$.) 9==!TZ-BCCy > 
 
 	1tT\RRRW$ 	 	
 	
 	
 *""#455_$MMMLLT4< 	 	
 	
 	
 !g66  
 
 (@!ABBr*   )r   r   r    r!   )r    r+   )
r3   r   r1   r   r2   r   r   r   r    r   )N)r8   r9   r:   r;   r    r<   )__name__
__module____qualname____doc__r   r   __annotations__r   r   r   r%   propertyr-   r0   classmethodr   r   r7   rN   __classcell__)r(   s   @r)   r   r      sT          uT***E****&&&&IJ%*****             X     X 
 )9-F
 
 
 
 
 [
* =AC C C C C C C C Cr*   r   N)rR   
__future__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.hugegraphr   r   r#   r*   r)   <module>ra      s1   & & " " " " " " , , , , , , , , , , , , ' ' ' ' ' ' ) ) ) ) ) ) ? ? ? ? ? ? < < < < < < 5 5 5 5 5 5              ; : : : : :uC uC uC uC uCu uC uC uC uC uCr*   