Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

Creating Intelligent Chatbots with ChatGPT

Save for later
  • 5 min read
  • 28 Sep 2023

article-image

Dive deeper into the world of AI innovation and stay ahead of the AI curve! Subscribe to our AI_Distilled newsletter for the latest insights and books. Don't miss out – sign up today!

Introduction

We are living in the era of Generative AI and Chatbots have evolved from simple rule-based systems to sophisticated AI-driven entities capable of holding intricate conversations. With tools like OpenAI's ChatGPT, creating a chatbot has never been more accessible. This article dives deep into how one can specialize these chatbots by fine-tuning them for specific industries and applications, using the Science QA dataset as an example.

Prerequisites

Before diving in, please ensure you have the necessary tools installed:

OpenAI - Create an account on OpenAI and Generate API keys

creating-intelligent-chatbots-with-chatgpt-img-0

  • Install Streamlit.
pip install streamlit
  • A dataset in CSV format for training

For this article, we have used the publicly available Science QA dataset for training and validation.

While OpenAI powers the chatbot's brain, Streamlit will serve as the platform to interact with it.

Understanding Fine-tuning

Fine-tuning is the process of training a pre-trained model on a new dataset to adapt to specific tasks. It's like teaching a general doctor to specialize in a field. The nuances and specific knowledge in the specialized field would aid and enhance to create your specialized dataset.

Fine-tuning with OpenAI

OpenAI's API simplifies the fine-tuning process. By providing your dataset, OpenAI trains your model to specialize in the subject of your data. 

First, we download the two CSV files from Science QA and save them in your project folder.

Next, we process these CSV files and convert them into JSONL format which is required by OpenAI.

import openai
import pandas as pd
# Use your Open AI access key
openai.api_key = "sk-**************************************"
# Load the training and validation datasets
train_df = pd.read_csv("science-train.csv")
val_df = pd.read_csv("science-val.csv")

def convert_dataset_to_jsonl(df, file_name):
df["conversation"] = df.apply(format_chat, axis=1)
   with open(file_name, 'w') as jsonl_file:
      for example in df["conversation"]:
         jsonl_file.write(example + '\n')
# Convert the training and validation datasets
convert_dataset_to_jsonl(train_df, "fine_tune_train_data.jsonl")
convert_dataset_to_jsonl(val_df, "fine_tune_val_data.jsonl")

After converting the datasets to JSONL format, we will upload these datasets to OpenAI for further consumption for model fine-tuning.

train = openai.File.create(
file=open("fine_tune_train_data.jsonl", "rb"),purpose='fine-tune',)
val = openai.File.create(
file=open("fine_tune_val_data.jsonl", "rb"),purpose='fine-tune',)
print(train) print(val)
After printing, save these files names for train and validation sets.
Next, we will create the fine-tuned model with your train and test data files. Pass the train and test data file names that we printed in step 3.
model = openai.FineTuningJob.create(
model = "gpt-3.5-turbo",
training_file = train_data,
validation_file = val_data,
suffix = "scienceqa")
print(model)

Now, you have successfully created your fine-tuned model. You can also check the status in your OpenAI account.

creating-intelligent-chatbots-with-chatgpt-img-1

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at €18.99/month. Cancel anytime

Building the Streamlit App

Streamlit is a game-changer for Python enthusiasts looking to deploy applications without the intricacies of web development. Integrating the fine-tuned model involves invoking OpenAI's API within the Streamlit interface. With customization features, tailor your app to resonate with the theme of your chatbot.

You will also add some styling to your web app to create a visually appealing platform for your fine-tuned model.

import streamlit as st
# Set OpenAI API key
openai.api_key = "sk-**********************"
# Use your fine-tuned model ID here:
FINE_TUNED_MODEL = "ft:gpt-3.5-turbo-****************"
# Message system setup
messages = [{
"role": "system",
"content": "You are an AI specialized in Science and Tech.. "},]

def get_chat():
return messages

def chatbot(input):
if input:
chat = get_chat()
chat.append({"role": "user", "content": input})
response = openai.ChatCompletion.create(
model="ft:gpt-3.5-turbo-*****************",
messages=chat,
max_tokens=150,
)
message = response['choices'][0]['message']['content']
chat.append({"role": "assistant", "content": message})
return message
# Custom vibrant styling
st.title('AI Chatbot Specialized in Science')
st.write("Ask me about Science and Technology)
# Sidebar for additional information or actions
with st.sidebar:
st.write("Instructions:")
st.write("1. Ask anything related to Science.")
st.write("2. Wait for the AI to provide an insightful answer!")

# Main Chat Interface
user_input = st.text_area("You: ", "")
if st.button("Ask"):
user_output = chatbot(user_input)
st.text_area("AI's Response:", user_output, height=200)

After saving the code, run the code using the following command-

strealmit run 'yourpyfilename.py'

Voila!! The app is ready!!

The app opens on a new web page in your browser. Here is how it looks- 

creating-intelligent-chatbots-with-chatgpt-img-2

creating-intelligent-chatbots-with-chatgpt-img-3

Deployment Considerations

Ensure your API keys remain confidential. As for scalability, remember each query costs money; caching frequent queries can be a cost-effective strategy.

Happy Fine Tuning!!

Conclusion

Fine-tuning a model like GPT-3.5 Turbo for specific industries can immensely boost its effectiveness in those domains. Using tools like Streamlit, the deployment becomes hassle-free. Experiment, iterate, and watch your specialized chatbot come to life!

Author Bio

Swagata Ashwani serves as a Principal Data Scientist at Boomi, where she leads the charge in deploying cutting-edge AI solutions, with a particular emphasis on Natural Language Processing (NLP). With a stellar track record in AI research, she is always on the lookout for the next state-of-the-art tool or technique to revolutionize the industry. Beyond her technical expertise, Swagata is a fervent advocate for women in tech. She believes in giving back to the community, regularly contributing to open-source initiatives that drive the democratization of technology.

Swagata's passion isn't limited to the world of AI; she is a nature enthusiast, often wandering beaches and indulging in the serenity they offer. With a cup of coffee in hand, she finds joy in the rhythm of dance and the tranquility of the great outdoors.