|
| 1 | +import copy |
| 2 | +import re |
| 3 | + |
| 4 | +from balrog.agents.base import BaseAgent |
| 5 | + |
| 6 | + |
| 7 | +class NaiveAgent(BaseAgent): |
| 8 | + """An agent that generates actions based on observations without complex reasoning.""" |
| 9 | + |
| 10 | + def __init__(self, client_factory, prompt_builder): |
| 11 | + """Initialize the NaiveAgent with a client and prompt builder.""" |
| 12 | + super().__init__(client_factory, prompt_builder) |
| 13 | + self.client = client_factory() |
| 14 | + |
| 15 | + def act(self, obs, prev_action=None): |
| 16 | + """Generate the next action based on the observation and previous action. |
| 17 | +
|
| 18 | + Args: |
| 19 | + obs (dict): The current observation in the environment. |
| 20 | + prev_action (str, optional): The previous action taken. |
| 21 | +
|
| 22 | + Returns: |
| 23 | + str: The selected action from the LLM response. |
| 24 | + """ |
| 25 | + if prev_action: |
| 26 | + self.prompt_builder.update_action(prev_action) |
| 27 | + |
| 28 | + self.prompt_builder.update_observation(obs) |
| 29 | + |
| 30 | + messages = self.prompt_builder.get_prompt() |
| 31 | + |
| 32 | + naive_instruction = """ |
| 33 | +It's your turn. What coordinate would you like to output? |
| 34 | + """.strip() |
| 35 | + |
| 36 | + if messages and messages[-1].role == "user": |
| 37 | + messages[-1].content += "\n\n" + naive_instruction |
| 38 | + |
| 39 | + response = self.client.generate(messages) |
| 40 | + |
| 41 | + final_answer = self._extract_final_answer(response) |
| 42 | + |
| 43 | + return final_answer |
| 44 | + |
| 45 | + def _extract_final_answer(self, answer): |
| 46 | + """Sanitize the final answer, keeping only alphabetic characters. |
| 47 | +
|
| 48 | + Args: |
| 49 | + answer (LLMResponse): The response from the LLM. |
| 50 | +
|
| 51 | + Returns: |
| 52 | + LLMResponse: The sanitized response. |
| 53 | + """ |
| 54 | + |
| 55 | + def filter_letters(input_string): |
| 56 | + return re.sub(r"[^a-zA-Z0-9\s:]", "", input_string) |
| 57 | + |
| 58 | + final_answer = copy.deepcopy(answer) |
| 59 | + final_answer = final_answer._replace(completion=filter_letters(final_answer.completion)) |
| 60 | + |
| 61 | + return final_answer |
0 commit comments