predictive_delegation

Predictive delegation, an optimization that can speed up responses that need the backend.

Pass a PredictiveDelegation as LLMWithBackend(predictive_delegation=...) to turn it on. Normally, a text frontend spends a whole inference deciding to call delegate before the backend can start. Predictive delegation uses a fast yes/no classifier to determine whether it needs to wait for that inference or can go ahead and trigger an early backend delegation.

How it works: when a user’s turn ends, the frontend runs in parallel (i.e. in a race) with the classifier (see pipecat.classifiers) that decides whether the turn needs the backend.

  • If the classifier says “yes” before the frontend has produced output:

    • the frontend’s inference is cancelled;

    • a delegate call to the backend is made right away, in its place.

  • Otherwise, the frontend handles the turn as usual, having waited for nothing (note that it may still choose to delegate).

class pipecat.pipeline.llm_with_backend.predictive_delegation.PredictiveDelegation(classifier: BaseClassifier, **kwargs)[source]

Bases: FrameProcessor

An optimization that can speed up responses that need backend involvement.

Alpha. It uses a fast yes/no classifier to determine, after each user request, whether it needs to wait for the frontend’s inference or can go ahead and trigger an early backend delegation. See pipecat.pipeline.llm_with_backend.predictive_delegation for how it works. Pass one to LLMWithBackend(predictive_delegation=...), which puts it just in front of the frontend; it needs a text frontend and a request strategy whose delegate call needs no arguments, such as the default TranscriptBackendRequestStrategy.

It races every turn that ends with a new user message, spoken or typed, and so also a user-role message the app appends and runs the frontend on. The classifier is asked whether that message needs the backend, given the frontend’s system instruction (where the app says what the backend is for, and the pairing instruction when to delegate), the delegate tool’s description, and the latest messages of the conversation, the backend’s included. A turn the frontend sees as speculative (eager end of turn), or with incomplete-turn filtering on, is left to the frontend.

Example:

llm = LLMWithBackend(
    frontend=OpenAILLMService(...),
    backend=BackendLLMWorker(...),
    predictive_delegation=PredictiveDelegation(JevClassifier(api_key=...)),
)
__init__(classifier: BaseClassifier, **kwargs)[source]

Initialize predictive delegation.

Parameters:
  • classifier – The yes/no classifier to ask. It answers within a bound of its own; an answer that comes after the frontend’s first output is ignored.

  • **kwargs – Additional arguments passed to the parent class.

async setup(setup: FrameProcessorSetup)[source]

Set up the classifier, reporting its metrics in the pipeline.

async cleanup()[source]

Stop any classification in progress and clean up the classifier.

async process_frame(frame: Frame, direction: FrameDirection)[source]

Race each new user turn; pass every frame on.

Parameters:
  • frame – The frame to process.

  • direction – The direction of frame processing.