Skip to content

Commit

Permalink
Make older functions compatible with tool calling (#1605)
Browse files Browse the repository at this point in the history
## Description

Some older custom function implementations where the description and
parameters were manually specified (e.g. as built in `composio-phidata`)
were not compatible with the new way of building tools for models.

This addresses it by keeping parameters/description if it was set on the
Function.

---------

Co-authored-by: Anurag <[email protected]>
  • Loading branch information
dirkbrnd and anuragts authored Dec 20, 2024
1 parent 30c962e commit 8683f07
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
1 change: 0 additions & 1 deletion cookbook/tools/composio_tools.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from phi.agent import Agent
from composio_phidata import Action, ComposioToolSet # type: ignore


toolset = ComposioToolSet()
composio_tools = toolset.get_tools(actions=[Action.GITHUB_STAR_A_REPOSITORY_FOR_THE_AUTHENTICATED_USER])

Expand Down
13 changes: 10 additions & 3 deletions phi/tools/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Function(BaseModel):
# The parameters the functions accepts, described as a JSON Schema object.
# To describe a function that accepts no parameters, provide the value {"type": "object", "properties": {}}.
parameters: Dict[str, Any] = Field(
default_factory=lambda: {"type": "object", "properties": {}},
default_factory=lambda: {"type": "object", "properties": {}, "required": []},
description="JSON Schema object describing function parameters",
)
strict: Optional[bool] = None
Expand Down Expand Up @@ -139,6 +139,12 @@ def process_entrypoint(self, strict: bool = False):
return

parameters = {"type": "object", "properties": {}, "required": []}

params_set_by_user = False
# If the user set the parameters (i.e. they are different from the default), we should keep them
if self.parameters != parameters:
params_set_by_user = True

try:
sig = signature(self.entrypoint)
type_hints = get_type_hints(self.entrypoint)
Expand Down Expand Up @@ -174,8 +180,9 @@ def process_entrypoint(self, strict: bool = False):
except Exception as e:
logger.warning(f"Could not parse args for {self.name}: {e}", exc_info=True)

self.description = getdoc(self.entrypoint) or self.description
self.parameters = parameters
self.description = self.description or getdoc(self.entrypoint)
if not params_set_by_user:
self.parameters = parameters
self.entrypoint = validate_call(self.entrypoint)

def get_type_name(self, t: Type[T]):
Expand Down

0 comments on commit 8683f07

Please sign in to comment.