
    Ng76                         d dl 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 d dlmZ d dlmZmZ d dlmZ d dlmZmZ d d	lmZmZ  G d
 de          Zdeeef         de
eef         fdZ G d de          ZdS )    N)datetime)Enum)AnyDictListOptionalTupleUnion)UUID)AgentActionAgentFinish)BaseCallbackHandler)BaseMessageChatMessage)
Generation	LLMResultc                       e Zd ZdZdZdZdS )LabelStudioModezLabel Studio mode enumerator.promptchatN)__name__
__module____qualname____doc__PROMPTCHAT     n/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/callbacks/labelstudio_callback.pyr   r      s        ''FDDDr   r   modereturnc                     t           j        j        dt           j        j        di}t	          | t
                    rt          |           } || j                 | fS )zGet default Label Studio configs for the given mode.

    Parameters:
        mode: Label Studio mode ("prompt" or "chat")

    Returns: Tuple of Label Studio config and mode
    a	  
<View>
<Style>
    .prompt-box {
        background-color: white;
        border-radius: 10px;
        box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
        padding: 20px;
    }
</Style>
<View className="root">
    <View className="prompt-box">
        <Text name="prompt" value="$prompt"/>
    </View>
    <TextArea name="response" toName="prompt"
              maxSubmissions="1" editable="true"
              required="true"/>
</View>
<Header value="Rate the response:"/>
<Rating name="rating" toName="prompt"/>
</View>a  
<View>
<View className="root">
     <Paragraphs name="dialogue"
               value="$prompt"
               layout="dialogue"
               textKey="content"
               nameKey="role"
               granularity="sentence"/>
  <Header value="Final response:"/>
    <TextArea name="response" toName="dialogue"
              maxSubmissions="1" editable="true"
              required="true"/>
</View>
<Header value="Rate the response:"/>
<Rating name="rating" toName="dialogue"/>
</View>)r   r   valuer   
isinstancestr)r    _default_label_configss     r   get_default_label_configsr'      s^     	$ '* 	" %-'R $ %t$$!$*-t33r   c                       e Zd ZU dZdZeed<   dddedej        fde	e         de	e         de	e
         ded	e	e         d
eeef         f fdZdedeee                  ddfdZdeeef         dee         deddfdZdedefdZdddddeeef         deee                  dede	e         de	ee                  de	eeef                  dede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!	 	 d4d+ed,e	e         d-e	e         deddf
d.Z"d ededdfd/Z#d0ededdfd1Z$d2e%deddfd3Z& xZ'S )5LabelStudioCallbackHandlera  Label Studio callback handler.
    Provides the ability to send predictions to Label Studio
    for human evaluation, feedback and annotation.

    Parameters:
        api_key: Label Studio API key
        url: Label Studio URL
        project_id: Label Studio project ID
        project_name: Label Studio project name
        project_config: Label Studio project config (XML)
        mode: Label Studio mode ("prompt" or "chat")

    Examples:
        >>> from langchain_community.llms import OpenAI
        >>> from langchain_community.callbacks import LabelStudioCallbackHandler
        >>> handler = LabelStudioCallbackHandler(
        ...             api_key='<your_key_here>',
        ...             url='http://localhost:8080',
        ...             project_name='LangChain-%Y-%m-%d',
        ...             mode='prompt'
        ... )
        >>> llm = OpenAI(callbacks=[handler])
        >>> llm.invoke('Tell me a story about a dog.')
    zLangChain-%Y-%m-%dDEFAULT_PROJECT_NAMENapi_keyurl
project_idproject_nameproject_configr    c                    t                                                       	 dd l}n+# t          $ r t          d| j        j         d          w xY w|s`t          j        d          r"t          t          j        d                    }n*t          d| j        j         d| j        j         d          || _
        |sZt          j        d          rt          j        d          }n1t          j        d|j         d	| j        j         d
           |j        }|| _        i | _        |                    | j        | j
                  | _        || _        |r|| _        d | _        nt+          |          \  | _        | _        |pt          j        d          | _        | j        2| j                            t1          | j                            | _        nt5          j                                        | j                  }| j                            |          }	|	r|	d         | _        | j        j        | _        n7| j                            || j                  | _        | j        j        | _        | j        j         | _         d\  | _!        | _"        | _#        | _$        | j         %                                D ]_\  }
}|d         dk    rN|
| _!        |d         d         | _"        |d         d         d         | _#        |d         d         d         | _$         n`| j!        s:d| j         d}| j        tL          j'        k    r|dz  }n|dz  }t          |          d S )Nr   zYou're using z in your code, but you don't have the LabelStudio SDK Python package installed or upgraded to the latest version. Please run `pip install -U label-studio-sdk` before using this callback.LABEL_STUDIO_API_KEYz in your code, Label Studio API key is not provided. Please provide Label Studio API key: go to the Label Studio instance, navigate to Account & Settings -> Access Token and copy the key. Use the key as a parameter for the callback: zr(label_studio_api_key='<your_key_here>', ...) or set the environment variable LABEL_STUDIO_API_KEY=<your_key_here>LABEL_STUDIO_URLz5Label Studio URL is not provided, using default URL: z8If you want to provide your own URL, use the parameter: zj(label_studio_url='<your_url_here>', ...) or set the environment variable LABEL_STUDIO_URL=<your_url_here>)r,   r+   LABEL_STUDIO_PROJECT_ID)title)r4   label_config)NNNNtypeTextAreato_nameinputsr#   zLabel Studio project "zI" does not have a TextArea tag. Please add a TextArea tag to the project.z
HINT: go to project Settings -> Labeling Interface -> Browse Templates and select "Generative AI -> Supervised Language Model Fine-tuning" template.z
HINT: go to project Settings -> Labeling Interface -> Browse Templates and check available templates under "Generative AI" section.)(super__init__label_studio_sdkImportError	__class__r   osgetenvr%   
ValueErrorr+   warningswarnLABEL_STUDIO_DEFAULT_URLr,   payloadClient	ls_clientr.   r/   r    r'   r-   get_projectint
ls_projectr   todaystrftimeget_projectsidcreate_projectparsed_label_config	from_namer8   r#   
input_typeitemsr   r   )selfr+   r,   r-   r.   r/   r    lsproject_titleexisting_projectstag_nametag_infoerror_messager>   s                r   r;   z#LabelStudioCallbackHandler.__init__j   s    			))))) 	 	 	0 7 0 0 0  	  	y/00 bi(>??@@ YDN$; Y Y ~.Y Y Y
 
 
  	2y+,, 2i 233X*,*EX X ~.X X X   1 )+txFF( 	M"0DDII-Ft-L-L*D$L	2K(L(L?&"n88T_9M9MNNDOO$N,,55d6GHHM $ ; ;- ; P P  5"3A"6"&/"4"&."?"?'d6I #@ # # #'/"4#'?#F E
Adj$/ #'":"@"@"B"B 	 	Hh:--!)'	215%h/27;
"*8"4Q"7"? . ~ 	,=): = = = 
 yO222G / ]+++)	, 	,s	   ( (Arun_idgenerationsr!   c           
         g }| j         |         d         }| j         |         d                             di                               d          }t          ||          D ]F\  }}|                    | j        |d|i| j        | j        ddd |D             id	g|d
gd           G| j                            |           d S )Npromptskwargsinvocation_params
model_namer[   textareatextc                     g | ]	}|j         
S r   )rc   ).0gs     r   
<listcomp>zFLabelStudioCallbackHandler.add_prompts_generations.<locals>.<listcomp>   s    6R6R6R!qv6R6R6Rr   )rQ   r8   r6   r#   )resultmodel_version)datapredictions)	rE   getzipappendr#   rQ   r8   rJ   import_tasks)rT   r[   r\   tasksr^   ri   r   
generations           r   add_prompts_generationsz2LabelStudioCallbackHandler.add_prompts_generations   s
    ,v&y1L *S$b))S 	
 #&g{";"; 	 	FJLL 
F & 26/3|,6.46R6Rz6R6R6R-S	!" !"' .;
 
$    * 	$$U+++++r   
serializedr^   r_   c                     | j         dk    r t          d| j         d| j          d          t          |d                   }||d| j        |<   dS ).Save the prompts in memory when an LLM starts.Text
Label Studio project "" has an input type <zw>. To make it work with the mode="chat", the input type should be <Text>.
Read more here https://labelstud.io/tags/textr[   )r^   r_   N)rR   rA   r.   r%   rE   )rT   rs   r^   r_   r[   s        r   on_llm_startz'LabelStudioCallbackHandler.on_llm_start  s     ?f$$A4+< A A&*oA A A   VH%&&+2fEEVr   messagec                 R    t          |t                    r|j        S |j        j        S )zGet the role of the message.)r$   r   roler>   r   )rT   rz   s     r   _get_message_rolez,LabelStudioCallbackHandler._get_message_role  s(    g{++ 	.<$--r   )parent_run_idtagsmetadatamessagesr~   r   r   c                >   | j         dk    r t          d| j         d| j          d          g }|D ]N}	g }
|	D ]2}|
                    |                     |          |j        d           3|                    |
           O||||||d| j        t          |          <   dS )ru   
Paragraphsrw   rx   z>. To make it work with the mode="chat", the input type should be <Paragraphs>.
Read more here https://labelstud.io/tags/paragraphs)r|   content)r^   r   r   r[   r~   r_   N)rR   rA   r.   rn   r}   r   rE   r%   )rT   rs   r   r[   r~   r   r   r_   r^   message_listdialogrz   s               r   on_chat_model_startz.LabelStudioCallbackHandler.on_chat_model_start  s     ?l**G4+< G G&*oG G G   $ 		# 		#LF'   $ 6 6w ? ?#*?     NN6"""" *%
 %
S[[!!!r   tokenc                     dS )z)Do nothing when a new token is generated.Nr   )rT   r   r_   s      r   on_llm_new_tokenz+LabelStudioCallbackHandler.on_llm_new_tokenG      r   responsec                     t          |d                   }|                     ||j                   | j                            |           dS )z>Create a new Label Studio task for each prompt and generation.r[   N)r%   rr   r\   rE   pop)rT   r   r_   r[   s       r   
on_llm_endz%LabelStudioCallbackHandler.on_llm_endK  sM    VH%&& 	$$VX-ABBB 	     r   errorc                     dS )z%Do nothing when LLM outputs an error.Nr   rT   r   r_   s      r   on_llm_errorz'LabelStudioCallbackHandler.on_llm_errorU  r   r   r9   c                     d S Nr   )rT   rs   r9   r_   s       r   on_chain_startz)LabelStudioCallbackHandler.on_chain_startY  s	     	r   outputsc                     d S r   r   )rT   r   r_   s      r   on_chain_endz'LabelStudioCallbackHandler.on_chain_end^  s    r   c                     dS )z+Do nothing when LLM chain outputs an error.Nr   r   s      r   on_chain_errorz)LabelStudioCallbackHandler.on_chain_errora  r   r   	input_strc                     dS )zDo nothing when tool starts.Nr   )rT   rs   r   r_   s       r   on_tool_startz(LabelStudioCallbackHandler.on_tool_starte  s	     	r   actionc                     dS )z.Do nothing when agent takes a specific action.Nr   )rT   r   r_   s      r   on_agent_actionz*LabelStudioCallbackHandler.on_agent_actionn  r   r   outputobservation_prefix
llm_prefixc                     dS )zDo nothing when tool ends.Nr   )rT   r   r   r   r_   s        r   on_tool_endz&LabelStudioCallbackHandler.on_tool_endr  s	     	r   c                     dS )z&Do nothing when tool outputs an error.Nr   r   s      r   on_tool_errorz(LabelStudioCallbackHandler.on_tool_error|  r   r   rc   c                     dS z
Do nothingNr   )rT   rc   r_   s      r   on_textz"LabelStudioCallbackHandler.on_text  r   r   finishc                     dS r   r   )rT   r   r_   s      r   on_agent_finishz*LabelStudioCallbackHandler.on_agent_finish  r   r   )NN)(r   r   r   r   r*   r%   __annotations__r   r   r   rI   r
   r;   r   r   rr   r   r   ry   r   r}   r   r   r   r   r   BaseExceptionr   r   r   r   r   r   r   r   r   r   r   r   __classcell__)r>   s   @r   r)   r)   N   s}         2 !5#444 "&!$(0(,,;,Bv, v,#v, c]v, SM	v,
 v, !v, C()v, v, v, v, v, v,p!,!,(,T*-=(>!,	!, !, !, !,FFcNF cF 	F
 
F F F F$. . . . . . )-$(-1'
 '
 '
cN'
 tK()'

 '
  ~'
 tCy!'
 4S>*'
 '
 
'
 '
 '
 '
Rc S T    !9 ! ! ! ! ! !- 3 4    sCx.26sCx.LO	   
DcN c d    M S T    cN  	
 
   k S S     -1$(	  %SM SM	
  
   = C D    C 3 4    k S T        r   r)   )r?   rB   r   enumr   typingr   r   r   r   r	   r
   uuidr   langchain_core.agentsr   r   langchain_core.callbacksr   langchain_core.messagesr   r   langchain_core.outputsr   r   r   r%   r'   r)   r   r   r   <module>r      sy   				              : : : : : : : : : : : : : : : :       : : : : : : : : 8 8 8 8 8 8 < < < < < < < < 8 8 8 8 8 8 8 8    d   64
_$
%64
3 64 64 64 64rx x x x x!4 x x x x xr   