o
    7ti                     @   sj   d dl Z d dlm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)deepcopy)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 )SQUADCompletionr   zhazyresearch/based-squaddefaultc                    s   t  jdd| jiid d S )Nmetadataversion)config)super__init__VERSION)selfkwargs	__class__ W/home/ubuntu/.local/lib/python3.10/site-packages/lm_eval/tasks/squad_completion/task.pyr      s   zSQUADCompletion.__init__c                 C      dS NFr   r   r   r   r   has_training_docs      z!SQUADCompletion.has_training_docsc                 C   r   )NTr   r   r   r   r   has_validation_docs   r   z#SQUADCompletion.has_validation_docsc                 C   r   r   r   r   r   r   r   has_test_docs   r   zSQUADCompletion.has_test_docsc                 C   s
   | j d S )N
validation)datasetr   r   r   r   validation_docs   s   
zSQUADCompletion.validation_docsc                 C      |d S )Ntextr   r   docr   r   r   doc_to_text      zSQUADCompletion.doc_to_textc                 C   r   )Nvaluer   r    r   r   r   doc_to_target"   r#   zSQUADCompletion.doc_to_targetNFc                 K   sL   t | jj}|ddg|d< |dd|d< t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`.
        until
max_gen_toks0   generate_untilr   )request_typer!   	argumentsidxNr   )r   r
   generation_kwargsgetr   )r   r!   ctxchat_templateapply_chat_templater   r,   r   r   r   construct_requests%   s   z"SQUADCompletion.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SQUADCompletion.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
        r4   )npmeanr   r   r   r   aggregationN   s   zSQUADCompletion.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
        r4   Tr   r   r   r   r   higher_is_betterX   s   z SQUADCompletion.higher_is_betterr   )__name__
__module____qualname__r   DATASET_PATHDATASET_NAMEr   r   r   r   r   r"   r%   r3   r8   r;   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rC   r   r   	<genexpr>d   s
    "
z!contains_score.<locals>.<genexpr>)max)rC   rD   r   rN   r   r5   c   s   r5   )rG   copyr   typingr   numpyr9   lm_eval.api.instancer   lm_eval.api.taskr   r   strr5   r   r   r   r   <module>   s    X