Ë
    µŒj’9  ã                   óz   — d dl Z d dlZd dl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  G d„ de«      Zy)	é    N)ÚAnyÚDictÚListÚOptionalÚcast)ÚAgentActionÚAgentFinish)ÚBaseCallbackHandler)Ú	LLMResult)Úparsec                   óô  ‡ — e Zd ZU dZdZeed<   e› d�Zeed<   dZeed<   dZ	eed	<   	 	 	 d-dede
e   de
e   de
e   dd
f
ˆ 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efd#„Z	 	 d.d$ed%e
e   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ˆ xZ S )/ÚArgillaCallbackHandleraÐ  Callback Handler that logs into Argilla.

    Args:
        dataset_name: name of the `FeedbackDataset` in Argilla. Note that it must
            exist in advance. If you need help on how to create a `FeedbackDataset` in
            Argilla, please visit
            https://docs.argilla.io/en/latest/tutorials_and_integrations/integrations/use_argilla_callback_in_langchain.html.
        workspace_name: name of the workspace in Argilla where the specified
            `FeedbackDataset` lives in. Defaults to `None`, which means that the
            default workspace will be used.
        api_url: URL of the Argilla Server that we want to use, and where the
            `FeedbackDataset` lives in. Defaults to `None`, which means that either
            `ARGILLA_API_URL` environment variable or the default will be used.
        api_key: API Key to connect to the Argilla Server. Defaults to `None`, which
            means that either `ARGILLA_API_KEY` environment variable or the default
            will be used.

    Raises:
        ImportError: if the `argilla` package is not installed.
        ConnectionError: if the connection to Argilla fails.
        FileNotFoundError: if the `FeedbackDataset` retrieval from Argilla fails.

    Examples:
        >>> from langchain_community.llms import OpenAI
        >>> from langchain_community.callbacks import ArgillaCallbackHandler
        >>> argilla_callback = ArgillaCallbackHandler(
        ...     dataset_name="my-dataset",
        ...     workspace_name="my-workspace",
        ...     api_url="http://localhost:6900",
        ...     api_key="argilla.apikey",
        ... )
        >>> llm = OpenAI(
        ...     temperature=0,
        ...     callbacks=[argilla_callback],
        ...     verbose=True,
        ...     openai_api_key="API_KEY_HERE",
        ... )
        >>> llm.generate([
        ...     "What is the best NLP-annotation tool out there? (no bias at all)",
        ... ])
        "Argilla, no doubt about it."
    z%https://github.com/argilla-io/argillaÚREPO_URLz/issuesÚ
ISSUES_URLzphttps://docs.argilla.io/en/latest/tutorials_and_integrations/integrations/use_argilla_callback_in_langchain.htmlÚBLOG_URLzhttp://localhost:6900ÚDEFAULT_API_URLNÚdataset_nameÚworkspace_nameÚapi_urlÚapi_keyÚreturnc                 óx  •— t         ‰
| �  «        	 ddl}|j                  | _        t        | j                  «      t        d«      k  rt        d| j                  › d�«      ‚|€Dt        j                  d«      €/t        j                  d| j                  › d	�«       | j                  }|€nt        j                  d
«      €Yt        | j                  «      t        d«      k  rdnd| _        t        j                  d| j                  › d�«       | j                  }	 |j                  ||¬«       || _        |xs |j%                  «       | _        	 i }t        | j                  «      t        d«      k  r,t        j                  d| j                  › d�t(        «       ddi} |j*                  j,                  d'| j"                  | j&                  dœ|¤Ž| _        dd g}|| j.                  j4                  D �	cg c]  }	|	j6                  ‘Œ c}	k7  ret9        d!| j"                  › d| j&                  › d"|› d#| j.                  j4                  D �	cg c]  }	|	j6                  ‘Œ c}	› d$| j2                  › d%�«      ‚i | _        t        j                  d&| j                   › d�«       y# t
        $ r t        d«      ‚w xY w# t        $ r"}t        d|› d| j                   › d�«      |‚d}~ww xY w# t        $ rI}t1        d|› d| j"                  › d| j&                  › d| j2                  › d| j                   › d�«      |‚d}~ww xY wc c}	w c c}	w )(a)  Initializes the `ArgillaCallbackHandler`.

        Args:
            dataset_name: name of the `FeedbackDataset` in Argilla. Note that it must
                exist in advance. If you need help on how to create a `FeedbackDataset`
                in Argilla, please visit
                https://docs.argilla.io/en/latest/tutorials_and_integrations/integrations/use_argilla_callback_in_langchain.html.
            workspace_name: name of the workspace in Argilla where the specified
                `FeedbackDataset` lives in. Defaults to `None`, which means that the
                default workspace will be used.
            api_url: URL of the Argilla Server that we want to use, and where the
                `FeedbackDataset` lives in. Defaults to `None`, which means that either
                `ARGILLA_API_URL` environment variable or the default will be used.
            api_key: API Key to connect to the Argilla Server. Defaults to `None`, which
                means that either `ARGILLA_API_KEY` environment variable or the default
                will be used.

        Raises:
            ImportError: if the `argilla` package is not installed.
            ConnectionError: if the connection to Argilla fails.
            FileNotFoundError: if the `FeedbackDataset` retrieval from Argilla fails.
        r   Nz‰To use the Argilla callback manager you need to have the `argilla` Python package installed. Please install it with `pip install argilla`z1.8.0z#The installed `argilla` version is z} but `ArgillaCallbackHandler` requires at least version 1.8.0. Please upgrade `argilla` with `pip install --upgrade argilla`.ÚARGILLA_API_URLz[Since `api_url` is None, and the env var `ARGILLA_API_URL` is not set, it will default to `z6`, which is the default API URL in Argilla Quickstart.ÚARGILLA_API_KEYz1.11.0zadmin.apikeyzowner.apikeyz[Since `api_key` is None, and the env var `ARGILLA_API_KEY` is not set, it will default to `z6`, which is the default API key in Argilla Quickstart.)r   r   z.Could not connect to Argilla with exception: 'z“'.
Please check your `api_key` and `api_url`, and make sure that the Argilla server is up and running. If the problem persists please report it to z as an `integration` issue.ú1.14.0zYou have Argilla z., but Argilla 1.14.0 or higher is recommended.Úwith_recordsF)ÚnameÚ	workspacez@`FeedbackDataset` retrieval from Argilla failed with exception `z+`.
Please check that the dataset with name=z in the workspace=zz exists in advance. If you need help on how to create a `langchain`-compatible `FeedbackDataset` in Argilla, please visit z.. If the problem persists please report it to ÚpromptÚresponsez`FeedbackDataset` with name=z] had fields that are not supported yet for the`langchain` integration. Supported fields are: z/, and the current `FeedbackDataset` fields are zl. For more information on how to create a `langchain`-compatible `FeedbackDataset` in Argilla, please visit Ú.zˆThe `ArgillaCallbackHandler` is currently in beta and is subject to change based on updates to `langchain`. Please report any issues to © )ÚsuperÚ__init__ÚargillaÚ__version__ÚARGILLA_VERSIONÚImportErrorr   ÚosÚgetenvÚwarningsÚwarnr   ÚDEFAULT_API_KEYÚinitÚ	ExceptionÚConnectionErrorr   r   Úget_workspacer   ÚUserWarningÚFeedbackDatasetÚfrom_argillaÚdatasetÚFileNotFoundErrorr   Úfieldsr   Ú
ValueErrorÚprompts)Úselfr   r   r   r   ÚrgÚeÚ
extra_argsÚsupported_fieldsÚfieldÚ	__class__s             €úx/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/langchain_community/callbacks/argilla_callback.pyr$   zArgillaCallbackHandler.__init__=   s²  ø€ ô< 	‰ÑÔð	Û à#%§>¡>ˆDÔ ô �×%Ñ%Ó&¬¨w«Ò7ÜØ5°d×6JÑ6JÐ5Kð LJð Jóð ð ˆ?œrŸy™yÐ):Ó;ÐCÜ�M‰Mð1Ø15×1EÑ1EÐ0Fð G>ð>ôð ×*Ñ*ˆGàˆ?œrŸy™yÐ):Ó;ÐCô ˜×-Ñ-Ó.´°x³Ò@ñ à#ð Ô ô �M‰Mð1Ø15×1EÑ1EÐ0Fð G>ð>ôð ×*Ñ*ˆGð	Ø�G‰G˜G¨WˆGÔ5ð )ˆÔØ,ÒB°×0@Ñ0@Ó0BˆÔð	ØˆJÜ�T×)Ñ)Ó*¬U°8«_Ò<Ü—‘Ø'¨×(<Ñ(<Ð'=ð >.ð .äôð
 -¨eÐ4�
Ø:˜2×-Ñ-×:Ñ:ð Ø×&Ñ&Ø×-Ñ-ñð ñˆDŒLð % jÐ1ÐØ¸¿¹×8KÒ8KÓLÑ8K¨u §
£
Ð8KÑLÒLÜØ.¨t×/@Ñ/@Ð.AÐASØ×&Ñ&Ð'ð (BØBRÐASð TAØZ^×ZfÑZf×ZmÒZmÓAnÑZmÐQVÀ%Ç*Ã*ÐZmÑAnÐ@oð p?à?C¿}¹}¸oÈQðPóð ð .0ˆŒä�‰ðà—O‘OÐ$Ð$?ðAõ	
øô} ò 	ÜðYóð ð	ûôX ò 	Ü!Ø@ÀÀð D'ð (,§¡Ð&7Ð7RðTóð
 ðûð	ûô6 ò 	Ü#ØRÐSTÐRUð V=Ø=A×=NÑ=NÐ<Oð PØ"×1Ñ1Ð2ð 3*à*.¯-©-¨ð 9(Ø(,¯©Ð'8Ð8SðUóð ðûð	üò  Mùò
 BosI   ‘J ÄJ/ ÅBK Ç/L2ÉL7ÊJ,Ê/	KÊ8KËKË	L/Ë&AL*Ì*L/Ú
serializedr9   Úkwargsc                 ód   — | j                   j                  t        |d   xs |d   «      |i«       y)z.Save the prompts in memory when an LLM starts.Úparent_run_idÚrun_idN)r9   ÚupdateÚstr)r:   rB   r9   rC   s       rA   Úon_llm_startz#ArgillaCallbackHandler.on_llm_startÈ   s/   € ð 	�‰×ÑœS ¨Ñ!8Ò!L¸FÀ8Ñ<LÓMÈwÐWÕXó    Útokenc                  ó   — y)z)Do nothing when a new token is generated.Nr"   )r:   rK   rC   s      rA   Úon_llm_new_tokenz'ArgillaCallbackHandler.on_llm_new_tokenÎ   ó   € àrJ   r    c           
      óà  — |d   ry| j                   t        |d   «         }t        ||j                  «      D ]L  \  }}| j                  j                  |D �cg c]!  }d||j                  j                  «       dœi‘Œ# c}¬«       ŒN | j                   j                  t        |d   «      «       t        | j                  «      t        d«      k  r| j                  j                  «        yyc c}w )z(Log records to Argilla when an LLM ends.rE   NrF   r7   ©r   r    ©Úrecordsr   )r9   rH   ÚzipÚgenerationsr5   Úadd_recordsÚtextÚstripÚpopr   r'   Úpush_to_argilla)r:   r    rC   r9   r   rT   Ú
generations          rA   Ú
on_llm_endz!ArgillaCallbackHandler.on_llm_endÒ   sê   € ð �/Ò"Øð —,‘,œs 6¨(Ñ#3Ó4Ñ5ˆÜ#& w°×0DÑ0DÖ#EÑˆF�KØ�L‰L×$Ñ$ñ '2óñ '2˜
ð !Ø&,Ø(2¯©×(=Ñ(=Ó(?ñ#òð '2ñð %õ 
ð $Fð 	�‰×Ñœ˜V HÑ-Ó.Ô/ä�×%Ñ%Ó&¬¨x«Ò8à�L‰L×(Ñ(Õ*ð 9ùòs   Á&C+
Úerrorc                  ó   — y)z%Do nothing when LLM outputs an error.Nr"   ©r:   r\   rC   s      rA   Úon_llm_errorz#ArgillaCallbackHandler.on_llm_errorï   rN   rJ   Úinputsc                 ó¦   — d|v rM| j                   j                  t        |d   xs |d   «      t        |d   t        «      r|d   n|d   gi«       yy)a  If the key `input` is in `inputs`, then save it in `self.prompts` using
        either the `parent_run_id` or the `run_id` as the key. This is done so that
        we don't log the same input prompt twice, once when the LLM starts and once
        when the chain starts.
        ÚinputrE   rF   N)r9   rG   rH   Ú
isinstanceÚlist)r:   rB   r`   rC   s       rA   Úon_chain_startz%ArgillaCallbackHandler.on_chain_startó   s`   € ð �fÑØ�L‰L×Ñä˜˜Ñ/ÒC°6¸(Ñ3CÓDä% f¨W¡o´tÔ<ð ˜wšà$ W™oÐ.ð	õð rJ   Úoutputsc                 ó  ‡ — t        ˆ fd„t        |d   «      t        |d   «      fD «       «      sy‰ j                  j                  t        |d   «      «      xs6 t	        t
        ‰ j                  j                  t        |d   «      g «      «      }|j                  «       D ]¤  \  }}t        |t        «      rP‰ j                  j                  t        ||«      D ��cg c]  \  }}d||d   j                  «       dœi‘Œ c}}¬«       Œf‰ j                  j                  dd	j                  |«      |j                  «       dœig¬«       Œ¦ t        |d   «      ‰ j                  v r'‰ j                  j                  t        |d   «      «       t        |d   «      ‰ j                  v r'‰ j                  j                  t        |d   «      «       t        ‰ j                   «      t        d
«      k  r‰ j                  j#                  «        yyc c}}w )z×If either the `parent_run_id` or the `run_id` is in `self.prompts`, then
        log the outputs to Argilla, and pop the run from `self.prompts`. The behavior
        differs if the output is a list or not.
        c              3   ó:   •K  — | ]  }|‰j                   v –— Œ y ­w)N)r9   )Ú.0Úkeyr:   s     €rA   Ú	<genexpr>z6ArgillaCallbackHandler.on_chain_end.<locals>.<genexpr>  s"   øè ø€ ð 
áL�ð �4—<‘<ÔÙLùs   ƒrE   rF   Nr7   rV   rP   rQ   Ú r   )ÚanyrH   r9   Úgetr   r   Úitemsrc   rd   r5   rU   rS   rW   ÚjoinrX   r   r'   rY   )r:   rf   rC   r9   Úchain_output_keyÚchain_output_valr   Úoutputs   `       rA   Úon_chain_endz#ArgillaCallbackHandler.on_chain_end  sç  ø€ ô
 ó 
ä˜F ?Ñ3Ó4´c¸&ÀÑ:JÓ6KÑLó
ô 
ð ØŸ™×(Ñ(¬¨V°OÑ-DÓ)EÓFò 
Ì$Ü�$—,‘,×"Ñ"¤3 v¨hÑ'7Ó#8¸"Ó=óK
ˆð 3:·-±-¶/Ñ.ÐÐ.ÜÐ*¬DÔ1à—‘×(Ñ(ô /2°'Ð;KÔ.Lôñ /M™N˜F Fð %Ø*0Ø,2°6©N×,@Ñ,@Ó,Bñ'òð /Mòð )õ 
ð —‘×(Ñ(ð %Ø*-¯(©(°7Ó*;Ø,<×,BÑ,BÓ,Dñ'ððð )õ 	ð! 3Bô8 ˆv�oÑ&Ó'¨4¯<©<Ñ7Ø�L‰L×ÑœS ¨Ñ!8Ó9Ô:Üˆv�hÑÓ  D§L¡LÑ0Ø�L‰L×ÑœS ¨Ñ!1Ó2Ô3ä�×%Ñ%Ó&¬¨x«Ò8à�L‰L×(Ñ(Õ*ð 9ùó;s   Ã"Hc                  ó   — y)z+Do nothing when LLM chain outputs an error.Nr"   r^   s      rA   Úon_chain_errorz%ArgillaCallbackHandler.on_chain_error8  rN   rJ   Ú	input_strc                  ó   — y)zDo nothing when tool starts.Nr"   )r:   rB   rw   rC   s       rA   Úon_tool_startz$ArgillaCallbackHandler.on_tool_start<  s   € ð 	rJ   Úactionc                  ó   — y)z.Do nothing when agent takes a specific action.Nr"   )r:   rz   rC   s      rA   Úon_agent_actionz&ArgillaCallbackHandler.on_agent_actionE  rN   rJ   rs   Úobservation_prefixÚ
llm_prefixc                  ó   — y)zDo nothing when tool ends.Nr"   )r:   rs   r}   r~   rC   s        rA   Úon_tool_endz"ArgillaCallbackHandler.on_tool_endI  s   € ð 	rJ   c                  ó   — y)z&Do nothing when tool outputs an error.Nr"   r^   s      rA   Úon_tool_errorz$ArgillaCallbackHandler.on_tool_errorS  rN   rJ   rV   c                  ó   — y©z
Do nothingNr"   )r:   rV   rC   s      rA   Úon_textzArgillaCallbackHandler.on_textW  rN   rJ   Úfinishc                  ó   — yr„   r"   )r:   r†   rC   s      rA   Úon_agent_finishz&ArgillaCallbackHandler.on_agent_finish[  rN   rJ   )NNN)NN)!Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rH   Ú__annotations__r   r   r   r   r$   r   r   r   rI   rM   r   r[   ÚBaseExceptionr_   re   rt   rv   ry   r   r|   r€   r‚   r…   r	   rˆ   Ú__classcell__)r@   s   @rA   r   r      si  ø… ñ)ðV <€HˆcÓ;Ø!˜
 'Ð*€J�Ó*ð G€Hˆcó  Gà2€O�SÓ2ð
 )-Ø!%Ø!%ñI
àðI
ð ! ™ðI
ð ˜#‘ð	I
ð
 ˜#‘ðI
ð 
õI
ðVYØ˜s C˜x™.ðYØ37¸±9ðYØHKðYà	óYð cð °Sð ¸Tó ð+ 9ð +¸ð +Àó +ð: -ð ¸3ð À4ó ðØ˜s C˜x™.ðØ26°s¸C°x±.ðØLOðà	óð&0+ D¨¨c¨¡Nð 0+¸cð 0+Àdó 0+ðd Mð ¸Sð ÀTó ðà˜˜c˜‘Nðð ðð ð	ð
 
óð kð ¸Sð ÀSó ð -1Ø$(ñ	àðð % S™Mðð ˜S‘Mð	ð
 ðð 
óð =ð ¸Cð ÀDó ð˜Cð ¨3ð °4ó ð kð ¸Sð ÀT÷ rJ   r   )r)   r+   Útypingr   r   r   r   r   Úlangchain_core.agentsr   r	   Úlangchain_core.callbacksr
   Úlangchain_core.outputsr   Úpackaging.versionr   r   r"   rJ   rA   Ú<module>r•      s,   ðÛ 	Û ß 2Õ 2ç :Ý 8Ý ,Ý #ôRÐ0õ RrJ   