§
    šŠtj¬R  ã                   óR  — d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	m
Z
mZmZ d dlmZ d dlmZ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mZmZ defd„Zdee ef         de!fd„Z"	 	 	 	 dde de#de#dede
ee ef                  de!fd„Z$de de defd„Z% G d„ dee¦  «        Z&dS )é    N)Údeepcopy)ÚPath)ÚAnyÚDictÚListÚOptionalÚSequenceÚUnion)Úwarn_deprecated)ÚAgentActionÚAgentFinish)ÚBaseCallbackHandler)Ú	LLMResult©Úguard_import)ÚBaseMetadataCallbackHandlerÚflatten_dictÚhash_stringÚimport_pandasÚimport_spacyÚimport_textstatÚreturnc                  ó    — t          d¦  «        S )zJImport the wandb python package and raise an error if it is not installed.Úwandbr   © ó    új/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/langchain_community/callbacks/wandb_callback.pyÚimport_wandbr      s   € å˜Ñ Ô Ð r   Ú	json_pathc                 ó~   — t          | d¦  «        5 }t          j        |¦  «        }ddd¦  «         n# 1 swxY w Y   |S )z·Load json file to a dictionary.

    Parameters:
        json_path (str): The path to the json file.

    Returns:
        (dict): The dictionary representation of the json file.
    ÚrN)ÚopenÚjsonÚload)r   ÚfÚdatas      r   Úload_json_to_dictr'      sx   € õ 
ˆi˜Ñ	Ô	ð  ÝŒy˜‰|Œ|ˆðð ð ñ ô ð ð ð ð ð ð øøøð ð ð ð à€Ks   ‘2²6¹6TÚtextÚcomplexity_metricsÚ	visualizeÚnlpÚ
output_dirc                 óÚ  — i }t          ¦   «         }t          ¦   «         }t          ¦   «         }|�rwi d|                     | ¦  «        “d|                     | ¦  «        “d|                     | ¦  «        “d|                     | ¦  «        “d|                     | ¦  «        “d|                     | ¦  «        “d| 	                    | ¦  «        “d| 
                    | ¦  «        “d	|                     | ¦  «        “d
|                     | ¦  «        “d|                     | ¦  «        “d|                     | ¦  «        “d|                     | ¦  «        “d|                     | ¦  «        “d|                     | ¦  «        “d|                     | ¦  «        “}	|                     |	¦  «         |�rA|�r>|��; || ¦  «        }
|j                             |
ddd¬¦  «        }t-          |t/          d| › �¦  «        dz   ¦  «        }|                     dd¬¦  «                             |¦  «         |j                             |
ddd¬¦  «        }t-          |t/          d| › �¦  «        dz   ¦  «        }|                     dd¬¦  «                             |¦  «         |                     t7          |¦  «        ¦  «        |                     t7          |¦  «        ¦  «        dœ}|                     |¦  «         |S )a  Analyze text using textstat and spacy.

    Parameters:
        text (str): The text to analyze.
        complexity_metrics (bool): Whether to compute complexity metrics.
        visualize (bool): Whether to visualize the text.
        nlp (spacy.lang): The spacy language model to use for visualization.
        output_dir (str): The directory to save the visualization files to.

    Returns:
        `dict` containing the complexity metrics and visualization
            files serialized in a wandb.Html element.
    Úflesch_reading_easeÚflesch_kincaid_gradeÚ
smog_indexÚcoleman_liau_indexÚautomated_readability_indexÚdale_chall_readability_scoreÚdifficult_wordsÚlinsear_write_formulaÚgunning_fogÚtext_standardÚfernandez_huertaÚszigriszt_pazosÚgutierrez_poliniÚcrawfordÚgulpease_indexÚosmanNÚdepFT)ÚstyleÚjupyterÚpagezdep-z.htmlÚwzutf-8)ÚencodingÚentzent-)Údependency_treeÚentities)r   r   r   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   ÚupdateÚdisplacyÚrenderr   r   r"   ÚwriteÚHtmlÚstr)r(   r)   r*   r+   r,   ÚrespÚtextstatr   ÚspacyÚtext_complexity_metricsÚdocÚdep_outÚdep_output_pathÚent_outÚent_output_pathÚtext_visualizationss                   r   Úanalyze_textrW   *   s-  € ð( €DÝÑ Ô €HÝ‰NŒN€EÝ‰NŒN€EØñ -ð#
Ø! 8×#?Ò#?ÀÑ#EÔ#Eð#
à" H×$AÒ$AÀ$Ñ$GÔ$Gð#
ð ˜(×-Ò-¨dÑ3Ô3ð#
ð ! (×"=Ò"=¸dÑ"CÔ"Cð	#
ð
 *¨8×+OÒ+OÐPTÑ+UÔ+Uð#
ð +¨H×,QÒ,QÐRVÑ,WÔ,Wð#
ð ˜x×7Ò7¸Ñ=Ô=ð#
ð $ X×%CÒ%CÀDÑ%IÔ%Ið#
ð ˜8×/Ò/°Ñ5Ô5ð#
ð ˜X×3Ò3°DÑ9Ô9ð#
ð  × 9Ò 9¸$Ñ ?Ô ?ð#
ð ˜x×7Ò7¸Ñ=Ô=ð#
ð  × 9Ò 9¸$Ñ ?Ô ?ð#
ð ˜×)Ò)¨$Ñ/Ô/ð#
ð ˜h×5Ò5°dÑ;Ô;ð#
ð  �X—^’^ DÑ)Ô)ð!#
Ðð$ 	�ŠÐ+Ñ,Ô,Ð,àñ )�Sñ )˜ZÑ3Øˆc�$‰iŒiˆà”.×'Ò'¨°5À%ÈdÐ'ÑSÔSˆÝ˜z­;°}¸d°}°}Ñ+EÔ+EÈÑ+OÑPÔPˆØ×Ò˜S¨7ÐÑ3Ô3×9Ò9¸'ÑBÔBÐBà”.×'Ò'¨°5À%ÈdÐ'ÑSÔSˆÝ˜z­;°}¸d°}°}Ñ+EÔ+EÈÑ+OÑPÔPˆØ×Ò˜S¨7ÐÑ3Ô3×9Ò9¸'ÑBÔBÐBð  %Ÿzšz­#¨oÑ*>Ô*>Ñ?Ô?ØŸ
š
¥3 Ñ#7Ô#7Ñ8Ô8ð
ð 
Ðð 	�ŠÐ'Ñ(Ô(Ð(à€Kr   ÚpromptÚ
generationc                 ó²   — t          ¦   «         }|                      dd¦  «        }|                     dd¦  «        }|                     d|› d|› d�d¬¦  «        S )zÌConstruct an html element from a prompt and a generation.

    Parameters:
        prompt (str): The prompt.
        generation (str): The generation.

    Returns:
        (wandb.Html): The html element.ú
z<br>z
    <p style="color:black;">z>:</p>
    <blockquote>
      <p style="color:green;">
        z"
      </p>
    </blockquote>
    F)Úinject)r   ÚreplacerK   )rX   rY   r   Úformatted_promptÚformatted_generations        r   Ú)construct_html_from_prompt_and_generationr`   k   s}   € õ ‰NŒN€EØ—~’~ d¨FÑ3Ô3ÐØ%×-Ò-¨d°FÑ;Ô;Ðà�:Š:ð	Ø-ð	ð 	ð 
ð		ð 	ð 	ð ð ñ 
ô 
ð 
r   c                   ó   ‡ — e Zd ZdZ	 	 	 	 	 	 	 	 	 	 d1dee         dee         dee         dee         d	ee         d
ee         dee         dedededdfˆ fd„Zde	fd„Z
de	eef         dee         deddfd„Zdededdfd„Zdededdfd„Zdededdfd„Zde	eef         de	eef         deddfd„Zde	eef         deddfd„Zdededdfd „Zde	eef         d!ededdfd"„Zd#ededdfd$„Zdededdfd%„Zd&ededdfd'„Zd(ededdfd)„Zd*ededefd+„Zdefd,„Z	 	 	 	 	 	 	 	 	 	 	 	 d2d.ed/ed(edee         dee         dee         dee         d	ee         d
ee         dee         dee         dee         ddfd0„Zˆ xZ S )3ÚWandbCallbackHandlera€  Callback Handler that logs to Weights and Biases.

    Parameters:
        job_type (str): The type of job.
        project (str): The project to log to.
        entity (str): The entity to log to.
        tags (list): The tags to log.
        group (str): The group to log to.
        name (str): The name of the run.
        notes (str): The notes to log.
        visualize (bool): Whether to visualize the run.
        complexity_metrics (bool): Whether to log complexity metrics.
        stream_logs (bool): Whether to stream callback actions to W&B

    This handler will utilize the associated callback method called and formats
    the input of each callback function with metadata regarding the state of LLM run,
    and adds the response to the list of records for both the {method}_records and
    action. It then logs the response using the run.log() method to Weights and Biases.
    NÚlangchain_callback_demoFÚjob_typeÚprojectÚentityÚtagsÚgroupÚnameÚnotesr*   r)   Ústream_logsr   c           	      óÀ  •— t          ¦   «         }t          ¦   «          t          ¦   «          t          ¦   «         }t	          ¦   «                              ¦   «          || _        || _        || _        || _	        || _
        || _        || _        || _        |	| _        |
| _        t!          j        ¦   «         | _        |                     | j        | j        | j        | j	        | j
        | j        | j        ¬¦  «        | _        d}|                     |d¬¦  «         g | _        g | _        |	| _        || _        |                     d¦  «        | _        t5          dddd¬	¦  «         d
S )zInitialize callback handler.)rd   re   rf   rg   rh   ri   rj   z˜DEPRECATION: The `WandbCallbackHandler` will soon be deprecated in favor of the `WandbTracer`. Please update your code to use the `WandbTracer` instead.F)ÚrepeatÚen_core_web_smz0.3.8zøPlease use the WeaveTracer instead of the WandbCallbackHandler. The WeaveTracer is a more flexible and powerful tool for logging and tracing your LangChain callables.Find more information at https://weave-docs.wandb.ai/guides/integrations/langchainz²Please instantiate the WeaveTracer from weave.integrations.langchain import WeaveTracer .For autologging simply use weave.init() and log all traces from your LangChain callables.)ÚpendingÚmessageÚalternativeN)r   r   r   r   ÚsuperÚ__init__rd   re   rf   rg   rh   ri   rj   r*   r)   rk   ÚtempfileÚTemporaryDirectoryÚtemp_dirÚinitÚrunÚtermwarnÚcallback_columnsÚaction_recordsr$   r+   r   )Úselfrd   re   rf   rg   rh   ri   rj   r*   r)   rk   r   rO   ÚwarningÚ	__class__s                 €r   rs   zWandbCallbackHandler.__init__š   sk  ø€ õ ‘”ˆÝ‰ŒˆÝÑÔÐÝ‘”ˆÝ‰Œ×ÒÑÔÐà ˆŒØˆŒØˆŒØˆŒ	ØˆŒ
ØˆŒ	ØˆŒ
Ø"ˆŒØ"4ˆÔØ&ˆÔå Ô3Ñ5Ô5ˆŒØ—:’:Ø”]Ø”LØ”;Ø”Ø”*Ø”Ø”*ð ñ 
ô 
ˆŒðð 	ð
 	�ŠØØð 	ñ 	
ô 	
ð 	
ð ')ˆÔØ$&ˆÔØ"4ˆÔØ"ˆŒØ—:’:Ð.Ñ/Ô/ˆŒÝØØðeð1ð	
ñ 	
ô 	
ð 	
ð 	
ð 	
r   c                 ó$   — d„ | j         D ¦   «         S )Nc                 ó   — i | ]}|d “ŒS )Nr   )Ú.0Úks     r   ú
<dictcomp>z3WandbCallbackHandler._init_resp.<locals>.<dictcomp>ä   s   € Ð7Ð7Ð7˜A��4Ð7Ð7Ð7r   )rz   )r|   s    r   Ú
_init_respzWandbCallbackHandler._init_respã   s   € Ø7Ð7 Ô!6Ð7Ñ7Ô7Ð7r   Ú
serializedÚpromptsÚkwargsc                 ó*  — | xj         dz  c_         | xj        dz  c_        | xj        dz  c_        |                      ¦   «         }|                     ddi¦  «         |                     t          |¦  «        ¦  «         |                     |                      ¦   «         ¦  «         |D ]k}t          |¦  «        }||d<   | j         	                    |¦  «         | j
         	                    |¦  «         | j        r| j                             |¦  «         ŒldS )zRun when LLM starts.é   ÚactionÚon_llm_startr†   N)ÚstepÚ
llm_startsÚstartsr„   rG   r   Úget_custom_callback_metar   Úon_llm_start_recordsÚappendr{   rk   rx   Úlog)r|   r…   r†   r‡   rM   rX   Úprompt_resps          r   r‹   z!WandbCallbackHandler.on_llm_startæ   s  € ð 	ˆ	Œ	�Q‰ˆ	Œ	ØˆŒ˜1ÑˆŒØˆŒ�qÑˆŒà�ŠÑ Ô ˆØ�Š�X˜~Ð.Ñ/Ô/Ð/Ø�Š•L Ñ,Ô,Ñ-Ô-Ð-Ø�Š�D×1Ò1Ñ3Ô3Ñ4Ô4Ð4àð 	*ð 	*ˆFÝ" 4™.œ.ˆKØ%+ˆK˜	Ñ"ØÔ%×,Ò,¨[Ñ9Ô9Ð9ØÔ×&Ò& {Ñ3Ô3Ð3ØÔð *Ø”—’˜[Ñ)Ô)Ð)øð	*ð 	*r   Útokenc                 óš  — | xj         dz  c_         | xj        dz  c_        |                      ¦   «         }|                     d|dœ¦  «         |                     |                      ¦   «         ¦  «         | j                             |¦  «         | j                             |¦  «         | j        r| j	         
                    |¦  «         dS dS )z#Run when LLM generates a new token.r‰   Úon_llm_new_token)rŠ   r”   N)rŒ   Úllm_streamsr„   rG   r�   Úon_llm_token_recordsr‘   r{   rk   rx   r’   )r|   r”   r‡   rM   s       r   r–   z%WandbCallbackHandler.on_llm_new_tokenû   sÊ   € àˆ	Œ	�Q‰ˆ	Œ	ØÐÔ˜AÑÐÔà�ŠÑ Ô ˆØ�ŠÐ1¸EÐBÐBÑCÔCÐCØ�Š�D×1Ò1Ñ3Ô3Ñ4Ô4Ð4àÔ!×(Ò(¨Ñ.Ô.Ð.ØÔ×"Ò" 4Ñ(Ô(Ð(ØÔð 	ØŒH�LŠL˜ÑÔÐÐÐð	ð 	r   Úresponsec                 ó4  — | xj         dz  c_         | xj        dz  c_        | xj        dz  c_        |                      ¦   «         }|                     ddi¦  «         |                     t          |j        pi ¦  «        ¦  «         |                     |                      ¦   «         ¦  «         |j        D ]ä}|D ]ß}t          |¦  «        }|                     t          | 
                    ¦   «         ¦  «        ¦  «         |                     t          |j        | j        | j        | j        | j        j        ¬¦  «        ¦  «         | j                             |¦  «         | j                             |¦  «         | j        r| j                             |¦  «         ŒàŒådS )zRun when LLM ends running.r‰   rŠ   Ú
on_llm_end)r)   r*   r+   r,   N)rŒ   Úllm_endsÚendsr„   rG   r   Ú
llm_outputr�   Úgenerationsr   ÚdictrW   r(   r)   r*   r+   rv   ri   Úon_llm_end_recordsr‘   r{   rk   rx   r’   )r|   r™   r‡   rM   rŸ   rY   Úgeneration_resps          r   r›   zWandbCallbackHandler.on_llm_end	  sŽ  € àˆ	Œ	�Q‰ˆ	Œ	ØˆŒ˜ÑˆŒØˆ	Œ	�Q‰ˆ	Œ	à�ŠÑ Ô ˆØ�Š�X˜|Ð,Ñ-Ô-Ð-Ø�Š•L Ô!4Ð!:¸Ñ;Ô;Ñ<Ô<Ð<Ø�Š�D×1Ò1Ñ3Ô3Ñ4Ô4Ð4à#Ô/ð 	2ð 	2ˆKØ)ð 2ð 2�
Ý"*¨4¡.¤.�Ø×&Ò&¥|°J·O²OÑ4EÔ4EÑ'FÔ'FÑGÔGÐGØ×&Ò&Ý Ø"œØ+/Ô+BØ"&¤.Ø œHØ#'¤=Ô#5ðñ ô ñô ð ð Ô'×.Ò.¨Ñ?Ô?Ð?ØÔ#×*Ò*¨?Ñ;Ô;Ð;ØÔ#ð 2Ø”H—L’L Ñ1Ô1Ð1øð2ð	2ð 	2r   Úerrorc                 óF   — | xj         dz  c_         | xj        dz  c_        dS )zRun when LLM errors.r‰   N©rŒ   Úerrors©r|   r£   r‡   s      r   Úon_llm_errorz!WandbCallbackHandler.on_llm_error&  ó%   € àˆ	Œ	�Q‰ˆ	Œ	ØˆŒ�qÑˆŒˆˆr   Úinputsc                 ó¦  — | xj         dz  c_         | xj        dz  c_        | xj        dz  c_        |                      ¦   «         }|                     ddi¦  «         |                     t          |¦  «        ¦  «         |                     |                      ¦   «         ¦  «         |d         }t          |t          ¦  «        rmt          |¦  «        }||d<   | j
                             |¦  «         | j                             |¦  «         | j        r| j                             |¦  «         dS dS t          |t           ¦  «        r€|D ]{}t          |¦  «        }|                     |¦  «         | j
                             |¦  «         | j                             |¦  «         | j        r| j                             |¦  «         Œ|dS t#          d¦  «        ‚)zRun when chain starts running.r‰   rŠ   Úon_chain_startÚinputz Unexpected data format provided!N)rŒ   Úchain_startsrŽ   r„   rG   r   r�   Ú
isinstancerL   r   Úon_chain_start_recordsr‘   r{   rk   rx   r’   ÚlistÚ
ValueError)r|   r…   rª   r‡   rM   Úchain_inputÚ
input_respÚinps           r   r¬   z#WandbCallbackHandler.on_chain_start+  sÎ  € ð 	ˆ	Œ	�Q‰ˆ	Œ	ØÐÔ˜QÑÐÔØˆŒ�qÑˆŒà�ŠÑ Ô ˆØ�Š�XÐ/Ð0Ñ1Ô1Ð1Ø�Š•L Ñ,Ô,Ñ-Ô-Ð-Ø�Š�D×1Ò1Ñ3Ô3Ñ4Ô4Ð4à˜W”oˆå�k¥3Ñ'Ô'ð 	AÝ! $™œˆJØ"-ˆJ�wÑØÔ'×.Ò.¨zÑ:Ô:Ð:ØÔ×&Ò& zÑ2Ô2Ð2ØÔð )Ø”—’˜ZÑ(Ô(Ð(Ð(Ð(ð)ð )å˜¥TÑ*Ô*ð 		AØ"ð -ð -�Ý% d™^œ^�
Ø×!Ò! #Ñ&Ô&Ð&ØÔ+×2Ò2°:Ñ>Ô>Ð>ØÔ#×*Ò*¨:Ñ6Ô6Ð6ØÔ#ð -Ø”H—L’L Ñ,Ô,Ð,øð-ð -õ Ð?Ñ@Ô@Ð@r   Úoutputsc                 óÆ  — | xj         dz  c_         | xj        dz  c_        | xj        dz  c_        |                      ¦   «         }|                     d|d         dœ¦  «         |                     |                      ¦   «         ¦  «         | j                             |¦  «         | j                             |¦  «         | j	        r| j
                             |¦  «         dS dS )zRun when chain ends running.r‰   Úon_chain_endÚoutput)rŠ   r¶   N)rŒ   Ú
chain_endsr�   r„   rG   r�   Úon_chain_end_recordsr‘   r{   rk   rx   r’   )r|   r¶   r‡   rM   s       r   r¸   z!WandbCallbackHandler.on_chain_endL  sÙ   € àˆ	Œ	�Q‰ˆ	Œ	ØˆŒ˜1ÑˆŒØˆ	Œ	�Q‰ˆ	Œ	à�ŠÑ Ô ˆØ�Š˜~¸'À(Ô:KÐLÐLÑMÔMÐMØ�Š�D×1Ò1Ñ3Ô3Ñ4Ô4Ð4àÔ!×(Ò(¨Ñ.Ô.Ð.ØÔ×"Ò" 4Ñ(Ô(Ð(ØÔð 	ØŒH�LŠL˜ÑÔÐÐÐð	ð 	r   c                 óF   — | xj         dz  c_         | xj        dz  c_        dS )zRun when chain errors.r‰   Nr¥   r§   s      r   Úon_chain_errorz#WandbCallbackHandler.on_chain_error[  r©   r   Ú	input_strc                 óþ  — | xj         dz  c_         | xj        dz  c_        | xj        dz  c_        |                      ¦   «         }|                     d|dœ¦  «         |                     t          |¦  «        ¦  «         |                     |                      ¦   «         ¦  «         | j                             |¦  «         | j	                             |¦  «         | j
        r| j                             |¦  «         dS dS )zRun when tool starts running.r‰   Úon_tool_start)rŠ   r¾   N)rŒ   Útool_startsrŽ   r„   rG   r   r�   Úon_tool_start_recordsr‘   r{   rk   rx   r’   )r|   r…   r¾   r‡   rM   s        r   rÀ   z"WandbCallbackHandler.on_tool_start`  sõ   € ð 	ˆ	Œ	�Q‰ˆ	Œ	ØÐÔ˜AÑÐÔØˆŒ�qÑˆŒà�ŠÑ Ô ˆØ�Š˜¸YÐGÐGÑHÔHÐHØ�Š•L Ñ,Ô,Ñ-Ô-Ð-Ø�Š�D×1Ò1Ñ3Ô3Ñ4Ô4Ð4àÔ"×)Ò)¨$Ñ/Ô/Ð/ØÔ×"Ò" 4Ñ(Ô(Ð(ØÔð 	ØŒH�LŠL˜ÑÔÐÐÐð	ð 	r   r¹   c                 óØ  — t          |¦  «        }| xj        dz  c_        | xj        dz  c_        | xj        dz  c_        |                      ¦   «         }|                     d|dœ¦  «         |                     |                      ¦   «         ¦  «         | j                             |¦  «         | j	                             |¦  «         | j
        r| j                             |¦  «         dS dS )zRun when tool ends running.r‰   Úon_tool_end)rŠ   r¹   N)rL   rŒ   Ú	tool_endsr�   r„   rG   r�   Úon_tool_end_recordsr‘   r{   rk   rx   r’   )r|   r¹   r‡   rM   s       r   rÄ   z WandbCallbackHandler.on_tool_endr  sß   € å�V‘”ˆØˆ	Œ	�Q‰ˆ	Œ	ØˆŒ˜!ÑˆŒØˆ	Œ	�Q‰ˆ	Œ	à�ŠÑ Ô ˆØ�Š˜}¸Ð?Ð?Ñ@Ô@Ð@Ø�Š�D×1Ò1Ñ3Ô3Ñ4Ô4Ð4àÔ ×'Ò'¨Ñ-Ô-Ð-ØÔ×"Ò" 4Ñ(Ô(Ð(ØÔð 	ØŒH�LŠL˜ÑÔÐÐÐð	ð 	r   c                 óF   — | xj         dz  c_         | xj        dz  c_        dS )zRun when tool errors.r‰   Nr¥   r§   s      r   Úon_tool_errorz"WandbCallbackHandler.on_tool_error‚  r©   r   r(   c                 óš  — | xj         dz  c_         | xj        dz  c_        |                      ¦   «         }|                     d|dœ¦  «         |                     |                      ¦   «         ¦  «         | j                             |¦  «         | j                             |¦  «         | j        r| j	         
                    |¦  «         dS dS )z+
        Run when agent is ending.
        r‰   Úon_text)rŠ   r(   N)rŒ   Útext_ctrr„   rG   r�   Úon_text_recordsr‘   r{   rk   rx   r’   )r|   r(   r‡   rM   s       r   rÊ   zWandbCallbackHandler.on_text‡  sÇ   € ð 	ˆ	Œ	�Q‰ˆ	Œ	ØˆŒ˜ÑˆŒà�ŠÑ Ô ˆØ�Š˜y°$Ð7Ð7Ñ8Ô8Ð8Ø�Š�D×1Ò1Ñ3Ô3Ñ4Ô4Ð4àÔ×#Ò# DÑ)Ô)Ð)ØÔ×"Ò" 4Ñ(Ô(Ð(ØÔð 	ØŒH�LŠL˜ÑÔÐÐÐð	ð 	r   Úfinishc                 óÜ  — | xj         dz  c_         | xj        dz  c_        | xj        dz  c_        |                      ¦   «         }|                     d|j        d         |j        dœ¦  «         |                     |                      ¦   «         ¦  «         | j         	                    |¦  «         | j
         	                    |¦  «         | j        r| j                             |¦  «         dS dS )zRun when agent ends running.r‰   Úon_agent_finishr¹   )rŠ   r¹   r’   N)rŒ   Ú
agent_endsr�   r„   rG   Úreturn_valuesr’   r�   Úon_agent_finish_recordsr‘   r{   rk   rx   )r|   rÍ   r‡   rM   s       r   rÏ   z$WandbCallbackHandler.on_agent_finish—  sï   € àˆ	Œ	�Q‰ˆ	Œ	ØˆŒ˜1ÑˆŒØˆ	Œ	�Q‰ˆ	Œ	à�ŠÑ Ô ˆØ�Šà+Ø Ô.¨xÔ8Ø”zðð ñ	
ô 	
ð 	
ð 	�Š�D×1Ò1Ñ3Ô3Ñ4Ô4Ð4àÔ$×+Ò+¨DÑ1Ô1Ð1ØÔ×"Ò" 4Ñ(Ô(Ð(ØÔð 	ØŒH�LŠL˜ÑÔÐÐÐð	ð 	r   rŠ   c                 óÜ  — | xj         dz  c_         | xj        dz  c_        | xj        dz  c_        |                      ¦   «         }|                     d|j        |j        |j        dœ¦  «         |                     |                      ¦   «         ¦  «         | j	         
                    |¦  «         | j         
                    |¦  «         | j        r| j                             |¦  «         dS dS )zRun on agent action.r‰   Úon_agent_action)rŠ   ÚtoolÚ
tool_inputr’   N)rŒ   rÁ   rŽ   r„   rG   rÕ   rÖ   r’   r�   Úon_agent_action_recordsr‘   r{   rk   rx   )r|   rŠ   r‡   rM   s       r   rÔ   z$WandbCallbackHandler.on_agent_action¬  sô   € àˆ	Œ	�Q‰ˆ	Œ	ØÐÔ˜AÑÐÔØˆŒ�qÑˆŒà�ŠÑ Ô ˆØ�Šà+ØœØ$Ô/Ø”zð	ð ñ	
ô 	
ð 	
ð 	�Š�D×1Ò1Ñ3Ô3Ñ4Ô4Ð4ØÔ$×+Ò+¨DÑ1Ô1Ð1ØÔ×"Ò" 4Ñ(Ô(Ð(ØÔð 	ØŒH�LŠL˜ÑÔÐÐÐð	ð 	r   c                 ó  — t          ¦   «         }|                     | j        ¦  «        }|                     | j        ¦  «        }|g d¢                              d¬¦  «                             ddid¬¦  «        }g }g }| j        rg d¢}| j        rddg}|g d	¢|z   |z                                 d¬¦  «                             d
ddœd¬¦  «        }|                     ||gd¬¦  «        }|ddg          	                    d„ d¬¦  «        |d<   |S )z=Create a dataframe with all the information from the session.)rŒ   r†   ri   r‰   )ÚaxisrŒ   Úprompt_step)r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   rE   rF   )rŒ   r(   Útoken_usage_total_tokensÚtoken_usage_prompt_tokensÚtoken_usage_completion_tokensÚoutput_stepr¹   )rŒ   r(   r†   c                 ó:   — t          | d         | d         ¦  «        S )Nr†   r¹   )r`   )Úrows    r   ú<lambda>zBWandbCallbackHandler._create_session_analysis_df.<locals>.<lambda>ù  s   € ÕAØ�I”  H¤ñô € r   Ú	chat_html)
r   Ú	DataFramer�   r¡   ÚdropnaÚrenamer)   r*   ÚconcatÚapply)	r|   ÚpdÚon_llm_start_records_dfÚon_llm_end_records_dfÚllm_input_prompts_dfÚcomplexity_metrics_columnsÚvisualizations_columnsÚllm_outputs_dfÚsession_analysis_dfs	            r   Ú_create_session_analysis_dfz0WandbCallbackHandler._create_session_analysis_dfÁ  so  € å‰_Œ_ˆØ"$§,¢,¨tÔ/HÑ"IÔ"IÐØ "§¢¨TÔ-DÑ EÔ EÐð $Ð$?Ð$?Ð$?Ô@ßŠV˜ˆV‰^Œ^ßŠV�V˜]Ð+°!ˆVÑ4Ô4ð 	ð
 &(Ð"Ø!#ÐàÔ"ð 	ð*ð *ð *Ð&ð& Œ>ð 	EØ&7¸Ð%DÐ"ð "ðð ð ð -ñ-ð )ñ)ô
÷ ŠV˜ˆV‰^Œ^ßŠV˜]°HÐ=Ð=ÀAˆVÑFÔFð 	ð !ŸišiÐ)=¸~Ð(NÐUV˜iÑWÔWÐØ+>Ø˜Ð!ô,
ç
Š%ðð ð ð	 ñ 
ô 
ð 	˜KÑ(ð #Ð"r   TÚlangchain_assetÚresetc                 ó`  — t          ¦   «         }t          ¦   «         }|                     |                     | j        ¦  «        ¬¦  «        }|                     |                      ¦   «         ¬¦  «        }| j                             ||dœ¦  «         |�r^t          | j	        j
        d¦  «        }|                     dd¬¦  «        }|                     |d¬¦  «         |                     |d¬¦  «         	 |                     |¦  «         |                     t          |¦  «        ¦  «         t!          |¦  «        |_        n˜# t$          $ rN |                     |¦  «         |                     t          |¦  «        ¦  «         t!          |¦  «        |_        Y nAt(          $ r5}t+          d	¦  «         t+          t-          |¦  «        ¦  «         Y d
}~nd
}~ww xY w| j                             |¦  «         |s|rF| j                             ¦   «          | j	                             ¦   «          |                      ¦   «          |rq|                      |r|n| j        |r|n| j        |r|n| j        |r|n| j        |r|n| j         |	r|	n| j
        |
r|
n| j!        |r|n| j"        |r|n| j#        ¬¦	  «	         d
S d
S )aS  Flush the tracker and reset the session.

        Args:
            langchain_asset: The langchain asset to save.
            reset: Whether to reset the session.
            finish: Whether to finish the run.
            job_type: The job type.
            project: The project.
            entity: The entity.
            tags: The tags.
            group: The group.
            name: The name.
            notes: The notes.
            visualize: Whether to visualize.
            complexity_metrics: Whether to compute complexity metrics.

            Returns:
                None
        )Ú	dataframe)r{   Úsession_analysisz
model.jsonÚmodel)ri   Útyper{   )ri   rõ   zCould not save model.N)	rd   re   rf   rg   rh   ri   rj   r*   r)   )$r   r   ÚTablerã   r{   rð   rx   r’   r   rv   ri   ÚArtifactÚaddÚsaveÚadd_filerL   r'   Úmetadatar²   Ú
save_agentÚNotImplementedErrorÚprintÚreprÚlog_artifactrÍ   ÚcleanupÚreset_callback_metars   rd   re   rf   rg   rh   rj   r*   r)   )r|   rñ   rò   rÍ   rd   re   rf   rg   rh   ri   rj   r*   r)   rè   r   Úaction_records_tableÚsession_analysis_tableÚlangchain_asset_pathÚmodel_artifactÚes                       r   Úflush_trackerz"WandbCallbackHandler.flush_tracker   s  € õD ‰_Œ_ˆÝ‘”ˆØ$Ÿ{š{°R·\²\À$ÔBUÑ5VÔ5V˜{ÑWÔWÐØ!&§¢Ø×6Ò6Ñ8Ô8ð "-ñ "
ô "
Ðð 	Œ�Šà"6Ø$:ðð ñ	
ô 	
ð 	
ð ñ 	2Ý#'¨¬Ô(:¸LÑ#IÔ#IÐ Ø"Ÿ^š^°¸w˜^ÑGÔGˆNØ×ÒÐ3Ð:JÐÑKÔKÐKØ×ÒÐ5Ð<NÐÑOÔOÐOðØ×$Ò$Ð%9Ñ:Ô:Ð:Ø×'Ò'­Ð,@Ñ(AÔ(AÑBÔBÐBÝ*;Ð<PÑ*QÔ*Q�Ô'Ð'øÝð Rð Rð RØ×*Ò*Ð+?Ñ@Ô@Ð@Ø×'Ò'­Ð,@Ñ(AÔ(AÑBÔBÐBÝ*;Ð<PÑ*QÔ*Q�Ô'Ð'Ð'Ý&ð ð ð ÝÐ-Ñ.Ô.Ð.Ý•d˜1‘g”g‘”�Ø����øøøøðøøøð ŒH×!Ò! .Ñ1Ô1Ð1àð 	'�Uð 	'ØŒH�OŠOÑÔÐØŒM×!Ò!Ñ#Ô#Ð#Ø×$Ò$Ñ&Ô&Ð&Øð 	Ø�MŠMØ%-Ð@˜˜°4´=Ø#*Ð<˜˜°´Ø!'Ð8�v�v¨T¬[Ø!Ð0�T�T t¤yØ$Ð4�e�e¨$¬*Ø!Ð0�T�T t¤yØ$Ð4�e�e¨$¬*Ø'0ÐD˜)˜)°d´nð *ð1Ð&Ð&àÔ0ð ñ ô ð ð ð ð	ð 	s    Ã3AD? Ä?AGÆ	GÆ+GÇG)
Nrc   NNNNNFFF)NTFNNNNNNNNN)!Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rL   r	   Úboolrs   r   r„   r   r   r‹   r–   r   r›   ÚBaseExceptionr¨   r¬   r¸   r½   rÀ   rÄ   rÈ   rÊ   r   rÏ   r   rÔ   rð   r
  Ú__classcell__)r~   s   @r   rb   rb   …   s¤  ø€ € € € € ðð ð, #'Ø!:Ø $Ø#'Ø#Ø"Ø#ØØ#(Ø!ðG
ð G
à˜3”-ðG
ð ˜#”ðG
ð ˜”ð	G
ð
 �xÔ ðG
ð ˜Œ}ðG
ð �sŒmðG
ð ˜Œ}ðG
ð ðG
ð !ðG
ð ðG
ð 
ðG
ð G
ð G
ð G
ð G
ð G
ðR8˜Dð 8ð 8ð 8ð 8ð*Ø˜s C˜xœ.ð*Ø37¸´9ð*ØHKð*à	ð*ð *ð *ð *ð* cð °Sð ¸Tð ð ð ð ð2 9ð 2¸ð 2Àð 2ð 2ð 2ð 2ð: -ð ¸3ð À4ð ð ð ð ð
AØ˜s C˜xœ.ðAØ26°s¸C°x´.ðAØLOðAà	ðAð Að Að AðB D¨¨c¨¤Nð ¸cð Àdð ð ð ð ð Mð ¸Sð ÀTð ð ð ð ð
Ø˜s C˜xœ.ðØ58ðØDGðà	ðð ð ð ð$ #ð °ð ¸ð ð ð ð ð  =ð ¸Cð ÀDð ð ð ð ð
˜Cð ¨3ð °4ð ð ð ð ð  kð ¸Sð ÀTð ð ð ð ð* kð ¸Sð ÀSð ð ð ð ð*=#¨Sð =#ð =#ð =#ð =#ðB  $ØØØ"&Ø!%Ø $Ø#'Ø#Ø"Ø#Ø$(Ø-1ðUð UàðUð ðUð ð	Uð
 ˜3”-ðUð ˜#”ðUð ˜”ðUð �xÔ ðUð ˜Œ}ðUð �sŒmðUð ˜Œ}ðUð ˜D”>ðUð % TœNðUð 
ðUð Uð Uð Uð Uð Uð Uð Ur   rb   )TTNN)'r#   rt   Úcopyr   Úpathlibr   Útypingr   r   r   r   r	   r
   Úlangchain_core._apir   Úlangchain_core.agentsr   r   Úlangchain_core.callbacksr   Úlangchain_core.outputsr   Úlangchain_core.utilsr   Ú#langchain_community.callbacks.utilsr   r   r   r   r   r   r   rL   r    r'   r  rW   r`   rb   r   r   r   ú<module>r     s:  ðØ €€€Ø €€€Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø =Ð =Ð =Ð =Ð =Ð =Ð =Ð =Ð =Ð =Ð =Ð =Ð =Ð =Ð =Ð =à /Ð /Ð /Ð /Ð /Ð /Ø :Ð :Ð :Ð :Ð :Ð :Ð :Ð :Ø 8Ð 8Ð 8Ð 8Ð 8Ð 8Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ø -Ð -Ð -Ð -Ð -Ð -ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð!�cð !ð !ð !ð !ð
  s¨D yÔ!1ð °dð ð ð ð ð   $ØØØ-1ð>ð >Ø
ð>àð>ð ð>ð 
ð	>ð
 ˜˜s D˜yÔ)Ô*ð>ð 
ð>ð >ð >ð >ðB°cð Àsð Èsð ð ð ð ð4Pð Pð Pð Pð PÐ6Ð8Kñ Pô Pð Pð Pð Pr   