Files
SIA/sia/base_agent.py
2024-11-01 18:31:12 +01:00

55 lines
1.8 KiB
Python

from abc import ABC, abstractmethod
from typing import List
import xml.etree.ElementTree as ET
from .llm_engine import LlmEngine
from .system_metrics import SystemMetrics
from .working_memory import WorkingMemory
from .xml_validator import XMLValidator
from .response_parser import ResponseParser
class BaseAgent(ABC):
"""
Abstract base class for SIA agents.
Provides core functionality for maintaining working memory, system metrics,
and coordinating components for LLM inference.
"""
def __init__(self,
action_schema: str,
working_memory: WorkingMemory,
system_metrics: SystemMetrics,
llm: LlmEngine,
validator: XMLValidator,
parser: ResponseParser):
"""
Initialize agent with required components.
"""
# Initialize components
self._working_memory = working_memory
self._metrics = system_metrics
self._llm = llm
self._validator = validator
self._parser = parser
self._action_schema = action_schema
def __del__(self):
"""Clean up resources on deletion."""
if hasattr(self, '_metrics'):
self._metrics.stop()
def _compile_context(self) -> str:
"""
Compile the current context for LLM inference.
Includes system metrics and working memory entries.
Returns:
str: Complete context as XML string
"""
memory_context = self._working_memory.generate_context()
context_size = len(memory_context) / 100
context = self._metrics.generate_context(context_size)
for entry in memory_context:
context.append(entry)
return ET.tostring(context, encoding="unicode")