Overview
LLMSwitcher
is a specialized version of ServiceSwitcher
designed specifically for managing multiple LLM services. Beyond basic service switching, it provides convenient methods for running ad-hoc inferences and registering function handlers across all LLMs simultaneously.
This is particularly useful when you need to switch between different LLM providers based on task complexity, cost optimization, or specific model capabilities while maintaining a consistent function calling interface.
Constructor
List of LLM service instances to switch between.
The strategy class to use for switching logic. Pass the class itself, not an
instance.
Properties
The list of LLM services managed by this switcher.
The currently active LLM service, or None if no LLMs are configured.
Methods
run_inference()
Run a one-shot inference with the currently active LLM, outside of the normal pipeline flow.The LLM context containing conversation history and messages.
Optional[str]
- The LLM’s response as a string, or None if no LLM is active.
register_function()
Register a function handler with all LLMs in the switcher, regardless of which is currently active.The name of the function to handle. Use None for a catch-all handler that
processes all function calls.
The async function handler. Should accept a single
FunctionCallParams
parameter.Legacy callback function (deprecated). Put initialization code at the top of
your handler instead.
Whether to cancel this function call when a user interruption occurs.