From 1603bdc6e2f736d4e6f3eee3397bea0198765179 Mon Sep 17 00:00:00 2001 From: Ben Cherry Date: Thu, 7 Aug 2025 15:36:03 -0700 Subject: [PATCH 1/2] Add latest agent improvements --- pyproject.toml | 2 +- src/agent.py | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index c75ae0a..60bae3e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ requires-python = ">=3.9" dependencies = [ "livekit-agents[openai,turn-detector,silero,cartesia,deepgram]~=1.2", - "livekit-plugins-noise-cancellation~=0.2.1", + "livekit-plugins-noise-cancellation~=0.2", "python-dotenv", ] diff --git a/src/agent.py b/src/agent.py index 00130c2..3df223e 100644 --- a/src/agent.py +++ b/src/agent.py @@ -2,19 +2,20 @@ from dotenv import load_dotenv from livekit.agents import ( + NOT_GIVEN, Agent, + AgentFalseInterruptionEvent, AgentSession, JobContext, JobProcess, RoomInputOptions, - RoomOutputOptions, + MetricsCollectedEvent, RunContext, WorkerOptions, cli, metrics, ) from livekit.agents.llm import function_tool -from livekit.agents.voice import MetricsCollectedEvent from livekit.plugins import cartesia, deepgram, noise_cancellation, openai, silero from livekit.plugins.turn_detector.multilingual import MultilingualModel @@ -28,7 +29,7 @@ def __init__(self) -> None: super().__init__( instructions="""You are a helpful voice AI assistant. You eagerly assist users with their questions by providing information from your extensive knowledge. - Your responses are concise, to the point, and without any complex formatting or punctuation. + Your responses are concise, to the point, and without any complex formatting or punctuation including emojis, asterisks, or other symbols. You are curious, friendly, and have a sense of humor.""", ) @@ -75,6 +76,9 @@ async def entrypoint(ctx: JobContext): # See more at https://docs.livekit.io/agents/build/turns turn_detection=MultilingualModel(), vad=ctx.proc.userdata["vad"], + # allow the LLM to generate a response while waiting for the end of turn + # See more at https://docs.livekit.io/agents/build/audio/#preemptive-generation + preemptive_generation=True, ) # 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): # llm=openai.realtime.RealtimeModel() # ) + # sometimes background noise could interrupt the agent session, these are considered false positive interruptions + # when it's detected, you may resume the agent's speech + @session.on("agent_false_interruption") + def _on_agent_false_interruption(ev: AgentFalseInterruptionEvent): + logger.info("false positive interruption, resuming") + session.generate_reply(instructions=ev.extra_instructions or NOT_GIVEN) + # Metrics collection, to measure pipeline performance # For more information, see https://docs.livekit.io/agents/build/metrics/ usage_collector = metrics.UsageCollector() From 25f96fe2cfd22984e798a59490720b5749eddb85 Mon Sep 17 00:00:00 2001 From: Ben Cherry Date: Thu, 7 Aug 2025 15:44:55 -0700 Subject: [PATCH 2/2] ruff --- src/agent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/agent.py b/src/agent.py index 3df223e..069e15e 100644 --- a/src/agent.py +++ b/src/agent.py @@ -8,8 +8,8 @@ AgentSession, JobContext, JobProcess, - RoomInputOptions, MetricsCollectedEvent, + RoomInputOptions, RunContext, WorkerOptions, cli,