Utilizing AWS RDS with Python PyMySQL for Efficient ChatGPT API Calls Management

Ayushman Pranav
4 min readDec 16, 2023

--

Do you Run a ChatGpt API Based business and want to reduce the API COSTS by 80% .

In today’s world of advanced AI and machine learning, the efficient management of API calls plays a crucial role in reducing operational costs and enhancing performance. This article explores a Python-based project that integrates AWS RDS with PyMySQL to optimize ChatGPT API usage through a smart caching system and similarity searches using TF-IDF and cosine similarity.

Fig 1. Project image

The general implemenation can using a CSV can be found on :-

AWS RDS complete version with caching can be found on :-

Introduction

Chatbot applications, particularly those powered by OpenAI’s ChatGPT, have witnessed widespread adoption across various industries. However, frequent API calls to these services can be expensive and affect application responsiveness. To address these challenges, a Python project was developed to reduce the frequency of API calls. This project implements a caching mechanism and utilizes similarity-based retrieval of responses, ultimately minimizing direct API calls to ChatGPT.

Fig 2. Project implementation

Project Overview

The project leverages AWS RDS (Amazon Web Services Relational Database Service) for database management and PyMySQL for interfacing with the database using Python. It employs TF-IDF (Term Frequency-Inverse Document Frequency) for assessing question similarity and cosine similarity for finding the most relevant pre-stored answers, thus reducing the dependency on direct API calls to ChatGPT.

Core Components and Implementation

1. AWS RDS and PyMySQL Integration

AWS RDS provides a scalable and cost-effective database solution. PyMySQL, a Python library, is used to interact with the RDS instance. This integration involves connecting to the database, executing SQL commands, and managing data transactions.

import pymysql.cursors
# Connect to the database
db = pymysql.connect(host='<AWS RDS URL>',
port=3306,
user='<UserID>',
password='<Password>',
db='<Database Name>',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
cursor = db.cursor()

2. Caching Mechanism

The caching mechanism stores question-answer pairs along with relevant metadata, such as MBTI type, learning style, and temperature settings. This data is saved in the AWS RDS, enabling quick retrieval of similar past queries.

qa_dict = {}
with open('question_answer.csv', 'r') as csv_file:
reader = csv.DictReader(csv_file)
for row in reader:
qa_dict[row['prompt']] = {
'returnstring': row['returnstring'],
'mbti': row['mbti'],
'learning': row['learning'],
'temperature': float(row['temperature'])
}

3. TF-IDF and Cosine Similarity for Query Matching

To find the most similar past query, the project employs TF-IDF for vectorizing the questions and cosine similarity for measuring the closeness between the new query and stored questions.

from SimilarText import TextSimilarity
text_similarity = TextSimilarity()
vectorstore = text_similarity.update_embeddings(qa_dict)

4. Reducing ChatGPT API Calls

When a new query is received, the system first checks for similarity with stored questions. If a similar question is found (based on a predefined similarity threshold), the stored answer is retrieved, bypassing the need for an API call.

promptsimilarityvalue = text_similarity.top_similar_prompts(prompt, vecstore)[0][1]
if promptsimilarityvalue > 0.2:
# Retrieve a similar conversation from the database
else:
# Make an API call to ChatGPT

5. Smart Caching and Performance Optimization

The project implements smart caching by storing recently used queries and their responses. This reduces the need for repeated API calls for the same or similar queries, enhancing both cost-efficiency and response time.

if prompt in qa_dict and qa_dict[prompt]['temperature'] == tem:
stored_data = qa_dict[prompt]
returnstring = Databasescore + " \nFound in Database \n" + stored_data['returnstring']
else:
# Perform similarity-based query search and response retrieval

Conclusion

By integrating AWS RDS with Python PyMySQL, this project demonstrates an effective way to manage ChatGPT API usage. The combination of the caching system, TF-IDF, and cosine similarity for query matching offers a cost-effective solution to handle repetitive queries and reduce the frequency of API calls. This approach not only saves costs but also enhances the response time of chatbot applications.

FAQs

Why is efficient API call management important for chatbot applications?

Efficient API call management helps reduce operational costs and improves the response time of chatbot applications, leading to a better user experience.

How does TF-IDF contribute to the project’s efficiency?

TF-IDF enables the vectorization of questions, making it easier to measure similarity between queries. This helps in identifying similar questions and retrieving pre-stored responses efficiently.

What are the key benefits of using AWS RDS in this project?

AWS RDS provides a scalable and reliable database solution, ensuring that the data required for query matching is readily accessible. It also helps in managing data transactions effectively.

Can this project be adapted for other chatbot applications?

Yes, the principles and components of this project, such as caching mechanisms and query similarity, can be adapted for other chatbot applications that rely on external APIs, making them more cost-efficient and responsive.

How can the project be further optimized for specific use cases?

The project can be optimized by fine-tuning the similarity threshold, implementing advanced caching strategies, and continuously updating the database with new questions and responses to enhance performance and accuracy.

--

--