llm_with_backend
The LLMWithBackend pipeline, which puts a frontend and its backend where an LLM goes.
- class pipecat.pipeline.llm_with_backend.llm_with_backend.LLMWithBackend(*, frontend: LLMService[Any], backend: BackendLLMWorker | str, connector: BackendConnector | None = None, predictive_delegation: PredictiveDelegation | None = None)[source]
Bases:
PipelineA conversational LLM, kept fast and light, with a backend that does the work it hands off.
Alpha. The API is unstable: names, arguments, the strategies and the job contract are likely to change between releases.
Put it where the LLM goes in a pipeline. It is a
Pipeline, not anLLMService: functions, settings updates, event handlers and aFlowManagergo onfrontend. It wraps the frontend service, installs the connector’s tools on it as built-in tools, appends the connector’s guidance to the frontend’s system instruction, adds a local backend worker to the pipeline worker so the app never wires it up, and holds the session with the backend for the pipeline’s life. A built-in tool is sent on every inference beside whatever tools the frontend has, and never enters the context’s tool set, so a tool change announced to the model never mentions it. The tools are the backend’s; the frontend hasdelegateand no more, though a tool it must keep, in its context or configured on the service, stays.The guidance is the connector’s pairing instruction (how to work with a backend: when and how to delegate, and what to do with the backend’s messages) with the strategy’s (how the request is worded) in place. The frontend’s own system instruction is the place to say what the backend is for, in plain words, as the backend’s own instruction does: no tool names, nothing to update when a tool changes.
Example:
llm = LLMWithBackend( frontend=OpenAILLMService(...), backend=BackendLLMWorker( llm=AnthropicLLMService(...), context=LLMContext(tools=[get_weather]), ), ) pipeline = Pipeline([transport.input(), stt, user_agg, llm, tts, transport.output(), assistant_agg])
- __init__(*, frontend: LLMService[Any], backend: BackendLLMWorker | str, connector: BackendConnector | None = None, predictive_delegation: PredictiveDelegation | None = None)[source]
Initialize the service.
- Parameters:
frontend – The conversational LLM service, text or speech-to-speech.
backend – The backend worker, or the name of one registered elsewhere (in the app, or in another process on a shared bus). A worker given here is added to the pipeline worker at setup.
connector – How delegation works. A default
BackendConnectorwhen omitted.predictive_delegation – A
PredictiveDelegation, to speed up responses that need backend involvement: its classifier determines whether to wait for the frontend’s inference or go ahead and trigger an early backend delegation. Only for a text frontend.
- Raises:
ValueError – If the frontend service declines the role; if the connector’s request strategy reads the frontend’s context and the frontend is speech-to-speech; or if predictive delegation is given with a speech-to-speech frontend or with a request strategy whose
delegatecall needs arguments.
- property frontend: LLMService[Any]
The frontend LLM service.
- property backend: BackendLLMWorker | str
The backend worker, or its name.
- property connector: BackendConnector
The connector joining the two layers.
- property predictive_delegation: PredictiveDelegation | None
The PredictiveDelegation in front of the frontend, if there is one.
- property tool: FunctionSchema
The
delegatetool installed on the frontend.
- async setup(setup: FrameProcessorSetup)[source]
Register a local backend worker as a child of the pipeline worker, set up the frontend, and open the session.