Call your own API from ChatGPT — Natural Language to Action

sourajit roy chowdhury
4 min readApr 8, 2023

In this article I will try to provide a basic tutorial on how to call your own API while conversing with ChatGPT. The tutorial follows Fast-API, OpenAI ChatGPT API

Disclaimer

  • The disclaimer pertains that this tutorial and code are intended for the purpose of quick prototyping. While there are various ways to optimize the code and architecture, my main intention is to provide you with a quick and easy understanding, and help you get started in the right direction.
Architecture Flow Diagram

Content

Quick Context

Designing custom conversation agents for specific domains requires several engineering techniques, despite the popularity and flexibility of using ChatGPT. The conversation agent must retrieve information from external sources to deliver valuable responses to customer queries. These external sources could include flat files, PDF files, databases, and custom APIs, which receive data requests and provide responses to the user.

This article will present a straightforward architecture and example codes to demonstrate how to interpret natural language queries and extract the appropriate data for the API and calling the correct API depending on the user intent, ultimately providing the user with an accurate response.

The Architecture

The above architecture flow diagram can be divided into two main sections. The right-hand side consists of the custom APIs created for different scenarios, while the left-hand side contains the conversation agent. The conversation agent’s goal is to comprehend the user’s query, determine the appropriate API to use, and provide the relevant information to the user.

Let’s break both the parts and understand them in little more detail.

Custom API

Developers create APIs for specific use cases, which are intuitive in nature. These APIs require certain input parameters in the request call, and upon processing the input parameters, provide information in the response call.

Conversation Agent

This is where things get tricky. While ChatGPT understands the user’s intent during conversation, this alone is insufficient when creating a custom conversation agent. Below, I have outlined the step-by-step process of the architecture flow diagram above:

  1. The user query is sent to the conversation agent (ChatGPT).
  2. The user query’s intent is detected (using Zero Shot) through ChatGPT.
  3. The intent to API mapper is used to identify which API needs to be called for the detected intent.
  4. After identifying the API, the API interface is accessed.
  5. Data required for the API call needs to be extracted from the user query using ChatGPT’s entity extraction process (Zero Shot).
  6. The extracted entities must then be processed to match the API’s expected format.
  7. Once the input data for the API has been processed, the API interface is accessed again.
  8. A request is made to the desired API using the input data.
  9. The response from the API is received.
  10. ChatGPT processes the response as human language.
  11. The conversation agent (ChatGPT) responds to the user.

Sample Use-case

For this article I have identified an easy to understand use case of bank FD inquiry.

Project Structure

root_folder
|___________ app.py (Main API file)
|___________ chat.py (Conversation Agent file)
|___________ fd_interest_rates.csv (database for first API)
|___________ fd_maturity.csv (database for second API)

API Design

The micro-service framework used to write the API is FastAPI. There are two APIs in total. The first API provides information on interest rates and maturity amount based on given initial investment amount and tenor. Meanwhile, the second API is responsible for providing the maturity date based on the customer ID. To simplify matters, both APIs fetch data from two separate CSV files.

Conversation Agent Design

Based on the aforementioned flow, the conversational agent tries to comprehend the user’s intent. In this particular scenario, we have developed two APIs as discussed above, and thus we will primarily focus on two intents that cater to customer queries. These intents are FIXED_DEPOSIT_ENQUIRY and FIXED_DEPOSIT_MATURITY_ENQUIRY, which correspond to the fdenquiry/ and fdmaturity/ APIs, respectively.

Both APIs require distinct input data (payload), and as a result, two additional functions have been created. These functions aim to extract the entities from the customer query and prepare them for use in the API payload. The fdenquiry/ API necessitates tenor and initial_investment_amount for a successful request, while fdmaturity/ requires customer_id as the payload for a successful request. Therefore, it is critical to extract and process the necessary data from the customer query before initiating the API call.

The main loop of the conversational agent executes all the operations mentioned in the previous sections. Additionally, it attempts to retrieve the necessary data for the API call when the customer (user) fails to provide the requested information. Below is a screenshot illustrating a basic demonstration.

Full Example Code in GitHub

You can get the full code on my GitHub repository.

Thank you for reading this article. Consider following me on medium and subscribe to this post if you found its contents valuable and enlightening. Your support will be a source of inspiration for crafting future posts that are equally informative and engaging.

--

--