CoolFace
Apppublic

pabloescobar18/Final_Assignment_Template

sourceHugging Faceapache-2.0updated 2d agoView on Hugging Face
0likes
agent.cpython-314.pyc67 linesDownload Raw Back to __pycache__
1+
2�j;��.�Rt^RIt^RIt^RIHt^RIHt^RIHt^RI	H3t4^RIHtH
t
^RIHt^RIHt^R	IHt^R5IHtHt^RIHtHtHt^RIHtHt^R
IHt^RI H!t!^RI"H#t#^RI$t$]!4]PJ!R4t&]'!R](!]PJ!R4424]
!RR7t)]$PT!R4]6!^RRR7t+]RRl4t,]RRl4t-]RRl4t.]RRl4t/]R R!l4t0].]/]0],]]!]#.t1R$R"R#llt2R#)%zLangGraph AgentN)�load_dotenv)�
BeautifulSoup)�WikipediaLoader)�WikipediaAPIWrapper)�HumanMessage�
SystemMessage)�tool)�ChatGoogleGenerativeAI)�ChatGroq)�ChatHuggingFace�HuggingFaceEndpoint)�START�
MessagesState�7StateGraph)�ToolNode�tools_condition)�duckduckgo_web_search)�python_interpreter)�transcribe_audio�GROQ_API_KEYz DEBUG: GROQ_API_KEY encontrada: z|You are a helpful AI assistant capable of answering questions using available tools. Be precise and concise in your answers.)�contentzZGAIA_LangGraph_Agent/1.0 (https://huggingface.co/agents-course; student_agent@example.com)i��en)�
top_k_results�doc_content_chars_max�langc�0�V^8�dQhR\R\/#)��query�return��str)�formats"�Ic:\Users\escob\Proyectos Programación\Agente Curso Hugging Face\agent.py�__annotate__r#2s��6�6�C�6�C�6�c��\PV4pV'gRVR2#V# \dpR\T42uRp?#Rp?ii;i)zgSearch Wikipedia for a query and return maximum 2 summary results.8 9Args:10    query: The search query.11z#No results found on Wikipedia for 'z'.zError searching Wikipedia: N)�api_wrapper�run�	Exceptionr )r�results�es&  r"�wikipedia_searchr+1sM��6��/�/�%�(���8���r�B�B�����6�,�S��V�H�5�5��6�s!�'�'�'�A12�
A�A13�A14c�0�V^8�dQhR\R\/#)r�urlrr)r!s"r"r#r#As��,;�,;�3�,;�3�,;r$c���RR/p\P!W^15R7pVP4\VPR4pV!RR.4FpVP4K	VP
RR7pR	VP44pR16V4pRPRV44p\V4R8�dVR
,R,pV# \dp	RT	2uRp	?	#Rp	?	ii;i)aDownloads and extracts clean plain text from a web page or file URL.17 18Use this tool when you need to read the full content of a specific web URL,19plain text file, or source code file. It automatically strips HTML tags,20scripts, and extra whitespace to return readable text.21 22Args:23    url: The full HTTP or HTTPS URL of the webpage or document to fetch (e.g.,24      'https://example.com/file.txt').25 26Returns:27    str: The clean text content of the page (truncated if too long), or an28    error message if the request fails.29z30User-AgentzMozilla/5.0)�headers�timeoutzhtml.parser�script�style�31)�	separatorc3�@"�TFqP4x�K	R#5i�N)�strip)�.0�lines& r"�	<genexpr>�$fetch_web_content.<locals>.<genexpr>_s���8�&7�d�Z�Z�\�\�&7�s�c3�n"�TF+qPR4Fq"P4x�K	K-	R#5i)z  N)�splitr7)r8r9�phrases&  r"r:r;`s&���
N���Z�Z��=M�6�l�l�n�n�=M�n��s�35c3�8"�TFq'gKVx�K	R#5ir6�)r8�chunks& r"r:r;as���>�f�U��5�5�f�s�	�32i�:Ni�Nz/33... [Texto truncado por longitud excesiva] ...z)Error al obtener el contenido de la URL: N)�requests�get�raise_for_statusr�text�	decompose�get_text�34splitlines�join�lenr()35r-r/�response�soupr1rE�lines�chunks�36clean_textr*s37&         r"�fetch_web_contentrP@s��� ;��]�+�G��|�|�C�"�=�H���������
�6�D���'�*�+������,��=�=�4�=�(�D�
9�d�o�o�&7�8�E�
N��
N�F����>�f�>�>�J��:����38�U�39�>�?��40���	�;�
6�q�c�:�:��;�s�CC�C+�C&� C+�&C+c�<�V^8�dQhR\R\R\/#�r�a�br��int)r!s"r"r#r#q�!���������r$c��W,#)zMultiply two numbers.r@�rSrTs&&r"�multiplyrZp���41��,r$c�<�V^8�dQhR\R\R\/#rRrU)r!s"r"r#r#ws!����3��3��3�r$c��W,#)zAdd two numbers.r@rYs&&r"�addr^vr[r$c�<�V^8�dQhR\R\R\/#rRrU)r!s"r"r#r#}rWr$c��W,42#)zSubtract two numbers.r@rYs&&r"�subtractra|r[r$c�$�V^8�dQhR\/#)r�providerr)r!s"r"r#r#�s��J�J�#�Jr$c�a�VR8XdP\P!R4;'g\P!R4p\RRRR7p\VR7pM�VR	8Xd\	R43^R7pM�VR8Xd�\P!R
4pV'g\R4h\PPRR4\PPRR4\PPRR4\RV^R^R7p\R4V'd\R4M\R4hVP\4oRV3RllpRRlp\\4pVPRV4VPRV4VPR\\44VP!\"R4VP!RR4VP%R\&4VP!RR4VP)4#) zBuild the graph�huggingface�HUGGINGFACEHUB_API_TOKEN�HF_TOKENzQwen/Qwen2.5-Coder-32B-Instructztext-generationg�������?)�repo_id�task�temperature)�llm�googlezgemini-2.0-flash)�modelrj�groqrzGROQ_API_KEY no encontrada.�OPENAI_BASE_URLN�OPENAI_API_BASE�OPENAI_API_KEYzopenai/gpt-oss-120bi)rm�api_keyrj�44max_tokens�max_retriesz%Modelo Groq cargado con URL protegidazModelo cargado correctamentez;Invalid provider. Choose 'huggingface', 'google' or 'groq'.c�$�V^8�dQhR\/#�r�state�r)r!s"r"r#�!build_graph.<locals>.__annotate__�s��	;�	;�}�	;r$c�<�VR,p\V4^8�dV^,.VRR,pRSPV4./#)zPAssistant node con control de ventana de contexto para no agotar tokens de Groq.�messagesN�����)rJ�invoke)rwr{�llm_with_toolss& �r"�	assistant�build_graph.<locals>.assistant�sJ����Z� �H��8�}�q���1�+���"�#��.�h���.�.�x�8�9�:�:r$c�$�V^8�dQhR\/#rvrx)r!s"r"r#ry�s��7�7�}�7r$c�0�R\.VR,,/#)zRetriever node simplifiedr{)�sys_msg)rws&r"�	retriever�build_graph.<locals>.retriever�s����	�E�*�$5�5�6�6r$r�r�tools)�os�getenvrrr	�45ValueError�environ�popr46�print�47bind_toolsr�rr�add_noder�add_edger
�add_conditional_edgesr�compile)	rc�hf_token�endpointrk�keyrr��builderr~s	&       @r"�build_graphr��s��������y�y�3�4�M�M��	�	�*�8M�H�"�/����H�48�h�49'�C��8��50 �'9�q�51I�C��6��52�)�)�N�53#�C���4�5�5��J�J�N�N�$�d�+��J�J�N�N�$�d�+��J�J�N�N�#�T�*��#������C�54551�2�56��*�+���E����>�>�%�(�.�	;�	;�7�
�}�%�'�	���;�	�*�	���;�	�*�	���7�H�U�O�,�	���5�+�&�	���;��,�	����_�=�	���7�K�(�	���	�r$)rn)3�__doc__r�rB�dotenvr�bs4r�$langchain_community.document_loadersr�langchain_community.utilitiesr�langchain_core.messagesrr�langchain_core.toolsr�langchain_google_genair	�langchain_groqr57�langchain_huggingfacerr�langgraph.graphr
rr�langgraph.prebuiltrr�tools.web_searchr�tools.code_interpreterr�tools.transcribe_audior�	wikipediar��groq_api_keyr��boolr��set_user_agentr&r+rPrZr^rar�r�r@r$r"�<module>r�s=���	����@�=�?�%�9�#�F�<�<�8�2�5�3���
��y�y��(���&�t�B�I�I�n�,E�'F�&G�H��58�	2���59���I��60"���	
����6��6��,;��,;�^����61����62����6364�3��"2�65�!3�66�	��67J�Jr$