can I user RPC with Livekit Agents Playground? how...
# ask-ai
r
can I user RPC with Livekit Agents Playground? how to register
register_rpc_method
? is there a simple example without custom frontend?
t
Thanks for reaching out, Ivan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely, you can use RPC with LiveKit Agents Playground without needing a custom frontend. You can conveniently implement RPC right within your agent code using the LiveKit Python SDK. To get started, you just need to register an RPC method using
ctx.room.local_participant.register_rpc_method("method_name", handler_function)
. This setup lets remote participants—like web clients or other agents—call your method to trigger actions or fetch data. Here’s a straightforward example to help you along:
Copy code
async def handle_diagnosis_request(data: rtc.RpcInvocationData):
    try:
        payload = json.loads(data.payload)
        symptom = payload.get("symptom", "unknown")
        response = {"diagnosis": f"Based on '{symptom}', try getting rest", "severity": "low"}
        return json.dumps(response)
    except Exception as e:
        return json.dumps({"error": str(e)})

# Register the RPC method
ctx.room.local_participant.register_rpc_method("request_diagnosis", handle_diagnosis_request)
In this example, we register an RPC method called
request_diagnosis
, define a handler to process incoming requests, and return a JSON response. It’s a simple way to get your RPC up and running! If you’d like to explore further, I recommend checking out the LiveKit RPC Documentation and Using RPC with Agents. They offer great insights and additional details to support you. Sources: rpc/rpc_agent.py | agent/game_host.py | Remote method calls | LiveKit docs | Tool definition and use | LiveKit docs