Ë
    µŒjq  ã                  ó®   — 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y)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œ	 	 	 	 	 	 	 	 	 	 	 	 	 dd„«       Z	 d	 	 	 	 	 d d„Zˆ xZS )!Ú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_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     €út/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/langchain_community/chains/graph_qa/sparql.pyr)   zGraphSparqlQAChain.__init__A   s9   ø€ ä‰ÑÑ"˜6Ò"Ø×(Ñ(°Ñ4ÜðWó
ð 
ð 5ó    c                ó   — | j                   gS )z7Return the input keys.

        :meta private:
        )r    )r+   s    r.   Ú
input_keyszGraphSparqlQAChain.input_keysQ   s   € ð —‘ÐÐr/   c                ó    — | j                   g}|S )z8Return the output keys.

        :meta private:
        )r"   )r+   Ú_output_keyss     r.   Úoutput_keyszGraphSparqlQAChain.output_keysY   s   € ð Ÿ™Ð(ˆØÐr/   )Ú	qa_promptÚsparql_select_promptÚsparql_update_promptÚsparql_intent_promptc               ó„   — t        ||¬«      }t        ||¬«      }t        ||¬«      }	t        ||¬«      }
 | d|||	|
dœ|¤ŽS )zInitialize from LLM.)ÚllmÚprompt)r   r   r   r   r'   r	   )Úclsr:   r5   r6   r7   r8   r,   r   r   r   r   s              r.   Úfrom_llmzGraphSparqlQAChain.from_llmb   sb   € ô  ¨IÔ6ˆÜ)1°cÐBVÔ)WÐ&Ü)1°cÐBVÔ)WÐ&Ü&¨3Ð7KÔLÐáð 
ØØ+IØ+IØ 3ñ	
ð
 ñ
ð 	
r/   c                óf  — |xs t        j                  «       }|j                  «       }|| j                     }| j                  j                  d|i|¬«      }|j                  «       }d|v rd|vr| j                  }d}n"d|v rd|vr| j                  }d}nt        d«      ‚|j                  dd| j                  ¬«       |j                  |d	d| j                  ¬
«       |j                  || j                  j                  dœ|¬«      }	|j                  dd| j                  ¬«       |j                  |	d	d| j                  ¬
«       |dk(  r‘| j                  j                  |	«      }
|j                  dd| j                  ¬«       |j                  t        |
«      d	d| j                  ¬
«       | j!                  ||
dœ|¬«      }|| j                   j"                     }n.|dk(  r| j                  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)ÚcolorrC   rD   )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_textrD   r   Ú
get_schemar   r   r   r"   Úupdater   r$   )r+   ÚinputsÚrun_managerÚ_run_managerr?   r;   Ú_intentÚintentÚsparql_generation_chainÚgenerated_sparqlrH   r!   ÚresÚchain_results                 r.   Ú_callzGraphSparqlQAChain._call{   s:  € ð #ÒSÔ&@×&QÑ&QÓ&SˆØ ×*Ñ*Ó,ˆ	Ø˜Ÿ™Ñ'ˆà×*Ñ*×.Ñ.°¸&Ð/AÈYÐ.ÓWˆØ—‘“ˆà�vÑ (°&Ñ"8Ø&*×&IÑ&IÐ#Ø‰FØ˜Ñ H°FÑ$:Ø&*×&IÑ&IÐ#Ø‰FäðIóð ð
 	×ÑÐ1°tÀTÇ\Á\ÐÔRØ×Ñ˜V¨7¸ÀdÇlÁlÐÔSà2×6Ñ6Ø¨¯©×)>Ñ)>Ñ?È9ð 7ó 
Ðð 	×ÑÐ0°dÀDÇLÁLÐÔQØ×ÑØ G°¸t¿|¹|ð 	ô 	
ð �XÒØ—j‘j×&Ñ&Ð'7Ó8ˆGà× Ñ  °dÀDÇLÁLÐ ÔQØ× Ñ Ü�G“ G°¸t¿|¹|ð !ô ð —]‘]Ø!¨gÑ6Ø#ð #ó ˆFð ˜Ÿ™×1Ñ1Ñ2‰CØ�xÒØ�J‰J×ÑÐ.Ô/ØA‰CäÐ=Ó>Ð>à(,¯©¸Ð'=ˆØ×#Ò#Ø2BˆL˜×.Ñ.Ñ/ØÐr/   )r,   r   ÚreturnÚNone)rZ   z	List[str])r:   r   r5   r   r6   r   r7   r   r8   r   r,   r   rZ   r   )N)rP   zDict[str, Any]rQ   z$Optional[CallbackManagerForChainRun]rZ   zDict[str, str])Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ú__annotations__r   r    r"   r$   r%   r)   Úpropertyr1   r4   Úclassmethodr   r   r   r   r=   rY   Ú__classcell__)r-   s   @r.   r   r      s%  ø… ññ  DÔ)€Eˆ8Ó)Ø$,Ó,Ø$,Ó,Ø!Ó!ØÓØ %Ð˜Ó%Ø€IˆsÓØ€J�ÓØ*Ð�cÓ*à%*Ð˜dÓ*ðõð  ò ó ð ð òó ðð ð
 )9Ø3RØ3RØ3Gñ
àð
ð &ð	
ð
 1ð
ð 1ð
ð 1ð
ð ð
ð 
ò
ó ð
ð6 =Að=àð=ð :ð=ð 
÷	=r/   r   N)r_   Ú
__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'   r/   r.   Ú<module>rn      sD   ðñõ #ç ,Ó ,å 'Ý )Ý ?Ý <Ý :Ý ÷ó õ :ô_˜õ _r/   