mirror of
https://github.com/OpenHands/OpenHands.git
synced 2025-12-26 05:48:36 +08:00
Fix unsupported MCP tools param (#8610)
This commit is contained in:
parent
37e9933092
commit
c26ef180f2
@ -10,6 +10,7 @@ if TYPE_CHECKING:
|
||||
from openhands.events.action import Action
|
||||
from openhands.llm.llm import ModelResponse
|
||||
|
||||
from openhands.llm.llm_utils import check_tools
|
||||
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
|
||||
@ -185,26 +186,7 @@ class CodeActAgent(Agent):
|
||||
params: dict = {
|
||||
'messages': self.llm.format_messages_for_llm(messages),
|
||||
}
|
||||
params['tools'] = self.tools
|
||||
|
||||
# Special handling for Gemini model which doesn't support default fields
|
||||
if self.llm.config.model == 'gemini-2.5-pro-preview-03-25':
|
||||
logger.info(
|
||||
f'Removing the default fields from tools for {self.llm.config.model} '
|
||||
"since it doesn't support them and the request would crash."
|
||||
)
|
||||
# prevent mutation of input tools
|
||||
params['tools'] = copy.deepcopy(params['tools'])
|
||||
# Strip off default fields that cause errors with gemini-preview
|
||||
for tool in params['tools']:
|
||||
if 'function' in tool and 'parameters' in tool['function']:
|
||||
if 'properties' in tool['function']['parameters']:
|
||||
for prop_name, prop in tool['function']['parameters'][
|
||||
'properties'
|
||||
].items():
|
||||
if 'default' in prop:
|
||||
del prop['default']
|
||||
# log to litellm proxy if possible
|
||||
params['tools'] = check_tools(self.tools, self.llm.config)
|
||||
params['extra_body'] = {'metadata': state.to_llm_metadata(agent_name=self.name)}
|
||||
response = self.llm.completion(**params)
|
||||
logger.debug(f'Response from LLM: {response}')
|
||||
|
||||
@ -279,9 +279,6 @@ class LLM(RetryMixin, DebugMixin):
|
||||
# Record start time for latency measurement
|
||||
start_time = time.time()
|
||||
# we don't support streaming here, thus we get a ModelResponse
|
||||
logger.debug(
|
||||
f'LLM: calling litellm completion with model: {self.config.model}, base_url: {self.config.base_url}, args: {args}, kwargs: {kwargs}'
|
||||
)
|
||||
resp: ModelResponse = self._completion_unwrapped(*args, **kwargs)
|
||||
|
||||
# Calculate and record latency
|
||||
|
||||
44
openhands/llm/llm_utils.py
Normal file
44
openhands/llm/llm_utils.py
Normal file
@ -0,0 +1,44 @@
|
||||
import copy
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from openhands.core.config import LLMConfig
|
||||
from openhands.core.logger import openhands_logger as logger
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from litellm import ChatCompletionToolParam
|
||||
|
||||
|
||||
def check_tools(
|
||||
tools: list['ChatCompletionToolParam'], llm_config: LLMConfig
|
||||
) -> list['ChatCompletionToolParam']:
|
||||
"""Checks and modifies tools for compatibility with the current LLM."""
|
||||
# Special handling for Gemini models which don't support default fields and have limited format support
|
||||
if 'gemini' in llm_config.model.lower():
|
||||
logger.info(
|
||||
f'Removing default fields and unsupported formats from tools for Gemini model {llm_config.model} '
|
||||
"since Gemini models have limited format support (only 'enum' and 'date-time' for STRING types)."
|
||||
)
|
||||
# prevent mutation of input tools
|
||||
checked_tools = copy.deepcopy(tools)
|
||||
# Strip off default fields and unsupported formats that cause errors with gemini-preview
|
||||
for tool in checked_tools:
|
||||
if 'function' in tool and 'parameters' in tool['function']:
|
||||
if 'properties' in tool['function']['parameters']:
|
||||
for prop_name, prop in tool['function']['parameters'][
|
||||
'properties'
|
||||
].items():
|
||||
# Remove default fields
|
||||
if 'default' in prop:
|
||||
del prop['default']
|
||||
|
||||
# Remove format fields for STRING type parameters if the format is unsupported
|
||||
# Gemini only supports 'enum' and 'date-time' formats for STRING type
|
||||
if prop.get('type') == 'string' and 'format' in prop:
|
||||
supported_formats = ['enum', 'date-time']
|
||||
if prop['format'] not in supported_formats:
|
||||
logger.info(
|
||||
f'Removing unsupported format "{prop["format"]}" for STRING parameter "{prop_name}"'
|
||||
)
|
||||
del prop['format']
|
||||
return checked_tools
|
||||
return tools
|
||||
Loading…
x
Reference in New Issue
Block a user