
    j                     @    d Z ddlmZmZmZ ddlmZmZ  G d d      Z	y)z;SimulatedUser class for multi-turn conversation simulation.    )ListDictOptional)getget_default_model_namec            	           e Zd ZdZ	 	 ddedee   deee      fdZdeeeef      defd	Z	deeeef      defd
Z
deeeef      defdZy)SimulatedUserz
    A simulated user that generates responses using LLMs or fixed responses.

    The user simulator generates string responses that are then incorporated
    into the conversation by the application logic.
    Npersonamodelfixed_responsesc                     || _         |xs
 t               | _        |xs g | _        d| _        t        | j                        | _        y)a  
        Initialize a simulated user.

        Args:
            persona: Description of the user's personality and behavior
            model: LLM model to use for generating responses. If omitted, uses the
                central default model resolved by `get_default_model_name()`
                (override with `OPIK_DEFAULT_LLM`).
            fixed_responses: Optional list of predefined responses to cycle through
        r   )
model_nameN)r
   r   r   r   _response_index	get_model_llm)selfr
   r   r   s       w/Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/simulation/simulated_user.py__init__zSimulatedUser.__init__   sB      646
.4"  4	    conversation_historyreturnc                     | j                   rF| j                   | j                  t        | j                         z     }| xj                  dz  c_        |S | j                  |      S )z
        Generate a response based on the conversation history.

        Args:
            conversation_history: List of message dicts with 'role' and 'content' keys

        Returns:
            String response from the simulated user
           )r   r   len_generate_llm_response)r   r   responses      r   generate_responsezSimulatedUser.generate_response*   sb     ++$$s4+?+?'@@H   A% O **+?@@r   c                     d| j                    d}d|dg}|j                  |       | j                  |      }	 | j                  j	                  |      }|S # t
        $ r}dt        |       dcY d}~S d}~ww xY w)	z(Generate response using the LLM backend.z5You are a simulated user with the following persona: z

Your task is to generate realistic user messages that this persona would send in a conversation.
Respond as if you are the user, not as an assistant describing the user.
Generate a single user message that fits your persona and the conversation context.system)rolecontent)inputz*I'm having trouble responding right now. ()N)r
   extend_format_messages_as_textr   generate_string	Exceptionstr)r   r   system_promptmessagesconversation_textr   es          r   r   z$SimulatedUser._generate_llm_response?   s     RRVR^R^Q_ `T W &-@A 	,- !99(C	Jyy007H0IHO 	J?AxqII	Js   A 	A:!A5/A:5A:r*   c                 .   g }|D ]~  }|d   }|d   }|dk(  r|j                  d|        '|dk(  r|j                  d|        A|dk(  r|j                  d|        [|j                  |j                          d	|         d
j                  |      S )z2Convert message list to text format for LLM input.r    r!   r   zSystem: userzUser: 	assistantzAssistant: z: 
)appendtitlejoin)r   r*   formatted_messagesmessager    r!   s         r   r%   z&SimulatedUser._format_messages_as_textY   s    G6?Di(Gx"))HWI*>?"))F7)*<=$"))Ky*AB"))TZZ\N"WI*FG   yy+,,r   )NN)__name__
__module____qualname____doc__r(   r   r   r   r   r   r   r%    r   r   r	   r	   
   s      $/3	55 }5 "$s),	50Ad4S>6J As A*J4S#X;O JTW J4-d38n1E -# -r   r	   N)
r9   typingr   r   r   %opik.evaluation.models.models_factoryr   r   r   r	   r:   r   r   <module>r=      s    A ' '^- ^-r   