Function Calling: The Most Consequential Update to OpenAI's API
OpenAI recently released a new feature called Function Calling, which is a powerful update to their API. This article looks at how this feature works and provides an example of how it can be used to solve complex questions.
Santiago
Machine Learning. I run https://t.co/iZifcK7n47 and write @0xbnomial.
-
OpenAI just introduced Function Calling.
— Santiago (@svpino) June 13, 2023
This is a killer feature! It's the most consequential update to their API since they released it.
Here is an example to show you how powerful this is: -
Look at this question:
— Santiago (@svpino) June 13, 2023
"What's the result of 22 plus 5 in decimal added to the hexadecimal number A?"
It's a complex question requiring adding numbers in different formats.
To solve it, we can combine functions with prompts. Here is what it looks like: -
I'm going to define two functions.
— Santiago (@svpino) June 13, 2023
1. One that knows how to add two decimal numbers.
2. The other knows how to add two hexadecimal numbers.
You define these functions as part of the Chat Completion call: pic.twitter.com/JGz8xym3Cv -
The implementation of these two functions is not particularly interesting.
— Santiago (@svpino) June 13, 2023
They parse the arguments and do the sum. pic.twitter.com/K9YtDvjrrM -
This is the cool part:
— Santiago (@svpino) June 13, 2023
I call OpenAI's API repeatedly until I find a "stop" reason.
Whenever the API finishes with a "function_call" reason, I call the specific function and pass the results back to the API. pic.twitter.com/86kfwzweOk -
The output from the script:
— Santiago (@svpino) June 13, 2023
• 22 + 5 = 27 (decimal)
• 27 + A = 31 (hex)
• The result of adding 22 and 5 in decimal, and then adding the hexadecimal number A, is 31.
GPT used both of my functions to compute the final result!
Here is the code: https://t.co/x2ef1MWySS -
GPT will solve the prompt in steps.
— Santiago (@svpino) June 13, 2023
First, it will ask me to call the "add_decimal" fn with the first 2 values.
I need to make the call. The API has no way to make the call by itself. In my example, I'm using a local function, but the function could be another API call or… -
Here is a bit more information about how this example works:https://t.co/eLMoHq5MfD
— Santiago (@svpino) June 13, 2023 -
Yes, OpenAI took this from @LangChainAI.
— Santiago (@svpino) June 13, 2023
No, this doesn't kill LangChain. There's much more functionality there.
But this brings the question:
How much more is OpenAI planning to lift from LangChain and integrate it natively on their API? -
"Explain to me the new features like I'm 5".
— Santiago (@svpino) June 14, 2023
Here it is:https://t.co/XnjnJskWSS