Creating a Chatbot with Generative AI: With ChatGPT(gpt-3.5-turbo) & User Defined Data for Project: DearDad

Ray Islam, PhD
3 min readOct 23, 2023

--

I’ve constructed a simple chatbot utilizing GPT-3.5-Turbo, enriched with User Defined Data. Our dataset, named Project: Dear Dad, comprises text entries exploring the intricate father-son relationship dynamics. I’m excited to announce a forthcoming series of videos delving into the realm of Generative AI, drawing from my hands-on experience with GenAI, spanning both Large Language Models (LLMs) and computer vision. Project: Dear Dad will serve as a cornerstone for demonstrations in this series, offering insights and applications of generative AI in understanding and simulating human relationships. Stay tuned!

Here is the link to the corresponding video channel: https://www.youtube.com/@rayisl5382/videos

Slides on chatbot (gpt-3.5-turbo) with user defined data

Python Code for the Chatbot [ChatGPT (gpt-3.5-turbo) and user defined data]: https://github.com/drrayislam/generativeAI/blob/4549f75bba0f93dab5b82f954f6b1b4357d2e6f3/Chatbot-ChatGPT-gpt-3.5-Turbo-Dear_Dad%20-%20with_own_data.py

#Intstall ipywidgets for interaction with Jupyter Notebook
!pip install openai ipywidgets

#import libraries
import os
import openai
import ipywidgets as widgets

#IPython : IPython is a command shell for interactive computing in multiple programming languages
#display: This is a function in the IPython.display module that can display elements graphically in a Jupyter Notebook env.
# clear_output: This function is used to clear the output of a cell in a Jupyter Notebook. It is helpful for updating cell outputs
from IPython.display import display, clear_output

#check directory
import os
print(os.getcwd())
C:\Users\ray\Documents\Knowledge\daddy

# Change the current working directory
os.chdir('C:/Users/ray/Documents/Knowledge/daddy')

#check again
import os
print(os.getcwd())
C:\Users\ray\Documents\Knowledge\daddy

# Get all .txt files in the current directory
txt_files = [f for f in os.listdir() if f.endswith('.txt')]

# Create a dictionary to store the content of each text file
file_contents = {}

# Read and print the content of each text file
for txt_file in txt_files:
with open(txt_file, 'r') as file:
content = file.read()
file_contents[txt_file] = content
print(f"Contents of {txt_file}:\n{content}\n" + "="*50)

#Combine all text file contents into one string
local_data = "\n".join(file_contents.values())

#Define a function to interact with GPT-3.5 Turbo:

API_KEY = '..........8eRo5lW4e................'
openai.api_key = API_KEY

def ask_gpt_turbo(question):
#to construct the message
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": local_data},
{"role": "user", "content": question}
]

response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages
)

return response['choices'][0]['message']['content'].strip()

#Create interactive widgets for the chatbot:
input_area = widgets.Textarea(
value='',
placeholder='Type your question here...',
description='Question:',
disabled=False,
layout=widgets.Layout(width='50%')
)

send_button = widgets.Button(description="Send")

output_area = widgets.Output(layout={'border': '1px solid black'})

def on_button_click(b):
with output_area:
question = input_area.value
answer = ask_gpt_turbo(question)
print(f"Dr. Ray: {question}\nGPT-3.5 Turbo: {answer}\n" + "-"*50)

send_button.on_click(on_button_click)

display(input_area, send_button, output_area)

About the Author

Dr. Ray Islam is a strategist and an expert in Generative AI. He also teaches Generative AI (NLP) at George Mason University, Fairfax, VA. and Cyber Security at the University of Maryland, College Park. With a distinguished career, he has held leadership roles in AI and ML at notable firms like Deloitte, Raytheon, Lockheed Martin and others. He has consulted for The National Aeronautics and Space Administration (NASA), The General Services Administration (GSA), Berkshire Hathaway, and American Institutes for Research (AIR) among others. In his leadership role at Deloitte, he spearheaded strategies for the Generative AI and Model Foundry team. Dr. Ray boasts a PhD from the University of Maryland, College Park, and holds degrees from Canada, Scotland, England. He is an associate editor of Journal of Prognostics and Health Management (IJPHM), published by Carleton University, Canada, and a reviewer for the journal of Reliability Engineering and System Safety, published by Elsevier. His primary research areas are Generative AI, XAI, and AI ethics.

--

--

Ray Islam, PhD
Ray Islam, PhD

Written by Ray Islam, PhD

PhD in ML | AI Scientist | Professor | Author | Speaker | Reviewer: ICLR; RESS; JPHM | Member: AAAI | Marquis Who's Who | PhD | MASc | MSc | MBA | BSc. Eng.

No responses yet