Agent
The Agent
base class serves as the foundation for creating custom agents. By inheriting from this class, you can define system prompts and register function tools.
Key Features:​
- Custom instruction configuration
- State management lifecycle hooks
- Function tool registration (internal and external)
Example Implementation:​
You can register function tools in two ways:
- External Tools: Defined as standalone functions outside the agent class and registered via the
tools
argument in the agent's constructor. - Internal Tools: Defined as methods inside the agent class and decorated with
@function_tool
.
from videosdk.agents import Agent, AgentState
# --- External Function Tool ---
@function_tool
def get_weather(latitude: str, longitude: str):
"""Called when the user asks about the weather. This function will return the weather for
the given location. When given a location, please estimate the latitude and longitude of the
location and do not ask the user for them.
Args:
latitude: The latitude of the location
longitude: The longitude of the location
"""
print(f"Getting weather for {latitude}, {longitude}")
url = f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}¤t=temperature_2m"
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
if response.status == 200:
data = await response.json()
return {
"temperature": data["current"]["temperature_2m"],
"temperature_unit": "Celsius",
}
else:
raise Exception(
f"Failed to get weather data, status code: {response.status}"
)
class VoiceAgent(Agent):
def __init__(self):
super().__init__(
instructions="You are a helpful voice assistant that can answer questions and help with tasks.",
tools=[get_weather] # Register external tools here
)
# --- Internal Function Tool ---
@function_tool
async def get_horoscope(self, sign: str) -> dict:
"""Get today's horoscope for a given zodiac sign.
Args:
sign: The zodiac sign (e.g., Aries, Taurus, Gemini, etc.)
Returns:
dict: Contains the sign and horoscope text
"""
horoscopes = {
"Aries": "Today is your lucky day!",
"Taurus": "Focus on your goals today.",
"Gemini": "Communication will be important today.",
}
return {
"sign": sign,
"horoscope": horoscopes.get(sign, "The stars are aligned for you today!"),
}
Got a Question? Ask us on discord