on
Using External Functions with OpenAI
An easy way to boost the capabilities of AI models is to provide it with external information that it may not have. e.g. maybe your company has some proprietary data that would never be included while training the models. But if you could provide the model with this info during inferencing, it would be able to provide more accurate results.
One way of doing that with OpenAI is via function calls
. You define a function with all its input parameters and describe what the function does. The model can then use this info and determine when it would be useful to call the function. The client then calls the function and provides the result back to the model for the final result.
Let’s clarify this with an example.
Let’s say we are storing user addresses for a ride sharing or delivery app. After the ride/delivery is complete we want to mask the address so as to protect user privacy. However we still want to be able to do analytics on this info e.g. how many rides from within a zip code, which area is getting the most deliveries etc.
One option is to geomask the address i.e. add a delta to the lat/long of the original address so that the original address is not identifiable but it’s still in the general vicinity. However, this masking is a function of population density in that area. For a urban area the delta needs to be small (as there is a high concentration of addresses) while in a rural area the delta probably needs to be large (as the closest neighbour may be a large distance away)
Let’s ask OpenAI to help us. We will in turn help OpenAI by providing it a function call that provides population densities given a zip code, so that it can mask an address accordingly.
The address I will use is 415 Mission St, San Francisco, CA 94105
(The salesforce tower)
|
|
Above, I defined a function: population_density_by_zipcode
and described its purpose to OpenAI (not implementation). This provides OpenAI with the information that such a function is available for its use, should it choose to use it.
When the above code is run, we get the following output:
|
|
OpenAI is telling us that the function we described needs to be called and the output of it sent back to it. The function call parameters are described as well.
So the client then calls the function with the provided parameters. (Note that this is external to OpenAI)
Let’s say the population density I got is: 5902.5400000000
|
|
Essentially, I replayed the message back to OpenAI, this time with the added result of the function call. Voila…
|
|
As you can see, the exact address has been masked to an approximate one. Yet it is in the same vicinity so it is still usable for any kind of analytics.
We can play around with different density values. e.g. if I decreased the population density to 2902.5400000000
, I get:
Masked Address: 400-500 Mission St, San Francisco, CA 94105
If I still further decrease it to 1902.5400000000
, I get:
Masked Address: : Mission St, San Francisco, CA
In this way we can augment the capability of any model by using functions that return internal data e.g. from a proprietary DB call, API call etc