a
    bgH                     @  s   d dl m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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mZ erd dl Z!ddddZ"G dd deeZ#dS )    )annotationsN)deepcopy)Path)TYPE_CHECKINGAnyDictListMappingOptionalSequence)AgentActionAgentFinish)BaseCallbackHandler)	LLMResultguard_import)BaseMetadataCallbackHandlerflatten_dicthash_stringimport_pandasimport_spacyimport_textstat	load_jsonr   returnc                   C  s   t dS )zLImport the clearml python package and raise an error if it is not installed.clearmlr    r   r   |/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/callbacks/clearml_callback.pyimport_clearml   s    r   c                
      s  e Zd ZdZdMdddddddd	d
 fddZddddZdddd	dddZddd	dddZddd	dddZddd	d d!d"Z	dddd	d#d$d%Z
ddd	d&d'd(Zddd	d d)d*Zdddd	d+d,d-Zddd	d.d/d0Zddd	d d1d2Zddd	d3d4d5Zd6dd	d7d8d9Zd:ddd;d<d=Zdd>d?d@dAZedBdCdDdBdEdFdGZdddHdIZdNdddd	dJdKdLZ  ZS )OClearMLCallbackHandlera   Callback Handler that logs to ClearML.

    Parameters:
        job_type (str): The type of clearml task such as "inference", "testing" or "qc"
        project_name (str): The clearml project name
        tags (list): Tags to add to the task
        task_name (str): Name of the clearml task
        visualize (bool): Whether to visualize the run.
        complexity_metrics (bool): Whether to log complexity metrics
        stream_logs (bool): Whether to stream callback actions to ClearML

    This handler will utilize the associated callback method 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 to the ClearML console.
    	inferencelangchain_callback_demoNFzOptional[str]zOptional[Sequence]boolNone)	task_typeproject_nametags	task_name	visualizecomplexity_metricsstream_logsr   c                   s   t  }t }	t   || _|| _|| _|| _|| _|| _	|| _
t | _|j rb|j | _n |jj| j| j| j| jdd| _| j | _d}
| jj|
ddd g | _g | _|| _	|| _|	d| _dS )zInitialize callback handler.T)r$   r%   r&   r'   Z
output_urizThe clearml callback is currently in beta and is subject to change based on updates to `langchain`. Please report any issues to https://github.com/allegroai/clearml/issues with the tag `langchain`.   )levelZprint_consoleZen_core_web_smN)r   r   super__init__r$   r%   r&   r'   r(   r)   r*   tempfileTemporaryDirectorytemp_dirTaskcurrent_tasktaskinitZ
get_loggerloggerreport_textcallback_columnsaction_recordsloadnlp)selfr$   r%   r&   r'   r(   r)   r*   r   spacywarning	__class__r   r   r.   2   s:    


zClearMLCallbackHandler.__init__r   r   c                 C  s   dd | j D S )Nc                 S  s   i | ]
}|d qS )Nr   ).0kr   r   r   
<dictcomp>e       z5ClearMLCallbackHandler._init_resp.<locals>.<dictcomp>)r8   )r<   r   r   r   
_init_respd   s    z!ClearMLCallbackHandler._init_respzDict[str, Any]z	List[str]r   )
serializedpromptskwargsr   c                 K  s   |  j d7  _ |  jd7  _|  jd7  _|  }|ddi |t| ||   |D ]>}t|}||d< | j	| | j
	| | jr`| j| q`dS )zRun when LLM starts.   actionon_llm_startrG   N)stepZ
llm_startsstartsrE   updater   get_custom_callback_metar   Zon_llm_start_recordsappendr9   r*   r6   r7   )r<   rF   rG   rH   resppromptZprompt_respr   r   r   rK   g   s    z#ClearMLCallbackHandler.on_llm_startstr)tokenrH   r   c                 K  sp   |  j d7  _ |  jd7  _|  }|d|d ||   | j| | j| | jrl| j	
| dS )z#Run when LLM generates a new token.rI   on_llm_new_token)rJ   rT   N)rL   Zllm_streamsrE   rN   rO   Zon_llm_token_recordsrP   r9   r*   r6   r7   )r<   rT   rH   rQ   r   r   r   rU   |   s    z'ClearMLCallbackHandler.on_llm_new_tokenr   )responserH   r   c                 K  s   |  j d7  _ |  jd7  _|  jd7  _|  }|ddi |t|jpNi  ||   |jD ]d}|D ]Z}t	|}|t|
  || |j | j| | j| | jrp| j| qpqhdS )zRun when LLM ends running.rI   rJ   
on_llm_endN)rL   Zllm_endsendsrE   rN   r   Z
llm_outputrO   generationsr   dictanalyze_texttexton_llm_end_recordsrP   r9   r*   r6   r7   )r<   rV   rH   rQ   rY   Z
generationZgeneration_respr   r   r   rW      s     
z!ClearMLCallbackHandler.on_llm_endBaseException)errorrH   r   c                 K  s    |  j d7  _ |  jd7  _dS )zRun when LLM errors.rI   NrL   errorsr<   r_   rH   r   r   r   on_llm_error   s    z#ClearMLCallbackHandler.on_llm_error)rF   inputsrH   r   c                 K  s  |  j d7  _ |  jd7  _|  jd7  _|  }|ddi |t| ||   |d|d}t|t	rt
|}||d< | j| | j| | jr| j| n\t|tr|D ]@}t
|}|| | j| | j| | jr| j| qntddS )zRun when chain starts running.rI   rJ   on_chain_startinputZhuman_inputz Unexpected data format provided!N)rL   Zchain_startsrM   rE   rN   r   rO   get
isinstancerS   r   Zon_chain_start_recordsrP   r9   r*   r6   r7   list
ValueError)r<   rF   rd   rH   rQ   Zchain_inputZ
input_respinpr   r   r   re      s0    

z%ClearMLCallbackHandler.on_chain_start)outputsrH   r   c              	   K  s   |  j d7  _ |  jd7  _|  jd7  _|  }|d|d|dd ||   | j| | j	| | j
r| j| dS )zRun when chain ends running.rI   on_chain_endoutputr\   )rJ   rl   N)rL   Z
chain_endsrX   rE   rN   rg   rO   Zon_chain_end_recordsrP   r9   r*   r6   r7   )r<   rl   rH   rQ   r   r   r   rm      s    z#ClearMLCallbackHandler.on_chain_endc                 K  s    |  j d7  _ |  jd7  _dS )zRun when chain errors.rI   Nr`   rb   r   r   r   on_chain_error   s    z%ClearMLCallbackHandler.on_chain_error)rF   	input_strrH   r   c                 K  s   |  j d7  _ |  jd7  _|  jd7  _|  }|d|d |t| ||   | j| | j	| | j
r| j| dS )zRun when tool starts running.rI   on_tool_start)rJ   rp   N)rL   tool_startsrM   rE   rN   r   rO   Zon_tool_start_recordsrP   r9   r*   r6   r7   )r<   rF   rp   rH   rQ   r   r   r   rq      s    z$ClearMLCallbackHandler.on_tool_start)rn   rH   r   c                 K  s   t |}|  jd7  _|  jd7  _|  jd7  _|  }|d|d ||   | j| | j	| | j
r| j| dS )zRun when tool ends running.rI   on_tool_end)rJ   rn   N)rS   rL   Z	tool_endsrX   rE   rN   rO   Zon_tool_end_recordsrP   r9   r*   r6   r7   )r<   rn   rH   rQ   r   r   r   rs      s    z"ClearMLCallbackHandler.on_tool_endc                 K  s    |  j d7  _ |  jd7  _dS )zRun when tool errors.rI   Nr`   rb   r   r   r   on_tool_error   s    z$ClearMLCallbackHandler.on_tool_error)r\   rH   r   c                 K  sp   |  j d7  _ |  jd7  _|  }|d|d ||   | j| | j| | jrl| j	
| dS )z+
        Run when agent is ending.
        rI   on_text)rJ   r\   N)rL   Ztext_ctrrE   rN   rO   Zon_text_recordsrP   r9   r*   r6   r7   )r<   r\   rH   rQ   r   r   r   ru     s    zClearMLCallbackHandler.on_textr   )finishrH   r   c                 K  s   |  j d7  _ |  jd7  _|  jd7  _|  }|d|jd |jd ||   | j	| | j
	| | jr| j| dS )zRun when agent ends running.rI   on_agent_finishrn   )rJ   rn   logN)rL   Z
agent_endsrX   rE   rN   Zreturn_valuesrx   rO   Zon_agent_finish_recordsrP   r9   r*   r6   r7   )r<   rv   rH   rQ   r   r   r   rw     s    z&ClearMLCallbackHandler.on_agent_finishr   )rJ   rH   r   c                 K  s   |  j d7  _ |  jd7  _|  jd7  _|  }|d|j|j|jd ||   | j	
| | j
| | jr| j| dS )zRun on agent action.rI   on_agent_action)rJ   tool
tool_inputrx   N)rL   rr   rM   rE   rN   rz   r{   rx   rO   Zon_agent_action_recordsrP   r9   r*   r6   r7   )r<   rJ   rH   rQ   r   r   r   ry   *  s     z&ClearMLCallbackHandler.on_agent_actionrZ   )r\   r   c                 C  s~  i }t  }t }| jr|||||||||||||	||
|||||||||||||||||d}|| | jrz| jrz| jjdurz| |}|jj|dddd}t| jjtd| d }|jd	d
d| |jj|dddd}	t| jjtd| d }
|
jd	d
d|	 | jjd||d | jjd||
d |S )zAnalyze text using textstat and spacy.

        Parameters:
            text (str): The text to analyze.

        Returns:
            (dict): A dictionary containing the complexity metrics.
        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-zDependencies Plot)Z
local_pathzEntities Plot) r   r   r)   r}   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rN   r(   r;   r1   nameZdisplacyrenderr   r   openwriter6   Zreport_media)r<   r\   rQ   Ztextstatr=   Ztext_complexity_metricsdocZdep_outZdep_output_pathZent_outZent_output_pathr   r   r   r[   ?  sV    	

z#ClearMLCallbackHandler.analyze_textzpd.DataFramer   r	   )base_dfbase_df_fields
rename_mapr   c                   sN    fddD fdd|  D }  jdd}|rJ|j|dd}|S )Nc                   s   g | ]}| v r|qS r   r   )rA   field)r   r   r   
<listcomp>~  rD   z8ClearMLCallbackHandler._build_llm_df.<locals>.<listcomp>c                   s   i | ]\}}| v r||qS r   r   )rA   Zmap_entry_kZmap_entry_v)r   r   r   rC     s   z8ClearMLCallbackHandler._build_llm_df.<locals>.<dictcomp>rI   Zaxis)itemsZdropnarename)r   r   r   Zllm_dfr   )r   r   r   _build_llm_dfz  s    
z$ClearMLCallbackHandler._build_llm_dfc                 C  s   t  }|| j}tj|ddgd|v r,dgndg ddid}g }g }| jrTg d}t|g d| | d	d
d}|j||gdd}|S )z=Create a dataframe with all the information from the session.rL   rG   r   idZprompt_step)r   r   r   r|   )rL   r\   Ztoken_usage_total_tokensZtoken_usage_prompt_tokensZtoken_usage_completion_tokensZoutput_steprn   )rL   r\   rI   r   )r   	DataFramer]   r   r   r)   concat)r<   pdZon_llm_end_records_dfZllm_input_prompts_dfZcomplexity_metrics_columnsZvisualizations_columnsZllm_outputs_dfsession_analysis_dfr   r   r   _create_session_analysis_df  s0    	z2ClearMLCallbackHandler._create_session_analysis_df)r   langchain_assetrv   r   c           
   
   C  sf  t  }t }| jjd||| jd |  }| jjd||d | jr`| j|| j|d |r(t	| j
jd}z6|| |j| jt|d}|jt|d|d W n| ty   || |j| jt|d}|jt|d|d Y n: ty& }	 z td	 tt|	 W Y d
}	~	n
d
}	~	0 0 | jjdd | j
  t | _
|   |rb| j  d
S )aU  Flush the tracker and setup the session.

        Everything after this will be a new table.

        Args:
            name: Name of the performed session so far so it is identifiable
            langchain_asset: The langchain asset to save.
            finish: Whether to finish the run.

            Returns:
                None
        zAction Records)Z
table_plotzSession Analysis)r9   Zsession_analysisz
model.json)r4   Zconfig_textF)Zweights_filenameZauto_delete_fileZtarget_filenamezCould not save model.NT)Zwait_for_uploads)r   r   r6   Zreport_tabler   r9   r   r*   r7   r   r1   r   saveZOutputModelr4   r   Zupdate_weightsrS   rj   Z
save_agentNotImplementedErrorprintreprflushcleanupr/   r0   Zreset_callback_metaclose)
r<   r   r   rv   r   r   r   Zlangchain_asset_pathZoutput_modeler   r   r   flush_tracker  s^    








z$ClearMLCallbackHandler.flush_tracker)r    r!   NNFFF)NNF)__name__
__module____qualname____doc__r.   rE   rK   rU   rW   rc   re   rm   ro   rq   rs   rt   ru   rw   ry   r[   staticmethodr   r   r   __classcell__r   r   r?   r   r       s>          "2!;4   r   )$
__future__r   r/   copyr   pathlibr   typingr   r   r   r   r	   r
   r   Zlangchain_core.agentsr   r   Zlangchain_core.callbacksr   Zlangchain_core.outputsr   Zlangchain_core.utilsr   Z#langchain_community.callbacks.utilsr   r   r   r   r   r   r   Zpandasr   r   r   r   r   r   r   <module>   s   $$
