Fixed context usage calculation
This commit is contained in:
@@ -18,27 +18,36 @@ class BaseAgent(ABC):
|
||||
and coordinating components for LLM inference.
|
||||
"""
|
||||
|
||||
def __init__(self,
|
||||
action_schema: str,
|
||||
working_memory: WorkingMemory,
|
||||
system_metrics: SystemMetrics,
|
||||
llm: LlmEngine,
|
||||
validator: XMLValidator,
|
||||
parser: ResponseParser):
|
||||
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 = system_metrics
|
||||
self._metrics = 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()
|
||||
|
||||
@property
|
||||
def system_prompt(self) -> str:
|
||||
"""Get the system prompt."""
|
||||
return f"{self._system_prompt}\n{self._action_schema}"
|
||||
|
||||
def _compile_context(self) -> str:
|
||||
"""
|
||||
@@ -48,14 +57,10 @@ class BaseAgent(ABC):
|
||||
Returns:
|
||||
str: Complete context as XML string
|
||||
"""
|
||||
# Get memory context and calculate size
|
||||
memory_context = self._working_memory.generate_context()
|
||||
context_size = len(memory_context) / 100
|
||||
|
||||
# Get system metrics
|
||||
metrics_data = self._metrics.get_metrics()
|
||||
|
||||
# Create context element with metrics
|
||||
# Create context element
|
||||
context = ET.Element("context")
|
||||
context.set("time", metrics_data["timestamp"])
|
||||
context.set("cpu", str(metrics_data["cpu"]))
|
||||
@@ -64,11 +69,20 @@ class BaseAgent(ABC):
|
||||
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("context", str(round(context_size * 100)))
|
||||
context.set("stdin", str(self._parser.io_buffer.buffer_length()))
|
||||
|
||||
# Add memory entries
|
||||
context.set("context", "100")
|
||||
|
||||
for entry in memory_context:
|
||||
context.append(entry)
|
||||
|
||||
return pretty_print_element(context)
|
||||
|
||||
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)
|
||||
Reference in New Issue
Block a user