Agent API
Agent API
The Agent
class is the central orchestrator that combines skills, sensors, and perceptors.
Creating an Agent
from composabl import Agent
# Create a new agent
agent = Agent()
# Create with ID
agent = Agent(id="temperature-controller-v1")
Agent Methods
Adding Components
# Add single sensor
agent.add_sensor(sensor)
# Add multiple sensors
agent.add_sensors([sensor1, sensor2, sensor3])
# Add skill
agent.add_skill(skill)
# Add multiple skills
agent.add_skills([skill1, skill2])
# Add perceptor
agent.add_perceptor(perceptor)
# Add scenario
agent.add_scenario(scenario)
Serialization
# Export to file
agent.export("path/to/agent.json")
# Load from file
agent = Agent.load("path/to/agent.json")
Visualization
# Display agent structure
agent.draw()
# Get structure as string
structure = agent.get_structure()
Complete Agent Example
from composabl import Agent, Sensor, Skill, Scenario, Perceptor
from composabl import MaintainGoal
# Create agent
agent = Agent(id="reactor-controller")
# Add sensors
agent.add_sensors([
Sensor("temp", "Temperature in Celsius", lambda obs: obs["temperature"]),
Sensor("pressure", "Pressure in bar", lambda obs: obs["pressure_reading"]),
Sensor("flow", "Flow rate L/min", lambda obs: obs["flow_rate"])
])
# Add perceptor
class RateCalculator(PerceptorImpl):
def __init__(self):
self.last_temp = None
async def compute(self, obs_spec, obs):
rate = 0
if self.last_temp is not None:
rate = obs["temperature"] - self.last_temp
self.last_temp = obs["temperature"]
return {"temp_rate": rate}
def filtered_sensor_space(self, obs):
return ["temperature"]
agent.add_perceptor(Perceptor("rate-calc", RateCalculator))
# Add skills
temp_skill = Skill("maintain-temp",
MaintainGoal("temp", "Keep temperature stable",
target=75.0, stop_distance=2.0))
agent.add_skill(temp_skill)
# Add scenarios
agent.add_scenario(Scenario({
"temperature": {"min": 70, "max": 80},
"pressure": {"min": 5, "max": 7}
}))
# Visualize
agent.draw()
Last updated