a
    bgb                     @  s   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mZ G dd de	ZdS )z Question answering over a graph.    )annotations)AnyDictListOptional)ChainLLMChain)CallbackManagerForChainRun)BaseLanguageModel)BasePromptTemplate)Field)ENTITY_EXTRACTION_PROMPTGRAPH_QA_PROMPT)NetworkxEntityGraphget_entitiesc                   @  s   e Zd ZU dZeddZded< ded< ded< d	Zd
ed< dZd
ed< e	ddddZ
e	ddddZeeefddddd dddZd!dddddd ZdS )"GraphQAChaina  Chain for question-answering against a graph.

    *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	   entity_extraction_chainqa_chainquerystr	input_keyresult
output_keyz	List[str])returnc                 C  s   | j gS )z,Input keys.

        :meta private:
        )r   )self r   v/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/chains/graph_qa/base.py
input_keys*   s    zGraphQAChain.input_keysc                 C  s   | j g}|S )z-Output keys.

        :meta private:
        )r   )r   Z_output_keysr   r   r   output_keys2   s    zGraphQAChain.output_keysr   r   r   )llm	qa_promptentity_promptkwargsr   c                 K  s,   t ||d}t ||d}| f ||d|S )zInitialize from LLM.)r"   prompt)r   r   r   )clsr"   r#   r$   r%   r   Zentity_chainr   r   r   from_llm;   s    	zGraphQAChain.from_llmNzDict[str, Any]z$Optional[CallbackManagerForChainRun]zDict[str, str])inputsrun_managerr   c                 C  s   |p
t  }|| j }| j|}|jdd| jd |j|dd| jd t|}d}g }|D ]}	|| j	
|	 q\d|}|jdd| jd |j|dd| jd | j||d| d	}
| j|
| jj iS )
z3Extract entities, look up info and answer question.zEntities Extracted:
)endverbosegreen)colorr,   r-    zFull Context:)questioncontext)	callbacks)r
   Zget_noop_managerr   r   runZon_textr-   r   extendr   Zget_entity_knowledgejoinr   Z	get_childr   )r   r)   r*   Z_run_managerr1   Zentity_stringentitiesr2   Zall_tripletsentityr   r   r   r   _callM   s(    


zGraphQAChain._call)N)__name__
__module____qualname____doc__r   r   __annotations__r   r   propertyr    r!   classmethodr   r   r(   r9   r   r   r   r   r      s    
 r   N)r=   
__future__r   typingr   r   r   r   Zlangchain.chains.baser   Zlangchain.chains.llmr	   Z langchain_core.callbacks.managerr
   Zlangchain_core.language_modelsr   Zlangchain_core.promptsr   Zpydanticr   Z+langchain_community.chains.graph_qa.promptsr   r   Z)langchain_community.graphs.networkx_graphr   r   r   r   r   r   r   <module>   s   