creamy-eye-58634
08/01/2025, 3:51 PMtall-airline-16756
08/01/2025, 3:51 PMtall-airline-16756
08/01/2025, 3:51 PM@function_tool() decorator. Implement the logic for handling RPC payloads.
2. Register the RPC Method: Register the RPC method in your agent's initialization code to make it available for remote invocation.
3. Invoke the RPC Method from the Frontend: Use the performRpc method on the frontend to call the registered RPC method, specifying the destination identity and payload.
For a detailed example and code snippets, refer to the RPC + State Agent Example. Let me know if you need further assistance!creamy-eye-58634
08/01/2025, 3:54 PMtall-airline-16756
08/01/2025, 3:55 PMroom.localParticipant.registerRpcMethod("client.flashcard", handleFlashcard);
async function handleFlashcard(data) {
const payload = JSON.parse(data.payload);
if (payload.action === "show") {
// Update UI to display the flashcard
console.log("Flashcard Data:", payload);
}
}
Agent Side (Python): Then, use perform_rpc to send the RPC call over to the frontend.
await ctx.room.local_participant.perform_rpc(
destination_identity="frontend-identity",
method="client.flashcard",
payload=json.dumps({
"action": "show",
"id": "card123",
"question": "What is the capital of France?",
"answer": "Paris"
})
)
This way, the agent can dynamically update the frontend UI whenever needed. If you’d like me to walk you through it more or share some extra examples, just let me know—I’m here to help!
Sources: Remote method calls (RPC) | LiveKit Docs | Function calling with Voice Agents | LiveKit Docscreamy-eye-58634
08/01/2025, 3:59 PMtall-airline-16756
08/01/2025, 3:59 PMget_weather from an Assistant Agent using an RPC method, here’s a simple way to do it:
1. Define the Function Tool: Inside your agent, just use the @function_tool() decorator to set up the function tool.
from livekit.agents import function_tool, Agent, RunContext
class MyAgent(Agent):
@function_tool()
async def get_weather(self, context: RunContext, location: str) -> dict:
"""Fetch weather information for a given location."""
return {"weather": "sunny", "temperature_f": 70}
1. Trigger the Function via RPC: Then, from the frontend, you can call the function tool using perform_rpc.
response = await ctx.room.local_participant.perform_rpc(
destination_identity="agent-identity",
method="get_weather",
payload=json.dumps({
"location": "New York"
}),
response_timeout=5.0
)
This way, your frontend can easily call the get_weather function tool via RPC. If anything’s unclear or you want more examples, just let me know—I’m here to help!
Sources: RPC | LiveKit Docs | Tool definition and use | LiveKit Docs | complex-agents/shopify-voice-shopper/shopify.py