
    9i                         S SK r S SKrS SKJrJrJrJr  SSKJrJ	r	    SS\
S\
S\\\
\4      S\\\
\4      S	\\\
   \\   4   4
S
 jjrS\
S\
4S jrS\
S\
4S jrS\
S\
4S jrS\
S\
4S jr\\\\S.rg)    N)DictListOptionalTuple   )split_str_parts_bysplit_parts_by_regexqueryresponseresponse_loss_scale_mapquery_loss_scale_mapreturnc                 0  ^ UbX  UR                  5        HD  nX@;   d  M
  [        X4   [        [        45      (       a  X4   /X4'   X4   S   nU/[        U5      /4s  $    [	        U4S jTR                  5        5       5      n[        X5      nTR                  5        VV	s0 s H  u  p[        U	5      S:X  d  M  X_M     n
nn	[        U
5      (       a  [        Xz5        / n/ nU H  n[        US   [        [        45      (       a  XS   /-  nUR                  US   5        M?  US   T;   aH  UTUS      S   /-  nUTUS      S   /-  nUR                  US   5        UR                  US   5        M  US/-  nUR                  US   5        M     X4$ s  sn	nf )a  Calculate the loss scale by splitting the agent response.

This algorithm comes from paper: https://arxiv.org/pdf/2309.00986.pdf

Agent response format:

```text
    Thought: you should always think about what to do
    Action: the action to take, should be one of the above tools[fire_recognition,
        fire_alert, call_police, call_fireman]
    Action Input: the input to the action
    Observation: the result of the action
    ... (this Thought/Action/Action Input/Observation can be repeated zero or more times)
    Thought: I now know the final answer
    Final Answer: the final answer to the original input question
```
Returns:
    A tuple of agent response parts and their weights.
r   c              3   P   >#    U  H  n[        TU   5      S :X  d  M  Uv   M     g7f)   N)len).0kr   s     m/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/preprocessors/templates/loss_scale.py	<genexpr>'calculate_loss_scale.<locals>.<genexpr>(   s)     h!?A3G^_`GaCbfgCgaa!?s   &	&r   keycontent      ?)
keys
isinstancefloatintlistr   itemsr   r	   append)r
   r   r   r   r   loss_scale_value
delimitersagent_partsr   vregex_delimitersweightsagent_contentcs     `           r   calculate_loss_scaler*   	   s   0 '',,.C|27%FF1E1J0K(-#7#<Q#?  zE*:$;#<<< / h!8!=!=!?hhJ$X:K)@)F)F)HX)HCPQFVWK)HX
[;GMah--%z!G  9.x223AeH=a@AA3AeH=a@AA$$QuX.$$Qy\2C5 $$Qy\2  !!% Ys   F7Fc                     [         R                  " 5       nSn[         R                  R                  X#5      n[	        US5       n[
        R                  " U5      nS S S 5        [        XW5      $ ! , (       d  f       N= f)Nz alpha_umi_loss_scale_config.jsonrosgetcwdpathjoinopenjsonloadr*   r
   r   cwdloss_scale_config_pathconfig_path	json_fileloss_scale_maps          r   alpha_umi_loss_scaler;   ?   sZ    
))+C?'',,s;K	k3	99- 
 @@ 
 	   A..
A<c                    [         R                  " 5       nSn[         R                  R                  X#5      n[	        US5       n[
        R                  " U5      nS S S 5        WS   nUS   n[        XX5      $ ! , (       d  f       N$= f)Nzagentflan.jsonr,   r
   r   r-   )	r
   r   r6   r7   r8   r9   r:   r   r   s	            r   agentflan_loss_scaler>   H   sq    
))+C-'',,s;K	k3	99- 
 )'2,Z81H__	 
 	s   A88
Bc                     [         R                  " 5       nSn[         R                  R                  X#5      n[	        US5       n[
        R                  " U5      nS S S 5        [        XW5      $ ! , (       d  f       N= f)Nzdefault_loss_scale_config.jsonr,   r-   r5   s          r   react_loss_scaler@   S   sZ    
))+C='',,s;K	k3	99- 
 @@ 
 	r<   c                     U/S/4$ )Nr    )r
   r   s     r   default_loss_scalerC   \   s    :u    )	agentflanreact	alpha_umidefault)NN)r3   r.   typingr   r   r   r   utilsr   r	   strr   r   r*   r;   r>   r@   rC   r:   rB   rD   r   <module>rL      s     	 . . ;
 OSKO3" 3"#&3"2:4T	?2K3" 08S$Y/H3" UZZ^_bZceijoepZpTq3"lA As A` `s `AC A3 Ac S 
 &%!	rD   