How to stop sending data to the next pipeline when...
# ask-ai
b
How to stop sending data to the next pipeline when specific keywords are detected in the following code
Copy code
async def stt_node(self, text: AsyncIterable[str], model_settings: Optional[dict] = None) -> Optional[AsyncIterable[rtc.AudioFrame]]:
        keywords = ["Shane", "hello", "thanks", "bye"]
        parent_stream = super().stt_node(text, model_settings)
        
        if parent_stream is None:
            return None
            
        async def process_stream():
            async for event in parent_stream:
                if hasattr(event, 'type') and str(event.type) == "SpeechEventType.FINAL_TRANSCRIPT" and event.alternatives:
                    transcript = event.alternatives[0].text
                    
                    for keyword in keywords:
                        if keyword.lower() in transcript.lower():
                            logger.info(f"Keyword detected: '{keyword}'")
                
                yield event
                
        return process_stream()
t
Thanks for reaching out, RikuKomiya. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To help you stop sending data to the next pipeline when certain keywords pop up, you can tweak your code so the stream ends early by stopping the generator. Here’s a friendly way to adjust your function:
Copy code
async def stt_node(self, text: AsyncIterable[str], model_settings: Optional[dict] = None) -> Optional[AsyncIterable[rtc.AudioFrame]]:
    keywords = ["Shane", "hello", "thanks", "bye"]
    parent_stream = super().stt_node(text, model_settings)

    if parent_stream is None:
        return None

    async def process_stream():
        async for event in parent_stream:
            if hasattr(event, 'type') and str(event.type) == "SpeechEventType.FINAL_TRANSCRIPT" and event.alternatives:
                transcript = event.alternatives[0].text

                for keyword in keywords:
                    if keyword.lower() in transcript.lower():
                        logger.info(f"Keyword detected: '{keyword}'")
                        return  # Stop the stream early

            yield event

    return process_stream()
With this approach, the generator will stop yielding events and exit as soon as it spots a keyword, which means the data flow to the next pipeline stage will pause right away. If anything’s unclear or you’d like me to walk you through it further, just let me know—I’m here to help! Sources: pipeline-llm/llm_powered_content_filter.py | Pipeline nodes and hooks | LiveKit docs | Camera & microphone | LiveKit Docs
b
retrunしたら、次の言葉からもその後喋らなくなります。
t
To help you make sure that only specific keywords stop the current utterance without interrupting the following speech, you can adjust your function to skip handling the detected keyword while still processing upcoming events. Here’s a refreshed example that might make things clearer:
Copy code
async def stt_node(self, text: AsyncIterable[str], model_settings: Optional[dict] = None) -> Optional[AsyncIterable[rtc.AudioFrame]]:
    keywords = ["Shane", "hello", "thanks", "bye"]
    parent_stream = super().stt_node(text, model_settings)

    if parent_stream is None:
        return None

    async def process_stream():
        async for event in parent_stream:
            if hasattr(event, 'type') and str(event.type) == "SpeechEventType.FINAL_TRANSCRIPT" and event.alternatives:
                transcript = event.alternatives[0].text

                for keyword in keywords:
                    if keyword.lower() in transcript.lower():
                        logger.info(f"Keyword detected: '{keyword}'")
                        break  # Skip processing this event but continue with the next
                else:
                    yield event  # Only yield if no keyword is detected
            else:
                yield event  # Pass through non-transcript events

    return process_stream()
This way, when a keyword pops up, the current event is gently skipped, but the stream keeps flowing smoothly with the next speech events. If anything feels unclear or you want me to walk you through more tweaks, just let me know—I’m here to help! Sources: Pipeline nodes and hooks | LiveKit docs | Camera & microphone | LiveKit Docs | livekit-plugins/livekit-plugins-speechmatics/livekit/plugins/speechmatics/s... | home_assistant/homeautomation.py
b
break is not work
t
If the
break
statement isn’t working quite as you expected, no worries! You can try a different approach to skip processing specific events in the async generator. Here’s an updated example that might help:
Copy code
async def stt_node(self, text: AsyncIterable[str], model_settings: Optional[dict] = None) -> Optional[AsyncIterable[rtc.AudioFrame]]:
    keywords = ["Shane", "hello", "thanks", "bye"]
    parent_stream = super().stt_node(text, model_settings)

    if parent_stream is None:
        return None

    async def process_stream():
        async for event in parent_stream:
            if hasattr(event, 'type') and str(event.type) == "SpeechEventType.FINAL_TRANSCRIPT" and event.alternatives:
                transcript = event.alternatives[0].text

                # Check for keywords
                if any(keyword.lower() in transcript.lower() for keyword in keywords):
                    logger.info(f"Keyword detected: '{transcript}'")
                    continue  # Skip this event and move to the next

            yield event  # Pass through all other events

    return process_stream()
In this version, the
continue
statement helps us skip the current event and smoothly move on to the next one. This way, the stream keeps processing all the following events without any hiccups. Please feel free to let me know if this helps or if you’d like me to walk you through any other adjustments. I’m here to help! Sources: Testing and evaluation | LiveKit Docs | Pipeline nodes and hooks | LiveKit docs | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real...