
    Ngq                        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mZmZ ddlmZ  G d de	          ZdS )z;
Question answering over an RDF or OWL graph using SPARQL.
    )annotations)AnyDictListOptional)ChainLLMChain)CallbackManagerForChainRun)BaseLanguageModel)BasePromptTemplate)Field)SPARQL_GENERATION_SELECT_PROMPTSPARQL_GENERATION_UPDATE_PROMPTSPARQL_INTENT_PROMPTSPARQL_QA_PROMPT)RdfGraphc                      e Zd ZU dZ ed          Zded<   ded<   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Z
ded<   dZded<   	 d. fdZed/d            Zed/d            Zeeeeedd0d&            Z	 d1d2d-Z xZS )3GraphSparqlQAChaina  Question-answering against an RDF or OWL graph by generating SPARQL 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
   sparql_generation_select_chainsparql_generation_update_chainsparql_intent_chainqa_chainFboolreturn_sparql_queryquerystr	input_keyresult
output_keysparql_querysparql_query_key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     f/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/chains/graph_qa/sparql.pyr,   zGraphSparqlQAChain.__init__A   sN    ""6"""(44W
 
 
 54    	List[str]c                    | j         gS )z7Return the input keys.

        :meta private:
        )r    )r.   s    r0   
input_keyszGraphSparqlQAChain.input_keysQ   s     r1   c                    | j         g}|S )z8Return the output keys.

        :meta private:
        )r"   )r.   _output_keyss     r0   output_keyszGraphSparqlQAChain.output_keysY   s     (r1   )	qa_promptsparql_select_promptsparql_update_promptsparql_intent_promptllmr   r8   r   r9   r:   r;   c                   t          ||          }t          ||          }t          ||          }	t          ||          }
 | d|||	|
d|S )zInitialize from LLM.)r<   prompt)r   r   r   r   r*   r	   )clsr<   r8   r9   r:   r;   r&   r   r   r   r   s              r0   from_llmzGraphSparqlQAChain.from_llmb   s     I666)1cBV)W)W)W&)1cBV)W)W)W&&37KLLLs 
+I+I 3	
 

 
 
 	
r1   NinputsDict[str, Any]run_manager$Optional[CallbackManagerForChainRun]Dict[str, str]c                2   |pt          j                    }|                                }|| j                 }| j                            d|i|          }|                                }d|v rd|vr
| j        }d}n!d|v rd|vr
| j        }d}nt          d          |
                    dd| j                   |
                    |d	d| j        
           |                    || j        j        d|          }	|
                    dd| j                   |
                    |	d	d| j        
           |dk    r| j                            |	          }
|
                    dd| j                   |
                    t          |
          d	d| j        
           |                     ||
d|          }|| j        j                 }n2|dk    r| j                            |	           d}nt          d          | j        |i}| j        r
|	|| j        <   |S )zt
        Generate SPARQL query, use it to retrieve a response from the gdb and answer
        the question.
        r>   )	callbacksSELECTUPDATEzuI am sorry, but this prompt seems to fit none of the currently supported SPARQL query types, i.e., SELECT and UPDATE.zIdentified intent:
)endverbosegreen)colorrK   rL   )r>   schemazGenerated SPARQL:zFull Context:)r>   contextz-Successfully inserted triples into the graph.zUnsupported SPARQL query type.)r   get_noop_manager	get_childr    r   runstripr   r   r-   on_textrL   r   
get_schemar   r   r   r"   updater   r$   )r.   rA   rC   _run_managerrG   r>   _intentintentsparql_generation_chaingenerated_sparqlrP   r!   reschain_results                 r0   _callzGraphSparqlQAChain._call{   s    #S&@&Q&S&S **,,	'*..&/AY.WWv(&"8"8&*&I#FFHF$:$:&*&I#FFI  
 	1tT\RRRV7dlSSS266)>??9 7 
 
 	0dDLQQQGt| 	 	
 	
 	
 Xj&&'788G  dDL QQQ  GGt| !    ]]!g66# #  F 12CCxJ.///ACC=>>>(,'=# 	C2BL./r1   )r&   r   r'   r(   )r'   r2   )r<   r   r8   r   r9   r   r:   r   r;   r   r&   r   r'   r   )N)rA   rB   rC   rD   r'   rE   )__name__
__module____qualname____doc__r   r   __annotations__r   r    r"   r$   r%   r,   propertyr4   r7   classmethodr   r   r   r   r@   r_   __classcell__)r/   s   @r0   r   r      s          eD)))E)))),,,,,,,,!!!! %%%%%IJ*****%*****             X     X 
 )93R3R3G
 
 
 
 
 [
6 =A= = = = = = = = =r1   r   N)rc   
__future__r   typingr   r   r   r   langchain.chains.baser   langchain.chains.llmr
   langchain_core.callbacksr   langchain_core.language_modelsr   langchain_core.prompts.baser   pydanticr   +langchain_community.chains.graph_qa.promptsr   r   r   r   $langchain_community.graphs.rdf_graphr   r   r*   r1   r0   <module>rr      sA    # " " " " " , , , , , , , , , , , , ' ' ' ' ' ' ) ) ) ) ) ) ? ? ? ? ? ? < < < < < < : : : : : :                  : 9 9 9 9 9_ _ _ _ _ _ _ _ _ _r1   