o
    7ti>                     @   s^   d dl Z d dlmZ d dlZd dlmZ d dlmZ G dd deZ	de
dee
 fd	d
ZdS )    N)ListInstance)ConfigurableTaskc                       s|   e Zd ZdZdZdZ fddZdd Zdd	 Zd
d Z	dd Z
dd Zdd Z	dddZdd Zdd Zdd Z  ZS )SWDEr   zhazyresearch/based-swde-v2defaultc                    s   t  jdd| jiid d S )Nmetadataversion)config)super__init__VERSION)selfkwargs	__class__ K/home/ubuntu/.local/lib/python3.10/site-packages/lm_eval/tasks/swde/task.pyr      s   zSWDE.__init__c                 C      dS NFr   r   r   r   r   has_training_docs      zSWDE.has_training_docsc                 C   r   )NTr   r   r   r   r   has_validation_docs   r   zSWDE.has_validation_docsc                 C   r   r   r   r   r   r   r   has_test_docs   r   zSWDE.has_test_docsc                 C   s
   | j d S )N
validation)datasetr   r   r   r   validation_docs   s   
zSWDE.validation_docsc                 C      |d S )Ntextr   r   docr   r   r   doc_to_text      zSWDE.doc_to_textc                 C   r   )Nvaluer   r    r   r   r   doc_to_target!   r#   zSWDE.doc_to_targetNFc                 K   s&   t dd||dgddfdd|gS )	a  Uses RequestFactory to construct Requests and returns an iterable of
        Requests which will be sent to the LM.

        :param doc:
            The document as returned from training_docs, validation_docs, or test_docs.
        :param ctx: str
            The context string, generated by fewshot_context. This includes the natural
            language description, as well as the few shot examples, and the question
            part of the document for `doc`.
        generate_until
0   )untilmax_gen_toksr   )request_typer!   	argumentsidxNr   r   )r   r!   ctxchat_templateapply_chat_templater   r   r   r   construct_requests$   s   zSWDE.construct_requestsc                 C   s   |}dt |d |d giS )a  Take a single document and the LM results and evaluates, returning a
        dict where keys are the names of submetrics and values are the values of
        the metric for that one document

        :param doc:
            The document as returned from training_docs, validation_docs, or test_docs.
        :param results:
            The results of the requests created in construct_requests.
        containsr   r$   )contains_score)r   r!   resultscontinuationr   r   r   process_results<   s   zSWDE.process_resultsc                 C   s
   dt jiS )z
        :returns: {str: [float] -> float}
            A dictionary where keys are the names of submetrics and values are
            functions that aggregate a list of metrics
        r2   )npmeanr   r   r   r   aggregationK   s   zSWDE.aggregationc                 C   s   ddiS )z
        :returns: {str: bool}
            A dictionary where keys are the names of submetrics and values are
            whether a higher value of the submetric is better
        r2   Tr   r   r   r   r   higher_is_betterU   s   zSWDE.higher_is_betterr   )__name__
__module____qualname__r   DATASET_PATHDATASET_NAMEr   r   r   r   r   r"   r%   r1   r6   r9   r:   __classcell__r   r   r   r   r   
   s     

r   
predictionlabelsc                    s   t  fdd|D S )Nc              
   3   s4    | ]}t tttt|tj V  qd S )N)intboolresearchcompileescape
IGNORECASE).0labelrA   r   r   	<genexpr>a   s
    "
z!contains_score.<locals>.<genexpr>)max)rA   rB   r   rL   r   r3   `   s   r3   )rE   typingr   numpyr7   lm_eval.api.instancer   lm_eval.api.taskr   r   strr3   r   r   r   r   <module>   s    V