
    qi                     t    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e	e         Zy	)
zbLLM switcher for switching between different LLMs at runtime, with different switching strategies.    )AnyListOptionalType)DirectFunction)ServiceSwitcherServiceSwitcherStrategyManualStrategyType)
LLMContext)
LLMServicec                        e Zd ZdZefdee   dee   f fdZ	e
dee   fd       Ze
defd       Zdedee   fd	Z	 dd
ddee   dedefdZd
ddedefdZ xZS )LLMSwitcherzA pipeline that switches between different LLMs at runtime.

    Example::

        llm_switcher = LLMSwitcher(llms=[openai_llm, anthropic_llm])
    llmsstrategy_typec                 &    t         |   ||       y)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__s      O/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/pipeline/llm_switcher.pyr   zLLMSwitcher.__init__   s     	}-    returnc                     | j                   S )zGet the list of LLMs managed by this switcher.

        Returns:
            List of LLM services managed by this switcher.
        )servicesr   s    r   r   zLLMSwitcher.llms+   s     }}r   c                 .    | j                   j                  S )zGet the currently active LLM.

        Returns:
            The currently active LLM service, or None if no LLM is active.
        )strategyactive_servicer   s    r   
active_llmzLLMSwitcher.active_llm4   s     }}+++r   contextc                 t   K   | j                   r& | j                   j                  dd|i| d{   S y7 w)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    N )r   run_inference)r   r    kwargss      r   r#   zLLMSwitcher.run_inference=   s:      ??666QwQ&QQQ Rs   .868T)cancel_on_interruptionfunction_namehandlerr%   c                P    | j                   D ]  }|j                  ||||        y)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.
        )r&   r'   start_callbackr%   N)r   register_function)r   r&   r'   r)   r%   llms         r   r*   zLLMSwitcher.register_functionL   s6    0 99 	C!!+-'=	 " 	r   c                L    | j                   D ]  }|j                  ||        y)aQ  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.
        )r'   r%   N)r   register_direct_function)r   r'   r%   r+   s       r   r-   z$LLMSwitcher.register_direct_functionl   s0     99 	C(('= ) 	r   )N)__name__
__module____qualname____doc__r	   r   r   r   r
   r   propertyr   r   r   r   strr#   r   boolr*   r   r-   __classcell__)r   s   @r   r   r      s     -J.:. L). d:&   ,J , ,: HSM & 	 (,}  !%H (,	 !%	r   r   N)r1   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   <module>r;      s:    i , , C 
 B 3h/,/ hr   