modern-winter-66316
12/06/2025, 12:31 AMtall-airline-16756
12/06/2025, 12:31 AMtall-airline-16756
12/06/2025, 12:31 AMmodern-winter-66316
12/06/2025, 12:31 AMtall-airline-16756
12/06/2025, 12:32 AMagent.py, but I need one quick clarification so I wire it exactly into your worker code.
Do you want:
1) The 11 persona classes as plain Python classes (each exposing a name, starter_prompt, and a respond(self, message) stub) that your worker will import and instantiate, or 2) The 11 persona classes implemented as FastAPI dependency-style objects or background tasks compatible with your existing worker structure (if your worker expects async methods like async respond(...))?
Also please paste (or describe) the current worker file’s interface for how it loads/uses a persona (class name, function names, sync vs async, and expected method signatures). If you prefer, paste the relevant snippet and I’ll generate agent.py that matches it exactly.modern-winter-66316
12/06/2025, 12:33 AMpersona (one of your list). The token’s RoomAgentDispatch.metadata should include "persona":"Taxi Driver" etc. The agent worker will read this metadata to choose persona.
Example (snippet to build room_config inside /start-lesson):
# inside /start-lesson handler
persona = requested_persona # validate against allowed list
room_config = proto_livekit.RoomConfiguration(
agents=[
proto_livekit.RoomAgentDispatch(
agent_name="cloud-english-teacher",
agent_id="CA_e7hLTus66nKZ",
metadata=f'{{"persona":"{persona}"}}',
)
]
)
2) Agent worker: base teaching agent + persona classes
• Create a TeachingAgent base that sets the shared system prompt (friendly, stay in character, one tip, adjust difficulty, no lists).
• Each persona provides a short role_description and a starter prompt (greeting + context).
• Worker reads job.dispatch_metadata (or job.request.metadata) to get persona and instantiates the right class.
Concise agent patterns:
TeachingAgent base (core rules)
class TeachingAgent(Agent):
def __init__(self, persona_name: str, role_description: str, starter_instructions: str, tts_voice: str = None):
system_prompt = f"""
You are {persona_name}, {role_description}.
Teaching style rules (apply always):
- Be human and friendly; stay in character.
- Speak naturally in English, 1–2 short sentences per turn (no lists).
- Adjust vocabulary and grammar to the learner's level.
- After a short conversational exchange, give one short actionable tip as spoken feedback.
- Do not output bullet lists or long explanations.
"""
super().__init__(chat_ctx=ChatContext())
# put the system prompt into the agent's chat context
self.chat_ctx.append({"role":"system","content": system_prompt})
# persona-specific starter
self.starter_instructions = starter_instructions
self.persona_name = persona_name
self.tts_voice = tts_voice
Example persona classes (two shown; repeat pattern for all 11)
class EnglishTeacher(TeachingAgent):
def __init__(self):
super().__init__(
persona_name="Alex (English Teacher)",
role_description="a general English teacher who guides conversational practice and corrects gently",
starter_instructions="Greet the student warmly and ask how they'd like to practice today."
)
class TaxiDriver(TeachingAgent):
def __init__(self):
super().__init__(
persona_name="Sam (Taxi Driver)",
role_description="a chatty taxi driver who speaks naturally about routes, destinations, and short small talk",
starter_instructions="Greet the passenger and ask where they'd like to go."
)
Persona registry (map persona string to class)
PERSONA_MAP = {
"English Teacher": EnglishTeacher,
"Taxi Driver": TaxiDriver,
"Hotel Receptionist": HotelReceptionist,
"Waiter": Waiter,
"Check-in Agent": CheckInAgent,
"Border Officer": BorderOfficer,
"Nurse": Nurse,
"Ticket Clerk": TicketClerk,
"Information Agent": InformationAgent,
"Museum Front Desk": MuseumFrontDesk,
"Bartender": Bartender,
}
3) Worker entrypoint: choose persona from dispatch metadata
• When dispatched, LiveKit provides metadata. Use it to pick the persona class; default to "English Teacher".
Concise entrypoint flow:
async def entrypoint(job: JobContext):
# read persona from dispatch metadata
md = {}
try:
md = job.request.metadata or {}
except Exception:
pass
persona_key = md.get("persona") or "English Teacher"
PersonaCls = PERSONA_MAP.get(persona_key, EnglishTeacher)
agent_instance = PersonaCls()
# Create AgentSession wired to LiveKit Inference model strings
session = AgentSession(
ctx=job,
agent=agent_instance,
stt="deepgram/nova-3",
llm="openai/gpt-4.1-mini",
tts="cartesia/sonic-2",
enable_noise_cancellation=True,
)
await session.start(room=job.room)
# Have persona produce greeting
await session.generate_reply(instructions=agent_instance.starter_instructions)
await session.wait_closed()
Note: exact metadata access may be job.request.metadata or job.dispatch_metadata depending on SDK version — check job object for available fields; print job to confirm.
4) Persona specifics to include
• persona_name (display)
• role_description (how to role-play)
• starter_instructions (first prompt)
• optional tts_voice variant string (cartesia voice id) — pass to AgentSession TTS param or override session.say voice if supported.
5) Prompt engineering tips (short)
• Keep system prompt concise and authoritative about “stay in character” and “only one tip after short exchange”.
• Use few-shot examples if needed (1–2 pairs) to demonstrate the feedback format.
• Track learner level in session userdata and prepend a line like "Learner level: intermediate" into prompts to influence vocabulary.
6) Persisting and using learner level
• Use AgentSession userdata to store { level: "beginner"/"intermediate"/"advanced" } and update it from agent logic or frontend controls.
• Example when building session:
session = AgentSession(..., userdata={"level":"A2"})
• When generating replies, include the current level in instructions:
"User level: A2. Use simple vocabulary."
7) Voice variants
• Use cartesia voice variants per persona: e.g., pass tts="cartesia/sonic-2:voice-male-1" if supported.
• If your TTS param supports a voice ID, expose persona.tts_voice and use it when creating the AgentSession or when calling say/generate_reply.
8) Minimal list of persona starter prompts (you can use these verbatim)
• English Teacher (Alex): "Hello — I'm Alex, your English teacher. How would you like to practice today?"
• Taxi Driver: "Hi, where can I take you today?"
• Hotel Receptionist: "Welcome. Do you have a reservation?"
• Waiter: "Hi, I'll be taking your order — what would you like?"
• Check-in Agent: "Good morning. May I have your name and reservation details?"
• Border Officer: "Passport and purpose of visit, please."
• Nurse: "Hello. What brings you in today? Describe your symptoms briefly."
• Ticket Clerk: "Hi — where are you traveling and when?"
• Information Agent: "How can I help you find information today?"
• Museum Front Desk: "Welcome to the museum — would you like a map or a guided tour?"
• Bartender: "What can I get you? Any preferences?"
9) Testing & validation
• Add logging in the agent to print persona selection on startup.
• Test by calling /start-lesson with persona set and verify the agent greets in that role.
• For remote frontend builds (Vercel), ensure BACKEND_URL and CORS are set.
10) Expansion ideas
• Provide an endpoint /start-lesson?persona=Taxi%20Driver so frontend can let user pick persona.
• Add small persona selector UI and pass chosen persona to backend.
If you’d like, I can:
• Produce the full agent.py file with all 11 persona classes and starter prompts wired exactly into your existing worker code, or
• Update FastAPI /start-lesson to accept persona in the request body and validate it.tall-airline-16756
12/06/2025, 12:33 AMrespond(self, message) (sync) or async respond(self, message) (async)?
2. What is the exact attribute name/path for dispatch metadata in your job object: job.request.metadata, job.dispatch_metadata, or something else? If unsure, paste the small snippet that shows how your worker receives the job.
3. Do you want AgentSession created exactly as in your example (STT deepgram/nova-3, LLM openai/gpt-4.1-mini, TTS cartesia/sonic-2) and enable_noise_cancellation=True hardcoded, or should those be configurable via environment variables?
Answer these and I’ll produce the full agent.py with all 11 persona classes, starter prompts, and an entrypoint matching your worker.modern-winter-66316
12/06/2025, 12:36 AMrespond(self, message) (sync) or async respond(self, message) (async)?
2. What is the exact attribute name/path for dispatch metadata in your job object: job.request.metadata, job.dispatch_metadata, or something else? If unsure, paste the small snippet that shows how your worker receives the job.tall-airline-16756
12/06/2025, 12:36 AMasync def (e.g., async def on_enter(self) and async def respond(self, message)), because the agent framework and session I/O are asynchronous.
• Read dispatch metadata from ctx.job.metadata (a JSON string). Parse it with json.loads(ctx.job.metadata) and read the "persona" key.
If that matches your worker, I’ll produce the full async agent.py with all 11 persona classes, starter prompts, persona registry, and an entrypoint that reads ctx.job.metadata. If your worker uses a different job variable name or a different metadata path, paste the small entrypoint snippet and I’ll match it exactly.
Sources: Agents and handoffs | LiveKit docs | Job lifecycle | LiveKit docs | Agent dispatch | LiveKit docsmodern-winter-66316
12/06/2025, 12:38 AMtall-airline-16756
12/06/2025, 12:38 AM