- Notifications
You must be signed in to change notification settings - Fork32
feat(toolbox-langchain): Support per-invocation auth viaRunnableConfig#291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
anubhav756 wants to merge4 commits intoanubhav-state-liChoose a base branch fromanubhav-self-auth-tools
base:anubhav-state-li
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Draft
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
4d62205 feat(toolbox-langchain): Implement self-authenticated tools
anubhav756f4c8a7b chore: Fix unit tests
anubhav75651d1e94 chore: Refactor getting tool to run into a reusable helper
anubhav7566f8cbfc chore: Make variable names more intuitive
anubhav756File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
35 changes: 33 additions & 2 deletionspackages/toolbox-langchain/src/toolbox_langchain/async_tools.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -15,6 +15,7 @@ | ||
| from typing import Any, Callable, Union | ||
| from deprecated import deprecated | ||
| from langchain_core.runnables import RunnableConfig | ||
| from langchain_core.tools import BaseTool | ||
| from toolbox_core.tool import ToolboxTool as ToolboxCoreTool | ||
| from toolbox_core.utils import params_to_pydantic_model | ||
| @@ -52,7 +53,11 @@ def __init__( | ||
| def _run(self, **kwargs: Any) -> str: | ||
| raise NotImplementedError("Synchronous methods not supported by async tools.") | ||
| async def _arun( | ||
| self, | ||
| config: RunnableConfig, | ||
| **kwargs: Any, | ||
| ) -> str: | ||
| """ | ||
| The coroutine that invokes the tool with the given arguments. | ||
| @@ -63,7 +68,33 @@ async def _arun(self, **kwargs: Any) -> str: | ||
| A dictionary containing the parsed JSON response from the tool | ||
| invocation. | ||
| """ | ||
| tool_to_run = self.__core_tool | ||
| if ( | ||
anubhav756 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| config | ||
| and "configurable" in config | ||
| and "auth_token_getters" in config["configurable"] | ||
| ): | ||
| auth_token_getters = config["configurable"]["auth_token_getters"] | ||
| if auth_token_getters: | ||
| # The `add_auth_token_getters` method requires that all provided | ||
| # getters are used by the tool. To prevent validation errors, | ||
| # filter the incoming getters to include only those that this | ||
| # specific tool requires. | ||
| req_auth_services = set(self.__core_tool._required_authz_tokens) | ||
| for auth_list in self.__core_tool._required_authn_params.values(): | ||
| req_auth_services.update(auth_list) | ||
| filtered_getters = { | ||
| k: v | ||
| for k, v in auth_token_getters.items() | ||
| if k in req_auth_services | ||
| } | ||
| if filtered_getters: | ||
| tool_to_run = self.__core_tool.add_auth_token_getters( | ||
| filtered_getters | ||
| ) | ||
| return await tool_to_run(**kwargs) | ||
| def add_auth_token_getters( | ||
| self, auth_token_getters: dict[str, Callable[[], str]] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.