Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prioritize Bash if LLM responses both Bash and Conclusion #716

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions agent/base_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def _filter_code(self, raw_code_block: str) -> str:
return filtered_code_block

def _format_bash_execution_result(self, process: sp.CompletedProcess) -> str:
"""Formats a prompt based on bash execution result."""
stdout = self.llm.truncate_prompt(process.stdout)
# TODO(dongge) Share input limit evenly if both stdout and stderr overlong.
stderr = self.llm.truncate_prompt(process.stderr, stdout)
Expand All @@ -76,18 +77,16 @@ def _format_bash_execution_result(self, process: sp.CompletedProcess) -> str:
f'<stdout>\n{stdout}\n</stdout>\n'
f'<stderr>\n{stderr}\n</stderr>\n')

def _container_handle_bash_command(self, cur_round: int, response: str,
def _container_handle_bash_command(self, command: str,
tool: BaseTool) -> Prompt:
"""Handles the command from LLM with container tool."""
command = self._parse_tag(response, 'bash')
if command:
prompt_text = self._format_bash_execution_result(tool.execute(command))
else:
logger.warning('ROUND %02d No BASH command from LLM response: %s',
cur_round, response)
prompt_text = ('No bash command received, Please follow the '
'interaction protocols:\n'
f'{tool.tutorial()}')
"""Handles the command from LLM with container |tool|."""
prompt_text = self._format_bash_execution_result(tool.execute(command))
return DefaultTemplateBuilder(self.llm, None, initial=prompt_text).build([])

def _container_handle_invalid_tool_usage(self, tool: BaseTool) -> Prompt:
"""Formats a prompt to re-teach LLM how to use the |tool|."""
prompt_text = (f'No valid instruction received, Please follow the '
f'interaction protocols:\n{tool.tutorial()}')
return DefaultTemplateBuilder(self.llm, None, initial=prompt_text).build([])

def _sleep_random_duration(self, min_sec: int = 1, max_sec: int = 60) -> None:
Expand Down
10 changes: 8 additions & 2 deletions agent/prototyper.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,17 @@ def _container_handle_conclusion(
def _container_tool_reaction(self, cur_round: int, response: str,
build_result: BuildResult) -> Optional[Prompt]:
"""Validates LLM conclusion or executes its command."""
# Prioritize Bash instructions.
if command := self._parse_tag(response, 'bash'):
return self._container_handle_bash_command(command, self.inspect_tool)

if self._parse_tag(response, 'conclusion'):
return self._container_handle_conclusion(cur_round, response,
build_result)
return self._container_handle_bash_command(cur_round, response,
self.inspect_tool)
# Other responses are invalid.
logger.warning('ROUND %02d Invalid response from LLM: %s', cur_round,
response)
return self._container_handle_invalid_tool_usage(self.inspect_tool)

def execute(self, result_history: list[Result]) -> BuildResult:
"""Executes the agent based on previous result."""
Expand Down