OpenAI API’s and Tools Calling

Sivaneni Prasanna
4 min readMar 9, 2024

--

In this we will be covering how to connect to openai api like chat completion and send the request and see how the custom functions(tools) getting called from the API.

Using OpenAI API we can integrate application as per our business needs.so consider if we want to create an chatbot where user need to get the real-time stock-price of a company or like get weather updates from external API’s,call on-premise database to get some data like TexttoSql generation.Lets discuss more in detail how we implement this in Python.

As of now we will see how we can call openAI chat completion API by passing header,json_data(model,tools, parameters of model) into the request.

"""
A function for making a chat completion request to the OpenAI API.

:param messages: List of messages to be completed.
:param tools: (optional) List of tools to be used for completion.
:param tool_choice: (optional) The specific tool to be used for completion.
:param model: (optional) The model to be used for completion.
:return: The response object from the API request.
"""
def chat_completion_request(messages, tools=None, tool_choice=None, model="gpt-3.5-turbo-0613"):
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + os.getenv("OPENAIAPI_KEY"),
}
print(headers)
json_data = {"model": model, "messages": messages}
if tools is not None:
json_data.update({"tools": tools})
if tool_choice is not None:
json_data.update({"tool_choice": tool_choice})
json_data.update({"temperature": 1.0})
try:
response = requests.post(
"https://api.openai.com/v1/chat/completions",
headers=headers,
json=json_data,
)
return response
except Exception as e:
print("Unable to generate ChatCompletion response")
print(f"Exception: {e}")
return e

Below is the request to API

{'id': 'chatcmpl-8w5GBamdqDGfByoYNll73t3ESYsj0', 'object': 'chat.completion', 'created': 1708853935, 'model': 'gpt-3.5-turbo-0613', 'choices': [
{'index': 0, 'message': {'role': 'assistant', 'content': None, 'tool_calls': [
{'id': 'call_Oci2kbUTmYrXzwTVTpaNwHf5', 'type': 'function', 'function': {'name': 'get_time_date_doctor_book_appointement', 'arguments': '{\n "timeslot": 16,\n "Date": "tommorrow",\n "name_of_doctor": "taylor"\n
}'
}
}
]
}, 'logprobs': None, 'finish_reason': 'tool_calls'
}
], 'usage': {'prompt_tokens': 879, 'completion_tokens': 43, 'total_tokens': 922
}, 'system_fingerprint': None
}

we provide inputs like messages(List Type) tools(list of functions) to the chat_completion_request and model will call the corresponding function based on the input message and previous context provided.in the above example i had passed to tools below functions tools(get_doctor_by_name,get_doctor_by_department,get_time_date_book_appointement,get_time_date_doctor_book_appointement)

when we provide a prompt like for example “Get an appointment with Dr X for tomorrow at 4 PM” and pass this request to the chat_completion_request method it calls get_time_date_doctor_book_appointement function automatically from tools and pass the input parameters accordingly.

{\n "timeslot": 16,\n "Date": "tomorrow",\n "name_of_doctor": "X"\n
}

the above function looks like this

tools[
{
"type": "function",
"function":{
"name": "get_time_date_doctor_book_appointement",
"description": """this function is called to fix an appointement with doctor
it will take below three parametrs o
1.timeslot
2.Date
3.name_of_doctor
timeslot would be like 4 PM,3 AM --etc and date would be today,tommorrow,feb26th,23-02-2024
name_of_doctor would be Dr. Smith,smith,doctor smith for all the examples with name_of_doctor should be passed as smith.
if timeslot is not provided it should set as null
eg:book a slot for tommorrow
eg:book an appointement with Dr smith at 4 PM for tommorrow
eg:book a slot with smith for today at 6 PM
for the first example timeslot must be null and Date will be tommorrow
for the second example timeslot will be 16:00 and Date will be tommorrow and name_of_doctor is smith
for the third example timeslot will be 18:00 and Date will be today and name_of_doctor is smith
""",
"parameters": {
"type": "object",
"properties": {
"timeslot": {
"type": "integer",
"description": "This is the time of the appointment we need to map.",
},
"Date":{
"type":"string",
"description":"This is Date of the appointement we need to map"
},
"name_of_doctor": {
"type": "string",
"description": "This is the name_of_doctor we need to map.",
},

},
"required": ["timeslot","Date","name_of_doctor"]

}
}
}


]

it’s very important to provide the complete information about the function so model can understand better and get exact results. Here we had followed one of the prompting technique “few-shot” prompting where we had given some examples how an user can provide request to.

you can follow this below link to work on other parameters we can supply to this API

https://platform.openai.com/docs/api-reference/chat/create

Note: This API charges a bit and you can find more details about it here

https://platform.openai.com/usage

i am using gpt-3.5-turbo-0613 model and it charges about

input to model $1.50 / 1M tokens

Output from Model $2.00 / 1M tokens

we will get $5.00 limit for free when we register

In further posts I will discuss more about a chat-bot application with RAG implementation.

If you like the content let’s connect on LinkedIn and share your thoughts.

Thank you! 😊 If you have any more questions or need further assistance, feel free to ask. Have a great day and be Healthy! 📚🌟

--

--