a
    ”˜ëjL  ã                   @   sŽ   d Z ddlZddlmZmZ ddlmZ ddlmZ ddl	m
Z
mZ dZeeedœd	d
„Zeedœdd„ZG dd„ deƒZG dd„ deƒZdS )z"Tracers that print to the console.é    N)ÚAnyÚCallable)Ú
BaseTracer)ÚRun)Úget_bolded_textÚget_colored_textiè  )ÚobjÚfallbackÚreturnc                 C   s.   zt j| ddd�W S  ty(   | Y S 0 dS )a  Try to stringify an object to JSON.

    Args:
        obj: Object to stringify.
        fallback: Fallback string to return if the object cannot be stringified.

    Returns:
        A JSON string if the object can be stringified, otherwise the fallback string.
    é   F)ÚindentÚensure_asciiN)ÚjsonÚdumpsÚ	Exception)r   r	   © r   ú\/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain_core/tracers/stdout.pyÚtry_json_stringify   s    
r   ©Úrunr
   c                 C   s8   | j | j }| ¡ }|dk r,|t d›d�S |d›d�S )zìGet the elapsed time of a run.

    Args:
        run: any object with a start_time and end_time attribute.

    Returns:
        A string with the elapsed time in seconds or
            milliseconds if time is less than a second.

    é   z.0fÚmsz.2fÚs)Zend_timeÚ
start_timeÚtotal_secondsÚMILLISECONDS_IN_SECOND)r   Zelapsed_timeÚsecondsr   r   r   Úelapsed   s
    r   c                       s  e Zd ZU dZdZeed< eegdf eddœ‡ fdd„Z	e
ddœd	d
„Ze
ee
 dœdd„Ze
edœdd„Ze
ddœdd„Ze
ddœdd„Ze
ddœdd„Ze
ddœdd„Ze
ddœdd„Ze
ddœdd„Ze
ddœdd„Ze
ddœdd„Ze
ddœdd „Z‡  ZS )!ÚFunctionCallbackHandlerz9Tracer that calls a function with a single str parameter.Zfunction_callback_handlerÚnameN)ÚfunctionÚkwargsr
   c                    s   t ƒ jf i |¤Ž || _dS )znCreate a FunctionCallbackHandler.

        Args:
            function: The callback function to call.
        N)ÚsuperÚ__init__Úfunction_callback)Úselfr    r!   ©Ú	__class__r   r   r#   6   s    z FunctionCallbackHandler.__init__r   c                 C   s   d S )Nr   )r%   r   r   r   r   Ú_persist_run?   s    z$FunctionCallbackHandler._persist_runc                 C   s<   g }|}|j r8| j t|j ƒ¡}|r8| |¡ |}qq8q|S )z–Get the parents of a run.

        Args:
            run: The run to get the parents of.

        Returns:
            A list of parent runs.
        )Zparent_run_idZrun_mapÚgetÚstrÚappend)r%   r   ÚparentsZcurrent_runÚparentr   r   r   Úget_parentsB   s    	
z#FunctionCallbackHandler.get_parentsc                 C   s4   |   |¡ddd… }d dd„ tg |¢|‘ƒD ƒ¡S )z±Get the breadcrumbs of a run.

        Args:
            run: The run to get the breadcrumbs of.

        Returns:
            A string with the breadcrumbs of the run.
        Néÿÿÿÿz > c                 s   s$   | ]\}}|j › d |j› �V  qdS )ú:N)Úrun_typer   )Ú.0Úir-   r   r   r   Ú	<genexpr>`   s   ÿz:FunctionCallbackHandler.get_breadcrumbs.<locals>.<genexpr>)r.   ÚjoinÚ	enumerate)r%   r   r,   r   r   r   Úget_breadcrumbsV   s    	
þz'FunctionCallbackHandler.get_breadcrumbsc              	   C   sT   |   |¡}|j ¡ }|  tddd�› d�td|› d|› d�ƒ t|jdƒ›  ¡ d S )	Nz[chain/start]Úgreen©Úcolorú ú[z] Entering z run with input:
ú[inputs])r7   r1   Ú
capitalizer$   r   r   r   Úinputs©r%   r   Úcrumbsr1   r   r   r   Ú_on_chain_startf   s    

ÿþÿz'FunctionCallbackHandler._on_chain_startc                 C   s^   |   |¡}|j ¡ }|  tddd�› d�td|› dt|ƒ› d|› d�ƒ t|jd	ƒ›  ¡ d S )
Nz[chain/end]Úbluer9   r;   r<   ú] [z
] Exiting z run with output:
z	[outputs])	r7   r1   r>   r$   r   r   r   r   Úoutputsr@   r   r   r   Ú_on_chain_endo   s    

ÿÿüÿz%FunctionCallbackHandler._on_chain_endc                 C   s^   |   |¡}|j ¡ }|  tddd�› d�td|› dt|ƒ› d|› d�ƒ t|jd	ƒ›  ¡ d S )
Nz[chain/error]Úredr9   r;   r<   rD   ú] z run errored with error:
ú[error])	r7   r1   r>   r$   r   r   r   r   Úerrorr@   r   r   r   Ú_on_chain_errorz   s    

ÿÿüÿz'FunctionCallbackHandler._on_chain_errorc                 C   sj   |   |¡}d|jv r,ddd„ |jd D ƒin|j}|  tddd�› d�td|› d	�ƒ t|d
ƒ›  ¡ d S )NZpromptsc                 S   s   g | ]}|  ¡ ‘qS r   )Ústrip)r2   Úpr   r   r   Ú
<listcomp>ˆ   ó    z9FunctionCallbackHandler._on_llm_start.<locals>.<listcomp>z[llm/start]r8   r9   r;   r<   z] Entering LLM run with input:
r=   )r7   r?   r$   r   r   r   )r%   r   rA   r?   r   r   r   Ú_on_llm_start…   s    
ÿýÿ
þÿz%FunctionCallbackHandler._on_llm_startc              	   C   sN   |   |¡}|  tddd�› d�td|› dt|ƒ› d�ƒ t|jdƒ›  ¡ d S )	Nz	[llm/end]rC   r9   r;   r<   rD   z] Exiting LLM run with output:
z
[response])r7   r$   r   r   r   r   rE   ©r%   r   rA   r   r   r   Ú_on_llm_end’   s    
ÿÿüÿz#FunctionCallbackHandler._on_llm_endc              	   C   sN   |   |¡}|  tddd�› d�td|› dt|ƒ› d�ƒ t|jdƒ›  ¡ d S )	Nz[llm/error]rG   r9   r;   r<   rD   z] LLM run errored with error:
rI   )r7   r$   r   r   r   r   rJ   rQ   r   r   r   Ú_on_llm_errorœ   s    
ÿÿüÿz%FunctionCallbackHandler._on_llm_errorc                 C   sL   |   |¡}|  tddd�› d�td|› d�ƒ d|jd  ¡ › d� ¡ d S )	Nz[tool/start]r8   r9   r;   r<   z ] Entering Tool run with input:
ú"Úinput)r7   r$   r   r   r?   rL   rQ   r   r   r   Ú_on_tool_start¦   s    
ÿþÿz&FunctionCallbackHandler._on_tool_startc              	   C   s`   |   |¡}|jr\|  tddd�› d�td|› dt|ƒ› d�ƒ dt|jd	 ƒ ¡ › d� ¡ d S )
Nz
[tool/end]rC   r9   r;   r<   rD   z ] Exiting Tool run with output:
rT   Úoutput)r7   rE   r$   r   r   r   r*   rL   rQ   r   r   r   Ú_on_tool_end®   s    
ÿÿüÿz$FunctionCallbackHandler._on_tool_endc              	   C   sL   |   |¡}|  tddd�› d�td|› dt|ƒ› d�ƒ d|j› � ¡ d S )	Nz[tool/error]rG   r9   r;   r<   rD   rH   zTool run errored with error:
)r7   r$   r   r   r   rJ   rQ   r   r   r   Ú_on_tool_error¹   s    
ÿÿþÿz&FunctionCallbackHandler._on_tool_error)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r*   Ú__annotations__r   r   r#   r   r(   Úlistr.   r7   rB   rF   rK   rP   rR   rS   rV   rX   rY   Ú__classcell__r   r   r&   r   r   /   s   
 		

r   c                       s6   e Zd ZU dZdZeed< eddœ‡ fdd„Z‡  Z	S )ÚConsoleCallbackHandlerz"Tracer that prints to the console.Zconsole_callback_handlerr   N)r!   r
   c                    s   t ƒ jf dti|¤Ž dS )z Create a ConsoleCallbackHandler.r    N)r"   r#   Úprint)r%   r!   r&   r   r   r#   È   s    zConsoleCallbackHandler.__init__)
rZ   r[   r\   r]   r   r*   r^   r   r#   r`   r   r   r&   r   ra   Ã   s   
ra   )r]   r   Útypingr   r   Zlangchain_core.tracers.baser   Zlangchain_core.tracers.schemasr   Zlangchain_core.utils.inputr   r   r   r*   r   r   r   ra   r   r   r   r   Ú<module>   s    