Groq <> Toolhouse
Using Toolhouse for Tool Use with Groq API
Toolhouse is the first complete infrastructure for tool use. With Toolhouse, you can equip your LLM with tools like Code Interpreter, Web Search, and Email tools, among others. This equips your LLMs with the ability to search the web, send the emails they write, or run the code they generate, without the need for your to code or prompt these tools. These tools can be used across any LLM supported by Groq.
In this short demo, we'll show how to help LLM generate precise answers to math problems. We'll use Toolhouse with the Groq API, in particular the Groq llama3-groq-70b-8192-tool-use-preview Large Language Model (LLM) that is fine-tuned for tool use, for effective and fast tool calling.
Setup
To integrate Groq and Toolhouse, you'll need to set up two environment variables: GROQ_API_KEY and TOOLHOUSE_API_KEY. Follow these steps to obtain your API keys:
- Groq API Key: Create a free Groq API key by visiting the Groq Console.
- Toolhouse API Key: Sign up for Toolhouse using this link to receive $150 in credits. You will receive your API key as part of the onboarding step, and you can always, navigate to the Toolhouse API Keys page to create and get an API key.
- Install Code Interpreter: In your Toolhouse dashboard, click Install next to Code Interpreter (direct link).
Once you have both API keys, set them as environment variables to start using Groq and Toolhouse.
We will use the llama-3.3-70b-versatile model for this tutorial.
Note: a prior version of this tutorial used Groq's custom fine-tuned Llama3 70B Tool Use model, which has since been deprecated. llama-3.3-70b-versatile offers superior tool use capabilities is the recommended model for tool use on Groq
If you like, you can use the th.get_tools() function to display all of the Toolhouse tools you have installed:
TOOLS AVAILABLE: Name: code_interpreter Type: function Description: Allows you to run the code you generate. You can use this tool to verify that the code you generate is valid and contains all the relevant dependencies. IMPORTANT: When sending code, end your code with a print statement of the result. If you return something, make sure to change that return statement for a print statement. Required Parameters: code_str - The code to execute. Only Python is supported at the moment. IMPORTANT: When sending code, end your code with a print statement of the result. If you return something, make sure to change that return statement for a print statement.
For this demo, we will be using the code_interpreter tool. This tool takes in the code_str parameter, which it identifies from the user message, and runs the code.
Configure Tool Call
First we'll configure the user message to the LLM with the code we want run:
We'll send this to our LLM via Groq API. Note that this works just like a typical Tool Use example in Groq, but instead of defining the tools ourselves we are using our existing tools in Toolhouse:
As you can see, the LLM properly identified that we'd like to invoke the code_interpreter tool, and properly passed our code to it via the code_str parameter:
ID: call_d47c
Type: function
Function: Function(arguments='{"code_str": "a = 409830948\\nprint(a / 9834294824)"}', name='code_interpreter')
Execute Tool Call
Now we'll run the Code Execution tool, get the result, and append it to the context. The tool gets run through Toolhouse via the run_tools command, with the parameters that were identified in the previous LLM call.
As you can see from this tool run, our messages list now has entries for the assistant (the tool call configuration) and the tool (the tool call result):
Role: assistant
Tool Calls: [{'id': 'call_d47c', 'function': {'arguments': '{"code_str": "a = 409830948\\nprint(a / 9834294824)"}', 'name': 'code_interpreter'}, 'type': 'function'}]
Content: None
Role: tool
Tool Call ID: call_d47c
Content: 0.04167364872973225
We can compare the tool call result to the actual result by running the Python code:
ACTUAL RESULT: 0.04167364872973225
Finally, we'll send our message list with the completed tool call back to the LLM to get a proper response:
LLM RESPONSE: The result of the division is approximately 0.04167364872973225.