o
    i`                     @   sl   d Z ddlmZmZmZmZ ddlmZ ddlm	Z	m
Z
mZ ddlmZ ddlmZ G dd de	e Zd	S )
zbLLM switcher for switching between different LLMs at runtime, with different switching strategies.    )AnyListOptionalType)DirectFunction)ServiceSwitcherServiceSwitcherStrategyManualStrategyType)
LLMContext)
LLMServicec                       s   e Zd ZdZefdee dee f fddZ	e
dee fddZe
defd	d
Zdedee fddZ	dddddee dededee fddZddddededee fddZ  ZS )LLMSwitcherzA pipeline that switches between different LLMs at runtime.

    Example::

        llm_switcher = LLMSwitcher(llms=[openai_llm, anthropic_llm])
    llmsstrategy_typec                    s   t  || dS )a/  Initialize the service switcher with a list of LLMs and a switching strategy.

        Args:
            llms: List of LLM services to switch between.
            strategy_type: The strategy class to use for switching between LLMs.
                Defaults to ``ServiceSwitcherStrategyManual``.
        N)super__init__)selfr   r   	__class__ Q/home/ubuntu/.local/lib/python3.10/site-packages/pipecat/pipeline/llm_switcher.pyr      s   zLLMSwitcher.__init__returnc                 C   s   | j S )zGet the list of LLMs managed by this switcher.

        Returns:
            List of LLM services managed by this switcher.
        )servicesr   r   r   r   r   +   s   zLLMSwitcher.llmsc                 C   s   | j jS )zGet the currently active LLM.

        Returns:
            The currently active LLM service, or None if no LLM is active.
        )strategyactive_servicer   r   r   r   
active_llm4   s   zLLMSwitcher.active_llmcontextc                    s(   | j r| j jdd|i|I dH S dS )a  Run a one-shot, out-of-band (i.e. out-of-pipeline) inference with the given LLM context, using the currently active LLM.

        Args:
            context: The LLM context containing conversation history.
            **kwargs: Additional arguments forwarded to the active LLM's run_inference
                (e.g. max_tokens, system_instruction).

        Returns:
            The LLM's response as a string, or None if no response is generated.
        r   Nr   )r   run_inference)r   r   kwargsr   r   r   r   =   s   zLLMSwitcher.run_inferenceNT)cancel_on_interruptiontimeout_secsfunction_namehandlerr   r    c                C   s$   | j D ]}|j|||||d qdS )a]  Register a function handler for LLM function calls, on all LLMs, active or not.

        Args:
            function_name: The name of the function to handle. Use None to handle
                all function calls with a catch-all handler.
            handler: The function handler. Should accept a single FunctionCallParams
                parameter.
            start_callback: Legacy callback function (deprecated). Put initialization
                code at the top of your handler instead.

                .. deprecated:: 0.0.59
                    The `start_callback` parameter is deprecated and will be removed in a future version.

            cancel_on_interruption: Whether to cancel this function call when an
                interruption occurs. Defaults to True.
            timeout_secs: Optional timeout in seconds for the function call.
        )r!   r"   start_callbackr   r    N)r   register_function)r   r!   r"   r#   r   r    llmr   r   r   r$   L   s   
zLLMSwitcher.register_functionc                C   s    | j D ]
}|j|||d qdS )a  Register a direct function handler for LLM function calls, on all LLMs, active or not.

        Args:
            handler: The direct function to register. Must follow DirectFunction protocol.
            cancel_on_interruption: Whether to cancel this function call when an
                interruption occurs. Defaults to True.
            timeout_secs: Optional timeout in seconds for the function call.
        )r"   r   r    N)r   register_direct_function)r   r"   r   r    r%   r   r   r   r&   o   s   
z$LLMSwitcher.register_direct_function)N)__name__
__module____qualname____doc__r   r   r   r   r	   r   propertyr   r   r
   r   strr   r   boolfloatr$   r   r&   __classcell__r   r   r   r   r      sF    

'r   N)r*   typingr   r   r   r   (pipecat.adapters.schemas.direct_functionr   !pipecat.pipeline.service_switcherr   r   r	   *pipecat.processors.aggregators.llm_contextr
   pipecat.services.llm_servicer   r   r   r   r   r   <module>   s   