
    dh9                     z    S SK r S SKrS SKJrJrJrJrJr  S SKJ	r	J
r
  S SKJr  S SKJr  S SKJr   " S S\5      rg)	    N)AnyDictListOptionalcast)AgentActionAgentFinish)BaseCallbackHandler)	LLMResult)parsec                     ^  \ rS rSr% SrSr\\S'   \ S3r\\S'   Sr	\\S'   S	r
\\S
'      S/S\S\\   S\\   S\\   SS4
U 4S jjjrS\\\4   S\\   S\SS4S jrS\S\SS4S jrS\S\SS4S jrS\S\SS4S jrS\\\4   S\\\4   S\SS4S jrS\\\4   S\SS4S jrS\S\SS4S  jrS\\\4   S!\S\SS4S" jrS#\S\S\4S$ jr  S0S%\S&\\   S'\\   S\SS4
S( jjrS\S\SS4S) jrS*\S\SS4S+ jrS,\S\SS4S- jr S.r!U =r"$ )1ArgillaCallbackHandler   a8  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                   > [         T
U ]  5          SSKnUR                  U l        [        U R                  5      [        S5      :  a  [        SU R                   S35      eUcG  [        R                  " S5      c0  [        R                  " SU R                   S	35        U R                  nUcr  [        R                  " S
5      c[  [        U R                  5      [        S5      :  a  SOSU l        [        R                  " SU R                   S35        U R                  n UR                  XCS9  Xl        U=(       d    UR%                  5       U l         0 n[        U R                  5      [        S5      :  a-  [        R                  " SU R                   S3[(        5        SS0nUR*                  R,                  " S'U R"                  U R&                  S.UD6U l        SS /nXR.                  R4                   V	s/ sH  oR6                  PM     sn	:w  af  [9        S!U R"                   SU R&                   S"U S#U R.                  R4                   V	s/ sH  oR6                  PM     sn	 S$U R2                   S%35      e0 U l        [        R                  " S&U R                    S35        g! [
         a    [        S5      ef = f! [         a"  n[        SU SU R                    S35      UeSnAff = f! [         aI  n[1        SU SU R"                   SU R&                   SU R2                   SU R                    S35      UeSnAff = fs  sn	f s  sn	f )(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   NzTo 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 .zThe `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             f/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/callbacks/argilla_callback.pyr%   ArgillaCallbackHandler.__init__=   s   < 		 #%>>D  %%&w75d6J6J5K LJ J  ?ryy):;CMM1151E1E0F G>> **G?ryy):;C --.x@ #   MM1151E1E0F G>> **G	GGGG5 ),B0@0@0B	JT))*U8_<'(<(<'= >. .
 -e4
--:: &&-- DL %j18K8KL8Ku

8KLL.t/@/@.AAS&&' (BBRAS TAZ^ZfZfZmZmAnZmQV**ZmAn@o p??C}}oQP  .0OO$$?A	
}  	Y 	X  	!@ D' (,&77RT
 	6  	#RSTRU V==A=N=N<O P"112 3**.-- 9((,'88SU 	  M
 BosI   J& !J? B	K. :MM	&J<?
K+	K&&K+.
M8AL<<M
serializedr:   kwargsc                 n    U R                   R                  [        US   =(       d    US   5      U05        g)z.Save the prompts in memory when an LLM starts.parent_run_idrun_idN)r:   updatestr)r;   rD   r:   rE   s       rB   on_llm_start#ArgillaCallbackHandler.on_llm_start   s/     	S!8!LF8<LMwWX    tokenc                     g)z)Do nothing when a new token is generated.Nr#   )r;   rN   rE   s      rB   on_llm_new_token'ArgillaCallbackHandler.on_llm_new_token       rM   r!   c           
         US   (       a  gU R                   [        US   5         n[        X1R                  5       HK  u  pEU R                  R                  U Vs/ sH"  nSUUR                  R                  5       S.0PM$     snS9  MM     U R                   R                  [        US   5      5        [        U R                  5      [        S5      :  a  U R                  R                  5         ggs  snf )z(Log records to Argilla when an LLM ends.rG   NrH   r8   r    r!   recordsr   )r:   rJ   zipgenerationsr6   add_recordstextstrippopr   r(   push_to_argilla)r;   r!   rE   r:   r    rX   
generations          rB   
on_llm_end!ArgillaCallbackHandler.on_llm_end   s     /" ,,s6(#345#&w0D0D#EFLL$$ '2 '2
 !&,(2(=(=(?# '2 % 
 $F 	VH-./%%&x8LL((* 9s   (C0
errorc                     g)z%Do nothing when LLM outputs an error.Nr#   r;   ra   rE   s      rB   on_llm_error#ArgillaCallbackHandler.on_llm_error   rR   rM   inputsc                     SU;   aW  U R                   R                  [        US   =(       d    US   5      [        US   [        5      (       a  US   OUS   /05        gg)zIf 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.
inputrG   rH   N)r:   rI   rJ   
isinstancelist)r;   rD   rf   rE   s       rB   on_chain_start%ArgillaCallbackHandler.on_chain_start   sc     fLL/C6(3CD%fWot<< w$Wo.	 rM   outputsc                 :  ^  [        U 4S j[        US   5      [        US   5      4 5       5      (       d  gT R                  R                  [        US   5      5      =(       d6    [	        [
        T R                  R                  [        US   5      / 5      5      nUR                  5        H  u  pE[        U[        5      (       aN  T R                  R                  [        X55       VVs/ sH  u  pgSUUS   R                  5       S.0PM     snnS9  Mh  T R                  R                  SS	R                  U5      UR                  5       S.0/S9  M     [        US   5      T R                  ;   a'  T R                  R                  [        US   5      5        [        US   5      T R                  ;   a'  T R                  R                  [        US   5      5        [        T R                   5      [        S
5      :  a  T R                  R#                  5         ggs  snnf )zIf 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   @   >#    U H  nUTR                   ;   v   M     g 7f)N)r:   ).0keyr;   s     rB   	<genexpr>6ArgillaCallbackHandler.on_chain_end.<locals>.<genexpr>  s!      
L 4<<Ls   rG   rH   Nr8   rZ   rT   rU    r   )anyrJ   r:   getr   r   itemsri   rj   r6   rY   rW   r[   joinr\   r   r(   r]   )r;   rm   rE   r:   chain_output_keychain_output_valr    outputs   `       rB   on_chain_end#ArgillaCallbackHandler.on_chain_end  s   
  
F?34c&:J6KL
 
 
 ((VO-D)EF 
$$,,""3vh'7#8"=K
 3:--/.*D11(( /2'.L /MNF %*0,26N,@,@,B' /M ) 
 (( %*-((7*;,<,B,B,D' ) 	! 3B8 vo&'4<<7LLS!89:vh DLL0LLS!123%%&x8LL((* 9;s   (#Hc                     g)z+Do nothing when LLM chain outputs an error.Nr#   rc   s      rB   on_chain_error%ArgillaCallbackHandler.on_chain_error8  rR   rM   	input_strc                     g)zDo nothing when tool starts.Nr#   )r;   rD   r   rE   s       rB   on_tool_start$ArgillaCallbackHandler.on_tool_start<  s     	rM   actionc                     g)z.Do nothing when agent takes a specific action.Nr#   )r;   r   rE   s      rB   on_agent_action&ArgillaCallbackHandler.on_agent_actionE  rR   rM   r{   observation_prefix
llm_prefixc                     g)zDo nothing when tool ends.Nr#   )r;   r{   r   r   rE   s        rB   on_tool_end"ArgillaCallbackHandler.on_tool_endI  s     	rM   c                     g)z&Do nothing when tool outputs an error.Nr#   rc   s      rB   on_tool_error$ArgillaCallbackHandler.on_tool_errorS  rR   rM   rZ   c                     gz
Do nothingNr#   )r;   rZ   rE   s      rB   on_textArgillaCallbackHandler.on_textW  rR   rM   finishc                     gr   r#   )r;   r   rE   s      rB   on_agent_finish&ArgillaCallbackHandler.on_agent_finish[  rR   rM   )r(   r.   r6   r   r:   r   )NNN)NN)#__name__
__module____qualname____firstlineno____doc__r   rJ   __annotations__r   r   r   r   r%   r   r   r   rK   rP   r   r_   BaseExceptionrd   rk   r|   r   r   r   r   r   r   r   r	   r   __static_attributes____classcell__)rA   s   @rB   r   r      st   )V <Hc;!
'*J* GHc  G2OS2
 )-!%!%I
I
 !I
 #	I

 #I
 
I
 I
VYsCx.Y379YHKY	Yc S T +9 + + +:- 3 4 sCx.26sCx.LO	&0+DcN 0+c 0+d 0+dM S T cN  	
 
k S S  -1$(	 %SM SM	
  
= C D C 3 4 k S T  rM   r   )r*   r,   typingr   r   r   r   r   langchain_core.agentsr   r	   langchain_core.callbacksr
   langchain_core.outputsr   packaging.versionr   r   r#   rM   rB   <module>r      s,    	  2 2 : 8 , #R0 RrM   