perf: improved speed of BDI
By efficiently checking when the next work has to be done, we can increase performance not having to "busy loop". Time from transcription -> message to LLM agent is now down to sub 1 millisecond. ref: N25B-316
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
from .bdi_core_agent.bdi_core_agent import BDICoreAgent as BDICoreAgent
|
||||
from .belief_collector_agent.belief_collector_agent import (
|
||||
from .belief_collector_agent import (
|
||||
BDIBeliefCollectorAgent as BDIBeliefCollectorAgent,
|
||||
)
|
||||
from .text_belief_extractor_agent.text_belief_extractor_agent import (
|
||||
from .text_belief_extractor_agent import (
|
||||
TextBeliefExtractorAgent as TextBeliefExtractorAgent,
|
||||
)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import asyncio
|
||||
import copy
|
||||
import json
|
||||
import time
|
||||
from collections.abc import Iterable
|
||||
|
||||
import agentspeak
|
||||
@@ -22,6 +23,7 @@ class BDICoreAgent(BaseAgent):
|
||||
self.env = agentspeak.runtime.Environment()
|
||||
# Deep copy because we don't actually want to modify the standard actions globally
|
||||
self.actions = copy.deepcopy(agentspeak.stdlib.actions)
|
||||
self._wake_bdi_loop = asyncio.Event()
|
||||
|
||||
async def setup(self) -> None:
|
||||
self.logger.debug("Setup started.")
|
||||
@@ -32,6 +34,7 @@ class BDICoreAgent(BaseAgent):
|
||||
|
||||
# Start the BDI cycle loop
|
||||
await self.add_behavior(self._bdi_loop())
|
||||
self._wake_bdi_loop.set()
|
||||
self.logger.debug("Setup complete.")
|
||||
|
||||
async def _load_asl(self):
|
||||
@@ -43,10 +46,28 @@ class BDICoreAgent(BaseAgent):
|
||||
self.bdi_agent = agentspeak.runtime.Agent(self.env, self.name)
|
||||
|
||||
async def _bdi_loop(self):
|
||||
"""Runs the AgentSpeak BDI loop."""
|
||||
"""
|
||||
Runs the AgentSpeak BDI loop. Efficiently checks for when the next expected work will be.
|
||||
"""
|
||||
while self._running:
|
||||
self.bdi_agent.step()
|
||||
await asyncio.sleep(0.01)
|
||||
await (
|
||||
self._wake_bdi_loop.wait()
|
||||
) # gets set whenever there's an update to the belief base
|
||||
|
||||
# Agent knows when it's expected to have to do its next thing
|
||||
maybe_more_work = True
|
||||
while maybe_more_work:
|
||||
maybe_more_work = False
|
||||
if self.bdi_agent.step():
|
||||
maybe_more_work = True
|
||||
|
||||
if not maybe_more_work:
|
||||
deadline = self.bdi_agent.shortest_deadline()
|
||||
if deadline:
|
||||
await asyncio.sleep(deadline - time.time())
|
||||
maybe_more_work = True
|
||||
else:
|
||||
self._wake_bdi_loop.clear()
|
||||
|
||||
async def handle_message(self, msg: InternalMessage):
|
||||
"""
|
||||
@@ -93,6 +114,9 @@ class BDICoreAgent(BaseAgent):
|
||||
term,
|
||||
agentspeak.runtime.Intention(),
|
||||
)
|
||||
|
||||
self._wake_bdi_loop.set()
|
||||
|
||||
self.logger.debug(f"Added belief {self.format_belief_string(name, args)}")
|
||||
|
||||
def _remove_belief(self, name: str, args: Iterable[str]):
|
||||
@@ -111,6 +135,7 @@ class BDICoreAgent(BaseAgent):
|
||||
|
||||
if result:
|
||||
self.logger.debug(f"Removed belief {self.format_belief_string(name, args)}")
|
||||
self._wake_bdi_loop.set()
|
||||
else:
|
||||
self.logger.debug("Failed to remove belief (it was not in the belief base).")
|
||||
|
||||
@@ -135,6 +160,8 @@ class BDICoreAgent(BaseAgent):
|
||||
)
|
||||
removed_count += 1
|
||||
|
||||
self._wake_bdi_loop.set()
|
||||
|
||||
self.logger.debug(f"Removed {removed_count} beliefs.")
|
||||
|
||||
def _add_custom_actions(self) -> None:
|
||||
|
||||
Reference in New Issue
Block a user