how to integrate deepgram with custom stt node in...
# ask-ai
s
how to integrate deepgram with custom stt node in livekit agent
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'm happy to help you integrate Deepgram with a custom STT node in LiveKit! Here's a simple step-by-step guide to get you started: 1. Set Up Deepgram STT: Begin by using the Deepgram plugin for Node.js or Python to initialize Deepgram as your STT provider. Just make sure your
DEEPGRAM_API_KEY
is set in your environment — this is key to getting everything running smoothly. 2. Override the
stt_node
Method:
In your custom agent, you’ll want to override the
stt_node
method. This is where you can add your own special touches like keyword detection or tweaking transcripts, all while leveraging Deepgram’s transcription capabilities. 3. Pass STT Instance to AgentSession: When you create an
AgentSession
, don’t forget to pass the Deepgram
STT
instance along. This step ensures transcription works seamlessly during your session. If you’d like to dive deeper or explore more customization options, I highly recommend checking out the Deepgram Plugin Guide and the Custom STT Node Documentation. They have some great examples that can really help you tailor things to your needs. Sources: docs/index.yaml | livekit-agents/livekit/agents/voice/agent.py | examples/src/cartersia_tts.ts | Speech-to-text (STT) models | LiveKit Docs | Deepgram STT plugin guide | LiveKit Docs
s
write the code snippet in python
t
I'd love to share a helpful Python code snippet that shows you how to integrate Deepgram with a custom STT node in LiveKit. Take a look:
Copy code
from livekit.plugins.deepgram import STT
from livekit.agents import rtc
from livekit.agents.models import ModelSettings
import logging

logger = logging.getLogger(__name__)

class CustomAgent:
    def __init__(self):
        self.default_stt = STT()

    async def stt_node(self, audio: rtc.AsyncIterable[rtc.AudioFrame], model_settings: ModelSettings):
        # Use the default STT stream (Deepgram)
        async for event in self.default_stt.stt_node(audio, model_settings):
            if event.type == STT.SpeechEventType.FINAL_TRANSCRIPT:
                transcript = event.alternatives[0].text.lower()
                keywords = ["hello", "thanks", "bye"]
                if any(keyword in transcript for keyword in keywords):
                    logger.info(f"Keyword detected: '{transcript}'")
            yield event

# Example usage
async def main():
    agent = CustomAgent()
    # Pass audio and model settings to the stt_node method
    # Example: await agent.stt_node(audio_stream, model_settings)
This snippet gently guides you through overriding the
stt_node
method to leverage Deepgram for transcription, while also adding a personal touch with keyword detection. When you're ready, just swap out the
audio_stream
and
model_settings
with your actual inputs. If you have any questions or want to explore this further, I'm here to help!
s
https://docs.livekit.io/agents/build/nodes/ read the docs .. how to create a agent with tts stt llm plugin .. but i want to take filter out some words .. before passing it to llm ... can we do this with the hooks
t
It sounds like you want to filter words from user speech before the LLM sees it. Yes—you can do this with hooks/nodes. Two common options: • Override
stt_node
to post-process transcripts as they’re produced • Use
on_user_turn_completed
to edit the final user message before it’s added to chat context Below is a minimal Python example using Deepgram STT and filtering in both places (pick one).
Copy code
from typing import AsyncIterable
from livekit.agents import Agent
from livekit.agents import rtc
from livekit.agents.types import ChatMessage, ChatContext, ModelSettings
from livekit.plugins.deepgram import STT
from livekit.plugins.openai import LLM, TTS

FILTER_WORDS = {"um", "uh", "like"}  # example fillers

def _filter_text(text: str) -> str:
  tokens = text.split()
  return " ".join(t for t in tokens if t.lower() not in FILTER_WORDS)

class MyAgent(Agent):
  def __init__(self):
    super().__init__(stt=STT(), tts=TTS(), llm=LLM())

  # Option A: Filter during STT streaming (affects what reaches the LLM)
  async def stt_node(self, audio: AsyncIterable[rtc.AudioFrame], model_settings: ModelSettings):
    async for evt in self.default.stt_node(audio, model_settings):
      if hasattr(evt, "alternatives") and evt.alternatives:
        alt = evt.alternatives[0]
        alt.text = _filter_text(alt.text)
      yield evt

  # Option B: Filter once per user turn (right before LLM sees it)
  async def on_user_turn_completed(self, turn_ctx: ChatContext, new_message: ChatMessage):
    if new_message.role == "user" and new_message.content:
      new_message.content = _filter_text(new_message.content)
    return await self.default.on_user_turn_completed(turn_ctx, new_message)
If you want the smallest change and clean, per-turn control, use
on_user_turn_completed
. If you need word-level cleanup as transcripts stream in, use
stt_node
. For reference: see pipeline nodes and hooks in the docs: Pipeline nodes and hooks | LiveKit docs. Would you like a version that only uses one of these paths and includes your specific word list? Sources: Pipeline nodes and hooks | LiveKit docs