Build Your First AI Chatbot Using Python (Lesson 11)

What You Will Learn

In this lesson, you’ll learn:

  • How to build an AI chatbot using Python
  • How to accept input from the user
  • How to send the user’s message to an AI model
  • How to display the AI’s reply
  • How a simple AI chatbot works

What Is an AI Chatbot?

An AI chatbot is a program that accepts a user’s message, sends it to an AI model, and displays the generated response. The basic flow looks like this:

User → Python Program → AI Model → Python Program → User

This is the foundation of most modern AI applications, including customer support bots, coding assistants, and virtual assistants.


Step 1: Open Your Project

Open your AI-Assistant project in VS Code. Open the file:

app.py


Step 2: Import the OpenAI SDK

from openai import OpenAI


Step 3: Create the Client

Replace "YOUR_API_KEY" with your own API key.

client = OpenAI(
    api_key="YOUR_API_KEY"
)


Step 4: Read the User’s Message

Use Python’s input() function.

user_message = input("You: ")

Whatever the user types will be stored in the user_message variable.

Example:

You: What is Python?


Step 5: Send the Message to the AI

response = client.responses.create(
    model="gpt-5",
    input=user_message
)

The program sends the user’s message directly to the AI model.


Step 6: Display the AI Response

print("\nAI:", response.output_text)


Complete Program

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY"
)

user_message = input("You: ")

response = client.responses.create(
    model="gpt-5",
    input=user_message
)

print("\nAI:", response.output_text)


Sample Output

You: What is Machine Learning?

AI:
Machine Learning is a branch of Artificial Intelligence that enables computers to learn patterns from data and make predictions or decisions without being explicitly programmed.

Your output may be different because AI generates responses dynamically.


How the Program Works

User Input

user_message = input("You: ")

Reads the message entered by the user.


AI Request

response = client.responses.create(...)

Sends the user’s message to the AI model.


AI Response

print(response.output_text)

Displays the generated response.


Current Limitation

This chatbot answers only one question and then exits.

Example:

You: Hello
AI: Hello! How can I help you today?

Program Ends

In the next lesson, we’ll improve it so you can chat continuously without restarting the program.


Common Errors

Invalid API Key

Verify that your API key is correct.


No Internet Connection

The program requires an internet connection to communicate with the AI model.


Empty Input

If the user presses Enter without typing anything, the AI may return an unexpected response.

We’ll handle this later in the series.


Key Points

  • input() reads the user’s message.
  • The message is sent to the AI model.
  • The AI generates a reply.
  • The response is displayed using print().

Mini Quiz

1. Which function reads user input?

A. print()

B. input()

C. open()

D. read()

Answer: B


2. Where is the user’s message stored?

A. response

B. client

C. user_message

D. output

Answer: C


3. Which line sends the prompt to the AI?

client.responses.create(...)

Answer: Correct


4. Which line displays the AI’s response?

print(response.output_text)

Answer: Correct


5. How many questions can this chatbot answer?

A. Unlimited

B. One

C. Ten

D. Five

Answer: B


Practice Exercise

Run the chatbot and ask different questions, such as:

  • What is Python?
  • Explain recursion in simple words.
  • Suggest three AI project ideas.
  • Write a motivational quote for programmers.
  • Explain variables with an example.

Observe how the AI responds to different prompts.


Lesson Summary

Congratulations!

You have built your first AI chatbot using Python. The chatbot accepts a user’s message, sends it to an AI model, and displays the generated response. Although it handles only one question, you’ve now built the core of an AI-powered application.


Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top