In livekit, how the agent use the tools and get th...
# ask-ai
s
In livekit, how the agent use the tools and get the result from the tools?
t
Thanks for reaching out, Takeshi. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
In LiveKit, agents use tools through a structured process that includes defining, executing, and processing results from those tools: • Tool Definition: Tools are defined using the
@function_tool
decorator. This makes them available for the agent to use. • Tool Execution: When the agent (LLM) decides to use a tool, it prepares the required arguments and calls the tool function. The function receives a
RunContext
object, which provides access to session data, speech controls, and other contextual information. • Result Processing: The tool's return value is automatically converted to a string and sent back to the agent. The agent then uses this result to generate a reply or decide on further actions. If the tool returns
None
, it completes silently without a reply. • Error Handling: If a tool encounters an error, it can raise a
ToolError
exception, which sends an error message back to the agent instead of a normal result. • Integration: Tool results are integrated into the conversation flow, allowing the agent to use them in real time and continue the interaction naturally. This system enables agents to interact with external systems, access real-time data, and perform complex operations while maintaining a seamless conversational experience. Sources: agents/src/pipeline/pipeline_agent.ts | Tool definition and use | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py
s
I defined the tool to search real estate and with the logging, I ensured the result is json style. I want the agent to send this result data to frontend to show the real estate data in frontend. How can I do that?
t
Hey there! Let me walk you through a super smooth way to share your real estate search results from your agent to the frontend. LiveKit's RPC (Remote Procedure Call) is like a friendly messenger that helps you send your data exactly where it needs to go. • First, set up a cozy little RPC method in your frontend that's ready to welcome and display your real estate data. • Then, from your agent's tool function, just use the
RunContext
to send over your JSON results - it's as easy as passing a note to a friend! • Your frontend will catch the data and instantly refresh the UI, making everything feel seamless and connected. Think of this as your data's direct express lane - clean, efficient, and perfectly designed to make your workflow feel smooth and intuitive.
s
Okay, please give me the sample code.
t
Hey there! Let me walk you through a cool way to send real estate search results from your agent tool to the frontend using LiveKit's RPC mechanism. It's super straightforward and flexible! Agent-side (Python):
Copy code
from livekit.agents import function_tool, RunContext
import json

@function_tool()
async def send_real_estate_results(context: RunContext, results: dict, participant_identity: str):
    # Send the results to the frontend via RPC
    try:
        await context.room.local_participant.perform_rpc(
            destination_identity=participant_identity,
            method="showRealEstateResults",
            payload=json.dumps(results),
            response_timeout=10.0,
        )
    except Exception as e:
        return f"Failed to send results: {e}"
Frontend-side: • Create an RPC handler for the
showRealEstateResults
method in your app. • When it's called, just parse the JSON and update your UI with the real estate data. Pretty neat, right? This approach lets you easily send any structured data to your frontend for display or further processing. Hope this helps! Sources: Function calling with Voice Agents | LiveKit Docs | RPC | LiveKit Docs
s
Give me getting participant_identity code and frontend code.
t
Getting
participant_identity
in the Agent Tool:
Copy code
from livekit.agents import get_job_context

room = get_job_context().room
participant_identity = next(iter(room.remote_participants))
Hey there! Let me walk you through how to grab the identity of the first remote participant - which is usually the awesome user on your frontend. It's super straightforward! Frontend Code to Handle the RPC Call:
Copy code
import { RpcError, RpcInvocationData } from 'livekit-client';

// Register the RPC method to handle data from the agent
localParticipant.registerRpcMethod(
  'showRealEstateResults',
  async (data: RpcInvocationData) => {
    try {
      // Parse the payload sent from the agent
      const realEstateData = JSON.parse(data.payload);
      
      // Display the data in your UI
      displayRealEstateResults(realEstateData);
      
      // Return a response to the agent
      return 'Results displayed successfully';
    } catch (error) {
      console.error('Error handling real estate results:', error);
      throw new RpcError('Failed to display results');
    }
  }
);

// Function to update your UI with the received data
function displayRealEstateResults(data) {
  // Example implementation - adapt to your UI framework
  const resultsContainer = document.getElementById('real-estate-results');
  resultsContainer.innerHTML = '';
  data.properties?.forEach(property => {
    const propertyElement = document.createElement('div');
    propertyElement.className = 'property-card';
    propertyElement.innerHTML = `
      ${property.address}
      Price: $${property.price}

      Bedrooms: ${property.bedrooms}

      Bathrooms: ${property.bathrooms}

    `;
    resultsContainer.appendChild(propertyElement);
  });
}
Now we're talking! This setup is like a friendly messenger between your agent and frontend. It lets your agent send over some cool real estate data, which then gets beautifully displayed in your UI. Pretty neat, right? 😊 Sources: Tool definition and use | LiveKit Docs | Remote method calls (RPC) | LiveKit Docs
s
The code says registerRpcMethod does not exist in livekit js sdk. why?
t
Hey there! I noticed you might be having a bit of trouble with the
registerRpcMethod
function. No worries – I can help you sort this out! Just a heads up, you'll want to call this method on the Room object, not
localParticipant
. The way you're currently doing it is actually a bit outdated. Let me show you the right way to set up your RPC handler in the frontend:
Copy code
room.registerRpcMethod(
  'showRealEstateResults',
  async (data) => {
    const realEstateData = JSON.parse(data.payload);
    displayRealEstateResults(realEstateData);
    return 'Results displayed successfully';
  }
);
Just swap out
room
with your specific
Room
instance. This approach is totally the way to go for handling RPC calls from agents in the LiveKit JS SDK. Hope this helps make things clearer! Sources: src/room/rpc.test.ts | Tool definition and use | LiveKit Docs | RPC | LiveKit Docs
s
I use livekit js sdk 2.15.2. And this code also wrong, what could be the problem?
t
Hey there! I see you're running into a bit of a tricky situation with the LiveKit JS SDK and the
registerRpcMethod
. No worries, we'll help you troubleshoot this together! 😊 • It looks like you might be using an older version of the SDK that doesn't have this method yet. • There's a chance your
Room
object might not be fully set up when you're trying to register the method. • You could also be working with a version that's a bit out of sync with the current documentation. Let's solve this! • First, take a quick peek at your
package.json
and make sure
livekit-client
is version 2.15.2 or newer. • Double-check that you're calling
room.registerRpcMethod
after your
Room
object is fully ready to go. • If
registerRpcMethod
is still playing hard to get, don't stress! You can use
registerTextStreamHandler
or
registerByteStreamHandler
as a backup for receiving custom data messages. Still stuck? No problem! If you can share the exact error message and a snippet of how you're setting up your
Room
object, we'll be able to pinpoint exactly what's going on and get you back on track. We've got your back! 👍 Sources: examples/rpc/rpc-demo.ts
s
RPC handler already registered for method showRealEstateResults, unregisterRpcMethod before trying to register again this is frontend error.
t
Hey there! 👋 Looks like you've run into a tricky little RPC handler error. Don't worry, we've got your back! The message "RPC handler already registered for method showRealEstateResults, unregisterRpcMethod before trying to register again" basically means you're trying to add a handler for a method that's already got one registered. Here's how to fix it: • Before adding a new handler, make sure to clear out the old one by calling
room.unregisterRpcMethod('showRealEstateResults')
. • If you're working with React or similar frameworks, remember to clean up your handler in your component (like in the return function of
useEffect
). • Try to avoid registering the same handler multiple times, especially when your component is reloading or remounting. Here's a quick example to help you out:
Copy code
useEffect(() => {
  if (!room) return;
  room.registerRpcMethod('showRealEstateResults', handler);
  return () => {
    room.unregisterRpcMethod('showRealEstateResults');
  };
}, [room]);
This approach will help you keep things clean and avoid those pesky errors. You've got this! 💪 Sources: complex-agents/role-playing/role_playing_frontend/components/character-port... | src/room/rpc.test.ts