22
33from dotenv import load_dotenv
44from livekit .agents import (
5+ NOT_GIVEN ,
56 Agent ,
7+ AgentFalseInterruptionEvent ,
68 AgentSession ,
79 JobContext ,
810 JobProcess ,
11+ MetricsCollectedEvent ,
912 RoomInputOptions ,
10- RoomOutputOptions ,
1113 RunContext ,
1214 WorkerOptions ,
1315 cli ,
1416 metrics ,
1517)
1618from livekit .agents .llm import function_tool
17- from livekit .agents .voice import MetricsCollectedEvent
1819from livekit .plugins import cartesia , deepgram , noise_cancellation , openai , silero
1920from livekit .plugins .turn_detector .multilingual import MultilingualModel
2021
@@ -28,7 +29,7 @@ def __init__(self) -> None:
2829 super ().__init__ (
2930 instructions = """You are a helpful voice AI assistant.
3031 You eagerly assist users with their questions by providing information from your extensive knowledge.
31- Your responses are concise, to the point, and without any complex formatting or punctuation.
32+ Your responses are concise, to the point, and without any complex formatting or punctuation including emojis, asterisks, or other symbols .
3233 You are curious, friendly, and have a sense of humor.""" ,
3334 )
3435
@@ -75,6 +76,9 @@ async def entrypoint(ctx: JobContext):
7576 # See more at https://docs.livekit.io/agents/build/turns
7677 turn_detection = MultilingualModel (),
7778 vad = ctx .proc .userdata ["vad" ],
79+ # allow the LLM to generate a response while waiting for the end of turn
80+ # See more at https://docs.livekit.io/agents/build/audio/#preemptive-generation
81+ preemptive_generation = True ,
7882 )
7983
8084 # To use a realtime model instead of a voice pipeline, use the following session setup instead:
@@ -83,6 +87,13 @@ async def entrypoint(ctx: JobContext):
8387 # llm=openai.realtime.RealtimeModel()
8488 # )
8589
90+ # sometimes background noise could interrupt the agent session, these are considered false positive interruptions
91+ # when it's detected, you may resume the agent's speech
92+ @session .on ("agent_false_interruption" )
93+ def _on_agent_false_interruption (ev : AgentFalseInterruptionEvent ):
94+ logger .info ("false positive interruption, resuming" )
95+ session .generate_reply (instructions = ev .extra_instructions or NOT_GIVEN )
96+
8697 # Metrics collection, to measure pipeline performance
8798 # For more information, see https://docs.livekit.io/agents/build/metrics/
8899 usage_collector = metrics .UsageCollector ()
0 commit comments