how gemini tool calling works ? help me to impleme...
# ask-ai
b
how gemini tool calling works ? help me to implement it
t
Thanks for reaching out, Mustafa. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're interested in how Gemini tool calling in LiveKit can help your agents interact with external systems and do more than just generate text responses. This is made possible by defining function tools within your agent, which then get transformed into Gemini-compatible function declarations. To get started with Gemini tool calling, you have two friendly options: • Using LiveKit Inference: This is the easiest way! LiveKit takes care of managing the Gemini service for you. You simply define your agent with tools using the
@function_tool
decorator and set it up with the Gemini model. • Using the Google Plugin: If you want a bit more control and access to Google-specific features, this method is great. You’ll configure the Gemini LLM with particular parameters and connect it with your agent. Both options will need you to set up authentication—either with a Google API key or Google Cloud credentials, depending on your setup. You can also fine-tune how the tools behave, adjust response randomness, and tweak other settings for more advanced needs. If you’d like, I’m here to walk you through detailed implementation steps or share some code examples. Just let me know! Sources: AI Agents Framework | livekit/livekit_composite | DeepWiki | Google Gemini LLM plugin guide | LiveKit Docs | Real-time AI Models & Streaming | livekit/livekit_composite | DeepWiki | docs/index.yaml | livekit-plugins/livekit-plugins-google/livekit/plugins/google/realtime/real...
b
show me the implementation code for gemini realtime
t
I'd love to share an example of how you can implement Gemini real-time tool calling in LiveKit. Take a look at this:
Copy code
from livekit.plugins.google.beta import RealtimeModel
from google.genai import types

# Configure the real-time Gemini model
model = RealtimeModel(
    model="gemini-2.0-flash-live-001",
    instructions="You are a helpful assistant with tool access.",
    tool_behavior=types.Behavior.BLOCK,  # Wait for tool completion
    tool_response_scheduling=types.FunctionResponseScheduling.WHEN_IDLE
)

# Example of defining a tool function
def example_tool_function(context, input_data):
    """Example tool function to process input data."""
    # Perform some processing
    result = f"Processed: {input_data}"
    return None, result

# Add the tool to the model
model.add_tool(
    name="example_tool",
    function=example_tool_function,
    description="A tool to process input data."
)

# Start the real-time session
async def start_realtime_session():
    session = await model.start_session()
    # Interact with the session as needed
    response = await session.send_message("Process this data")
    print(response)

# Run the session
if __name__ == "__main__":
    import asyncio
    asyncio.run(start_realtime_session())
This snippet walks you through setting up a real-time Gemini model, creating a tool function, and kicking off a session for real-time interactions. If you have any questions or want me to walk you through more examples, just let me know—I’m here to help!
b
what is possible FunctionResponseScheduling types, and how they works ?
t
Here are the possible
FunctionResponseScheduling
types you can use in the Gemini real-time tool calling: • `WHEN_IDLE`: The model patiently waits until it’s idle (meaning it’s not actively generating a response) before it processes the results from function or tool calls. This helps make sure that any ongoing responses aren’t interrupted by incoming tool results. • `IMMEDIATELY`: Tool responses are handled as soon as they come in, without waiting for the model to become idle. This lets you have dynamic updates to the session state and you can even change this setting on the fly during a session without needing to restart. The
tool_response_scheduling
parameter is what controls when the model processes those function call outputs. You can set it up when you initialize the
RealtimeModel
, or update it anytime during a session using
session.update_options()
. If you’d like, I’m here to help with more details or examples on how to use these types effectively. Just let me know! Sources: livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti... | Real-time AI Models & Streaming | livekit/livekit_composite | DeepWiki
b
realtime model has no add_tool method
t
Hi there! I wanted to share some helpful info about the
RealtimeModel
in LiveKit. It doesn’t have an
add_tool
method, but no worries—you can manage your tools dynamically using the
update_tools
method during a session. Here’s a friendly guide on how to do it: 1. Define Custom Tools: You can register functions for the model to use with the
@function_tool
decorator. For example:
Copy code
from livekit.agents import function_tool

@function_tool()
async def get_weather(location: str) -> str:
    return f"The weather in {location} is sunny."
2. Initialize the Agent with Tools: When you create your agent instance, just add your tools like this:
Copy code
agent = MyAgent(
    instructions="You are a helpful assistant.",
    tools=[get_weather],
)
3. Dynamically Update Tools: If you want to add or change tools after your agent is up and running, just use
update_tools
like this:
Copy code
async def random_number() -> int:
    import random
    return random.randint(0, 100)

await agent.update_tools([
    *agent.tools.values(),
    function_tool(random_number, name="random_number", description="Get a random number")
])
4. Enable Built-in Google Tools: You can also specify built-in tools right when you initialize your
RealtimeModel
by using the
_gemini_tools
parameter. If you have any questions or want me to walk you through more examples, just let me know—I’m here to help! Sources: examples/voice_agents/dynamic_tool_creation.py | Gemini Live API plugin | LiveKit Docs | Docs::Agents | Google Gemini LLM plugin guide | LiveKit Docs | Agent Session & Client Integration | livekit/livekit_composite | DeepWiki