o
    *j                     @   s   d dl Z d dlZd dlmZmZmZmZ ddlmZm	Z	 		dde
de
deee
ef  deee
ef  d	eee
 ee f f
d
dZde
de
fddZde
de
fddZde
de
fddZde
de
fddZeeeedZdS )    N)DictListOptionalTuple   )split_str_parts_bysplit_parts_by_regexqueryresponseresponse_loss_scale_mapquery_loss_scale_mapreturnc                    s^  |dur/|  D ]&}|| v r.t|| ttfr|| g||< || d }|gt|gf  S qt fdd   D }t||}dd   D }t|rSt|| g }	g }
|D ]Q}t|d ttfrs|	|d g7 }	|
	|d  qY|d  v r|	 |d  d g7 }	|	 |d  d	 g7 }	|
	|d  |
	|d  qY|	d
g7 }	|
	|d  qY|
|	fS )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.
    Nr   c                 3   s$    | ]}t  | d kr|V  qdS )   Nlen).0kr    n/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/preprocessors/templates/loss_scale.py	<genexpr>(   s   " z'calculate_loss_scale.<locals>.<genexpr>c                 S   s"   i | ]\}}t |d kr||qS )r   r   )r   r   vr   r   r   
<dictcomp>*   s   " z(calculate_loss_scale.<locals>.<dictcomp>keycontentr         ?)
keys
isinstancefloatintlistr   itemsr   r   append)r	   r
   r   r   r   Zloss_scale_value
delimitersZagent_partsZregex_delimitersweightsZagent_contentcr   r   r   calculate_loss_scale	   s6   


r&   c                 C   Z   t  }d}t j||}t|d}t|}W d    n1 s"w   Y  t| ||S )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_mapr   r   r   alpha_umi_loss_scale?      r7   c           	      C   sl   t  }d}t j||}t|d}t|}W d    n1 s"w   Y  |d }|d }t| |||S )Nzagentflan.jsonr(   r	   r
   r)   )	r	   r
   r2   r3   r4   r5   r6   r   r   r   r   r   agentflan_loss_scaleH   s   r9   c                 C   r'   )Nzdefault_loss_scale_config.jsonr(   r)   r1   r   r   r   react_loss_scaleS   r8   r:   c                 C   s   |gdgfS )Nr   r   )r	   r
   r   r   r   default_loss_scale\   s   r;   )Z	agentflanreactZ	alpha_umidefault)NN)r/   r*   typingr   r   r   r   utilsr   r   strr    r   r&   r7   r9   r:   r;   r6   r   r   r   r   <module>   s0   
6		
