mirror of
https://github.com/All-Hands-AI/OpenHands.git
synced 2026-04-29 03:00:45 -04:00
Possibility to disable default tools (#7951)
This commit is contained in:
committed by
GitHub
parent
ae43744052
commit
5b3270be2d
@@ -1,7 +1,19 @@
|
||||
import os
|
||||
from collections import deque
|
||||
|
||||
from litellm import ChatCompletionToolParam
|
||||
|
||||
import openhands.agenthub.codeact_agent.function_calling as codeact_function_calling
|
||||
from openhands.agenthub.codeact_agent.tools.bash import create_cmd_run_tool
|
||||
from openhands.agenthub.codeact_agent.tools.browser import BrowserTool
|
||||
from openhands.agenthub.codeact_agent.tools.finish import FinishTool
|
||||
from openhands.agenthub.codeact_agent.tools.ipython import IPythonTool
|
||||
from openhands.agenthub.codeact_agent.tools.llm_based_edit import LLMBasedFileEditTool
|
||||
from openhands.agenthub.codeact_agent.tools.str_replace_editor import (
|
||||
create_str_replace_editor_tool,
|
||||
)
|
||||
from openhands.agenthub.codeact_agent.tools.think import ThinkTool
|
||||
from openhands.agenthub.codeact_agent.tools.web_read import WebReadTool
|
||||
from openhands.controller.agent import Agent
|
||||
from openhands.controller.state.state import State
|
||||
from openhands.core.config import AgentConfig
|
||||
@@ -67,15 +79,7 @@ class CodeActAgent(Agent):
|
||||
super().__init__(llm, config)
|
||||
self.pending_actions: deque[Action] = deque()
|
||||
self.reset()
|
||||
|
||||
built_in_tools = codeact_function_calling.get_tools(
|
||||
enable_browsing=self.config.enable_browsing,
|
||||
enable_jupyter=self.config.enable_jupyter,
|
||||
enable_llm_editor=self.config.enable_llm_editor,
|
||||
llm=self.llm,
|
||||
)
|
||||
|
||||
self.tools = built_in_tools
|
||||
self.tools = self._get_tools()
|
||||
|
||||
self.prompt_manager = PromptManager(
|
||||
prompt_dir=os.path.join(os.path.dirname(__file__), 'prompts'),
|
||||
@@ -89,6 +93,40 @@ class CodeActAgent(Agent):
|
||||
|
||||
self.response_to_actions_fn = codeact_function_calling.response_to_actions
|
||||
|
||||
def _get_tools(self) -> list[ChatCompletionToolParam]:
|
||||
SIMPLIFIED_TOOL_DESCRIPTION_LLM_SUBSTRS = ['gpt-', 'o3', 'o1']
|
||||
|
||||
use_simplified_tool_desc = False
|
||||
if self.llm is not None:
|
||||
use_simplified_tool_desc = any(
|
||||
model_substr in self.llm.config.model
|
||||
for model_substr in SIMPLIFIED_TOOL_DESCRIPTION_LLM_SUBSTRS
|
||||
)
|
||||
|
||||
tools = []
|
||||
if self.config.enable_cmd:
|
||||
tools.append(
|
||||
create_cmd_run_tool(use_simplified_description=use_simplified_tool_desc)
|
||||
)
|
||||
if self.config.enable_think:
|
||||
tools.append(ThinkTool)
|
||||
if self.config.enable_finish:
|
||||
tools.append(FinishTool)
|
||||
if self.config.enable_browsing:
|
||||
tools.append(WebReadTool)
|
||||
tools.append(BrowserTool)
|
||||
if self.config.enable_jupyter:
|
||||
tools.append(IPythonTool)
|
||||
if self.config.enable_llm_editor:
|
||||
tools.append(LLMBasedFileEditTool)
|
||||
elif self.config.enable_editor:
|
||||
tools.append(
|
||||
create_str_replace_editor_tool(
|
||||
use_simplified_description=use_simplified_tool_desc
|
||||
)
|
||||
)
|
||||
return tools
|
||||
|
||||
def reset(self) -> None:
|
||||
"""Resets the CodeAct Agent."""
|
||||
super().reset()
|
||||
|
||||
@@ -6,7 +6,6 @@ This is similar to the functionality of `CodeActResponseParser`.
|
||||
import json
|
||||
|
||||
from litellm import (
|
||||
ChatCompletionToolParam,
|
||||
ModelResponse,
|
||||
)
|
||||
|
||||
@@ -41,7 +40,6 @@ from openhands.events.action import (
|
||||
from openhands.events.action.mcp import McpAction
|
||||
from openhands.events.event import FileEditSource, FileReadSource
|
||||
from openhands.events.tool import ToolCallMetadata
|
||||
from openhands.llm import LLM
|
||||
from openhands.mcp import MCPClientTool
|
||||
|
||||
|
||||
@@ -237,39 +235,3 @@ def response_to_actions(response: ModelResponse) -> list[Action]:
|
||||
|
||||
assert len(actions) >= 1
|
||||
return actions
|
||||
|
||||
|
||||
def get_tools(
|
||||
enable_browsing: bool = False,
|
||||
enable_llm_editor: bool = False,
|
||||
enable_jupyter: bool = False,
|
||||
llm: LLM | None = None,
|
||||
) -> list[ChatCompletionToolParam]:
|
||||
SIMPLIFIED_TOOL_DESCRIPTION_LLM_SUBSTRS = ['gpt-', 'o3', 'o1']
|
||||
|
||||
use_simplified_tool_desc = False
|
||||
if llm is not None:
|
||||
use_simplified_tool_desc = any(
|
||||
model_substr in llm.config.model
|
||||
for model_substr in SIMPLIFIED_TOOL_DESCRIPTION_LLM_SUBSTRS
|
||||
)
|
||||
|
||||
tools = [
|
||||
create_cmd_run_tool(use_simplified_description=use_simplified_tool_desc),
|
||||
ThinkTool,
|
||||
FinishTool,
|
||||
]
|
||||
if enable_browsing:
|
||||
tools.append(WebReadTool)
|
||||
tools.append(BrowserTool)
|
||||
if enable_jupyter:
|
||||
tools.append(IPythonTool)
|
||||
if enable_llm_editor:
|
||||
tools.append(LLMBasedFileEditTool)
|
||||
else:
|
||||
tools.append(
|
||||
create_str_replace_editor_tool(
|
||||
use_simplified_description=use_simplified_tool_desc
|
||||
)
|
||||
)
|
||||
return tools
|
||||
|
||||
@@ -7,28 +7,30 @@ from openhands.core.logger import openhands_logger as logger
|
||||
|
||||
|
||||
class AgentConfig(BaseModel):
|
||||
"""Configuration for the agent.
|
||||
|
||||
Attributes:
|
||||
enable_browsing: Whether browsing delegate is enabled in the action space. Default is False. Only works with function calling.
|
||||
enable_llm_editor: Whether LLM editor is enabled in the action space. Default is False. Only works with function calling.
|
||||
enable_jupyter: Whether Jupyter is enabled in the action space. Default is False.
|
||||
llm_config: The name of the llm config to use. If specified, this will override global llm config.
|
||||
enable_prompt_extensions: Whether to use prompt extensions (e.g., microagents, inject runtime info). Default is True.
|
||||
disabled_microagents: A list of microagents to disable (by name, without .py extension, e.g. ["github", "lint"]). Default is None.
|
||||
condenser: Configuration for the memory condenser. Default is NoOpCondenserConfig.
|
||||
enable_history_truncation: Whether history should be truncated to continue the session when hitting LLM context length limit.
|
||||
enable_som_visual_browsing: Whether to enable SoM (Set of Marks) visual browsing. Default is False.
|
||||
"""
|
||||
|
||||
llm_config: str | None = Field(default=None)
|
||||
"""The name of the llm config to use. If specified, this will override global llm config."""
|
||||
enable_browsing: bool = Field(default=True)
|
||||
"""Whether to enable browsing tool"""
|
||||
enable_llm_editor: bool = Field(default=False)
|
||||
"""Whether to enable LLM editor tool"""
|
||||
enable_editor: bool = Field(default=True)
|
||||
"""Whether to enable the standard editor tool (str_replace_editor), only has an effect if enable_llm_editor is False."""
|
||||
enable_jupyter: bool = Field(default=True)
|
||||
"""Whether to enable Jupyter tool"""
|
||||
enable_cmd: bool = Field(default=True)
|
||||
"""Whether to enable bash tool"""
|
||||
enable_think: bool = Field(default=True)
|
||||
"""Whether to enable think tool"""
|
||||
enable_finish: bool = Field(default=True)
|
||||
"""Whether to enable finish tool"""
|
||||
enable_prompt_extensions: bool = Field(default=True)
|
||||
"""Whether to enable prompt extensions"""
|
||||
disabled_microagents: list[str] = Field(default_factory=list)
|
||||
"""A list of microagents to disable (by name, without .py extension, e.g. ["github", "lint"]). Default is None."""
|
||||
enable_history_truncation: bool = Field(default=True)
|
||||
"""Whether history should be truncated to continue the session when hitting LLM context length limit."""
|
||||
enable_som_visual_browsing: bool = Field(default=True)
|
||||
"""Whether to enable SoM (Set of Marks) visual browsing."""
|
||||
condenser: CondenserConfig = Field(
|
||||
default_factory=lambda: NoOpCondenserConfig(type='noop')
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user