Model Name | Tier |
---|---|
gpt-4o | High |
gpt-4-turbo | High |
gpt-4 | High |
gpt-4-eu | High |
gpt-4o-mini | High |
gpt-4o-mini-eu | High |
mistral-8x7b | High |
claude-3.5-sonnet | High |
claude-3.5-sonnet-v2 | High |
claude-3-haiku | High |
claude-3-opus | High |
claude-3-sonnet | High |
llama-3.1-8b | High |
llama-3.3-70b | High |
llama-3-8b | High |
llama-3-70b | High |
Setup prem client
We first instantiate our prem client.Make sure you have an account at Prem AI Platform and a valid project id and an API Key. The
project_id
used here is a dummy id.Define functions
Letβs start with defining some simple functions. In our example, we will define simple arithmetic functions likeaddition
and division
. At the end of this example, we will show you why you need to define simple arithmetic functions when dealing with something related to LLMs and crunching numbers.
Testing LLMs without using external functions
We first test an LLMβs arithmetic capability without providing any external tool. This will help us understand the significance of using tools.Output without using tools
Output without using tools
To solve the expression ( \frac + 16238137181 ):
- First, perform the division: [ \frac \approx 355172013094.0 ]
- Then, add the result to 16238137181: [ 355172013094.0 + 16238137181 = 371410150275.0 ]
371410150275.0
, but the answer was 355140529450725.56
. They do not match, and hence, we get an impression of the LLM hallucinating when the number gets too large. Letβs do the same thing, but we will use function calling this time.
Using function calls
The whole point of using function/tool calls is that we do not want LLM to guess answers. Instead, we want it to get the information with the help of external tools and then use its expressive power to give a better and more complete answer. So, the objective becomes LLMβs capability to understand the functions given to it and when to call what.Define the set of tools for the LLM.
We first define all the tools that the LLM needs to choose from that set of tools when given a userβs query.function
here). Then, we define the properties of the function, like name
, description
, parameters
(which are function arguments), their type (whether the parameter is an integer or a string or some object), and all the required parameters of the function.
Feeding the tools to the LLM
The overall tool calling of an LLM can be divided into two phases (which use two explicit LLM calls) as follows:- In the first pass, we pass our query along with the tools (as shown above), and we get a JSON response back, which gives us the information about what functions the LLM calls sequentially.
- In the second pass, we parse all the functions LLM needs to call, and we call it and get the results with the arguments that the LLM gave us and get our result. We gather the results to feed them into the LLM.
Output
Output
division
operation first and then addition
.
Now we parse this output and then call the functions (as mentioned by the LLM) on by one and gather our results.
Calling and gathering the functions response
We first define a simple map between name of the function and the function object.- First, take all the intermediate results (sequence of tools called by the LLM, shown above) inside a text prompt and then append that prompt inside the
messages
list with therole
ofassistant
. - Parse each tool in
tool_calls
and then call the function, get the result, take all three values, and fit them into our prompt. - Finally, append this prompt as the role
user
in our existingmessages
list.
messages
that we can use it in our second pass.
Output
Output
Output
Output
The result of dividing 9297838399297827278289292 by 26181927361 is approximately 355124291313544.56. Adding 16238137181 to this result gives:[ 355124291313544.56 + 16238137181 = 355140529450725.56 ]So, the final result is approximately 355140529450725.56.
355140529450725.56
Summary
So that is how you do function calling using Prem. You can re-use this helper function:insert_tool_messages
so that your toolcalling workflow becomes easier. Here is your final code:
Final Code
Final Code