top of page

Welcome
to NumpyNinja Blogs

NumpyNinja: Blogs. Demystifying Tech,

One Blog at a Time.
Millions of views. 

Building a Python Chatbot with OpenAI: My First Real AI Project

May 1, 2025
4 min read

Why I Wanted to Try This


I've been teaching myself Python lately, and while tutorials are fine, I hit a point where I just needed to build something real. Not a calculator or a to-do list—I wanted something I’d actually use, even if just for fun.


That’s when I thought: hey, a chatbot! It seemed like a cool balance of being simple to start but still practical. Plus, I’d get to mess around with real AI, which felt exciting.


I wasn’t aiming for anything fancy. Just a tiny, working tool I could learn from—and maybe even show off a little.


What I Used


Here's what I had going:


  • Python 3.10

  • My OpenAI API key

  • The openai Python library

  • VS Code as my editor (because... why not?)


To get the library installed, all it takes is:


pip install openai


Super straightforward.


Getting the API Working


The first thing I had to figure out was how to actually talk to the OpenAI API. Turned out to be really simple:


import openai


# Drop your real key in here

openai.api_key = "sk-your-api-key-here"


That's it. Once that's in place, you’re ready to send messages.


First time creating API KEY, refer to this site https://platform.openai.com/settings/organization/api-keys


The Function That Talks to the Bot


Here’s the little function I wrote to handle the back-and-forth:


def talk_to_bot(user_input):

    response = openai.ChatCompletion.create(

        model="gpt-3.5-turbo",

        messages=[

            {"role": "system", "content": "You are a helpful assistant."},

            {"role": "user", "content": user_input}

        ]

    )

    return response[‘choices'][0]['message']['content']


It sends off what you say and grabs the bot's response. Nothing too fancy, but it works!


Making It Actually Chat


Next, I wanted to make this interactive. Something I could run in the terminal and talk to:


print("Chatbot ready. Type 'exit' to quit.\n")


while True:

    user_message = input("You: ")

    if user_message.lower() == 'exit':

        print("Bot: Goodbye.")

        break


    bot_reply = talk_to_bot(user_message)

    print("Bot:", bot_reply)


With this loop, I could talk to the bot like a normal conversation. It was kind of wild seeing it respond in real time!


Giving It a Memory (Sort Of)


After a few chats, I realized it was kind of annoying that the bot kept forgetting what we talked about. So I added a little memory using a list to store messages:


chat_log = [

    {"role": "system", "content": "You are a helpful assistant."}

]


while True:

    msg = input("You: ")

    if msg.lower() == "exit":

        print("Bot: Take care.")

        break


    chat_log.append({"role": "user", "content": msg})


    response = openai.ChatCompletion.create(

        model="gpt-3.5-turbo",

        messages=chat_log

    )


    reply = response['choices'][0]['message']['content']

    chat_log.append({"role": "assistant", "content": reply})


    print("Bot:", reply)


Sample Chat Session

Once I ran the script, I had this little conversation with the bot in the terminal. It gives a pretty good sense of how it responds and maintains context:



It’s surprisingly conversational, even with just a few lines of Python.


How It All Fits Together (Visual!)




I made a simple flow diagram to show how the pieces connect:


Here’s what’s happening in the background:


  1. User Input – You type a message in the terminal.

  2. Append to chat_log – That message gets added to a list that tracks the conversation.

  3. Send to OpenAI API – The full conversation (chat log) is sent to the API.

  4. Receive Response – The AI replies with a response based on the history.

  5. Append Reply to chat_log – The bot’s reply is saved to the same list.

  6. Display Bot Response – The response is printed back in the terminal.

  7. Repeat Loop – The process continues until you type “exit”.


It’s a simple flow, but keeping track of conversation history makes the chatbot feel much smarter!


Nothing too fancy, but it helped me visualize what was going on before I started adding extra features.


What I Took Away from This


This wasn't the biggest project in the world, but honestly? It felt great.


I got to use a real-world API, learned how to pass data back and forth, and saw how powerful these AI tools can be—without needing a ton of code. I even picked up some new habits around structuring functions and handling responses.


I’ve already got ideas for what to do next—maybe hook this up to a web interface, or log conversations somewhere. For now, I’ve got a working chatbot I built from scratch, and I’m pretty proud of that.


If you're looking for a first project with OpenAI's tools, this one’s a great place to start.


Why I Think This Is Worth Trying


To be honest, I wasn’t sure how useful a chatbot would be at first. But after building one—even a simple version—I started seeing how flexible they are. It’s not just for customer support. I could imagine one helping students, managing reminders, or even just being a personal study tool.


Also, actually talking to the bot I coded? That felt really cool. It’s one thing to write some code and get an output, but this was like building something that talks back. That was the “aha” moment for me.


What I’d Like to Try Next


I’m thinking about a few small upgrades:


  • Add a voice input/output layer with speech_recognition

  • Create a simple UI using tkinter or maybe Streamlit

  • Let the bot respond based on a custom personality or tone (fun, formal, sarcastic, etc.)


For now, though, I’ve got a chatbot that runs, remembers things, and gives decent answers. I’ll call that a win.

 
 

+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2025 by Numpy Ninja Inc.

  • Twitter
  • LinkedIn
bottom of page