Ë
    µŒj76  ã                   óä   — 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y)é    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y)ÚLabelStudioModezLabel Studio mode enumerator.ÚpromptÚchatN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚPROMPTÚCHAT© ó    ú|/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/langchain_community/callbacks/labelstudio_callback.pyr   r      s   „ Ù'à€FØ�Dr   r   ÚmodeÚreturnc                 óÆ   — t         j                  j                  dt         j                  j                  di}t	        | t
        «      rt        | «      } || j                     | fS )z³Get 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'      sa   € ô 	×Ñ×$Ñ$ð 'ô* 	×Ñ×"Ñ"ð %ð-'Ðô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}|snt        j                  d«      rt        t        j                  d«      «      }n:t        d| j                  j
                  › d| j                  j
                  › d�«      ‚|| _
        |sqt        j                  d«      rt        j                  d«      }nFt        j                  d|j                  › d	| j                  j
                  › d
�«       |j                  }|| _        i | _        |j!                  | j                  | j                  ¬«      | _        || _        |r|| _        d | _        nt+        |«      \  | _        | _        |xs t        j                  d«      | _        | j,                  �4| j"                  j/                  t1        | j,                  «      «      | _        n¸t5        j6                  «       j9                  | j$                  «      }| j"                  j;                  |¬«      }	|	r&|	d   | _        | j2                  j<                  | _        nG| j"                  j?                  || j&                  ¬«      | _        | j2                  j<                  | _        | j2                  j@                  | _         d\  | _!        | _"        | _#        | _$        | j@                  jK                  «       D ]C  \  }
}|d   dk(  sŒ|
| _!        |d   d   | _"        |d   d   d   | _#        |d   d   d   | _$         n | jB                  sMd| j$                  › d�}| j(                  tL        jN                  k(  r|dz  }t        |«      ‚|dz  }t        |«      ‚y # t        $ r$ t        d| j                  j
                  › d�«      ‚w xY w)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   so  ø€ ô 	‰ÑÔð		Û)ñ Ü�y‰yÐ/Ô0ÜœbŸi™iÐ(>Ó?Ó@‘ä Ø# D§N¡N×$;Ñ$;Ð#<ð =Dð —~‘~×.Ñ.Ð/ðXðYó
ð 
ð ˆŒáÜ�y‰yÐ+Ô,Ü—i‘iÐ 2Ó3‘ä—‘ð*Ø*,×*EÑ*EÐ)FØNØ—~‘~×.Ñ.Ð/ðWð	Xôð ×1Ñ1�ØˆŒð )+ˆŒàŸ™ t§x¡x¸¿¹˜ÓFˆŒØ(ˆÔÙØ"0ˆDÔØˆD�Iä-FÀtÓ-LÑ*ˆDÔ ¤à$ÒL¬¯	©	Ð2KÓ(LˆŒØ�?‰?Ð&Ø"Ÿn™n×8Ñ8¼¸T¿_¹_Ó9MÓNˆD�Oä$ŸN™NÓ,×5Ñ5°d×6GÑ6GÓHˆMØ $§¡× ;Ñ ;À-Ð ;Ó PÐÙ Ø"3°AÑ"6�”Ø"&§/¡/×"4Ñ"4�•à"&§.¡.×"?Ñ"?Ø'°d×6IÑ6Ið #@ó #�”ð #'§/¡/×"4Ñ"4�”Ø#'§?¡?×#FÑ#FˆÔ ðE
ÑAˆŒ˜œ d¤j°$´/ð #'×":Ñ":×"@Ñ"@Ö"BÑˆH�hØ˜Ñ :Ó-Ø!)�”Ø'¨	Ñ2°1Ñ5�”Ø% hÑ/°Ñ2°7Ñ;�”
Ø"*¨8Ñ"4°QÑ"7¸Ñ"?�”Ùð #Cð �~Š~à(¨×):Ñ):Ð(;ð <<ð =ð ð
 �y‰yœO×2Ñ2Ò2ØðGñ�ô ˜]Ó+Ð+ð ð/ñ�ô ˜]Ó+Ð+ð) øôi ò 	ÜØ §¡× 7Ñ 7Ð8ð 9/ð 0óð ð	ús   ‘M/ Í/-NÚrun_idÚgenerationsr!   c                 óª  — g }| j                   |   d   }| j                   |   d   j                  di «      j                  d«      }t        ||«      D ]_  \  }}|j                  | j                  |d|i| j
                  | j                  dd|D �cg c]  }|j                  ‘Œ c}idœg|d	œgd
œ«       Œa | j                  j                  |«       y c c}w )NÚpromptsÚkwargsÚinvocation_paramsÚ
model_namer[   ÚtextareaÚtext)rQ   r8   r6   r#   )ÚresultÚmodel_version)ÚdataÚpredictions)
rE   ÚgetÚzipÚappendr#   rQ   r8   rc   rJ   Úimport_tasks)	rT   r[   r\   Útasksr^   re   r   Ú
generationÚgs	            r   Úadd_prompts_generationsz2LabelStudioCallbackHandler.add_prompts_generationsâ   sè   € ð ˆØ—,‘,˜vÑ& yÑ1ˆà�L‰L˜Ñ  Ñ*ß‰SÐ$ bÓ)ß‰S�Óð 	ô
 #& g¨{Ö";ÑˆF�JØ�L‰Lð Ÿ
™
 FØ  &ðð 26·±Ø/3¯|©|Ø,6Ø.4ÁzÓ6RÁzÀ!°q·v³vÀzÑ6RÐ-Sñ	!"ð'ð .;ñ
ð$ñõð #<ð, 	�‰×$Ñ$ UÕ+ùò 7Ss   ÂCÚ
serializedr^   r_   c                 ó®   — | j                   dk7  r&t        d| j                  › d| j                   › d�«      ‚t        |d   «      }||dœ| j                  |<   y)ú.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   rp   r^   r_   r[   s        r   Úon_llm_startz'LabelStudioCallbackHandler.on_llm_start  sj   € ð �?‰?˜fÒ$ÜØ*¨4×+<Ñ+<Ð*=ð >&Ø&*§o¡oÐ%6ð 7@ðAóð ô �V˜HÑ%Ó&ˆØ+2¸fÑEˆ�‰�VÒr   Úmessagec                 óf   — t        |t        «      r|j                  S |j                  j                  S )zGet the role of the message.)r$   r   Úroler>   r   )rT   rw   s     r   Ú_get_message_rolez,LabelStudioCallbackHandler._get_message_role  s(   € ä�gœ{Ô+Ø—<‘<Ðà×$Ñ$×-Ñ-Ð-r   )Úparent_run_idÚtagsÚmetadataÚmessagesr{   r|   r}   c                óL  — | j                   dk7  r&t        d| j                  › d| j                   › d�«      ‚g }|D ]I  }	g }
|	D ]/  }|
j                  | j	                  |«      |j
                  dœ«       Œ1 |j                  |
«       ŒK ||||||dœ| j                  t        |«      <   y)rr   Ú
Paragraphsrt   ru   zƒ>. To make it work with the mode="chat", the input type should be <Paragraphs>.
Read more here https://labelstud.io/tags/paragraphs)ry   Úcontent)r^   r|   r}   r[   r{   r_   N)rR   rA   r.   rj   rz   r�   rE   r%   )rT   rp   r~   r[   r{   r|   r}   r_   r^   Úmessage_listÚdialogrw   s               r   Úon_chat_model_startz.LabelStudioCallbackHandler.on_chat_model_start  sÂ   € ð �?‰?˜lÒ*ÜØ*¨4×+<Ñ+<Ð*=ð >&Ø&*§o¡oÐ%6ð 7FðGóð ð ˆÛ$ˆLØˆFÛ'�Ø—‘à $× 6Ñ 6°wÓ ?Ø#*§?¡?ñõð (ð �N‰N˜6Õ"ð %ð ØØ ØØ*Øñ%
ˆ�‰”S˜“[Ò!r   Útokenc                  ó   — y)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                  «       | j                  j	                  |«       y)z>Create a new Label Studio task for each prompt and generation.r[   N)r%   ro   r\   rE   Úpop)rT   r‰   r_   r[   s       r   Ú
on_llm_endz%LabelStudioCallbackHandler.on_llm_endK  s=   € ä�V˜HÑ%Ó&ˆð 	×$Ñ$ V¨X×-AÑ-AÔBð 	�‰×Ñ˜Õ r   Úerrorc                  ó   — y)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                  ó   — y ©Nr   )rT   rp   r9   r_   s       r   Úon_chain_startz)LabelStudioCallbackHandler.on_chain_startY  s   € ð 	r   Úoutputsc                  ó   — y r’   r   )rT   r”   r_   s      r   Úon_chain_endz'LabelStudioCallbackHandler.on_chain_end^  s   € Ør   c                  ó   — y)z+Do nothing when LLM chain outputs an error.Nr   r�   s      r   Úon_chain_errorz)LabelStudioCallbackHandler.on_chain_errora  rˆ   r   Ú	input_strc                  ó   — y)zDo nothing when tool starts.Nr   )rT   rp   r™   r_   s       r   Úon_tool_startz(LabelStudioCallbackHandler.on_tool_starte  s   € ð 	r   Úactionc                  ó   — y)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                  ó   — y)zDo nothing when tool ends.Nr   )rT   rŸ   r    r¡   r_   s        r   Úon_tool_endz&LabelStudioCallbackHandler.on_tool_endr  s   € ð 	r   c                  ó   — y)z&Do nothing when tool outputs an error.Nr   r�   s      r   Úon_tool_errorz(LabelStudioCallbackHandler.on_tool_error|  rˆ   r   rc   c                  ó   — y©z
Do nothingNr   )rT   rc   r_   s      r   Úon_textz"LabelStudioCallbackHandler.on_text€  rˆ   r   Úfinishc                  ó   — y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   ro   r   r   rv   r   rz   r   r„   r‡   r   rŒ   ÚBaseExceptionr�   r“   r–   r˜   r›   r   rž   r£   r¥   r¨   r   r«   Ú__classcell__)r>   s   @r   r)   r)   N   s1  ø… ñð2 !5Ð˜#Ó4ð "&Ø!Ø$(Ø0Ø(,Ø,;×,BÑ,Bñv,à˜#‘ðv,ð �c‰]ðv,ð ˜S‘Mð	v,ð
 ðv,ð ! ™ðv,ð �C˜Ð(Ñ)õv,ðp!,Øð!,Ø(,¨T°*Ñ-=Ñ(>ð!,à	ó!,ðFFà˜˜c˜‘NðFð �c‘ðFð ð	Fð
 
óFð$.¨ð .¸ó .ð )-Ø$(Ø-1ò'
à˜˜c˜‘Nð'
ð �t˜KÑ(Ñ)ð'
ð
 ð'
ð   ‘~ð'
ð �t˜C‘yÑ!ð'
ð ˜4  S ™>Ñ*ð'
ð ð'
ð 
ó'
ðR cð °Sð ¸Tó ð! 9ð !¸ð !Àó !ð -ð ¸3ð À4ó ðØ˜s C˜x™.ðØ26°s¸C°x±.ðØLOðà	óð
 D¨¨c¨¡Nð ¸cð Àdó ð Mð ¸Sð ÀTó ðà˜˜c˜‘Nðð ðð ð	ð
 
óð kð ¸Sð ÀSó ð -1Ø$(ñ	àðð % S™Mðð ˜S‘Mð	ð
 ðð 
óð =ð ¸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¶      sk   ðÛ 	Û Ý Ý ß :× :Ý ç :Ý 8ß <ß 8ô�dô ð64Ø
��_Ð$Ñ
%ð64à
ˆ3�ÐÑ ó64ôrxÐ!4õ xr   