witty-yak-62929
07/24/2025, 1:18 PMtall-airline-16756
07/24/2025, 1:18 PMtall-airline-16756
07/24/2025, 1:19 PMfrom livekit.agents import Agent
class TwoStepTranslator(Agent):
def __init__(self, target_language: str = "French"):
super().__init__(
instructions=f"""
You are a two-step translator. For each user message:
1. Translate the user's speech to English if it's not already in English.
2. Translate the English version to {target_language}.
Format your response as:
English: [English translation]
{target_language}: [{target_language} translation]
""",
stt=deepgram.STT(),
llm=openai.LLM(model="gpt-4o"),
tts=elevenlabs.TTS(model="eleven_multilingual_v2"),
vad=silero.VAD.load()
)
2. Using Google Translate API for Sequential Translation:
• Use the Google Translate API to translate text to English first and then to the target language.
• Example:
class SequentialTranslator(Agent):
async def translate_text(self, text: str, target_lang: str):
# Use Google Translate API for translation
pass
3. Using Gladia STT with Built-in Translation:
• Configure Gladia STT for real-time translation to English and then use another instance for the second step.
4. Multi-Language Translator with Dynamic Language Switching:
• Create a sophisticated system that dynamically switches between languages and uses LLM for contextual translations.
Key Considerations:
• Choose an STT provider that supports multiple languages.
• LLM-based translation often provides better context awareness compared to API-based services.
• Consider latency for real-time performance.
Let me know if you need further details or assistance with implementation!
Sources: Gladia integration guide | LiveKit Docs | README.md | livekit-plugins/livekit-plugins-turn-detector/README.md | Gladia integration guide | LiveKit Docs | README.mdwitty-yak-62929
07/24/2025, 1:22 PMtall-airline-16756
07/24/2025, 1:23 PMAgent instance. - Check out this friendly example that shows exactly how it works:
from livekit.agents import Agent, function_tool
class TranslationCoordinatorAgent(Agent):
def __init__(self):
super().__init__(
instructions="You coordinate translation requests and hand off to appropriate translation agents."
)
@function_tool()
async def handoff_to_translator(self, target_language: str):
"""Hand off to a specialized translation agent for the target language."""
translator_agent = SpecializedTranslatorAgent(target_language)
return translator_agent
2. Context Preservation During Handoff: Want to keep the conversation flowing smoothly? No problem! We can pass along the chat context to make sure nothing gets lost in translation. - This means the new agent knows exactly what's been discussed before. - Here's how you can do that:
class SpecializedTranslatorAgent(Agent):
def __init__(self, target_language: str, chat_ctx: Optional[ChatContext] = None):
super().__init__(
instructions=f"You are a specialized translator for {target_language}.",
chat_ctx=chat_ctx # Preserve conversation history
)
3. Direct Agent Update: Sometimes you just want a quick switch - and we've got you covered! - You can directly update the agent using the update_agent method. - It's as simple as:
session.update_agent(SpecializedTranslatorAgent(target_language))
4. Handoff with Return Values: Want to add a little extra communication during the handoff? We can do that too! - Return both the new agent and a friendly message. - Check out this approach:
@function_tool()
async def handoff_to_translator(self, target_language: str):
translator_agent = SpecializedTranslatorAgent(target_language)
return translator_agent, f"Switching to {target_language} translation specialist."
5. Tracking Handoffs: Don't worry about losing track - the system automatically monitors agent handoffs using AgentHandoffEvent. It keeps a record of the old and new agents, making debugging a breeze. These methods make switching between agents feel totally natural and seamless. Need me to break anything down further? Just ask - I'm here to help! 😊 Sources: Tool definition and use | LiveKit Docs | Workflows | LiveKit Docs | main.py | examples/voice_agents/restaurant_agent.pywitty-yak-62929
07/24/2025, 1:37 PMtall-airline-16756
07/24/2025, 1:38 PM@function_tool()
async def on_consent_given(self):
return HelpfulAssistant()
2. Direct Agent Update: - This is like having a backstage crew quickly switching performers during a show. - Great for: - Switching between different assistant personalities - When your app needs to make quick, specific transitions - Letting users choose their preferred assistant - Here's a quick peek at how that works:
session.update_agent(HelpfulAssistant())
Key Considerations: - Use tool-based handoff when you want a smart, adaptive transition - Choose direct agent updates for precise, behind-the-scenes control - You can always reset or keep the conversation context as needed Got questions? I'm here to help you find the perfect handoff method for your project. Just ask, and we'll figure it out together! Sources: Workflows | LiveKit Docs | Workflows | LiveKit Docs | Agents v0.x migration guide | LiveKit Docs | multi-agent/long_or_short_agent.pywitty-yak-62929
07/24/2025, 1:40 PMtall-airline-16756
07/24/2025, 1:41 PMasync def entrypoint(ctx: JobContext):
# Create agent with initial tools
agent = MyAgent(
instructions="You are a helpful assistant that can answer questions and help with tasks.",
tools=[
function_tool(
_get_course_info,
name="get_course_info",
description="Get information about a course",
)
],
)
# Direct agent update: Adding new tools after creation
async def _random_number() -> int:
num = random.randint(0, 100)
<http://logger.info|logger.info>(f"random_number called: {num}")
return num
await agent.update_tools(
agent.tools
+ [function_tool(_random_number, name="random_number", description="Get a random number")]
)
Practical Use Cases: 1. Dynamic Tool Loading: Think of this like giving your agent a quick skill upgrade mid-conversation. 2. Real-time Configuration: Imagine tweaking your agent's abilities or voice on the spot – super flexible! 3. Contextual Adaptation: Changing what your agent can do based on how the chat is going. Isn't it awesome how you can smoothly modify your agent without any interruption? If you're curious about how this works or want to dive deeper, I'm totally here to help! Feel free to ask me anything. Sources: examples/avatar_agents/bey/README.md | agent/main.pywitty-yak-62929
07/24/2025, 1:44 PMtall-airline-16756
07/24/2025, 1:45 PM@ctx.room.local_participant.register_rpc_method("pg.updateConfig")
async def update_config(data: rtc.rpc.RpcInvocationData):
new_config = parse_session_config(json.loads(data.payload))
if config != new_config:
session = model.sessions[0]
session.session_update(
instructions=new_config.instructions,
voice=new_config.voice,
temperature=new_config.temperature,
max_response_output_tokens=new_config.max_response_output_tokens,
turn_detection=new_config.turn_detection,
modalities=new_config.modalities,
)
return json.dumps({"changed": True})
Use Case: - Imagine you want to tweak how your agent talks or behaves right in the middle of a conversation. This method makes it super easy! It's like having a remote control for your AI buddy. Key Methods for Dynamic Updates: 1. update_instructions(instructions: str): Change up the agent's core instructions. 2. update_tools(tools: list[FunctionTool | RawFunctionTool]): Swap out the tools the agent can use. 3. update_chat_ctx(chat_ctx: ChatContext): Refresh the conversation context. Advantages: - No need to restart the whole session - just make changes on the spot! - Customize your agent's behavior in real-time to match exactly what you need. Got questions? I'm all ears! Feel free to ask, and I'll be happy to explain more. 😊 Sources: livekit-agents/livekit/agents/voice/agent_activity.py | complex-agents/nutrition-assistant/README.md | agent/main.py