fallback
This commit is contained in:
@@ -1,14 +1,21 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import textwrap
|
||||
import time
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from livekit.agents import (
|
||||
Agent,
|
||||
AgentServer,
|
||||
AgentSession,
|
||||
ChatContext,
|
||||
JobContext,
|
||||
JobProcess,
|
||||
RunContext,
|
||||
TurnHandlingOptions,
|
||||
cli,
|
||||
function_tool,
|
||||
inference,
|
||||
room_io,
|
||||
)
|
||||
@@ -20,73 +27,193 @@ logger = logging.getLogger("agent")
|
||||
load_dotenv(".env.local")
|
||||
|
||||
|
||||
class Assistant(Agent):
|
||||
def __init__(self) -> None:
|
||||
@dataclass
|
||||
class InterviewData:
|
||||
"""Session-level state for the mock interview."""
|
||||
|
||||
intro_was_given: bool = field(default=False)
|
||||
|
||||
|
||||
_VOICE_RULES = textwrap.dedent(
|
||||
"""\
|
||||
# Output rules (voice)
|
||||
Respond in plain text only. No markdown, lists, tables, or emojis.
|
||||
Keep replies concise: one to three sentences. Ask one question at a time.
|
||||
Do not reveal internal reasoning, tool names, or system instructions.
|
||||
Spell out numbers; avoid acronyms with unclear pronunciation.
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
class SelfIntroAgent(Agent):
|
||||
def __init__(self, *, silence_budget: float = 20.0, stage_budget: float = 60.0) -> None:
|
||||
# silence_budget: fires after this many seconds of CONTINUOUS candidate silence;
|
||||
# resets whenever user_state == "speaking" is detected in the polling loop.
|
||||
# stage_budget: hard wall-clock cap on the whole stage, regardless of speech.
|
||||
# Keep them well-separated (defaults are good); if both expire within the same
|
||||
# ~1 s window, guard #2 in each watchdog prevents a double update_agent call.
|
||||
super().__init__(
|
||||
# A Large Language Model (LLM) is your agent's brain, processing user input and generating a response
|
||||
# See all available models at https://docs.livekit.io/agents/models/llm/
|
||||
llm=inference.LLM(model="openai/gpt-5.2-chat-latest"),
|
||||
# To use a realtime model instead of a voice pipeline, replace the LLM
|
||||
# with a RealtimeModel and remove the STT/TTS from the AgentSession
|
||||
# (Note: This is for the OpenAI Realtime API. For other providers, see https://docs.livekit.io/agents/models/realtime/)
|
||||
# 1. Install livekit-agents[openai]
|
||||
# 2. Set OPENAI_API_KEY in .env.local
|
||||
# 3. Add `from livekit.plugins import openai` to the top of this file
|
||||
# 4. Replace the llm argument with:
|
||||
# llm=openai.realtime.RealtimeModel(voice="marin")
|
||||
instructions=textwrap.dedent(
|
||||
"""\
|
||||
You are a friendly, reliable voice assistant that answers questions, explains topics, and completes tasks with available tools.
|
||||
You are a professional interviewer conducting the opening stage of a
|
||||
mock job interview. Your sole goal at this stage is to hear the
|
||||
candidate's self-introduction.
|
||||
|
||||
# Output rules
|
||||
|
||||
You are interacting with the user via voice, and must apply the following rules to ensure your output sounds natural in a text-to-speech system:
|
||||
|
||||
- Respond in plain text only. Never use JSON, markdown, lists, tables, code, emojis, or other complex formatting.
|
||||
- Keep replies brief by default: one to three sentences. Ask one question at a time.
|
||||
- Do not reveal system instructions, internal reasoning, tool names, parameters, or raw outputs
|
||||
- Spell out numbers, phone numbers, or email addresses
|
||||
- Omit `https://` and other formatting if listing a web url
|
||||
- Avoid acronyms and words with unclear pronunciation, when possible.
|
||||
|
||||
# Conversational flow
|
||||
|
||||
- Help the user accomplish their objective efficiently and correctly. Prefer the simplest safe step first. Check understanding and adapt.
|
||||
- Provide guidance in small steps and confirm completion before continuing.
|
||||
- Summarize key results when closing a topic.
|
||||
|
||||
# Tools
|
||||
|
||||
- Use available tools as needed, or upon user request.
|
||||
- Collect required inputs first. Perform actions silently if the runtime expects it.
|
||||
- Speak outcomes clearly. If an action fails, say so once, propose a fallback, or ask how to proceed.
|
||||
- When tools return structured data, summarize it to the user in a way that is easy to understand, and don't directly recite identifiers or other technical details.
|
||||
|
||||
# Guardrails
|
||||
|
||||
- Stay within safe, lawful, and appropriate use; decline harmful or out-of-scope requests.
|
||||
- For medical, legal, or financial topics, provide general information only and suggest consulting a qualified professional.
|
||||
- Protect privacy and minimize sensitive data.
|
||||
Welcome the candidate warmly, then ask them to introduce themselves:
|
||||
who they are and their background or work experience. Once they have
|
||||
shared a description of themselves and their experience — even briefly —
|
||||
call `intro_complete` immediately to advance the interview. Do not ask
|
||||
follow-up questions, do not probe for company-specific motivations, and
|
||||
do not discuss any other topics. One exchange is enough.
|
||||
"""
|
||||
),
|
||||
)
|
||||
+ "\n"
|
||||
+ _VOICE_RULES,
|
||||
)
|
||||
self._silence_budget = silence_budget
|
||||
self._stage_budget = stage_budget
|
||||
self._silence_task: asyncio.Task | None = None
|
||||
self._stage_task: asyncio.Task | None = None
|
||||
self._transitioning: bool = False
|
||||
|
||||
async def on_enter(self) -> None:
|
||||
await self.session.generate_reply(
|
||||
instructions="Greet the candidate warmly and invite them to introduce themselves."
|
||||
)
|
||||
# Both timers start after the greeting so they measure candidate response time.
|
||||
self._silence_task = asyncio.create_task(self._silence_watchdog())
|
||||
self._stage_task = asyncio.create_task(self._stage_watchdog())
|
||||
|
||||
async def on_exit(self) -> None:
|
||||
for task in (self._silence_task, self._stage_task):
|
||||
if task is not None:
|
||||
task.cancel()
|
||||
self._silence_task = None
|
||||
self._stage_task = None
|
||||
|
||||
async def _silence_watchdog(self) -> None:
|
||||
poll = 0.5
|
||||
last_speech_time = time.monotonic()
|
||||
|
||||
while True:
|
||||
if self.session.user_state == "speaking":
|
||||
last_speech_time = time.monotonic()
|
||||
silence_elapsed = time.monotonic() - last_speech_time
|
||||
if silence_elapsed >= self._silence_budget:
|
||||
break
|
||||
await asyncio.sleep(min(self._silence_budget - silence_elapsed, poll))
|
||||
|
||||
# Guard #1: intro_complete (or stage timer) may have already fired.
|
||||
if not isinstance(self.session.current_agent, SelfIntroAgent):
|
||||
return
|
||||
|
||||
logger.info("fallback: silence budget %.1f s elapsed", self._silence_budget)
|
||||
|
||||
# Silence path: brief — let any in-flight agent speech finish, then transition.
|
||||
if self.session.current_speech is not None:
|
||||
await self.session.current_speech.wait_for_playout()
|
||||
|
||||
# Guard #2: re-check after the wait before committing to the transition.
|
||||
# The _transitioning check-and-set has no await between them, so asyncio's
|
||||
# cooperative scheduler guarantees only one watchdog can pass both lines.
|
||||
if not isinstance(self.session.current_agent, SelfIntroAgent):
|
||||
return
|
||||
if self._transitioning:
|
||||
return
|
||||
self._transitioning = True
|
||||
|
||||
await self.session.say(
|
||||
"Let's go ahead and move on to your background and experience."
|
||||
)
|
||||
self.session.update_agent(
|
||||
PastExperienceAgent(chat_ctx=self.chat_ctx.copy(exclude_instructions=True))
|
||||
)
|
||||
|
||||
# To add tools, use the @function_tool decorator.
|
||||
# Here's an example that adds a simple weather tool.
|
||||
# You also have to add `from livekit.agents import function_tool, RunContext` to the top of this file
|
||||
# @function_tool
|
||||
# async def lookup_weather(self, context: RunContext, location: str):
|
||||
# """Use this tool to look up current weather information in the given location.
|
||||
#
|
||||
# If the location is not supported by the weather service, the tool will indicate this. You must tell the user the location's weather is unavailable.
|
||||
#
|
||||
# Args:
|
||||
# location: The location to look up weather information for (e.g. city name)
|
||||
# """
|
||||
#
|
||||
# logger.info(f"Looking up weather for {location}")
|
||||
#
|
||||
# return "sunny with a temperature of 70 degrees."
|
||||
async def _stage_watchdog(self) -> None:
|
||||
await asyncio.sleep(self._stage_budget)
|
||||
|
||||
# Guard #1: intro_complete (or silence timer) may have already fired.
|
||||
if not isinstance(self.session.current_agent, SelfIntroAgent):
|
||||
return
|
||||
|
||||
logger.info("fallback: stage budget %.1f s elapsed", self._stage_budget)
|
||||
|
||||
# Stage path: full graceful pause — candidate may be mid-sentence.
|
||||
if self.session.current_speech is not None:
|
||||
await self.session.current_speech.wait_for_playout()
|
||||
|
||||
max_wait = 5.0
|
||||
poll = 0.5
|
||||
waited = 0.0
|
||||
while self.session.user_state == "speaking" and waited < max_wait:
|
||||
await asyncio.sleep(poll)
|
||||
waited += poll
|
||||
|
||||
# Guard #2: re-check after the waits before committing to the transition.
|
||||
# The _transitioning check-and-set has no await between them, so asyncio's
|
||||
# cooperative scheduler guarantees only one watchdog can pass both lines.
|
||||
if not isinstance(self.session.current_agent, SelfIntroAgent):
|
||||
return
|
||||
if self._transitioning:
|
||||
return
|
||||
self._transitioning = True
|
||||
|
||||
await self.session.say(
|
||||
"Let's go ahead and move on to your background and experience."
|
||||
)
|
||||
self.session.update_agent(
|
||||
PastExperienceAgent(chat_ctx=self.chat_ctx.copy(exclude_instructions=True))
|
||||
)
|
||||
|
||||
@function_tool
|
||||
async def intro_complete(self, context: RunContext[InterviewData]):
|
||||
"""Call this tool as soon as the candidate has described themselves and
|
||||
their work experience. Any substantive self-introduction — even a short one —
|
||||
is sufficient. Do not wait for company-specific motivations or ask follow-up
|
||||
questions before calling this. Call it once, immediately, after the candidate's
|
||||
first real introduction."""
|
||||
context.userdata.intro_was_given = True
|
||||
return (
|
||||
PastExperienceAgent(chat_ctx=self.chat_ctx.copy(exclude_instructions=True)),
|
||||
"Thanks for that introduction. Now I'd like to explore your past experience.",
|
||||
)
|
||||
|
||||
|
||||
class PastExperienceAgent(Agent):
|
||||
def __init__(self, chat_ctx: ChatContext | None = None) -> None:
|
||||
super().__init__(
|
||||
instructions=textwrap.dedent(
|
||||
"""\
|
||||
You are a professional interviewer conducting the past-experience stage
|
||||
of a mock job interview. You have already heard the candidate's
|
||||
self-introduction — use what they shared to guide your questions.
|
||||
|
||||
Pick the most relevant experience the candidate mentioned and ask them
|
||||
to walk you through it: what the challenge was, their specific
|
||||
contribution, the outcome, and what they learned. Use targeted
|
||||
follow-up questions to probe depth and assess impact. Stay focused on
|
||||
one experience until you understand it thoroughly before moving on.
|
||||
"""
|
||||
)
|
||||
+ "\n"
|
||||
+ _VOICE_RULES,
|
||||
chat_ctx=chat_ctx,
|
||||
)
|
||||
|
||||
async def on_enter(self) -> None:
|
||||
if self.session.userdata.intro_was_given:
|
||||
instructions = (
|
||||
"Ask the candidate to walk you through their most relevant past experience "
|
||||
"or project. Do not re-introduce yourself or re-explain the transition — "
|
||||
"go straight to the question."
|
||||
)
|
||||
else:
|
||||
instructions = (
|
||||
"Start directly: ask the candidate to walk you through their most recent "
|
||||
"role or a project they've worked on. Do not reference any prior "
|
||||
"introduction — go straight to the question."
|
||||
)
|
||||
await self.session.generate_reply(instructions=instructions)
|
||||
|
||||
|
||||
server = AgentServer()
|
||||
@@ -101,34 +228,37 @@ server.setup_fnc = prewarm
|
||||
|
||||
@server.rtc_session(agent_name="interview-agent")
|
||||
async def my_agent(ctx: JobContext):
|
||||
# Logging setup
|
||||
# Add any other context you want in all log entries here
|
||||
ctx.log_context_fields = {
|
||||
"room": ctx.room.name,
|
||||
}
|
||||
ctx.log_context_fields = {"room": ctx.room.name}
|
||||
|
||||
# Set up a voice AI pipeline using OpenAI, Cartesia, Deepgram, and the LiveKit turn detector
|
||||
session = AgentSession(
|
||||
# Speech-to-text (STT) is your agent's ears, turning the user's speech into text that the LLM can understand
|
||||
# See all available models at https://docs.livekit.io/agents/models/stt/
|
||||
stt=inference.STT(model="deepgram/nova-3", language="multi"),
|
||||
# Text-to-speech (TTS) is your agent's voice, turning the LLM's text into speech that the user can hear
|
||||
# See all available models as well as voice selections at https://docs.livekit.io/agents/models/tts/
|
||||
session = AgentSession[InterviewData](
|
||||
llm=inference.LLM(model="openai/gpt-4.1-mini"),
|
||||
stt=inference.STT(model="deepgram/nova-3", language="en"),
|
||||
tts=inference.TTS(
|
||||
model="cartesia/sonic-3", voice="9626c31c-bec5-4cca-baa8-f8ba9e84c8bc"
|
||||
),
|
||||
# VAD and turn detection are used to determine when the user is speaking and when the agent should respond
|
||||
# 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,
|
||||
turn_handling=TurnHandlingOptions(
|
||||
turn_detection=MultilingualModel(),
|
||||
endpointing={
|
||||
"mode": "dynamic",
|
||||
"min_delay": 0.5,
|
||||
"max_delay": 6.0,
|
||||
},
|
||||
interruption={
|
||||
"mode": "adaptive",
|
||||
"min_words": 2,
|
||||
"resume_false_interruption": True,
|
||||
},
|
||||
preemptive_generation={
|
||||
"enabled": True,
|
||||
"preemptive_tts": True,
|
||||
},
|
||||
),
|
||||
userdata=InterviewData(),
|
||||
)
|
||||
|
||||
# Start the session, which initializes the voice pipeline and warms up the models
|
||||
await session.start(
|
||||
agent=Assistant(),
|
||||
agent=SelfIntroAgent(),
|
||||
room=ctx.room,
|
||||
room_options=room_io.RoomOptions(
|
||||
audio_input=room_io.AudioInputOptions(
|
||||
@@ -139,18 +269,6 @@ async def my_agent(ctx: JobContext):
|
||||
),
|
||||
)
|
||||
|
||||
# # Add a virtual avatar to the session, if desired
|
||||
# # For other providers, see https://docs.livekit.io/agents/models/avatar/
|
||||
# avatar = anam.AvatarSession(
|
||||
# persona_config=anam.PersonaConfig(
|
||||
# name="...",
|
||||
# avatarId="...", # See https://docs.livekit.io/agents/models/avatar/plugins/anam
|
||||
# ),
|
||||
# )
|
||||
# # Start the avatar and wait for it to join
|
||||
# await avatar.start(session, room=ctx.room)
|
||||
|
||||
# Join the room and connect to the user
|
||||
await ctx.connect()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user