Approve context

This commit is contained in:
2024-11-02 19:17:27 +01:00
parent 96d14fc46d
commit 2a15708145
9 changed files with 296 additions and 149 deletions

View File

@@ -54,7 +54,6 @@ class WebAgent(BaseAgent):
self._command_result: Optional[CommandResult] = None
self.context = self._compile_context()
@property
def current_state(self) -> WebAgentState:
"""Get the current state of the agent."""
@@ -93,43 +92,68 @@ class WebAgent(BaseAgent):
def _notify_response_change(self) -> None:
"""Notify all handlers of response change."""
for handler in self._response_change_handlers:
handler(self._response)
handler(self.response)
def _set_response(self, response: str) -> None:
"""Set response and notify handlers."""
self.response = response
self._validation_error = self._validator.validate(response)
for handler in self._response_change_handlers:
try:
handler(response)
except Exception as e:
print(f"Error in response handler: {e}")
def _set_state(self, new_state: WebAgentState) -> None:
"""
Set the current response and notify handlers.
Set the current state and notify handlers.
Args:
response: New response
new_state: New state to set
"""
self._response = response
self._validation_error = self._validator.validate(self._response)
self._notify_response_change()
def approve_context(self) -> None:
if self._current_state != WebAgentState.CONTEXT_APPROVAL:
raise Exception("Not in CONTEXT_APPROVAL state")
self._set_response("")
self._current_state = WebAgentState.INFERENCE
self._notify_state_change()
response_token_iter = self._llm.infer(self._system_prompt, self.context)
for token in response_token_iter:
self._set_response(self._response + token)
self._current_state = WebAgentState.RESPONSE_APPROVAL
self._current_state = new_state
self._notify_state_change()
async def approve_context(self) -> None:
if self._current_state != WebAgentState.CONTEXT_APPROVAL:
error_msg = f"Not in CONTEXT_APPROVAL state (current: {self._current_state})"
raise Exception(error_msg)
self._set_state(WebAgentState.INFERENCE)
self._set_response("")
response_token_iter = self._llm.infer(self._system_prompt, self.context)
response = ""
try:
for token in response_token_iter:
response += token
await self._set_response(response)
print(f"{token}")
except Exception as e:
print(f"Error during inference: {e}")
self._set_state(WebAgentState.CONTEXT_APPROVAL)
return
await self._set_response(response)
self._set_state(WebAgentState.RESPONSE_APPROVAL)
async def _set_response(self, response: str) -> None:
"""Set response and notify handlers asynchronously."""
self.response = response
self._validation_error = self._validator.validate(response)
for handler in self._response_change_handlers:
try:
await handler(response)
except Exception as e:
print(f"Error in response handler: {e}")
def approve_response(self) -> None:
if self._current_state != WebAgentState.RESPONSE_APPROVAL:
raise Exception("Not in RESPONSE_APPROVAL state")
self._current_state = WebAgentState.UPDATE
self._notify_state_change()
self._set_state(WebAgentState.UPDATE)
parse_result = self._parser.parse(self._response)
parse_result = self._parser.parse(self.response)
if isinstance(parse_result, Command):
result = parse_result.execute(self._working_memory)
self._command_result = result
@@ -137,7 +161,6 @@ class WebAgent(BaseAgent):
self._working_memory.add_entry(parse_result)
self._working_memory.update()
self._context = self._compile_context()
self.context = self._compile_context()
self._current_state = WebAgentState.CONTEXT_APPROVAL
self._notify_state_change()
self._set_state(WebAgentState.CONTEXT_APPROVAL)