Skip to content

Commit fe876de

Browse files
committed
Fixed optionality of CrewAI
1 parent 9e61030 commit fe876de

1 file changed

Lines changed: 55 additions & 48 deletions

File tree

graphlit_tools/crewai_converter.py

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,59 @@
11
from typing import Any, cast
2-
3-
try:
4-
from crewai_tools.tools.base_tool import BaseTool as CrewAIBaseTool
5-
except ImportError as e:
6-
raise ImportError(
7-
"CrewAIConverter requires the `crewai_tools` package. "
8-
"Install it using `pip install graphlit-tools[crewai]`."
9-
) from e
10-
112
from .base_tool import BaseTool
123

13-
class CrewAIConverter(CrewAIBaseTool):
14-
"""Tool to convert Graphlit tools into CrewAI tools."""
15-
16-
graphlit_tool: Any
17-
18-
def _run(
19-
self,
20-
*args: Any,
21-
**kwargs: Any,
22-
) -> Any:
23-
tool = cast(BaseTool, self.graphlit_tool)
24-
25-
return tool.run(*args, **kwargs)
4+
CrewAIBaseTool: Any = None
265

27-
async def _arun(
28-
self,
29-
*args: Any,
30-
**kwargs: Any,
31-
) -> Any:
32-
tool = cast(BaseTool, self.graphlit_tool)
33-
34-
return await tool.arun(*args, **kwargs)
35-
36-
@classmethod
37-
def from_tool(cls, tool: Any, **kwargs: Any) -> "CrewAIConverter":
38-
if not isinstance(tool, BaseTool):
39-
raise ValueError(f"Expected a Graphlit tool, got {type(tool)}")
40-
41-
tool = cast(BaseTool, tool)
42-
43-
if tool.args_schema is None:
44-
raise ValueError("Invalid arguments JSON schema.")
45-
46-
return cls(
47-
name=tool.name,
48-
description=tool.description,
49-
args_schema=tool.args_schema,
50-
graphlit_tool=tool,
51-
**kwargs,
52-
)
6+
try:
7+
from crewai_tools.tools.base_tool import BaseTool as CrewAIBaseTool
8+
except ImportError:
9+
CrewAIBaseTool = None
10+
11+
if CrewAIBaseTool:
12+
class CrewAIConverter(CrewAIBaseTool):
13+
"""Tool to convert Graphlit tools into CrewAI tools."""
14+
15+
graphlit_tool: Any
16+
17+
def _run(
18+
self,
19+
*args: Any,
20+
**kwargs: Any,
21+
) -> Any:
22+
tool = cast(BaseTool, self.graphlit_tool)
23+
24+
return tool.run(*args, **kwargs)
25+
26+
async def _arun(
27+
self,
28+
*args: Any,
29+
**kwargs: Any,
30+
) -> Any:
31+
tool = cast(BaseTool, self.graphlit_tool)
32+
33+
return await tool.arun(*args, **kwargs)
34+
35+
@classmethod
36+
def from_tool(cls, tool: Any, **kwargs: Any) -> "CrewAIConverter":
37+
if not isinstance(tool, BaseTool):
38+
raise ValueError(f"Expected a Graphlit tool, got {type(tool)}")
39+
40+
tool = cast(BaseTool, tool)
41+
42+
if tool.args_schema is None:
43+
raise ValueError("Invalid arguments JSON schema.")
44+
45+
return cls(
46+
name=tool.name,
47+
description=tool.description,
48+
args_schema=tool.args_schema,
49+
graphlit_tool=tool,
50+
**kwargs,
51+
)
52+
else:
53+
class CrewAIConverter:
54+
"""Fallback CrewAIConverter if crewai_tools is not installed."""
55+
def __init__(self, *args, **kwargs):
56+
raise ImportError(
57+
"CrewAIConverter requires the crewai_tools package. "
58+
"Install it using pip install graphlit-tools[crewai]."
59+
)

0 commit comments

Comments
 (0)