Skip to content

Commit

Permalink
chore: add from_autogen_tool classmethod to LLMCallableTool
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Feb 11, 2025
1 parent 58b5142 commit c82f84d
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/llmling/tools/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,50 @@ def from_langchain_tool(
schema_override=schema_override,
)

@classmethod
def from_autogen_tool(
cls,
tool: Any,
*,
name_override: str | None = None,
description_override: str | None = None,
schema_override: py2openai.OpenAIFunctionDefinition | None = None,
) -> Self:
"""Create a tool from a AutoGen tool."""
try:
from autogen_core import CancellationToken
from autogen_core.tools import BaseTool
except ImportError as e:
msg = "autogent_core package not found."
raise ImportError(msg) from e

if not isinstance(tool, BaseTool):
msg = f"Expected AutoGent BaseTool, got {type(tool)}"
raise TypeError(msg)
token = CancellationToken()

input_model = tool.__class__.__orig_bases__[0].__args__[0] # type: ignore

name = name_override or tool.name or tool.__class__.__name__.removesuffix("Tool")
description = (
description_override
or tool.description
or inspect.getdoc(tool.__class__)
or ""
)

async def wrapper(**kwargs: Any) -> Any:
# Convert kwargs to the expected input model
model = input_model(**kwargs)
return await tool.run(model, cancellation_token=token)

return cls.from_callable(
wrapper, # type: ignore
name_override=name,
description_override=description,
schema_override=schema_override,
)

async def execute(self, *args, **params: Any) -> Any:
"""Execute the wrapped callable."""
if inspect.iscoroutinefunction(self.callable):
Expand Down

0 comments on commit c82f84d

Please sign in to comment.