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

Add converter to smolagents tool #8

Merged
merged 1 commit into from
Jan 24, 2025
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
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ extra-dependencies = [
"mypy>=1.0.0",
"pytest",
"llama-index-core",
"smolagents",
]
[tool.hatch.envs.types.scripts]
check = "mypy --install-types --non-interactive {args:src/cleanlab_codex tests}"
Expand All @@ -51,6 +52,7 @@ allow-direct-references = true
[tool.hatch.envs.hatch-test]
extra-dependencies = [
"llama-index-core",
"smolagents; python_version >= '3.10'",
]

[tool.hatch.envs.hatch-test.env-vars]
Expand Down
11 changes: 11 additions & 0 deletions src/cleanlab_codex/codex_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ def to_openai_tool(self) -> dict[str, Any]:
required_properties=self._tool_requirements,
)

def to_smolagents_tool(self) -> Any:
"""Converts the tool to a smolagents tool."""
from cleanlab_codex.utils.smolagents import CodexTool as SmolagentsCodexTool

return SmolagentsCodexTool(
query=self.query,
tool_name=self._tool_name,
tool_description=self._tool_description,
inputs=self._tool_properties,
)

def to_llamaindex_tool(self) -> Any:
"""Converts the tool to a LlamaIndex FunctionTool."""
from llama_index.core.tools import FunctionTool
Expand Down
22 changes: 22 additions & 0 deletions src/cleanlab_codex/utils/smolagents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import Callable, Dict, Optional

from smolagents import Tool # type: ignore


class CodexTool(Tool):
def __init__(
self,
query: Callable[[str], Optional[str]],
tool_name: str,
tool_description: str,
inputs: Dict[str, Dict[str, str]],
):
super().__init__()
self._query = query
self.name = tool_name
self.description = tool_description
self.inputs = inputs
self.output_type = "string"

def forward(self, question: str) -> Optional[str]:
return self._query(question)
13 changes: 13 additions & 0 deletions tests/test_codex_tool.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sys
from unittest.mock import MagicMock

import pytest
from llama_index.core.tools import FunctionTool

from cleanlab_codex.codex_tool import CodexTool
Expand All @@ -21,3 +23,14 @@ def test_to_llamaindex_tool(mock_client: MagicMock): # noqa: ARG001
assert llama_index_tool.metadata.name == tool.tool_name
assert llama_index_tool.metadata.description == tool.tool_description
assert llama_index_tool.fn == tool.query


@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python3.10 or higher")
def test_to_smolagents_tool(mock_client: MagicMock): # noqa: ARG001
from smolagents import Tool # type: ignore

tool = CodexTool.from_access_key("")
smolagents_tool = tool.to_smolagents_tool()
assert isinstance(smolagents_tool, Tool)
assert smolagents_tool.name == tool.tool_name
assert smolagents_tool.description == tool.tool_description