Ë
    µŒjp'  ã                  ó²   — 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mZ ddlmZ  G d„ de
«      Zy)z Question answering over a graph.é    )ÚannotationsN)ÚAnyÚDictÚListÚOptional)ÚChain©ÚLLMChain)ÚCallbackManagerForChainRun)ÚBaseLanguageModel)ÚBasePromptTemplate)ÚField)ÚAQL_FIX_PROMPTÚAQL_GENERATION_PROMPTÚAQL_QA_PROMPT)ÚArangoGraphc                  óP  ‡ — e Zd ZU dZ ed¬«      Z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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$d„«       Zeeeedœ	 	 	 	 	 	 	 	 	 	 	 d%d „«       Z	 d&	 	 	 	 	 d'd!„Zˆ xZS )(ÚArangoGraphQAChainaæ  Chain for question-answering against a graph by generating AQL 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
   Úaql_generation_chainÚaql_fix_chainÚqa_chainÚqueryÚstrÚ	input_keyÚresultÚ
output_keyé
   ÚintÚtop_kÚ Úaql_examplesFÚboolÚreturn_aql_queryÚreturn_aql_resulté   Úmax_aql_generation_attemptsÚ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     €úv/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/langchain_community/chains/graph_qa/arangodb.pyr-   zArangoGraphQAChain.__init__K   s9   ø€ ä‰ÑÑ"˜6Ò"Ø×(Ñ(°Ñ4ÜðWó
ð 
ð 5ó    c                ó   — | j                   gS ©N)r   ©r/   s    r2   Ú
input_keyszArangoGraphQAChain.input_keys[   s   € à—‘ÐÐr3   c                ó   — | j                   gS r5   )r   r6   s    r2   Úoutput_keyszArangoGraphQAChain.output_keys_   s   € à—‘Ð Ð r3   c                 ó   — y)NÚgraph_aql_chainr+   r6   s    r2   Ú_chain_typezArangoGraphQAChain._chain_typec   s   € à r3   )Ú	qa_promptÚaql_generation_promptÚaql_fix_promptc               óh   — t        ||¬«      }t        ||¬«      }t        ||¬«      } | d|||dœ|¤ŽS )zInitialize from LLM.)ÚllmÚprompt)r   r   r   r+   r	   )	ÚclsrA   r=   r>   r?   r0   r   r   r   s	            r2   Úfrom_llmzArangoGraphQAChain.from_llmg   sO   € ô  ¨IÔ6ˆÜ'¨CÐ8MÔNÐÜ  S°Ô@ˆáð 
ØØ!5Ø'ñ
ð ñ	
ð 	
r3   c                óê  — |xs t        j                  «       }|j                  «       }|| j                     }| j                  j                  | j                  j                  | j                  |dœ|¬«      }d}d}d}	d}
|	�€|
| j                  dz   k  �rd}t        j                  ||t        j                  «      }|sK|j                  dd| j                  ¬	«       |j                  |d
d| j                  ¬«       t        d|› �«      ‚|d   }|j                  d|
› d�| j                  ¬«       |j                  |dd| j                  ¬«       ddlm} 	 | j                  j%                  || j&                  «      }	|
dz  }
|	€|
| j                  dz   k  r�Œ|	€d|› d�}t        |«      ‚|j                  dd| j                  ¬	«       |j                  t-        |	«      dd| j                  ¬«       | j/                  | j                  j                  |||	dœ|¬«      }| j0                  || j.                  j0                     i}| j2                  r||d<   | j4                  r|	|d<   |S # |$ r‰}|j(                  }|j                  dd| j                  ¬	«       |j                  |dd| j                  ¬«       | j*                  j                  | j                  j                  ||dœ|¬«      }Y d}~�Œrd}~ww xY w)aw  
        Generate an AQL statement from user input, use it retrieve a response
        from an ArangoDB Database instance, and respond to the user input
        in natural language.

        Users can modify the following ArangoGraphQAChain Class Variables:

        :var top_k: The maximum number of AQL Query Results to return
        :type top_k: int

        :var aql_examples: A set of AQL Query Examples that are passed to
            the AQL Generation Prompt Template to promote few-shot-learning.
            Defaults to an empty string.
        :type aql_examples: str

        :var return_aql_query: Whether to return the AQL Query in the
            output dictionary. Defaults to False.
        :type return_aql_query: bool

        :var return_aql_result: Whether to return the AQL Query in the
            output dictionary. Defaults to False
        :type return_aql_result: bool

        :var max_aql_generation_attempts: The maximum amount of AQL
            Generation attempts to be made prior to raising the last
            AQL Query Execution Error. Defaults to 3.
        :type max_aql_generation_attempts: int
        )Ú
adb_schemar#   Ú
user_input)Ú	callbacksr"   Né   z```(?i:aql)?(.*?)```zInvalid Response: Ú
)ÚendÚverboseÚred)ÚcolorrK   rL   zResponse is Invalid: r   zAQL Query (z):)rL   Úgreen)ÚAQLQueryExecuteErrorzAQL Query Execution Error: Úyellowz

)rF   Ú	aql_queryÚ	aql_errorz¦
                Maximum amount of AQL Query Generation attempts reached.
                Unable to execute the AQL Query due to the following error:
                z
            zAQL Result:)rF   rG   rR   Ú
aql_resultrR   rT   )r   Úget_noop_managerÚ	get_childr   r   Úrunr   Úschemar#   r(   ÚreÚfindallÚDOTALLÚon_textrL   r.   ÚarangorP   r   r!   Úerror_messager   r   r   r   r%   r&   )r/   ÚinputsÚrun_managerÚ_run_managerrH   rG   Úaql_generation_outputrR   rS   rT   Úaql_generation_attemptÚpatternÚmatchesrP   ÚeÚmr   s                    r2   Ú_callzArangoGraphQAChain._call}   sA  € ðB #ÒSÔ&@×&QÑ&QÓ&SˆØ ×*Ñ*Ó,ˆ	Ø˜DŸN™NÑ+ˆ
ð !%× 9Ñ 9× =Ñ =à"Ÿj™j×/Ñ/Ø $× 1Ñ 1Ø(ñð
  ð !>ó !
Ðð ˆ	Øˆ	Øˆ
Ø!"Ðð ÑØ&¨×)IÑ)IÈAÑ)MÓMð .ˆGÜ—j‘j Ð*?ÄÇÁÓKˆGÙØ×$Ñ$Ø(¨d¸D¿L¹Lð %ô ð ×$Ñ$Ø)°¸DÈ$Ï,É,ð %ô ô !Ð#8Ð9NÐ8OÐ!PÓQÐQà ™
ˆIð × Ñ ØÐ4Ð5°RÐ8À$Ç,Á,ð !ô ð × Ñ Ø ¨d¸D¿L¹Lð !ô õ 4ðØ!ŸZ™Z×-Ñ-¨i¸¿¹ÓD�
ð2 # aÑ'Ð"ðq ÐØ&¨×)IÑ)IÈAÑ)MÔMðr Ððð �ð ðˆAô
 ˜Q“-Ðà×Ñ˜]°¸d¿l¹lÐÔKØ×ÑÜ�
‹O 7°¸d¿l¹lð 	ô 	
ð —‘à"Ÿj™j×/Ñ/Ø(Ø&Ø(ñ	ð  ð ó 
ˆð —/‘/ 6¨$¯-©-×*BÑ*BÑ#CÐDˆà× Ò Ø"+ˆF�;Ñà×!Ò!Ø#-ˆF�<Ñ àˆøð{ (ò ØŸO™O�	à×$Ñ$Ø1°tÀTÇ\Á\ð %ô ð ×$Ñ$Ø X°6À4Ç<Á<ð %ô ð )-×(:Ñ(:×(>Ñ(>à&*§j¡j×&7Ñ&7Ø%.Ø%.ñð
 (ð )?ó )Ö%ûðús   Å&I$ É$K2É)A>K-Ë-K2)r0   r   ÚreturnÚNone)ri   z	List[str])ri   r   )rA   r   r=   r   r>   r   r?   r   r0   r   ri   r   r5   )r_   úDict[str, Any]r`   z$Optional[CallbackManagerForChainRun]ri   rk   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ú__annotations__r   r   r!   r#   r%   r&   r(   r)   r-   Úpropertyr7   r9   r<   Úclassmethodr   r   r   rD   rh   Ú__classcell__)r1   s   @r2   r   r      sR  ø… ññ  tÔ,€Eˆ;Ó,Ø"Ó"ØÓØÓØ€IˆsÓØ€J�Óð €Eˆ3ƒOð €L�#Óð #Ð�dÓ"ð $Ð�tÓ#ð ()Ð Ó(à%*Ð˜dÓ*ðõð  ò ó ð ð ò!ó ð!ð ò!ó ð!ð ð
 )6Ø4IØ-;ñ
àð
ð &ð	
ð
  2ð
ð +ð
ð ð
ð 
ò
ó ð
ð0 =AðTàðTð :ðTð 
÷	Tr3   r   )ro   Ú
__future__r   rY   Ú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   r   Ú)langchain_community.graphs.arangodb_graphr   r   r+   r3   r2   Ú<module>r~      sD   ðÙ &å "ã 	ß ,Ó ,å 'Ý )Ý ?Ý <Ý 5Ý ÷ñ õ
 Bôz˜õ zr3   