89 lines
3.0 KiB
Python
89 lines
3.0 KiB
Python
from abc import ABC, abstractmethod
|
|
from typing import Iterator, Callable, Optional, List
|
|
import xml.etree.ElementTree as ET
|
|
|
|
from .command import Command
|
|
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
|
|
from .parse_error_entry import ParseErrorEntry
|
|
from .io_buffer import IOBuffer
|
|
|
|
class BaseAgent(ABC):
|
|
"""
|
|
Abstract base class for SIA agents.
|
|
|
|
Provides core functionality for maintaining working memory, system metrics,
|
|
and coordinating components for LLM inference.
|
|
|
|
Private Attributes:
|
|
_working_memory: Collection of current entries
|
|
_metrics: System resource monitoring
|
|
_llm: LLM inference engine
|
|
_parser: XML response parser
|
|
_validator: XML response validator
|
|
_io_buffer: Input/output operations buffer
|
|
_system_prompt: System prompt template
|
|
_action_schema: XML schema for action validation
|
|
"""
|
|
|
|
def __init__(self,
|
|
model_path: str,
|
|
system_prompt: str,
|
|
action_schema: str,
|
|
io_buffer: IOBuffer):
|
|
"""
|
|
Initialize agent with required components.
|
|
|
|
Args:
|
|
model_path: Path to LLM model
|
|
system_prompt: System prompt template
|
|
action_schema: XML schema for actions
|
|
io_buffer: IO buffer implementation to use
|
|
"""
|
|
# Initialize components
|
|
self._working_memory = WorkingMemory()
|
|
self._metrics = SystemMetrics()
|
|
self._llm = LlmEngine(model_path)
|
|
self._validator = XMLValidator(action_schema)
|
|
self._io_buffer = io_buffer
|
|
self._parser = ResponseParser(io_buffer)
|
|
|
|
# Store prompts
|
|
self._system_prompt = system_prompt
|
|
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
|
|
"""
|
|
# Get usage details to include in context
|
|
context_size = 0 # TODO: Implement context size tracking
|
|
|
|
# Get current system metrics
|
|
metrics_context = self._metrics.generate_context(context_size)
|
|
|
|
# Get working memory entries
|
|
memory_context = self._working_memory.generate_context()
|
|
|
|
# Create root element
|
|
root = ET.Element("state")
|
|
|
|
# Add metrics and memory entries
|
|
root.append(metrics_context)
|
|
for entry in memory_context:
|
|
root.append(entry)
|
|
|
|
# Convert to string with basic formatting
|
|
return ET.tostring(root, encoding="unicode") |