Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

feat!: Add support for sync operations #20

Merged
merged 89 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from 83 commits
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
bd9c7b0
chore: chore: separate sdk from server repo
kurtisvg Feb 17, 2025
bc512a4
chore: fix package name
kurtisvg Feb 17, 2025
d67475c
fix!: Improve PyPI package name.
twishabansal Feb 18, 2025
2800310
Merge branch 'kvg-fork' of https://github.com/googleapis/genai-toolbo…
twishabansal Feb 18, 2025
1285c24
improve package name
twishabansal Feb 18, 2025
ee4b176
fix package name
twishabansal Feb 18, 2025
b955931
lint
twishabansal Feb 18, 2025
16febee
lint
twishabansal Feb 18, 2025
fbb2d13
Merge branch 'main' into kvg-fork
twishabansal Feb 19, 2025
72a2645
change license year for header checker lint
twishabansal Feb 19, 2025
4dd769b
Merge branch 'kvg-fork' of https://github.com/googleapis/genai-toolbo…
twishabansal Feb 20, 2025
41398b8
reformat
twishabansal Feb 20, 2025
7618faa
Change mypy command
twishabansal Feb 20, 2025
ba9f1f9
change back mypy command
twishabansal Feb 20, 2025
97733c0
chore: rename folder
kurtisvg Feb 20, 2025
84c7a3b
lint
twishabansal Feb 21, 2025
fd5d464
feat(llamaindex-sdk): Add support for Bound Params through ToolboxToo…
anubhav756 Jan 17, 2025
2a3de6e
fix(llamaindex-sdk): Improve session cleanup
anubhav756 Jan 21, 2025
fcb03b9
Merge branch 'main' into twisha-bound-params
twishabansal Feb 24, 2025
5a89b19
spacing
twishabansal Feb 24, 2025
a113496
spacing
twishabansal Feb 24, 2025
0ef6874
correct copyright year for header check
twishabansal Feb 24, 2025
6749137
Fix year to pass header checks
twishabansal Feb 24, 2025
12aabca
lint
twishabansal Feb 24, 2025
80759f4
Merge branch 'main' into twisha-bound-params
twishabansal Feb 25, 2025
44c865e
Merge branch 'main' into twisha-bound-params
twishabansal Feb 25, 2025
1e8751c
Merge branch 'main' into twisha-bound-params
twishabansal Feb 25, 2025
f07706c
Added async tools file
twishabansal Feb 25, 2025
4db0447
remove unused function
twishabansal Feb 25, 2025
7989a59
lint
twishabansal Feb 25, 2025
cbd8bec
lint
twishabansal Feb 26, 2025
ec549f1
tools file with some tests
twishabansal Feb 26, 2025
adaa6e1
comment out failing tests
twishabansal Feb 26, 2025
e8dbd1c
lint
twishabansal Feb 26, 2025
3f39c76
Modify async tools to inherit from BaseTool
twishabansal Feb 27, 2025
a8f34cd
Inherit tools from BaseTool
twishabansal Feb 27, 2025
386145d
fix some tests
twishabansal Feb 27, 2025
ecf6433
small fix
twishabansal Feb 27, 2025
d52603e
tests fix
twishabansal Feb 27, 2025
30b7032
lint
twishabansal Feb 27, 2025
4f14159
lint
twishabansal Feb 27, 2025
28d865e
separated client file into client and async client
twishabansal Feb 27, 2025
1f63665
lint
twishabansal Feb 27, 2025
14574ad
syntax change to work with py 3.9
twishabansal Feb 27, 2025
df5866c
fix args
twishabansal Feb 27, 2025
3db4e0a
fix e2e tests
twishabansal Feb 27, 2025
03e863d
Merge branch 'twisha-bound-params' into twisha-sync-client
twishabansal Feb 27, 2025
c0f7602
lint
twishabansal Feb 27, 2025
558815c
fix tests
twishabansal Feb 27, 2025
5b1dc61
Merge remote-tracking branch 'origin/twisha-sync-client' into twisha-…
twishabansal Feb 27, 2025
cf021cd
lint
twishabansal Feb 27, 2025
b34a9c2
test fix for test_run_tool_wrong_auth
twishabansal Feb 27, 2025
89a3ca0
try test fix for test_run_tool_wrong_auth
twishabansal Feb 27, 2025
6f9c524
fix e2e tests
twishabansal Feb 27, 2025
0fd6ab1
lint
twishabansal Feb 27, 2025
d61a96e
send error in tool output
twishabansal Feb 27, 2025
33032c7
lint
twishabansal Feb 27, 2025
aebd3cc
add comments and docstrings
twishabansal Feb 27, 2025
cc7b740
lint
twishabansal Feb 27, 2025
de70a47
remove unwanted tests
twishabansal Feb 28, 2025
cafbc65
cleanup tests
twishabansal Feb 28, 2025
f7ee7f1
add tests
twishabansal Feb 28, 2025
cbf48f2
lint
twishabansal Feb 28, 2025
56876cc
fix name
twishabansal Feb 28, 2025
0570c5d
Update src/toolbox_llamaindex/async_tools.py
twishabansal Feb 28, 2025
39abdf0
Update src/toolbox_llamaindex/async_tools.py
twishabansal Feb 28, 2025
b93d363
nit: add newline
twishabansal Feb 28, 2025
5625690
change input type
twishabansal Feb 28, 2025
9e60fc6
lint
twishabansal Feb 28, 2025
67640a2
Merge remote-tracking branch 'origin/twisha-sync-client' into twisha-…
twishabansal Feb 28, 2025
6f2a69f
lint
twishabansal Feb 28, 2025
9bf7ba5
fix tests
twishabansal Feb 28, 2025
8e7d1f4
skip type check for inherited classes
twishabansal Feb 28, 2025
415cc8c
lint
twishabansal Feb 28, 2025
23e500f
Merge branch 'main' into twisha-sync-client
twishabansal Mar 3, 2025
aa7d378
lint
twishabansal Mar 3, 2025
7ca7143
enforce tool schema type for async tool init
twishabansal Mar 3, 2025
b8a4236
cleanup
twishabansal Mar 3, 2025
6eafccb
cleanup
twishabansal Mar 3, 2025
4dc980b
change how error is thrown
twishabansal Mar 3, 2025
b52529e
fix e2e tests
twishabansal Mar 3, 2025
05c7ff7
lint
twishabansal Mar 3, 2025
ec56929
Merge branch 'main' into twisha-sync-client
twishabansal Mar 3, 2025
939a88c
fix nit
twishabansal Mar 4, 2025
3b3cce6
small fix
twishabansal Mar 4, 2025
443bdc6
revert small fix
twishabansal Mar 4, 2025
782448d
lint fix
twishabansal Mar 4, 2025
ab034af
Merge branch 'main' into twisha-sync-client
twishabansal Mar 4, 2025
957930c
Merge branch 'main' into twisha-sync-client
twishabansal Mar 12, 2025
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
171 changes: 171 additions & 0 deletions src/toolbox_llamaindex/async_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, Callable, Optional, Union
from warnings import warn

from aiohttp import ClientSession

from .tools import AsyncToolboxTool
from .utils import ManifestSchema, _load_manifest


# This class is an internal implementation detail and is not exposed to the
# end-user. It should not be used directly by external code. Changes to this
# class will not be considered breaking changes to the public API.
class AsyncToolboxClient:

def __init__(
self,
url: str,
session: ClientSession,
):
"""
Initializes the AsyncToolboxClient for the Toolbox service at the given URL.

Args:
url: The base URL of the Toolbox service.
session: An HTTP client session.
"""
self.__url = url
self.__session = session

async def aload_tool(
self,
tool_name: str,
auth_tokens: dict[str, Callable[[], str]] = {},
auth_headers: Optional[dict[str, Callable[[], str]]] = None,
bound_params: dict[str, Union[Any, Callable[[], Any]]] = {},
strict: bool = True,
) -> AsyncToolboxTool:
"""
Loads the tool with the given tool name from the Toolbox service.

Args:
tool_name: The name of the tool to load.
auth_tokens: An optional mapping of authentication source names to
functions that retrieve ID tokens.
auth_headers: Deprecated. Use `auth_tokens` instead.
bound_params: An optional mapping of parameter names to their
bound values.
strict: If True, raises a ValueError if any of the given bound
parameters are missing from the schema or require
authentication. If False, only issues a warning.

Returns:
A tool loaded from the Toolbox.
"""
if auth_headers:
if auth_tokens:
warn(
"Both `auth_tokens` and `auth_headers` are provided. `auth_headers` is deprecated, and `auth_tokens` will be used.",
DeprecationWarning,
)
else:
warn(
"Argument `auth_headers` is deprecated. Use `auth_tokens` instead.",
DeprecationWarning,
)
auth_tokens = auth_headers

url = f"{self.__url}/api/tool/{tool_name}"
manifest: ManifestSchema = await _load_manifest(url, self.__session)

return AsyncToolboxTool(
tool_name,
manifest.tools[tool_name],
self.__url,
self.__session,
auth_tokens,
bound_params,
strict,
)

async def aload_toolset(
self,
toolset_name: Optional[str] = None,
auth_tokens: dict[str, Callable[[], str]] = {},
auth_headers: Optional[dict[str, Callable[[], str]]] = None,
bound_params: dict[str, Union[Any, Callable[[], Any]]] = {},
strict: bool = True,
) -> list[AsyncToolboxTool]:
"""
Loads tools from the Toolbox service, optionally filtered by toolset
name.

Args:
toolset_name: The name of the toolset to load. If not provided,
all tools are loaded.
auth_tokens: An optional mapping of authentication source names to
functions that retrieve ID tokens.
auth_headers: Deprecated. Use `auth_tokens` instead.
bound_params: An optional mapping of parameter names to their
bound values.
strict: If True, raises a ValueError if any of the given bound
parameters are missing from the schema or require
authentication. If False, only issues a warning.

Returns:
A list of all tools loaded from the Toolbox.
"""
if auth_headers:
if auth_tokens:
warn(
"Both `auth_tokens` and `auth_headers` are provided. `auth_headers` is deprecated, and `auth_tokens` will be used.",
DeprecationWarning,
)
else:
warn(
"Argument `auth_headers` is deprecated. Use `auth_tokens` instead.",
DeprecationWarning,
)
auth_tokens = auth_headers

url = f"{self.__url}/api/toolset/{toolset_name or ''}"
manifest: ManifestSchema = await _load_manifest(url, self.__session)
tools: list[AsyncToolboxTool] = []

for tool_name, tool_schema in manifest.tools.items():
tools.append(
AsyncToolboxTool(
tool_name,
tool_schema,
self.__url,
self.__session,
auth_tokens,
bound_params,
strict,
)
)
return tools

def load_tool(
self,
tool_name: str,
auth_tokens: dict[str, Callable[[], str]] = {},
auth_headers: Optional[dict[str, Callable[[], str]]] = None,
bound_params: dict[str, Union[Any, Callable[[], Any]]] = {},
strict: bool = True,
) -> AsyncToolboxTool:
raise NotImplementedError("Synchronous methods not supported by async client.")

def load_toolset(
self,
toolset_name: Optional[str] = None,
auth_tokens: dict[str, Callable[[], str]] = {},
auth_headers: Optional[dict[str, Callable[[], str]]] = None,
bound_params: dict[str, Union[Any, Callable[[], Any]]] = {},
strict: bool = True,
) -> list[AsyncToolboxTool]:
raise NotImplementedError("Synchronous methods not supported by async client.")
Loading