§
    šŠtjf  ã                  óÚ   — d dl mZ d dlZd dlmZmZmZmZmZm	Z	m
Z
mZ d dlmZ d dlmZ d dlmZ erd dlmZ d dlmZ  ej        e¦  «        Z G d	„ d
e¦  «        Z G d„ de¦  «        ZdS )é    )ÚannotationsN)ÚTYPE_CHECKINGÚDictÚIterableÚIteratorÚListÚOptionalÚUnionÚcast)ÚBaseChatLoader)ÚChatSession)Úload©ÚClient©ÚRunc                  ón   — e Zd ZdZ	 ddd„Zedd„¦   «         Zedd„¦   «         Zedd„¦   «         Zdd„Z	dS )ÚLangSmithRunChatLoaderzí
    Load chat sessions from a list of LangSmith "llm" runs.

    Attributes:
        runs (Iterable[Union[str, Run]]): The list of LLM run IDs or run objects.
        client (Client): Instance of LangSmith client for fetching data.
    NÚrunsúIterable[Union[str, Run]]ÚclientúOptional['Client']c                óB   — ddl m} || _        |p	 |¦   «         | _        dS )zù
        Initialize a new LangSmithRunChatLoader instance.

        :param runs: List of LLM run IDs or run objects.
        :param client: An instance of LangSmith client, if not provided,
            a new client instance will be created.
        r   r   N)Úlangsmith.clientr   r   r   )Úselfr   r   r   s       úh/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/langchain_community/chat_loaders/langsmith.pyÚ__init__zLangSmithRunChatLoader.__init__   s3   € ð 	,Ð+Ð+Ð+Ð+Ð+àˆŒ	ØÐ(  ¡¤ˆŒˆˆó    Úllm_runú'Run'Úreturnr   c                ó|   — t                                | ¦  «        }t                                | ¦  «        }|r||d<   |S )zµ
        Convert an individual LangSmith LLM run to a ChatSession.

        :param llm_run: The LLM run object.
        :return: A chat session representing the run's data.
        Ú	functions)r   Ú_get_messages_from_llm_runÚ_get_functions_from_llm_run)r   Úchat_sessionr#   s      r   Ú_load_single_chat_sessionz0LangSmithRunChatLoader._load_single_chat_session)   sB   € õ .×HÒHÈÑQÔQˆÝ*×FÒFÀwÑOÔOˆ	Øð 	2Ø(1ˆL˜Ñ%ØÐr   c                óZ  — | j         dk    rt          d| j         › �¦  «        ‚d| j        vrt          d| j        › �¦  «        ‚| j        st          d¦  «        ‚t	          | j        ¦  «        d         }t	          | j        ¦  «        d         d         d         }t          ||gz   ¬	¦  «        S )
z£
        Extract messages from a LangSmith LLM run.

        :param llm_run: The LLM run object.
        :return: ChatSession with the extracted messages.
        ÚllmúExpected run of type llm. Got: Úmessagesz"Run has no 'messages' inputs. Got zCannot convert pending runÚgenerationsr   Úmessage)r+   )Úrun_typeÚ
ValueErrorÚinputsÚoutputsr   r   )r   r+   Úmessage_chunks      r   r$   z1LangSmithRunChatLoader._get_messages_from_llm_run7   sµ   € ð Ô˜uÒ$Ð$ÝÐQ¸wÔ?OÐQÐQÑRÔRÐRØ˜Wœ^Ð+Ð+ÝÐRÀ'Ä.ÐRÐRÑSÔSÐSØŒð 	;ÝÐ9Ñ:Ô:Ð:Ý˜œÑ'Ô'¨
Ô3ˆÝ˜Wœ_Ñ-Ô-¨mÔ<¸QÔ?À	ÔJˆÝ H°¨Ñ$>Ð?Ñ?Ô?Ð?r   úOptional[List[Dict]]c                ó¦   — | j         dk    rt          d| j         › �¦  «        ‚| j        pi                      di ¦  «                             d¦  «        S )z©
        Extract functions from a LangSmith LLM run if they exist.

        :param llm_run: The LLM run object.
        :return: Functions from the run or None.
        r)   r*   Úinvocation_paramsr#   )r.   r/   ÚextraÚget)r   s    r   r%   z2LangSmithRunChatLoader._get_functions_from_llm_runI   sX   € ð Ô˜uÒ$Ð$ÝÐQ¸wÔ?OÐQÐQÑRÔRÐRØ”Ð# ×(Ò(Ð)<¸bÑAÔA×EÒEÀkÑRÔRÐRr   úIterator[ChatSession]c           
   #  ó\  K  — ddl m} | j        D ]›}	 t          |d¦  «        r|}n| j                             |¦  «        }|                      t          ||¦  «        ¦  «        }|V — ŒX# t          $ r7}t           
                    d|› dt          |¦  «        › �¦  «         Y d}~Œ”d}~ww xY wdS )a	  
        Lazy load the chat sessions from the iterable of run IDs.

        This method fetches the runs and converts them to chat sessions on-the-fly,
        yielding one session at a time.

        :return: Iterator of chat sessions containing messages.
        r   r   ÚidzCould not load run z: N)Úlangsmith.schemasr   r   Úhasattrr   Úread_runr'   r   r/   ÚloggerÚwarningÚrepr)r   r   Úrun_objÚrunÚsessionÚes         r   Ú	lazy_loadz LangSmithRunChatLoader.lazy_loadU   sá   è è € ð 	*Ð)Ð)Ð)Ð)Ð)à”yð 
	ð 
	ˆGð	Ý˜7 DÑ)Ô)ð 8Ø!�C�Càœ+×.Ò.¨wÑ7Ô7�CØ×8Ò8½¸cÀ3¹¼ÑHÔH�Ø����øÝð ð ð Ý—’ÐI°WÐIÐIÅÀQÁÄÐIÐIÑJÔJÐJØ����øøøøðøøøð
	ð 
	s   “AA(Á(
B)Á2-B$Â$B))N)r   r   r   r   )r   r    r!   r   )r   r    r!   r3   ©r!   r8   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústaticmethodr'   r$   r%   rE   © r   r   r   r      sº   € € € € € ðð ð MQð)ð )ð )ð )ð )ð ðð ð ñ „\ðð ð@ð @ð @ñ „\ð@ð" ð	Sð 	Sð 	Sñ „\ð	Sðð ð ð ð ð r   r   c                  ó(   — e Zd ZdZddœdd„Zdd„ZdS )ÚLangSmithDatasetChatLoaderzé
    Load chat sessions from a LangSmith dataset with the "chat" data type.

    Attributes:
        dataset_name (str): The name of the LangSmith dataset.
        client (Client): Instance of LangSmith client for fetching data.
    N)r   Údataset_nameÚstrr   r   c               óŠ   — 	 ddl m} n"# t          $ r}t          d¦  «        |‚d}~ww xY w|| _        |p	 |¦   «         | _        dS )a  
        Initialize a new LangSmithChatDatasetLoader instance.

        :param dataset_name: The name of the LangSmith dataset.
        :param client: An instance of LangSmith client; if not provided,
            a new client instance will be created.
        r   r   zkThe LangSmith client is required to load LangSmith datasets.
Please install it with `pip install langsmith`N)r   r   ÚImportErrorrO   r   )r   rO   r   r   rD   s        r   r   z#LangSmithDatasetChatLoader.__init__v   sx   € ð	Ø/Ð/Ð/Ð/Ð/Ð/Ð/øÝð 	ð 	ð 	ÝðAñô ð ðøøøøð	øøøð )ˆÔØÐ(  ¡¤ˆŒˆˆs   ‚	 ‰
(“#£(r!   r8   c              #  óî   ‡K  — ddl mŠ | j                             | j        ¬¦  «        }|D ]H}t          ˆfd„|                     dg ¦  «        D ¦   «         |                     d¦  «        ¬¦  «        V — ŒIdS )	a:  
        Lazy load the chat sessions from the specified LangSmith dataset.

        This method fetches the chat data from the dataset and
        converts each data point to chat sessions on-the-fly,
        yielding one session at a time.

        :return: Iterator of chat sessions containing messages.
        r   )Úopenai)rO   c                ó:   •— g | ]}‰                      |¦  «        ‘ŒS rL   )Úconvert_dict_to_message)Ú.0ÚmÚoai_adapters     €r   ú
<listcomp>z8LangSmithDatasetChatLoader.lazy_load.<locals>.<listcomp>š   s7   ø€ ð ð ð àð  ×7Ò7¸Ñ:Ô:ðð ð r   r+   r#   )r+   r#   N)Úlangchain_community.adaptersrT   r   Úread_dataset_openai_finetuningrO   r   r7   )r   ÚdataÚ
data_pointrY   s      @r   rE   z$LangSmithDatasetChatLoader.lazy_load‰   sÄ   øè è € ð 	GÐFÐFÐFÐFÐFàŒ{×9Ò9ØÔ*ð :ñ 
ô 
ˆð ð 	ð 	ˆJÝðð ð ð à'Ÿ^š^¨J¸Ñ;Ô;ðñ ô ð %Ÿ.š.¨Ñ5Ô5ðñ ô ð ð ð ð ð	ð 	r   )rO   rP   r   r   rF   )rG   rH   rI   rJ   r   rE   rL   r   r   rN   rN   m   sW   € € € € € ðð ð KOð )ð )ð )ð )ð )ð )ð&ð ð ð ð ð r   rN   )Ú
__future__r   ÚloggingÚtypingr   r   r   r   r   r	   r
   r   Úlangchain_core.chat_loadersr   Úlangchain_core.chat_sessionsr   Úlangchain_core.load.loadr   r   r   r;   r   Ú	getLoggerrG   r>   r   rN   rL   r   r   ú<module>rf      s7  ðØ "Ð "Ð "Ð "Ð "Ð "à €€€Ø WÐ WÐ WÐ WÐ WÐ WÐ WÐ WÐ WÐ WÐ WÐ WÐ WÐ WÐ WÐ WÐ WÐ WÐ WÐ Wà 6Ð 6Ð 6Ð 6Ð 6Ð 6Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø )Ð )Ð )Ð )Ð )Ð )àð &Ø'Ð'Ð'Ð'Ð'Ð'Ø%Ð%Ð%Ð%Ð%Ð%à	ˆÔ	˜8Ñ	$Ô	$€ðYð Yð Yð Yð Y˜^ñ Yô Yð Yðx2ð 2ð 2ð 2ð 2 ñ 2ô 2ð 2ð 2ð 2r   