Call Your Own API Inside AI Assistant — OpenAI Function Calling

Shweta Lodha
4 min readNov 21, 2023

In this article, I’ll explain how we can make a call to external functions and this we will be doing using OpenAI’s Assistant API. If you’re not sure about how to get started with Assistant API, then I would recommend you to go back and have a look at my previous article ‘Create Your First AI Assistant — OpenAI Assistant API’.

Well, as we know that using Assistant API, we can build AI assistants in our own applications with just few sets of instructions.

Before we get started, make sure that you have installed OpenAI, a python library. Let’s get started by grabbing the keys.

Get OpenAI API Key And Organization ID

To get the keys, you need to go to https://openai.com/, login and then grab the keys using highlighted way.

Here we need to grab 2 things — API key and Organization ID

Then set these values in an environment variable:

import os
os.environ["ORGANIZATION_ID"] = "ORG_ID_HERE"
os.environ["OPENAI_API_KEY"] = "API_KEY_HERE"

Import Required Packages And Create Client Object

Do install the dependent library OpenAI and import the below package:

from openai import OpenAI

Once package is installed, we are good to go ahead and create client object for OpenAI, which takes 2 parameters:

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"],organization=os.environ["ORGANIZATION_ID"])

Add Function

Next, we need to create a function, along with all the required properties for parameters:

get_homeless_count = {
"name" : "get_homeless_count",
"description" : "Function takes in country name, year and returns count of homeless people",
"parameters" : {
"type" : "object",
"properties" : {
"year": {
"type":"integer",
"description" : "Year for which data needs to be fetched"…

--

--