
    dh76                         S SK r S SKrS SKJr  S SKJr  S SKJrJrJrJ	r	J
r
Jr  S SKJr  S SKJrJr  S SKJr  S SKJrJr  S S	KJrJr   " S
 S\5      rS\\\4   S\
\\4   4S jr " S S\5      rg)    N)datetime)Enum)AnyDictListOptionalTupleUnion)UUID)AgentActionAgentFinish)BaseCallbackHandler)BaseMessageChatMessage)
Generation	LLMResultc                        \ rS rSrSrSrSrSrg)LabelStudioMode   zLabel Studio mode enumerator.promptchat N)__name__
__module____qualname____firstlineno____doc__PROMPTCHAT__static_attributes__r       j/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/callbacks/labelstudio_callback.pyr   r      s    'FDr!   r   modereturnc                     [         R                  R                  S[         R                  R                  S0n[	        U [
        5      (       a  [        U 5      n XR                     U 4$ )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*      sb     	$$ '* 	"" %-'R $t$!**-t33r!   c                     ^  \ rS rSr% SrSr\\S'   SSS\S\R                  4S\
\   S\
\   S\
\   S	\S
\
\   S\\\4   4U 4S jjjrS\S\\\      SS4S jrS\\\4   S\\   S\SS4S jrS\S\4S jrSSSS.S\\\4   S\\\      S\S\
\   S\
\\      S\
\\\4      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\\\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"  S6S,\S-\
\   S.\
\   S\SS4
S/ jjr#S!\S\SS4S0 jr$S1\S\SS4S2 jr%S3\&S\SS4S4 jr'S5r(U =r)$ )7LabelStudioCallbackHandlerN   ae  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  > [         TU ]  5          SS KnU(       du  [        R                  " S5      (       a   [        [        R                  " S5      5      nO:[        SU R                  R
                   SU R                  R
                   S35      eXl
        U(       dy  [        R                  " S5      (       a  [        R                  " S5      nOG[        R                  " SUR                   S	U R                  R
                   S
35        UR                  nX l        0 U l        UR!                  U R                  U R                  S9U l        X@l        U(       a  XPl        S U l        O[+        U5      u  U l        U l        U=(       d    [        R                  " S5      U l        U R,                  b4  U R"                  R/                  [1        U R,                  5      5      U l        O[4        R6                  " 5       R9                  U R$                  5      nU R"                  R;                  US9n	U	(       a&  U	S   U l        U R2                  R<                  U l        OCU R"                  R?                  XR&                  S9U l        U R2                  R<                  U l        U R2                  R@                  U l         Su  U l!        U l"        U l#        U l$        U R@                  RK                  5        HC  u  pUS   S:X  d  M  Xl!        US   S   U l"        US   S   S   U l#        US   S   S   U l$          O   U RB                  (       dD  SU R$                   S3nU R(                  [L        RN                  :X  a  US-  nOUS-  n[        U5      eg ! [         a$    [        SU R                  R
                   S35      ef = f)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>)r0   r/   LABEL_STUDIO_PROJECT_ID)title)r8   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_URLr0   payloadClient	ls_clientr2   r3   r#   r*   r1   get_projectint
ls_projectr   todaystrftimeget_projectsidcreate_projectparsed_label_config	from_namer<   r&   
input_typeitemsr   r   )selfr/   r0   r1   r2   r3   r#   lsproject_titleexisting_projectstag_nametag_infoerror_messagerB   s                r"   r?   #LabelStudioCallbackHandler.__init__j   sZ    			) yy/00bii(>?@ #DNN$;$;#< =D ~~../XY
 
 yy+,,ii 23**,*E*E)FN~~../W	X 11 )+txxF("0DI-Ft-L*D$L		2K(L??&"nn88T__9MNDO$NN,55d6G6GHM $ ; ;- ; P "3A"6"&//"4"4"&.."?"?'6I6I #@ # #'//"4"4#'??#F#F E
Adj$/ #'":":"@"@"BH:-!)'	215%h/27;
"*8"4Q"7"? #C ~~():):(; << = 
 yyO222G / ]++) i  	 7 78 9/ 0 	s   N	 	.N7run_idgenerationsr$   c                    / nU R                   U   S   nU R                   U   S   R                  S0 5      R                  S5      n[        XB5       H`  u  pgUR                  U R                  USU0U R
                  U R                  SSU Vs/ sH  oR                  PM     sn0S./US	./S
.5        Mb     U R                  R                  U5        g s  snf )Npromptskwargsinvocation_params
model_namer`   textareatext)rU   r<   r:   r&   )resultmodel_version)datapredictions)
rI   getzipappendr&   rU   r<   rh   rN   import_tasks)	rX   r`   ra   tasksrc   rj   r   
generationgs	            r"   add_prompts_generations2LabelStudioCallbackHandler.add_prompts_generations   s     ,,v&y1LL *S$b)S 	
 #&g";FLL 

F & 26/3||,6.4z6Rz!vvz6R-S	!"' .;
$ #<, 	$$U+ 7Ss   C
serializedrc   rd   c                     U R                   S:w  a&  [        SU R                   SU R                    S35      e[        US   5      nX#S.U R                  U'   g).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`   )rc   rd   N)rV   rE   r2   r(   rI   )rX   rv   rc   rd   r`   s        r"   on_llm_start'LabelStudioCallbackHandler.on_llm_start  sh     ??f$*4+<+<*= >&&*oo%6 7@A  VH%&+2EVr!   messagec                 p    [        U[        5      (       a  UR                  $ UR                  R                  $ )zGet the role of the message.)r'   r   rolerB   r   )rX   r~   s     r"   _get_message_role,LabelStudioCallbackHandler._get_message_role  s+    g{++<<$$---r!   )parent_run_idtagsmetadatamessagesr   r   r   c                V   U R                   S:w  a&  [        SU R                   SU R                    S35      e/ nU HL  n	/ n
U	 H0  nU
R                  U R	                  U5      UR
                  S.5        M2     UR                  U
5        MN     UUUUUUS.U R                  [        U5      '   g)rx   
Paragraphsrz   r{   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)rc   r   r   r`   r   rd   N)rV   rE   r2   ro   r   r   rI   r(   )rX   rv   r   r`   r   r   r   rd   rc   message_listdialogr~   s               r"   on_chat_model_start.LabelStudioCallbackHandler.on_chat_model_start  s     ??l**4+<+<*= >&&*oo%6 7FG  $LF' $ 6 6w ?#*?? ( NN6" %  *%
S[!r!   tokenc                     g)z)Do nothing when a new token is generated.Nr   )rX   r   rd   s      r"   on_llm_new_token+LabelStudioCallbackHandler.on_llm_new_tokenG      r!   responsec                     [        US   5      nU R                  X1R                  5        U R                  R	                  U5        g)z>Create a new Label Studio task for each prompt and generation.r`   N)r(   rt   ra   rI   pop)rX   r   rd   r`   s       r"   
on_llm_end%LabelStudioCallbackHandler.on_llm_endK  s;    VH%& 	$$V-A-AB 	 r!   errorc                     g)z%Do nothing when LLM outputs an error.Nr   rX   r   rd   s      r"   on_llm_error'LabelStudioCallbackHandler.on_llm_errorU  r   r!   r=   c                     g Nr   )rX   rv   r=   rd   s       r"   on_chain_start)LabelStudioCallbackHandler.on_chain_startY  s     	r!   outputsc                     g r   r   )rX   r   rd   s      r"   on_chain_end'LabelStudioCallbackHandler.on_chain_end^  s    r!   c                     g)z+Do nothing when LLM chain outputs an error.Nr   r   s      r"   on_chain_error)LabelStudioCallbackHandler.on_chain_errora  r   r!   	input_strc                     g)zDo nothing when tool starts.Nr   )rX   rv   r   rd   s       r"   on_tool_start(LabelStudioCallbackHandler.on_tool_starte  s     	r!   actionc                     g)z.Do nothing when agent takes a specific action.Nr   )rX   r   rd   s      r"   on_agent_action*LabelStudioCallbackHandler.on_agent_actionn  r   r!   outputobservation_prefix
llm_prefixc                     g)zDo nothing when tool ends.Nr   )rX   r   r   r   rd   s        r"   on_tool_end&LabelStudioCallbackHandler.on_tool_endr  s     	r!   c                     g)z&Do nothing when tool outputs an error.Nr   r   s      r"   on_tool_error(LabelStudioCallbackHandler.on_tool_error|  r   r!   rh   c                     gz
Do nothingNr   )rX   rh   rd   s      r"   on_text"LabelStudioCallbackHandler.on_text  r   r!   finishc                     gr   r   )rX   r   rd   s      r"   on_agent_finish*LabelStudioCallbackHandler.on_agent_finish  r   r!   )r/   rU   rV   rK   rN   r#   rT   rI   r3   r1   r2   r<   r0   r&   )NN)*r   r   r   r   r   r.   r(   __annotations__r   r   r   rM   r
   r?   r   r   rt   r   r   r|   r   r   r   r   r   r   r   BaseExceptionr   r   r   r   r   r   r   r   r   r   r   r   r    __classcell__)rB   s   @r"   r,   r,   N   s<   2 !5#4 "&!$(0(,,;,B,Bv,#v, c]v, SM	v,
 v, !v, C()v, v,p!,!,(,T*-=(>!,	!,FFcNF c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,   )rC   rF   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      sk    	    : :  : 8 < 8d 64
_$
%64
3 64rx!4 xr!   