Update ServiceSwitcher and LLMSwitcher docstrings

This commit is contained in:
Mark Backman
2025-10-22 08:58:14 -04:00
parent 54691ee781
commit f1040100f4
2 changed files with 67 additions and 9 deletions

View File

@@ -14,20 +14,41 @@ from pipecat.services.llm_service import LLMService
class LLMSwitcher(ServiceSwitcher[StrategyType]): class LLMSwitcher(ServiceSwitcher[StrategyType]):
"""A pipeline that switches between different LLMs at runtime.""" """A pipeline that switches between different LLMs at runtime.
Example::
llm_switcher = LLMSwitcher(
llms=[openai_llm, anthropic_llm],
strategy_type=ServiceSwitcherStrategyManual
)
"""
def __init__(self, llms: List[LLMService], strategy_type: Type[StrategyType]): def __init__(self, llms: List[LLMService], strategy_type: Type[StrategyType]):
"""Initialize the service switcher with a list of LLMs and a switching strategy.""" """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.
"""
super().__init__(llms, strategy_type) super().__init__(llms, strategy_type)
@property @property
def llms(self) -> List[LLMService]: def llms(self) -> List[LLMService]:
"""Get the list of LLMs managed by this switcher.""" """Get the list of LLMs managed by this switcher.
Returns:
List of LLM services managed by this switcher.
"""
return self.services return self.services
@property @property
def active_llm(self) -> Optional[LLMService]: def active_llm(self) -> Optional[LLMService]:
"""Get the currently active LLM, if any.""" """Get the currently active LLM.
Returns:
The currently active LLM service, or None if no LLM is active.
"""
return self.strategy.active_service return self.strategy.active_service
async def run_inference(self, context: LLMContext) -> Optional[str]: async def run_inference(self, context: LLMContext) -> Optional[str]:

View File

@@ -21,10 +21,22 @@ from pipecat.processors.frame_processor import FrameDirection, FrameProcessor
class ServiceSwitcherStrategy: class ServiceSwitcherStrategy:
"""Base class for service switching strategies.""" """Base class for service switching strategies.
Note:
Strategy classes are instantiated internally by ServiceSwitcher.
Developers should pass the strategy class (not an instance) to ServiceSwitcher.
"""
def __init__(self, services: List[FrameProcessor]): def __init__(self, services: List[FrameProcessor]):
"""Initialize the service switcher strategy with a list of services.""" """Initialize the service switcher strategy with a list of services.
Note:
This is called internally by ServiceSwitcher. Do not instantiate directly.
Args:
services: List of frame processors to switch between.
"""
self.services = services self.services = services
self.active_service: Optional[FrameProcessor] = None self.active_service: Optional[FrameProcessor] = None
@@ -46,10 +58,24 @@ class ServiceSwitcherStrategyManual(ServiceSwitcherStrategy):
This strategy allows the user to manually select which service is active. This strategy allows the user to manually select which service is active.
The initial active service is the first one in the list. The initial active service is the first one in the list.
Example::
stt_switcher = ServiceSwitcher(
services=[stt_1, stt_2],
strategy_type=ServiceSwitcherStrategyManual
)
""" """
def __init__(self, services: List[FrameProcessor]): def __init__(self, services: List[FrameProcessor]):
"""Initialize the manual service switcher strategy with a list of services.""" """Initialize the manual service switcher strategy with a list of services.
Note:
This is called internally by ServiceSwitcher. Do not instantiate directly.
Args:
services: List of frame processors to switch between.
"""
super().__init__(services) super().__init__(services)
self.active_service = services[0] if services else None self.active_service = services[0] if services else None
@@ -85,7 +111,12 @@ class ServiceSwitcher(ParallelPipeline, Generic[StrategyType]):
"""A pipeline that switches between different services at runtime.""" """A pipeline that switches between different services at runtime."""
def __init__(self, services: List[FrameProcessor], strategy_type: Type[StrategyType]): def __init__(self, services: List[FrameProcessor], strategy_type: Type[StrategyType]):
"""Initialize the service switcher with a list of services and a switching strategy.""" """Initialize the service switcher with a list of services and a switching strategy.
Args:
services: List of frame processors to switch between.
strategy_type: The strategy class to use for switching between services.
"""
strategy = strategy_type(services) strategy = strategy_type(services)
super().__init__(*self._make_pipeline_definitions(services, strategy)) super().__init__(*self._make_pipeline_definitions(services, strategy))
self.services = services self.services = services
@@ -100,7 +131,13 @@ class ServiceSwitcher(ParallelPipeline, Generic[StrategyType]):
active_service: FrameProcessor, active_service: FrameProcessor,
direction: FrameDirection, direction: FrameDirection,
): ):
"""Initialize the service switcher filter with a strategy and direction.""" """Initialize the service switcher filter with a strategy and direction.
Args:
wrapped_service: The service that this filter wraps.
active_service: The currently active service.
direction: The direction of frame flow to filter.
"""
async def filter(_: Frame) -> bool: async def filter(_: Frame) -> bool:
return self._wrapped_service == self._active_service return self._wrapped_service == self._active_service