VOOZH about

URL: https://thenewstack.io/build-a-python-chatgpt-3-5-chatbot-in-10-minutes/

⇱ Build a Python + ChatGPT-3.5 Chatbot in 10 Minutes - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2025-05-02 14:00:24
Build a Python + ChatGPT-3.5 Chatbot in 10 Minutes
tutorial,
AI Engineering / Python / Software Development

Build a Python + ChatGPT-3.5 Chatbot in 10 Minutes

Learn to build a simple chatbot using Python and OpenAI's API in just minutes, with code examples that help beginners.
May 2nd, 2025 2:00pm by Jessica Wachtel
👁 Featued image for: Build a Python + ChatGPT-3.5 Chatbot in 10 Minutes
Featured image via Unsplash.

Today, I’m at peace with the existence of ChatGPT — but when it first launched, I was terrified. My mother, a fourth-grade teacher, promptly informed me (a career writer and technical content manager) that I’d soon be out of a job. Years later, still employed, I now strive to make ChatGPT my ally. Not only have I come to appreciate how helpful it can be, but I also figure it’s wise not to be enemy No. 1 when the robots take over.

That said, why am I rambling on about this? Well, understanding the enemy was a step forward in befriending it. Working with AI is an incredibly valuable skill. And this extends past just asking ChatGPT questions (which I now love). Learning how to code with or alongside the model is the next step in really harnessing the power of GPT.

I designed this tutorial to help beginners get started in understanding ChatGPT’s API and response logic. It won’t teach you how to build a fancy application on top of GPT, but it will help you understand how to write code that interacts with OpenAI’s (creators of ChatGPT) API. Since this is a no-frills tutorial, I built it using a Jupyter Notebook. Jupyter Notebooks are great for prototyping and testing ideas. If you are unfamiliar with Jupyter Notebooks, check out this guide on getting started.

Get Started With OpenAI

  1. Sign up at platform.openai.com.
  2. Access the API dashboard.
    • Go to your API keys page, click “Create new secret key” and copy the key immediately. You won’t be able to view it again later.
  3. Add billing information.
    • While OpenAI offers a free trial, I added a payment method. A $10 credit is usually enough to experiment with GPT-3.5.
    • Note: You’ll need either the free trial or active billing to use GPT-3.5.

Building the Chatbot With ChatGPT-3.5

Import Required Libraries and Dependencies

Paste the following into your first Jupyter Notebook cell:

`openai` lets you interact with OpenAI’s models, like GPT-3.5. ` python-dotenv` securely manages sensitive information like API keys, loading them from a `.env` file into your Python environment.

Import the Modules

Use the following code to import `openai`, `os` and `dotenv`, which help configure your environment and interact with the API:

Load Your API Key

Make sure you’ve created an API key in your OpenAI dashboard. You’ll now set up a `.env` file to store it securely:

Paste your actual API key where it says `sk-proj-api-key-here`. Next, load the key in your notebook:

Define the Chatbot Function

This is where we define how the chatbot communicates with OpenAI’s API and handles the conversation.

The method `openai.chat.completions.create()`is the latest API call designed specifically for interactive chat. This method allows real-time communication with GPT-3.5 by sending a list of messages and receiving a relevant response based on the ongoing conversation.

GPT models like GPT-3.5 are designed to retain memory during a session. This means they reference earlier parts of the conversation to provide more coherent and helpful responses. Our `chat_with_gpt` function accepts a list of messages, which serves as the ongoing dialogue history. This helps the assistant respond in context.

Each message in the list includes a `”role”` value to tell the model who the message is from:

  • `”user”` for the person asking questions
  • `”assistant”` for GPT responses
  • `”system”` sets behavior instructions (optional)

The `temperature` value controls the randomness or creativity of the assistant’s response. The scale ranges from 0 to 1, with 0 being the predictable end of responses and 1 being more random and creative. 0.7 is suggested as the best balance.

The API returns a response object. From there, we extract the assistant’s reply with `choices[0].message.content`, which refers to the actual text generated by GPT.

Run the Chat Loop

Now we’ll make the chatbot interactive so the user can have a continuous conversation with the model.

This section sets up an infinite loop, allowing the chatbot to keep running until the user types `exit` or `quit`. Infinite loops make the chatbot conversational and “aware” of past messages by keeping track of what’s already been said. This loop keeps the conversation going, while preserving context. That context allows GPT-3.5 to respond in more natural, meaningful ways.

Here’s what’s happening step by step:

  • The chatbot prompts the user for input using `input()`.
  • If the user types “exit” or “quit”, the loop breaks and the chatbot says goodbye.
  • If the user does not type “exit” or “quit”, `messages.append(…)` adds the user’s last message to the conversation history.
  • The function `chat_with_gpt(messages)` is called to get the assistant’s response.
  • The assistant’s reply is also added to the history using `messages.append(…)`.
  • Finally, the response is printed to the console so the user can read it.

Befriending the Robots

This project gives you a basic understanding of how to build a chatbot using GPT-3.5. While it’s just the beginning, you now know how to authenticate with the OpenAI API, maintain a conversation loop and use Jupyter Notebooks to test and run code. It’s a great first step toward more advanced AI development and befriending the robots.

TRENDING STORIES
Jessica Wachtel is a developer marketing writer at InfluxData where she creates content that helps make the world of time series data more understandable and accessible. Jessica has a background in software development and technical journalism.
Read more from Jessica Wachtel
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: OpenAI.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.