how can i set up a sequential llm pipeline where o...
# ask-ai
b
how can i set up a sequential llm pipeline where one llm output is given as an input to the other and is then passed on to the tts
t
Thanks for reaching out, Harsh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd be happy to help you set up a sequential LLM pipeline where the output from one LLM feeds into another, and then is sent to TTS. You can achieve this by creating a custom solution using LiveKit's flexible pipeline architecture. Here's a simple overview of the steps: 1. Override the
llm_node
in a custom agent to capture the output from the first LLM. 2. Take that output to build a new
ChatContext
for the second LLM. 3. Stream the second LLM's output directly to the TTS node for synthesis. This does involve some custom coding since LiveKit doesn’t have built-in support for chaining multiple LLMs in sequence. But the great news is that the framework offers the essential building blocks, like overridable pipeline nodes and stream manipulation tools, to help you put this together smoothly. If you'd like, I can provide detailed guidance on how to implement this, including code examples or support for features like chunk-by-chunk streaming. Just let me know how I can best assist you! Sources: Pipeline nodes and hooks | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py | chain-of-thought-tts/agent.py | docs/index.yaml
b
give me an ready to use code for the same
t
I'm happy to share a ready-to-use example of a sequential LLM pipeline where the output from one LLM is passed as input to another, and then sent to TTS. Let's walk through it together:
Copy code
from livekit.agents import VoicePipelineAgent, llm
from livekit.plugins import openai
from typing import AsyncIterable

class SequentialLLMAgent(VoicePipelineAgent):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # Initialize the second LLM (e.g., GPT-4)
        self.second_llm = openai.LLM(model="gpt-4")

    async def llm_node(
        self,
        chat_ctx: llm.ChatContext,
        tools: list[llm.FunctionTool | llm.RawFunctionTool],
        model_settings: llm.ModelSettings,
    ) -> AsyncIterable[llm.ChatChunk | str]:
        # Step 1: Run the first LLM
        first_llm_stream = await super().llm_node(chat_ctx, tools, model_settings)

        # Step 2: Collect the full output from the first LLM
        first_output = ""
        async for chunk in first_llm_stream:
            if isinstance(chunk, str):
                first_output += chunk
            elif isinstance(chunk, llm.ChatChunk) and chunk.delta and chunk.delta.content:
                first_output += chunk.delta.content

        # Step 3: Use the first LLM's output as input to the second LLM
        second_chat_ctx = llm.ChatContext().append(
            role="user",
            text=f"Refine or elaborate on this response: {first_output}"
        )
        second_tools = []  # Define tools if needed
        second_model_settings = llm.ModelSettings()

        # Call the second LLM
        second_llm_stream = await self.second_llm.chat(
            chat_ctx=second_chat_ctx,
            tools=second_tools,
            tool_choice=second_model_settings.tool_choice,
            conn_options=None,
        )

        # Step 4: Stream the second LLM's output
        async for chunk in second_llm_stream:
            yield chunk

# Instantiate the agent
agent = SequentialLLMAgent(
    vad=openai.VAD(),
    stt=openai.STT(),
    llm=openai.LLM(model="gpt-3.5-turbo"),  # First LLM
    tts=openai.TTS(),
)
This agent will: • Use the first LLM (like GPT-3.5) to generate an initial response. • Then, it takes that response and passes it on as input to the second LLM (like GPT-4) for refinement. • Finally, it streams the polished response to TTS for synthesis. If you have any questions or would like help adding features like chunk-by-chunk streaming, please don’t hesitate to ask. I’m here to help!