Support `load_tool`

#1
by lysandre HF staff - opened

Hello! I'm opening this PR so that it is simpler to use your tool directly within the runtime.

Now instead of copy/pasting the code, users can do the following:

from transformers import load_tool

tool = load_tool('LecJackS/wolfram-alpha-query')

In order to do so, I instantiated a tool with your code and then used push_to_hub on it (it's implemented!). Please let me know in case something is unclear.

Thanks for your awesome tool ๐Ÿคฉ

lysandre changed pull request title from support-load_tool to Support `load_tool`
lysandre changed pull request status to open

Thank you very much!

I tried load_tool yesterday, but at first, an error was raised about a trophy emoji ๐Ÿ† Unicode character not recognized in the space description. I removed the emoji line, and another error was raised about the tool instance/class being in main or something like that, so I assumed was not implemented or had an error.

Will merge this pull request and try again.

LecJackS changed pull request status to merged

Please let me know if it doesn't work! Running it locally now that you have merged it works like so now:

In [1]: from transformers import load_tool

In [2]: load_tool('LecJackS/wolfram-alpha-query')
Downloading (โ€ฆ)ain/tool_config.json: 100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 276/276 [00:00<00:00, 341kB/s]
Downloading (โ€ฆ)olfram_alpha_tool.py: 100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 1.47k/1.47k [00:00<00:00, 15.6MB/s]
A new version of the following files was downloaded from https://huggingface.co/space/LecJackS/wolfram-alpha-query:
- wolfram_alpha_tool.py
. Make sure to double-check they do not contain any added malicious code. To avoid downloading new versions of the code file, you can pin a revision.
Out[2]: <transformers_modules.LecJackS.wolfram-alpha-query.b1b6f4696724af566b2ec41630606b03460d8488.wolfram_alpha_tool.WolframAlpha at 0x7fe840336710>

In [3]:

Excellent! I wrote a notebook using it and tried to push_to_hub() at the end of it, but I'm getting the following error:

Notebook:
https://colab.research.google.com/drive/1wRv65uzfHO3WUJCo1tcRZgadq4XQ-o--

Error:

ValueError: We can't save the code defining <__main__.WolframAlpha object at 0x7f1e78515510> in /tmp/tmpm9rapcqd as
it's been defined in __main__. You have to put this code in a separate module so we can include it in the saved 
folder.

As image:
image.png

Ah yes indeed, that's a bit of a technical limitation as we can't really see the code declared in a notebook and save it (it's split in cells, and we don't have access to the notebook dynamically). We can rework it to eventually support it, but meanwhile we would recommend saving your tool in a standalone module (exactly like the python file you currently have in your repository right now), import the tool in the notebook and push it to the Hub from there.

Another unrelated unsolved question (maybe I should ask somewhere else) is what's the HF way to let the user add the tool's API key (in this case, wolfram's API key) to use the tool? Is adding an additional parameter okay? (WolframAlpha requires a (free) API key to use their API)

So after:

tool = load_tool('LecJackS/wolfram-alpha-query')

one can use the tool directly as:

query="2+2"
key="123456"
tool(query, key)

But what happens when the agent uses it?

agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder", additional_tools=[wolframalpha_tool])
res = agent.run(f"Solve the following math query: {query}", return_code=False)

Ah yes indeed, that's a bit of a technical limitation as we can't really see the code declared in a notebook and save it (it's split in cells, and we don't have access to the notebook dynamically). We can rework it to eventually support it, but meanwhile we would recommend saving your tool in a standalone module (exactly like the python file you currently have in your repository right now), import the tool in the notebook and push it to the Hub from there.

Oh! it was that! Simpler than expected. Thank you, I'll do that.

Ah yeah that's a great question. I think we'll want the agent to be able to instantiate any tool without needing to handle API keys, which sounds like a bit of a security risk.

We'll likely work on some way to do that natively, but in the meantime, I would advise doing some work with environment variables, for example during the __init__() or setup() step of your tool:

self.api_key = os.environ.get('WOLFRAM_API_KEY')
if self.api_key is None:
    raise ValueError("Please set the `WOLFRAM_API_KEY` as an environment variable in order to instantiate the wolfram tool.")

This way the agent doesn't have to manage keys. It can be a bit annoying to handle if we end up having many tools that have such requirements, so we'll look into building something to accommodate such needs.

Ah yeah that's a great question. I think we'll want the agent to be able to instantiate any tool without needing to handle API keys, which sounds like a bit of a security risk.

We'll likely work on some way to do that natively, but in the meantime, I would advise doing some work with environment variables, for example during the __init__() or setup() step of your tool:

self.api_key = os.environ.get('WOLFRAM_API_KEY')
if self.api_key is None:
    raise ValueError("Please set the `WOLFRAM_API_KEY` as an environment variable in order to instantiate the wolfram tool.")

This way the agent doesn't have to manage keys. It can be a bit annoying to handle if we end up having many tools that have such requirements, so we'll look into building something to accommodate such needs.

Understood! Thank you very much for your time. push_to_hub() worked nicely after doing what you suggested ๐Ÿ‘Œ

Sign up or log in to comment