Use Python to Create an AI Chatbot

Gavin
Gavin
3 min read •

In this blog, you’ll learn how to build your own terminal-based ChatGPT using Python, and even feed it live data from a Google Sheet.

ChatGPT is a powerful tool, but sometimes you want something a bit more tailored; maybe a custom chatbot you can run from Terminal, or a way to plug in your own live data.

In this guide, I’ll show you how to:

  • Build a simple ChatGPT-style chatbot in your terminal using Python and OpenAI’s API.

  • Customise your chatbot’s behaviour using a system prompt.

  • Feed live data from a Google Sheet directly into the bot using CSV Getter.

If you prefer watching instead of reading, I cover everything step-by-step in this YouTube video:

Part 1: Python and OpenAI: Basic Terminal Chat

Below is a simple Python script that lets you chat with OpenAI directly from your terminal. This is a great way to understand how the OpenAI Python package works and begin customising your own chatbot experience.

import openai

OPENAI_API_KEY = "Your OPEN AI API Key"

openai.api_key = OPENAI_API_KEY

messages = []

while True:
    user_input = input("You: ")

    if user_input.lower() == "exit":
        print("Ending the conversation. Goodbye!")
        break

    messages.append({"role": "user", "content": user_input})

    try:
        response = openai.chat.completions.create(
            model="gpt-4o-mini",
            messages=messages
        )

        assistant_reply = response.choices[0].message.content
        print(f"Servicebot: {assistant_reply}")

        messages.append({"role": "assistant", "content": assistant_reply})

    except Exception as e:
        print(f"An error occurred: {e}")
        break

What’s Happening Here?

This code creates a simple chatbot using OpenAI's API. It takes user input in a loop and sends it to the GPT-4o-mini model. The model replies with a response, which is printed to the screen. All messages are stored to keep the conversation flowing naturally. Typing "exit" ends the chat.

Tips

  • Install the OpenAI package with pip install openai

  • Replace "Your OPEN AI API Key" with your actual API key from OpenAI

  • gpt-4o-mini is fast, efficient, and great for lightweight chatbot builds

  • Keep your API key private and secure

  • Exit the chat any time by typing exit

  • You can shape the bot’s tone or focus by adding a system message at the start of the messages list

Example Output (Default Bot)

Here’s what happens when you ask the default chatbot about your service rates:

You: What are your rates? 

Servicebot: I don’t have rates or pricing because I'm a free service provided by OpenAI. You can use me without cost for assistance, information, and conversation. If you have specific questions or need help with something, feel free to ask!

As expected, it gives a generic answer. It doesn’t know anything about your business yet. Let’s change that by giving it context and live data.

Part 2: System Prompt and Live Data from Google Sheets

Now we’ll upgrade the chatbot with real knowledge, using data from a Google Sheet. We’ll pull in the latest info using CSV Getter, a snappy tool that lets your code access live data directly from your sheet — even private ones.

CSV Getter acts as a lightweight API layer for your sheet. It keeps your data where it belongs, while letting your application query and use it in real time. No need to mess with the official Google Sheets API.

The Code

import requests
import openai

data = requests.get('https://api.csvgetter.com/YlqvW8PFN050Zgjae8CN?type=json_records')

my_rates = data.text

OPENAI_API_KEY = "Your Open AI API Key"

openai.api_key = OPENAI_API_KEY

context = "My service rates are " + my_rates

messages = [
    {"role": "system", "content": context}
]

while True:
    user_input = input("You: ")

    if user_input.lower() == "exit":
        print("Ending the conversation. Goodbye!")
        break

    messages.append({"role": "user", "content": user_input})

    try:
        response = openai.chat.completions.create(
            model="gpt-4o-mini",
            messages=messages
        )

        assistant_reply = response.choices[0].message.content
        print(f"Servicebot: {assistant_reply}")

        messages.append({"role": "assistant", "content": assistant_reply})

    except Exception as e:
        print(f"An error occurred: {e}")
        break

The Google Sheet

With CSV Getter, your Google Sheet becomes a live, queryable data source. The link you use can be made private and secure, meaning you control what gets exposed, and what doesn’t.

This setup avoids hardcoding information into your scripts and allows updates directly from the spreadsheet. It's clean, efficient, and flexible.

Example Output (With Custom Rates)

You: What are your rates? 

Servicebot: My service rates are as follows:

- Service A: $200 (Really good)
- Service B: $300 (Pretty bad)
- Service C: $200 (OK)

The chatbot now responds with specific service information, straight from your Google Sheet. This turns a general-purpose model into a context-aware assistant for your business or project.

Summary

With a few lines of Python and the help of OpenAI and CSV Getter, you can build a customised chatbot that reflects your own knowledge and data. The terminal interface gives you full control, while tools like CSV Getter make it easy to connect to live, editable sources like Google Sheets.