how to create dynamic function tools with descript...
# ask-ai
f
how to create dynamic function tools with description in livekit
t
Thanks for reaching out, Viren. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To create dynamic function tools with descriptions in LiveKit, you can use the
function_tool
decorator or function from the
livekit.agents
module. Here are some friendly ways to approach this: • Using
function_tool
Directly:
You can create tools dynamically by calling
function_tool
with a name, description, and your callable function. For example:
Copy code
async def _get_weather(location: str) -> str:
    return f"The weather in {location} is sunny."

weather_tool = function_tool(
    _get_weather,
    name="get_weather",
    description="Get the weather in a specific location"
)
• At Agent Initialization: Another great way is to define tools when you create the agent by passing them in the
tools
list. For example:
Copy code
agent = MyAgent(
    instructions="You are a helpful assistant.",
    tools=[
        function_tool(
            _get_course_info,
            name="get_course_info",
            description="Get information about a course"
        )
    ],
)
• Updating Tools at Runtime: If you want to modify tools on the fly, you can use
agent.update_tools()
. Here's how:
Copy code
async def _random_number() -> int:
    return random.randint(0, 100)

await agent.update_tools(
    agent.tools + [
        function_tool(
            _random_number,
            name="random_number",
            description="Get a random number"
        )
    ]
)
These methods give you the flexibility to create tools that fit your unique needs perfectly. If you have any questions or need a hand with anything else, just let me know—I'm here to help! Sources: livekit-plugins/livekit-plugins-google/livekit/plugins/google/utils.py | Tool definition and use | LiveKit docs
f
can you give an example of _get_course_info tool with definition and how to use it At Agent Initialization:
t
I'd love to walk you through an example of the
_get_course_info
tool, showing its definition and how you can use it when initializing your agent: Definition of the Tool:
Copy code
from enum import Enum
from pydantic import BaseModel

# Define the course types dynamically
courses = ["Math", "Science", "History"]
CourseType = Enum("CourseType", {c.replace(" ", "_"): c for c in courses})

# Define the input model for the tool
class CourseInfo(BaseModel):
    course: CourseType
    location: str  # e.g., "online" or "in-person"

# Define the tool function
async def _get_course_info(info: CourseInfo) -> str:
    return f"The course {info.course.value} is available {info.location}."
Using the Tool at Agent Initialization:
Copy code
from livekit.agents import function_tool, MyAgent

# Initialize the agent with the tool
agent = MyAgent(
    instructions="You are a helpful assistant.",
    tools=[
        function_tool(
            _get_course_info,
            name="get_course_info",
            description="Get information about a course, including its type and location."
        )
    ],
)
To help you understand better: • The
_get_course_info
function takes a
CourseInfo
object as input, which includes details like the course type and where it's offered. • We register the tool right when we initialize the agent, using the
function_tool
function. • And the tool’s description clearly states it’s for getting info about a course, including its type and location. If you have any questions or would like me to provide more examples, please don’t hesitate to ask. I’m here to help!
f
in my current code i have function tools defined with decorator @function_tool how will this new initialization work?
t
If your current code defines function tools using the
@function_tool
decorator, you can absolutely continue using those same tools when initializing your agent. The decorator does the handy job of automatically registering your function as a tool, so all you need to do is include the decorated function in the
tools
list during agent setup. Here’s a little walkthrough to make it clearer: Example with Decorator:
Copy code
from livekit.agents import function_tool, MyAgent

# Define the tool using the decorator
@function_tool(name="get_course_info", description="Get information about a course.")
async def _get_course_info(course: str, location: str) -> str:
    return f"The course {course} is available {location}."

# Initialize the agent with the decorated tool
agent = MyAgent(
    instructions="You are a helpful assistant.",
    tools=[_get_course_info],
)
In this example: • The
@function_tool
decorator takes care of registering the function as a tool with the name and description you provide. • When initializing your agent, just include the decorated function (like
_get_course_info
) in the
tools
list. This way, you keep everything compatible with your current code and smoothly initialize your agent with the tools you’ve already created. If you have any questions or want me to walk through anything else, just let me know—I’m here to help!