Skip to content
Open
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
6 changes: 5 additions & 1 deletion src/minisweagent/models/litellm_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ def __init__(self, *, config_class: Callable = LitellmModelConfig, **kwargs):
if self.config.litellm_model_registry and Path(self.config.litellm_model_registry).is_file():
litellm.utils.register_model(json.loads(Path(self.config.litellm_model_registry).read_text()))

def _tools(self) -> list[dict]:
"""Tool schemas offered to the model. Override to expose more than bash."""
return [BASH_TOOL]

def _query(self, messages: list[dict[str, str]], **kwargs):
try:
return litellm.completion(
model=self.config.model_name,
messages=messages,
tools=[BASH_TOOL],
tools=self._tools(),
**(self.config.model_kwargs | kwargs),
)
except litellm.exceptions.AuthenticationError as e:
Expand Down
32 changes: 32 additions & 0 deletions tests/models/test_litellm_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,38 @@ def test_query_includes_bash_tool(self, mock_cost, mock_completion):
mock_completion.assert_called_once()
assert mock_completion.call_args.kwargs["tools"] == [BASH_TOOL]

def test_tools_defaults_to_bash_tool(self):
model = LitellmModel(model_name="gpt-4")
assert model._tools() == [BASH_TOOL]

@patch("minisweagent.models.litellm_model.litellm.completion")
@patch("minisweagent.models.litellm_model.litellm.cost_calculator.completion_cost")
def test_tools_override_changes_tools_passed_to_completion(self, mock_cost, mock_completion):
custom_tool = {
"type": "function",
"function": {
"name": "custom",
"parameters": {"type": "object", "properties": {}, "required": []},
},
}

class CustomModel(LitellmModel):
def _tools(self):
return [BASH_TOOL, custom_tool]

tool_call = MagicMock()
tool_call.function.name = "bash"
tool_call.function.arguments = '{"command": "echo test"}'
tool_call.id = "call_1"
mock_completion.return_value = _mock_litellm_response([tool_call])
mock_cost.return_value = 0.001

model = CustomModel(model_name="gpt-4")
model.query([{"role": "user", "content": "test"}])

mock_completion.assert_called_once()
assert mock_completion.call_args.kwargs["tools"] == [BASH_TOOL, custom_tool]

@patch("minisweagent.models.litellm_model.litellm.completion")
@patch("minisweagent.models.litellm_model.litellm.cost_calculator.completion_cost")
def test_parse_actions_valid_tool_call(self, mock_cost, mock_completion):
Expand Down