Skip to content

Commit d0f967c

Browse files
authored
Merge pull request #12 from livekit-examples/bcherry/improvements
Add latest agent improvements
2 parents aaecb0e + 25f96fe commit d0f967c

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ requires-python = ">=3.9"
1010

1111
dependencies = [
1212
"livekit-agents[openai,turn-detector,silero,cartesia,deepgram]~=1.2",
13-
"livekit-plugins-noise-cancellation~=0.2.1",
13+
"livekit-plugins-noise-cancellation~=0.2",
1414
"python-dotenv",
1515
]
1616

src/agent.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22

33
from dotenv import load_dotenv
44
from 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
)
1618
from livekit.agents.llm import function_tool
17-
from livekit.agents.voice import MetricsCollectedEvent
1819
from livekit.plugins import cartesia, deepgram, noise_cancellation, openai, silero
1920
from 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

Comments
 (0)