Added base agent
This commit is contained in:
89
sia/base_agent.py
Normal file
89
sia/base_agent.py
Normal file
@@ -0,0 +1,89 @@
|
||||
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")
|
||||
41
sia/io_buffer.py
Normal file
41
sia/io_buffer.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
class IOBuffer(ABC):
|
||||
"""
|
||||
Abstract base class defining the interface for input/output operations.
|
||||
|
||||
This interface allows for different implementations of IO handling,
|
||||
such as direct system IO or buffered web interface communication.
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def read(self) -> str:
|
||||
"""
|
||||
Read and return available input.
|
||||
|
||||
Should clear the input buffer after reading.
|
||||
|
||||
Returns:
|
||||
str: Content from input buffer, or empty string if no input available
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def write(self, content: str) -> None:
|
||||
"""
|
||||
Write content to output.
|
||||
|
||||
Args:
|
||||
content: String content to write
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def buffer_length(self) -> int:
|
||||
"""
|
||||
Get the current length of buffered input.
|
||||
|
||||
Returns:
|
||||
int: Number of characters in the input buffer
|
||||
"""
|
||||
pass
|
||||
@@ -1,6 +1,8 @@
|
||||
import sys
|
||||
|
||||
class StandardIOBuffer:
|
||||
from .io_buffer import IOBuffer
|
||||
|
||||
class StandardIOBuffer(IOBuffer):
|
||||
"""
|
||||
IOBuffer implementation that uses system standard input/output.
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
class WebIOBuffer:
|
||||
from .io_buffer import IOBuffer
|
||||
|
||||
class WebIOBuffer(IOBuffer):
|
||||
"""
|
||||
IOBuffer implementation for web interface communication.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user