o
    5ti;                      @  s   d dl mZ d dl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 e	r4d dlmZ d dlmZ eeZeG d	d
 d
ZeG dd deZdS )    )annotationsN)asdict	dataclass)	getsource)TYPE_CHECKINGAny)default_gen_kwargs)Callable)
OutputTypec                
   @  s   e Zd ZU dZdZded< dZded< dZded	< dZd
ed< dZ	ded< dZ
ded< dZded< dZded< dZded< dZded< dZded< dd ZeddddddddddddZdS ) FewshotConfigzConfiguration for few-shot example formatting.

    These fields override the parent TaskConfig fields when formatting
    few-shot examples (as opposed to the test example).

    note: num_fewshot is also runtime dependent, so is not included here.
    defaultstrsamplerN
str | Nonesplit Callable[..., list[dict]] | Noneprocess_docszlist[int] | Nonefewshot_indicesz,list[dict] | Callable[[], list[dict]] | Nonesamplesstr | Callable[..., str] | Nonedoc_to_text-str | Callable[..., str] | dict | list | Nonedoc_to_choicedoc_to_target
gen_prefixfewshot_delimitertarget_delimiterc                 C  s*   | j d ur| jd urtd d S d S d S )NzABoth split and samples are configured; split will take precedence)r   r   eval_loggerwarningself r!   G/home/ubuntu/.local/lib/python3.10/site-packages/lm_eval/config/task.py__post_init__+   s
   zFewshotConfig.__post_init__)fewshot_splitr   r   r   r   r   r   r   cfgdictr$   returnc             	   K  s8   ||||||||	d||
}| dd | di |S )Nr   r   r   r   r   r   r   r   r   r   r!   )
setdefault)clsr%   r$   r   r   r   r   r   r   r   	overloadscfg_dictr!   r!   r"   	from_dict1   s   	
zFewshotConfig.from_dict)r%   r&   r$   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r'   r   )__name__
__module____qualname____doc__r   __annotations__r   r   r   r   r   r   r   r   r   r   r#   classmethodr-   r!   r!   r!   r"   r      s0   
 r   c                   @  s  e Zd ZU dZded< dZded< dZded< dZded< dZded	< dZ	ded
< dZ
ded< dZded< dZded< dZded< dZded< dZded< dZded< dZded< dZded< dZded< dZded< dZded< dZded< dZded< dZded < d!Zded"< d#Zded$< dZd%ed&< dZd'ed(< dZd)ed*< d+Zd,ed-< dZded.< d/Z d0ed1< dZ!ded2< dZ"ded3< dZ#ded4< dZ$ded5< dZ%ded6< dGd9d:Z&d;d< Z'd=d> Z(dHdIdAdBZ)	dHdJdEdFZ*dS )K
TaskConfigNr   task
task_aliaszstr | list | NonetagzCallable | Nonecustom_datasetdataset_pathdataset_namezdict | Nonedataset_kwargstraining_splitvalidation_split
test_splitr$   r   zCallable | str | Noner   r   doc_to_imagedoc_to_audioFboolunsafe_codez#Callable | str | dict | list | Noner   process_results
use_prompt r   description r   z

r   z%dict[str, Any] | FewshotConfig | Nonefewshot_configz
int | Nonenum_fewshotzlist | Nonemetric_listgenerate_untilr
   output_typegeneration_kwargs   intrepeatsfilter_listshould_decontaminatedoc_to_decontamination_queryr   metadatar'   Nonec                 C  s  | j d ur>| jdkrtd| j d d| j v r#t| j d | j d< d| j vr=t| j dt| j  | jg| j d< n| jdkrUt| j| _ t| j d| j   t	| j
ts`| j
d u r|tj| j
pfi | j| j| j| j| j| j| j| jd	| _
d S | j
| _
d S )	NrK   [zJ] passed `generation_kwargs`, but not using `output_type: generate_until`!temperatureuntilzS: No `until` specified in `generation_kwargs`! Defaulting to the fewshot_delimiter=zA: No `generation_kwargs` specified in task config, defaulting to r(   )rM   rL   r   r   r5   floatreprr   r   
isinstancerH   r&   r   r-   r$   r   r   r   r   r   r   r   r!   r!   r"   r#      sJ   







zTaskConfig.__post_init__c                 C  s
   t | |S N)getattr)r    itemr!   r!   r"   __getitem__   s   
zTaskConfig.__getitem__c                 C  s   t | ||S r\   )setattr)r    r^   valuer!   r!   r"   __setitem__   s   zTaskConfig.__setitem__keep_callabler&   c                 C  s   t | }t| D ]?\}}|du r|| q
|dkr<|D ]}| D ]\}}t|r5| j||d||< q$q|||< q
t|rI| j||d||< q
|S )ae  Dumps the current config as a dictionary object, as a printable format.

        null fields will not be printed.
        Used for dumping results alongside full task configuration

        :return: dict
            A printable dictionary version of the TaskConfig object.

        # TODO: should any default value in the TaskConfig not be printed?
        NrJ   )rc   )r   listitemspopcallableserialize_function)r    rc   r,   kvmetric_dict
metric_keymetric_valuer!   r!   r"   to_dict   s$   

zTaskConfig.to_dictra   Callable | strc              	   C  s2   |r|S zt |W S  ttfy   t| Y S w )zSerializes a given function or string.

        If 'keep_callable' is True, the original callable is returned.
        Otherwise, attempts to return the source code of the callable using 'getsource'.
        )r   	TypeErrorOSErrorr   )r    ra   rc   r!   r!   r"   rh      s   
zTaskConfig.serialize_function)r'   rU   )F)rc   rA   r'   r&   )ra   ro   r'   ro   )+r.   r/   r0   r5   r2   r6   r7   r8   r9   r:   r;   r<   r=   r>   r$   r   r   r   r?   r@   rB   r   rC   rD   rF   r   r   rH   rI   rJ   rL   rM   rP   rQ   rR   rS   r   rT   r#   r_   rb   rn   rh   r!   r!   r!   r"   r4   Q   sV   
 


(r4   )
__future__r   loggingdataclassesr   r   inspectr   typingr   r   lm_eval.defaultsr   collections.abcr	   lm_eval.api.instancer
   	getLoggerr.   r   r   r&   r4   r!   r!   r!   r"   <module>   s    
<