Action
What are Actions?
Actions form the critical bridge between an agent’s internal decision-making processes and the external world. They allow the system to effect change, adapt to varying circumstances, and achieve specific objectives by responding to observed conditions. This interplay between action selection and environmental feedback is fundamental in developing adaptive, intelligent systems capable of learning from experience and improving their performance over time.
Using Actions
There are 2 main ways to use actions:
Directly through the agent - actions are linked to a specific agent
Standalone - actions can be reused by multiple agents.
The below examples assume the pre-requisite setup for a SwiftAgent
from swiftagent import SwiftAgent
agent = SwiftAgent()Agent-Based Actions
You can define agent-based actions using the SwiftAgent.action decorator on a function. Different example cases are shown below.
@agent.action(description="action to get weather")
async def get_weather(location: str, unit: str = "celsius"):
return f"The weather in {location} is sunny and 22°{unit[0].upper()}"@agent.action()
async def get_weather(location: str, unit: str = "celsius"):
"""
action to get weather
"""
return f"The weather in {location} is sunny and 22°{unit[0].upper()}"@agent.action(
name="get_weather",
description="Get the current weather for a specific location",
params={
"location": "The city or location to get weather for",
"unit": "Temperature unit (celsius/fahrenheit)"
},
strict=False
)
async def get_weather(location: str, unit: str = "celsius"):
return f"The weather in {location} is sunny and 22°{unit[0].upper()}"@agent.action(
name="get_weather",
description="Get the current weather for a specific location",
params={
"location": "The city or location to get weather for",
"unit": "Temperature unit (celsius/fahrenheit)"
},
strict=False
)
async def get_weather(location: str, unit: str = "celsius"):
return f"The weather in {location} is sunny and 22°{unit[0].upper()}"Standalone Actions
Standalone actions are defined by the agent decorator, in the exact same way as agent-based actions are defined. The only additional step is that you need to call SwiftAgent.add_action on the standalone action to add it to an agent.
For example,
from swiftagent.actions import action
# define action
@action()
async def get_weather(location: str, unit: str = "celsius"):
"""
action to get weather
"""
return f"The weather in {location} is sunny and 22°{unit[0].upper()}"
# add action
agent.add_action(get_weather)Action Attributes
Name
name
Optional[str]
Name of the action. Defaults to the name of the function.
Description
description
Optional[str]
Description of the function. Defaults to the docstring of the function if present. If no docstring, defaults to an empty string.
Parameters
params
Optional[dict]
Optional descriptions of the parameters.
Strict Mode
strict
bool
Whether or not all the parameters are required.
Last updated