Creating Custom Chatbots Using CSV Data with Python and OpenAI API

Miluska Romero
6 min readMar 17, 2024

--

Introduction

This step-by-step guide is designed to help you create a chatbot that utilizes your own CSV data for personalized interactions. By leveraging Python and the OpenAI API within Google Colab, you’ll develop a chatbot capable of engaging users with tailored responses based on the contents of your CSV file.

Prerequisites

  • Basic understanding of Python
  • An OpenAI API key
  • A CSV file with your data (for this example, soft_drinks.csv)

Google Colab Full Code

https://colab.research.google.com/drive/1bQRkSmduWg4wSkvttkSvgk3GMIW8ef8z?usp=sharing

Step-by-Step Guide

Step 1: Setting Up Google Colab

  • Access Google Colab: Visit Google Colab and sign in.
  • Create a New Notebook: Start a new project by clicking ‘New Notebook’.

Step 2: Preparing the Environment

  • Install OpenAI Library: In a new cell, install the openai library using pip.
!pip install openai==0.28
  • Import Necessary Libraries: Import Python libraries required for the project.
import openai

Let’s incorporate the suggested changes for a more secure way to handle the OpenAI API key within the Google Colab environment. This adjustment ensures that the API key is securely retrieved from the user’s Colab session data, minimizing the risk of accidental exposure.

Step 3: Acquiring and Setting the OpenAI API Key

  • Get Your OpenAI API Key: If you haven’t already, visit OpenAI, sign up for an account, and generate your API key.

Securely Add Your API Key to Colab:

  • First, securely store your OpenAI API key in Google Colab’s session data for use in the notebook. This can be done by using the userdata feature to set and get session-specific information without hardcoding sensitive data into your notebook.
from google.colab import userdata

# Retrieve your OpenAI API key
OPENAI_KEY = userdata.get('openai-api-key')
openai.api_key = OPENAI_KEY

Step 4: Loading and Preparing Your CSV Data

The with open statement is crucial here. It's a context manager that safely opens your file for reading and automatically closes it once the block of code is executed, ensuring efficient resource management and preventing file corruption or data leaks.

  • Upload Your CSV File: Use the file upload feature in Colab to add your soft_drinks.csv.
  • Read the CSV Data
# The 'with open' statement ensures safe and efficient file handling
with open("soft_drinks.csv") as file:
product_data = file.read()

This step reads your CSV file into a variable, making its contents available for the chatbot to reference in conversations.

Step 5: Define Your Chatbot’s Rules and Context

Draft interaction rules and combine them with your CSV data to form the chatbot’s knowledge base:

# Initialize the context with interaction rules and product data
context = []

# Define the chatbot's interaction rules here, including how it should greet users, provide product information, manage order inquiries, and handle payment processing.

rules = """
You are a virtual waiter in a soft drink café. \
Be cordial at all times. \
If customers haven't chosen yet, ask what they would like to drink. \
Offer recommendations if customers are undecided between the soft drinks available: Coca Cola, Pepsi, Sprite, Fanta, and 7 Up. \
Based on the customer's preference or past orders, you may suggest a specific drink they might enjoy. \
After selecting a drink, confirm their choice and ask if they'd like to add another beverage to their order. \
When asked for the bill, provide a summary of the selected drinks, including the quantity, name, price per item, and the total amount due. \
Below is your list of soft drinks available. \
"""

context.append({'role': 'system', 'content': f"""{rules} {product_data}"""})

Step 6: Craft Conversation Management Functions

Implement functions to handle chatbot responses using the OpenAI API.

In this step, we introduce two key functions: fetch_messages and refresh_conversation. Here’s what each function does and how it contributes to the chatbot's functionality:

1.- fetch_messages:

# Function to fetch messages from the OpenAI Chat model
def fetch_messages(messages, model="gpt-3.5-turbo", temperature=0):
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature,
)
return response.choices[0].message["content"]

Parameters:

  • messages: A list that represents the conversation history, including both the user’s questions and the chatbot’s responses. This history helps the model understand the context of the conversation.
  • model: Specifies which OpenAI GPT model to use. gpt-4 is chosen for its advanced understanding and response capabilities.
  • temperature: Controls the randomness of the response. A lower temperature makes the response more predictable and conservative. We use 0 for consistent, reliable answers.
    Process:

The function calls openai.ChatCompletion.create(), passing in the conversation history and other parameters. This API call asks OpenAI to process the inputs and generate a suitable response based on the data it has been trained on and the specific instructions (rules and product data) we’ve provided.

Output: It returns the generated response, which is then used to continue the conversation with the user.

2.- refresh_conversation:

This function updates the conversation context with new inputs from the user and outputs from the chatbot. It ensures the chatbot can follow along with the conversation, maintaining coherence and relevance in its responses.

# Function to refresh and update the conversation context based on user input

def refresh_conversation(chat):
context.append({'role': 'user', 'content': f"{chat}"})
response = fetch_messages(context, temperature=0.7)
context.append({'role': 'assistant', 'content': f"{response}"})
print(response)

Parameters:

  • chat: The latest message from the user. This could be a question, a comment, or any input that the chatbot needs to respond to.

Process:

  • The user’s latest message is appended to the context list as a new entry with the role ‘user’. This update signifies a new piece of information added to the conversation history.
  • fetch_messages is called with the updated context to generate a response. This response considers the entire conversation history, including the latest input.
  • The generated response is then appended back to the context list as an entry with the role ‘assistant’, indicating it’s a response from the chatbot.

Outcome: This updated context helps maintain a flowing, coherent conversation. It ensures that each response from the chatbot is informed by the entire conversation, not just the latest input.

Note: Through these functions, our chatbot becomes capable of dynamic interactions. It can understand queries, refer to its rules and the product data from the CSV file, and generate appropriate responses. This step is vital in bringing the chatbot to life, transforming it from a static program to an interactive conversational agent.

Step 7: Engage with Your Chatbot

Initiate your chatbot and start interacting:

# Define the main function to start our chatbot.
def main():
# Start an infinite loop to keep the chatbot running.
while True:
# Ask the user to enter a message or 'exit' to leave.
message = input("Please enter a message (or 'exit' to leave): ")

# Check if the message is 'exit', to end the loop and shut down the chatbot.
if message.lower() == 'exit':
break # Breaks the loop, ending the function and closing the chatbot.

# If the message isn't 'exit', continue by updating the conversation with the new message.
refresh_conversation(message)

# Check if this script is the main program being executed.
if __name__ == '__main__':
main() # If it is, run the main() function to start the chatbot.

This function is designed to run an infinite loop, continually waiting for user input and generating responses until the user decides to exit the conversation.

Conclusion

You’ve now created a functional chatbot in Google Colab that utilizes Python, the OpenAI API, and your own CSV data for dynamic conversations. This foundation allows for extensive customization and scalability, depending on your project needs. Experiment with different datasets, refine your chatbot’s rules, and explore the vast capabilities of OpenAI’s GPT models to enhance your chatbot’s interactions.

--

--