
    j              	          d dl mZmZmZ ddlmZ ddlmZ deej                     de	deeej                     edf   fd	Z
deej                     dej                  fd
Zdej                  de	deej                     fdZg dZy)    )Any	GeneratorList   )types)build_conversation_turnsturnswindow_sizereturnNc              #   r   K   t        t        |             D ]  }| t        d||z
  dz         |dz      yw)a   
    Generates windows of conversation turns of a fixed size from a list of turns.
    This function creates a sliding window over the list of conversation turns.
    Each window includes the current turn and up to `window_size - 1` previous
    conversation turns. If there are fewer turns available than the `window_size`,
    the window will consist of all available turns up to the current turn.
    Args:
        turns: List of conversation turn objects representing the interactions
            in a conversation.
        window_size: Integer specifying the maximum number of turns to include
            in each window.
    Yields:
        A generator that produces lists of conversation turns, where each list
        represents a sliding window of turns.
    r   r   N)rangelenmax)r	   r
   is      /Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/evaluation/metrics/conversation/helpers.pyget_turns_in_sliding_windowr      s<     & 3u:C1{?Q./!a%88 s   57c                 V    g }| D ]!  }|j                  |j                                # |S )a  
    Merges a list of conversation turns into a single conversation.
    This function takes a list of conversation turns and combines them
    into a single conversation by extending the output list with the data
    from each turn.
    Args:
        turns: A list of conversation turn  objects to be combined.
    Returns:
        types.Conversation: A combined conversation object containing all
            the turns from the input list.
    )extendas_list)r	   outputturns      r   merge_turnsr      s*     "$Fdlln% M    conversationc                     t        |       dk(  rt        d      t        |       }t        |      dk(  rt        d      t        ||      D cg c]  }t	        |       }}|S c c}w )ae  
    Extracts a list of conversation windows based on turns using a sliding window
    approach. This function divides a conversation into consecutive overlapping
    windows, where each window contains a specified number of turns.
    Args:
        conversation: The input conversation from which turns will be processed.
        window_size: The number of turns to include in each sliding window.
    Returns:
        A list of conversations, each representing a window of turns specified
        by the given window size.
    Raises:
        ValueError: If the conversation is empty or if it has no turns.
    r   zConversation is empty)r   zConversation has no turns)r   
ValueErrorr   r   r   )r   r
   r	   turns_windowturns_windowss        r   'extract_turns_windows_from_conversationr   1   sy    " <A011$,?E
5zQ455 8{K/KL 	L!K  / 	/s   A#)r   r   r   )typingr   r   r    r   conversation_turns_factoryr   ConversationTurnintr   Conversationr   r   __all__ r   r   <module>r(      s    ' '  @9&&'9699tE**+S$679.tE223 8J8J &$$36	%

>r   