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
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ async def aload_tool(
auth_headers: Optional[dict[str, Callable[[], str]]] = None,
bound_params: dict[str, Union[Any, Callable[[], Any]]] = {},
telemetry_attributes: Optional[TelemetryAttributes] = None,
secure_params: dict[str, Union[Any, Callable[[], Any]]] = {},
) -> AsyncToolboxTool:
"""
Loads the tool with the given tool name from the Toolbox service.
Expand All @@ -78,6 +79,8 @@ async def aload_tool(
bound values.
telemetry_attributes: Optional telemetry attributes (model, user
id, agent id) sent to the server with every tool invocation.
secure_params: An optional mapping of secure parameter names to their
bound values.

Returns:
A tool loaded from the Toolbox.
Expand Down Expand Up @@ -108,10 +111,15 @@ async def aload_tool(
)
auth_token_getters = auth_headers

kwargs: dict[str, Any] = {}
if secure_params:
kwargs["secure_params"] = secure_params

core_tool = await self.__core_client.load_tool(
name=tool_name,
auth_token_getters=auth_token_getters,
bound_params=bound_params,
**kwargs,
)
if telemetry_attributes is not None:
core_tool = core_tool.add_telemetry_attributes(telemetry_attributes)
Expand All @@ -126,6 +134,7 @@ async def aload_toolset(
bound_params: dict[str, Union[Any, Callable[[], Any]]] = {},
strict: bool = False,
telemetry_attributes: Optional[TelemetryAttributes] = None,
secure_params: dict[str, Union[Any, Callable[[], Any]]] = {},
) -> list[AsyncToolboxTool]:
"""
Loads tools from the Toolbox service, optionally filtered by toolset
Expand All @@ -141,12 +150,14 @@ async def aload_toolset(
bound_params: An optional mapping of parameter names to their
bound values.
strict: If True, raises an error if *any* loaded tool instance fails
to utilize at least one provided parameter or auth token (if any
to utilize all of the given parameters, auth tokens, or secure parameters (if any
provided). If False (default), raises an error only if a
user-provided parameter or auth token cannot be applied to *any*
user-provided parameter, auth token, or secure parameter cannot be applied to *any*
loaded tool across the set.
telemetry_attributes: Optional telemetry attributes (model, user
id, agent id) sent to the server with every tool invocation.
secure_params: An optional mapping of secure parameter names to their
bound values.

Returns:
A list of all tools loaded from the Toolbox.
Expand Down Expand Up @@ -177,11 +188,16 @@ async def aload_toolset(
)
auth_token_getters = auth_headers

kwargs: dict[str, Any] = {}
if secure_params:
kwargs["secure_params"] = secure_params

core_tools = await self.__core_client.load_toolset(
name=toolset_name,
auth_token_getters=auth_token_getters,
bound_params=bound_params,
strict=strict,
**kwargs,
)

tools = []
Expand All @@ -198,6 +214,7 @@ def load_tool(
auth_tokens: Optional[dict[str, Callable[[], str]]] = None,
auth_headers: Optional[dict[str, Callable[[], str]]] = None,
bound_params: dict[str, Union[Any, Callable[[], Any]]] = {},
secure_params: dict[str, Union[Any, Callable[[], Any]]] = {},
) -> AsyncToolboxTool:
raise NotImplementedError("Synchronous methods not supported by async client.")

Expand All @@ -209,6 +226,7 @@ def load_toolset(
auth_headers: Optional[dict[str, Callable[[], str]]] = None,
bound_params: dict[str, Union[Any, Callable[[], Any]]] = {},
strict: bool = False,
secure_params: dict[str, Union[Any, Callable[[], Any]]] = {},
) -> list[AsyncToolboxTool]:
raise NotImplementedError("Synchronous methods not supported by async client.")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,59 @@ def bind_param(
returns the value.

Returns:
A new ToolboxTool instance that is a deep copy of the current
A new AsyncToolboxTool instance that is a deep copy of the current
instance, with added bound param.

Raises:
ValueError: If the provided bound param is already bound.
"""
return self.bind_params({param_name: param_value})

def bind_secure_params(
self,
bound_secure_params: dict[str, Union[Any, Callable[[], Any]]],
) -> "AsyncToolboxTool":
"""
Registers values or functions to retrieve the value for the
corresponding bound secure parameters.

Args:
bound_secure_params: A dictionary of the bound secure parameter name to the
value or function of the bound secure value.

Returns:
A new AsyncToolboxTool instance that is a deep copy of the current
instance, with added bound secure params.

Raises:
ValueError: If any of the provided bound secure params is already bound.
"""
new_core_tool = self.__core_tool.bind_secure_params(bound_secure_params)
return AsyncToolboxTool(core_tool=new_core_tool)

def bind_secure_param(
self,
param_name: str,
param_value: Union[Any, Callable[[], Any]],
) -> "AsyncToolboxTool":
"""
Registers a value or a function to retrieve the value for a given bound
secure parameter.

Args:
param_name: The name of the bound secure parameter.
param_value: The value of the bound secure parameter, or a callable that
returns the value.

Returns:
A new AsyncToolboxTool instance that is a deep copy of the current
instance, with added bound secure param.

Raises:
ValueError: If the provided bound secure param is already bound.
"""
return self.bind_secure_params({param_name: param_value})

def add_telemetry_attributes(
self, telemetry_attributes: TelemetryAttributes
) -> "AsyncToolboxTool":
Expand Down
40 changes: 36 additions & 4 deletions packages/toolbox-llamaindex/src/toolbox_llamaindex/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ async def aload_tool(
auth_headers: Optional[dict[str, Callable[[], str]]] = None,
bound_params: dict[str, Union[Any, Callable[[], Any]]] = {},
telemetry_attributes: Optional[TelemetryAttributes] = None,
secure_params: dict[str, Union[Any, Callable[[], Any]]] = {},
) -> ToolboxTool:
"""
Loads the tool with the given tool name from the Toolbox service.
Expand All @@ -73,6 +74,8 @@ async def aload_tool(
bound values.
telemetry_attributes: Optional telemetry attributes (model, user
id, agent id) sent to the server with every tool invocation.
secure_params: An optional mapping of secure parameter names to their
bound values.

Returns:
A tool loaded from the Toolbox.
Expand Down Expand Up @@ -103,11 +106,16 @@ async def aload_tool(
)
auth_token_getters = auth_headers

kwargs: dict[str, Any] = {}
if secure_params:
kwargs["secure_params"] = secure_params

core_tool = await to_thread(
self.__core_client.load_tool,
name=tool_name,
auth_token_getters=auth_token_getters,
bound_params=bound_params,
**kwargs,
)
if telemetry_attributes is not None:
core_tool = core_tool.add_telemetry_attributes(telemetry_attributes)
Expand All @@ -122,6 +130,7 @@ async def aload_toolset(
bound_params: dict[str, Union[Any, Callable[[], Any]]] = {},
strict: bool = False,
telemetry_attributes: Optional[TelemetryAttributes] = None,
secure_params: dict[str, Union[Any, Callable[[], Any]]] = {},
) -> list[ToolboxTool]:
"""
Loads tools from the Toolbox service, optionally filtered by toolset
Expand All @@ -137,12 +146,14 @@ async def aload_toolset(
bound_params: An optional mapping of parameter names to their
bound values.
strict: If True, raises an error if *any* loaded tool instance fails
to utilize at least one provided parameter or auth token (if any
to utilize all of the given parameters, auth tokens, or secure parameters (if any
provided). If False (default), raises an error only if a
user-provided parameter or auth token cannot be applied to *any*
user-provided parameter, auth token, or secure parameter cannot be applied to *any*
loaded tool across the set.
telemetry_attributes: Optional telemetry attributes (model, user
id, agent id) sent to the server with every tool invocation.
secure_params: An optional mapping of secure parameter names to their
bound values.

Returns:
A list of all tools loaded from the Toolbox.
Expand Down Expand Up @@ -173,12 +184,17 @@ async def aload_toolset(
)
auth_token_getters = auth_headers

kwargs: dict[str, Any] = {}
if secure_params:
kwargs["secure_params"] = secure_params

core_tools = await to_thread(
self.__core_client.load_toolset,
name=toolset_name,
auth_token_getters=auth_token_getters,
bound_params=bound_params,
strict=strict,
**kwargs,
)

tools = []
Expand All @@ -196,6 +212,7 @@ def load_tool(
auth_headers: Optional[dict[str, Callable[[], str]]] = None,
bound_params: dict[str, Union[Any, Callable[[], Any]]] = {},
telemetry_attributes: Optional[TelemetryAttributes] = None,
secure_params: dict[str, Union[Any, Callable[[], Any]]] = {},
) -> ToolboxTool:
"""
Loads the tool with the given tool name from the Toolbox service.
Expand All @@ -210,6 +227,8 @@ def load_tool(
bound values.
telemetry_attributes: Optional telemetry attributes (model, user
id, agent id) sent to the server with every tool invocation.
secure_params: An optional mapping of secure parameter names to their
bound values.

Returns:
A tool loaded from the Toolbox.
Expand Down Expand Up @@ -240,10 +259,15 @@ def load_tool(
)
auth_token_getters = auth_headers

kwargs: dict[str, Any] = {}
if secure_params:
kwargs["secure_params"] = secure_params

core_sync_tool = self.__core_client.load_tool(
name=tool_name,
auth_token_getters=auth_token_getters,
bound_params=bound_params,
**kwargs,
)
if telemetry_attributes is not None:
core_sync_tool = core_sync_tool.add_telemetry_attributes(
Expand All @@ -260,6 +284,7 @@ def load_toolset(
bound_params: dict[str, Union[Any, Callable[[], Any]]] = {},
strict: bool = False,
telemetry_attributes: Optional[TelemetryAttributes] = None,
secure_params: dict[str, Union[Any, Callable[[], Any]]] = {},
) -> list[ToolboxTool]:
"""
Loads tools from the Toolbox service, optionally filtered by toolset
Expand All @@ -275,12 +300,14 @@ def load_toolset(
bound_params: An optional mapping of parameter names to their
bound values.
strict: If True, raises an error if *any* loaded tool instance fails
to utilize at least one provided parameter or auth token (if any
to utilize all of the given parameters, auth tokens, or secure parameters (if any
provided). If False (default), raises an error only if a
user-provided parameter or auth token cannot be applied to *any*
user-provided parameter, auth token, or secure parameter cannot be applied to *any*
loaded tool across the set.
telemetry_attributes: Optional telemetry attributes (model, user
id, agent id) sent to the server with every tool invocation.
secure_params: An optional mapping of secure parameter names to their
bound values.

Returns:
A list of all tools loaded from the Toolbox.
Expand Down Expand Up @@ -311,11 +338,16 @@ def load_toolset(
)
auth_token_getters = auth_headers

kwargs: dict[str, Any] = {}
if secure_params:
kwargs["secure_params"] = secure_params

core_sync_tools = self.__core_client.load_toolset(
name=toolset_name,
auth_token_getters=auth_token_getters,
bound_params=bound_params,
strict=strict,
**kwargs,
)

tools = []
Expand Down
45 changes: 45 additions & 0 deletions packages/toolbox-llamaindex/src/toolbox_llamaindex/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,51 @@ def bind_param(
"""
return self.bind_params({param_name: param_value})

def bind_secure_params(
self,
bound_secure_params: dict[str, Union[Any, Callable[[], Any]]],
) -> "ToolboxTool":
"""
Registers values or functions to retrieve the value for the
corresponding bound secure parameters.

Args:
bound_secure_params: A dictionary of the bound secure parameter name to the
value or function of the bound secure value.

Returns:
A new ToolboxTool instance that is a deep copy of the current
instance, with added bound secure params.

Raises:
ValueError: If any of the provided bound secure params is already bound.
"""
new_core_tool = self.__core_tool.bind_secure_params(bound_secure_params)
return ToolboxTool(core_tool=new_core_tool)

def bind_secure_param(
self,
param_name: str,
param_value: Union[Any, Callable[[], Any]],
) -> "ToolboxTool":
"""
Registers a value or a function to retrieve the value for a given bound
secure parameter.

Args:
param_name: The name of the bound secure parameter.
param_value: The value of the bound secure parameter, or a callable that
returns the value.

Returns:
A new ToolboxTool instance that is a deep copy of the current
instance, with added bound secure param.

Raises:
ValueError: If the provided bound secure param is already bound.
"""
return self.bind_secure_params({param_name: param_value})

def add_telemetry_attributes(
self, telemetry_attributes: TelemetryAttributes
) -> "ToolboxTool":
Expand Down
Loading
Loading