88 lines
3.0 KiB
Python
88 lines
3.0 KiB
Python
from abc import ABC, abstractmethod
|
|
from typing import List
|
|
import xml.etree.ElementTree as ET
|
|
import time
|
|
|
|
from .llm_engine import LlmEngine
|
|
from .response_parser import ResponseParser
|
|
from .system_metrics import SystemMetrics
|
|
from .util import pretty_print_element
|
|
from .working_memory import WorkingMemory
|
|
from .xml_validator import XMLValidator
|
|
|
|
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,
|
|
system_prompt: str,
|
|
action_schema: str,
|
|
working_memory: WorkingMemory,
|
|
metrics: SystemMetrics,
|
|
llm: LlmEngine,
|
|
validator: XMLValidator,
|
|
parser: ResponseParser
|
|
):
|
|
"""
|
|
Initialize agent with required components.
|
|
"""
|
|
self._system_prompt = system_prompt
|
|
self._action_schema = action_schema
|
|
self._working_memory = working_memory
|
|
self._metrics = metrics
|
|
self._llm = llm
|
|
self._validator = validator
|
|
self._parser = parser
|
|
|
|
def __del__(self):
|
|
"""Clean up resources on deletion."""
|
|
if hasattr(self, '_metrics'):
|
|
self._metrics.stop()
|
|
|
|
@property
|
|
def system_prompt(self) -> str:
|
|
"""Get the system prompt."""
|
|
return f"{self._system_prompt}\n{self._action_schema}"
|
|
|
|
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()
|
|
metrics_data = self._metrics.get_metrics()
|
|
|
|
# Create context element
|
|
context = ET.Element("context")
|
|
context.set("time", metrics_data["timestamp"])
|
|
context.set("cpu", str(metrics_data["cpu"]))
|
|
context.set("gpu", str(metrics_data["gpu"]))
|
|
context.set("memory_used", str(metrics_data["memory_used"]))
|
|
context.set("memory_total", str(metrics_data["memory_total"]))
|
|
context.set("disk_used", str(metrics_data["disk_used"]))
|
|
context.set("disk_total", str(metrics_data["disk_total"]))
|
|
context.set("stdin", str(self._parser.io_buffer.buffer_length()))
|
|
context.set("context", "100")
|
|
|
|
for entry in memory_context:
|
|
context.append(entry)
|
|
|
|
context_str = pretty_print_element(context)
|
|
|
|
# Calculate token usage percentage
|
|
token_count = self._llm.token_count(self.system_prompt, context_str)
|
|
token_limit = self._llm.token_limit()
|
|
context_usage = (float(token_count) / float(token_limit)) * 100.0
|
|
|
|
# Update context usage metric
|
|
context.set("context", str(round(context_usage, 2)))
|
|
|
|
return pretty_print_element(context) |