You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
reflexion-human-eval/webshop_runs/env_history.py

52 lines
1.7 KiB
Python

from typing import List, Dict
class EnvironmentHistory:
def __init__(self, base_query: str, start_info, memory: List[str], history: List[Dict[str, str]] = []) -> None:
self._cur_query: str = f'{_get_base_query(base_query, start_info, memory)}'
self._history: List[Dict[str, str]] = history
self._last_action: str = ''
self._is_exhausted: bool = False
def add(self, label: str, value: str) -> None:
assert label in ['action', 'observation', 'human_edit']
self._history += [{
'label': label,
'value': value,
}]
if label == 'action':
if value == self._last_action:
self._is_exhausted = True
else:
self._last_action = value
def check_is_exhausted(self) -> bool:
return self._is_exhausted
def reset(self) -> None:
self._history = []
def __str__(self) -> str:
s: str = self._cur_query + '\n'
for i, item in enumerate(self._history):
if item['label'] == 'action':
s += f'> {item["value"]}'
elif item['label'] == 'observation':
s += item['value']
elif item['label'] == 'human_edit':
s += f'[human edit]: {item["value"]}'
if i != len(self._history) - 1:
s += '\n'
return s
def _get_base_query(base_query: str, start_info: str, memory: List[str]) -> str:
query = base_query
# add memory if it exists
if len(memory) > 0:
query += '\nYour memory for the task below:'
for i, m in enumerate(memory):
query += f'\nTrial {i}:\n{m.strip()}'
query += f"\nHere is the task:\n{start_info}"
return query