Build a Simple AI Chatbot With Python and OpenAI

Huy Hoàng · Updated Nov 19, 2026

Chatbot AI Đơn Giản với Python và OpenAI: Hướng Dẫn Chi Tiết

Quick answer — build a simple AI chatbot with Python and OpenAI: (1) install the library: pip install openai; (2) get an API key from your OpenAI account and store it in an environment variable (never hard-code it); (3) call the Chat Completions API: create a client, send a list of messages (system/user roles) and read the response; (4) wrap it in a loop to keep chatting, storing the conversation history so the bot “remembers” context; (5) add a system prompt to set its role and tone; (6) handle errors and limit costs (cap tokens and requests). Next steps: add a web interface (Flask/Streamlit) or RAG to answer from your own documents. Keep your API key secret and monitor API costs. It’s a great starting point for automating customer support.

Build a simple AI chatbot with Python and OpenAI

AI chatbots are increasingly popular for improving communication and customer interaction, and building one is no longer difficult. This guide walks you through creating a simple chatbot with Python and OpenAI step by step.

What is an AI chatbot and why build one?

An AI chatbot is an application that talks with people through messages using artificial intelligence. Chatbots can automate many tasks, saving businesses time and resources.

  • Uses: customer service, e-commerce, answering questions, providing product information and more.
  • Benefits: businesses use chatbots to streamline work and improve customer experience; individuals can build one for fun or to help with daily tasks.

Set up your Python development environment

  • Install Python and pip: install Python from the official website; pip, Python’s package manager, is installed with it.
  • Create a virtual environment: run python -m venv env, then activate it with source env/bin/activate on macOS/Linux or env\Scripts\activate on Windows.

OpenAI and its API

OpenAI is an AI research company whose API lets developers integrate advanced language models into their applications.

  • The models: OpenAI’s large language models (the GPT family) let a chatbot understand questions and reply in natural language. The model line-up changes often, so check OpenAI’s documentation to choose one that balances quality and cost.
  • Get an API key: sign in to OpenAI’s API dashboard, create a key and store it in the OPENAI_API_KEY environment variable. Never put the key directly in your code or push it to GitHub.

Build the chatbot step by step

Step 1: Install the library: pip install openai.

Step 2: Create chatbot.py with the following code:

import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
MODEL = os.getenv("OPENAI_MODEL", "gpt-4o-mini")

messages = [{"role": "system", "content": "You are a helpful IT support assistant."}]

while True:
    user_input = input("You: ")
    if user_input.lower() in ("exit", "quit"):
        break
    messages.append({"role": "user", "content": user_input})
    response = client.chat.completions.create(model=MODEL, messages=messages, max_tokens=500)
    reply = response.choices[0].message.content
    messages.append({"role": "assistant", "content": reply})
    print("Bot:", reply)

Step 3: Run python chatbot.py and start chatting. The messages list stores the conversation so the bot remembers context; the system prompt sets its role and tone.

Extending your chatbot

  • Web interface: use Flask or Streamlit to build a browser chat UI.
  • Answers from your own documents (RAG): combine search over internal documents so the bot answers accurately from your business data.
  • Cost control: limit tokens, the amount of history kept and the number of requests; monitor usage in OpenAI’s dashboard.
  • Error handling: catch connection and rate-limit errors and retry sensibly.

Conclusion

With Python and the OpenAI API, you can build a simple AI chatbot in a few dozen lines of code — a great starting point for automating customer support or building an internal assistant. Keep your API key secret and keep an eye on costs.

Contact IT Systems Vietnam

Contact IT Systems at hotline 028.3995.03.59 or contact@itsystems.com.vn. Follow us on Facebook and YouTube.

Does this keep happening in your office?

The IT Systems Helpdesk logs requests as tickets, responds remotely within 30 minutes and comes on site in Ho Chi Minh City and Hanoi when needed.

IT Helpdesk services · Call +84 28 3995 0359

Request adviceChat on ZaloCall hotline