o
    5ti                     @  sz   d dl mZ d dlmZ d dlmZ d#d$ddZd%ddZd&ddZeG dd dZ	d'ddZ
d(ddZd)d*d!d"ZdS )+    )annotations)	dataclass)Any prefix
str | Nonesuffix	delimiterstrreturnc                 C  sD   | s|pdS |s| pdS | d   s|d   s| | | S | | S )zXJoin prefix and suffix, adding delimiter only if neither has whitespace at the boundary. r   isspace)r   r   r	    r   E/home/ubuntu/.local/lib/python3.10/site-packages/lm_eval/api/utils.pymaybe_delimit   s   

r   boolc                 C  s&   | dkrdS | d   p|d    S )zBReturn True if neither string has whitespace at the join boundary.r   Fr   r   r   )r   r   r   r   r   requires_delimiter   s   r   sc                 C  s   t | o	| d  S )z/Return True if the string ends with whitespace.r   )r   r   )r   r   r   r   ends_with_whitespace      r   c                   @  sB   e Zd ZU dZded< ded< dZded< dd	d
ZdddZdS )Messagead  A single message in a prompt, supporting both chat and plain-text formats.

    Used by `build_qa_turn()` and `fewshot_context()` to construct prompts that can
    be rendered either as plain text or as chat-formatted messages.

    Attributes:
        role (str): "system", "user", or "assistant".
        content (str): The message text.
        _delimiter (str): Suffix appended when rendering as plain text (via `to_text()`).
            Prefixed with `_` so it's excluded from the ` to_dict () ` output. Typically used
            for target delimiters (e.g., " ") or fewshot delimiters (e.g., "\n\n").
    r
   rolecontentr   
_delimiterr   dict[str, str]c                 C  s   dd | j  D S )zIConvert to chat format dict, excluding internal fields like `_delimiter`.c                 S  s    i | ]\}}| d s||qS )_)
startswith).0kvr   r   r   
<dictcomp>5   s     z#Message.to_dict.<locals>.<dictcomp>)__dict__itemsselfr   r   r   to_dict3   r   zMessage.to_dictc                 C  s   | j | j S )z-Render as plain text with delimiter appended.)r   r   r%   r   r   r   to_text7   s   zMessage.to_textN)r   r   )r   r
   )__name__
__module____qualname____doc____annotations__r   r'   r(   r   r   r   r   r       s   
 
r   messageslist[Message]c                 C  s   d dd | D S )zLConcatenate all messages into plain text, using each message's `_delimiter`.r   c                 s      | ]}|  V  qd S Nr(   r   mr   r   r   	<genexpr>>       z#messages_to_text.<locals>.<genexpr>)join)r.   r   r   r   messages_to_text<   r   r8   list[dict[str, Any]]c                 C  s   | d j dkr| d | dd fnd| f\}} | d j dkrB| dd }d|d _tddd	d
 |D d g| d  g }nd| d _tdddd
 | D d g}|ra| g| S |S )zYCollapse multi-turn messages into a single user message (plus optional system/assistant).r   system   Nr   	assistantr   userc                 s  r0   r1   r2   r3   r   r   r   r5   M   r6   z*multiturn_to_singleturn.<locals>.<genexpr>c                 s  r0   r1   r2   r3   r   r   r   r5   Q   r6   )r   r   r   r7   r'   )r.   r:   _user_messageresr   r   r   multiturn_to_singleturnA   s   

"r@   Nr   r   typer   c                 C  s   |s|| dS ||| dS )zACreate a chat message dict with role, content, and optional type.)r   r   )rA   r   r   r   )r   r   rA   r   r   r   format_turnV   s
   

rB   )r   )r   r   r   r   r	   r
   r   r
   )r   r
   r   r
   r   r   )r   r
   r   r   )r.   r/   r   r
   )r.   r/   r   r9   r1   )r   r
   r   r
   rA   r   r   r   )
__future__r   dataclassesr   typingr   r   r   r   r   r8   r@   rB   r   r   r   r   <module>   s    



