Source code for council.contexts._context_base
from ._agent_context_store import AgentContextStore
from ._budget import Budget
from ._chat_history import ChatHistory
from ._execution_context import ExecutionContext
from ._execution_log_entry import ExecutionLogEntry
from ._monitored import Monitored
from ._monitored_budget import MonitoredBudget
[docs]
class ContextBase:
"""
base class for context.
It provides a secured and monitored access to the data generated during the execution.
The actual data are stored in the underlying `class`:AgentContextStore`.
"""
def __init__(self, store: AgentContextStore, execution_context: ExecutionContext, budget: Budget):
self._store = store
self._execution_context = execution_context
self._budget = MonitoredBudget(execution_context.entry, budget)
@property
def iteration_count(self) -> int:
"""
the number of iteration for this execution
"""
return len(self._store.iterations)
@property
def log_entry(self) -> ExecutionLogEntry:
"""
the log entry
"""
return self._execution_context.entry
@property
def budget(self) -> Budget:
"""
the budget
"""
return self._budget
@property
def chat_history(self) -> ChatHistory:
"""
the chat history
"""
return self._store.chat_history
def __enter__(self):
self.log_entry.__enter__()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.log_entry.__exit__(exc_type, exc_val, exc_tb)
[docs]
def new_log_entry(self, monitored: Monitored) -> ExecutionLogEntry:
"""
creates a new log entry from this context
"""
return self._execution_context.new_for(monitored).entry