Anasayfa / Software / How to Build a Simple AI Chatbot Using Python and the OpenAI API

How to Build a Simple AI Chatbot Using Python and the OpenAI API

Python AI chatbot

Artificial intelligence is no longer a futuristic concept – it’s a tool you can start using today. In this guide we’ll walk you through creating a simple, yet functional, AI chatbot with Python and the OpenAI API. By the end you’ll have a command‑line bot that can hold a basic conversation, remember context, and be easily expanded into a web or messaging app. The steps are written for developers who are comfortable with Python basics but are new to working with language‑model APIs.

What You’ll Need

  • Python 3.8+ installed on your machine
  • An OpenAI account with API access
  • A text editor or IDE (VS Code, PyCharm, etc.)
  • Basic knowledge of virtual environments
  • Internet connection for API calls

Step 1: Set Up a Clean Python Environment

Creating an isolated environment prevents package conflicts. Open a terminal and run:

python -m venv chatbot-env
source chatbot-env/bin/activate   # macOS/Linux
chatbot-envScriptsactivate      # Windows

After activation, upgrade pip to avoid installation hiccups:

pip install --upgrade pip

Step 2: Obtain Your OpenAI API Key

Log in to OpenAI’s platform and click “Create new secret key”. Copy the key – you’ll need it in the next step. **Never** commit this key to a public repository.

Step 3: Install the OpenAI Python Library

With the virtual environment active, install the official client:

pip install openai

We’ll also install python-dotenv to keep the API key out of the source code:

pip install python-dotenv

Step 4: Create a Minimal Chat Script

Create a new file called chatbot.py and add the following boilerplate:

import os
from dotenv import load_dotenv
import openai

load_dotenv()  # Loads variables from .env into the environment
openai.api_key = os.getenv("OPENAI_API_KEY")

def ask_gpt(prompt):
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": prompt}],
        max_tokens=150,
        n=1,
        temperature=0.7,
    )
    return response.choices[0].message["content"].strip()

if __name__ == "__main__":
    while True:
        user_input = input("You: ")
        if user_input.lower() in {"exit", "quit"}:
            print("Bot: Goodbye!")
            break
        reply = ask_gpt(user_input)
        print(f"Bot: {reply}")

Next, create a .env file in the same directory with the line:

OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxx

Replace the placeholder with the key you copied earlier.

Step 5: Add Conversation Memory

The script above treats each user message as an isolated request. To make the bot remember the flow, keep a list of messages and pass the whole history to the API.

conversation = []

while True:
    user_input = input("You: ")
    if user_input.lower() in {"exit", "quit"}:
        print("Bot: Bye!")
        break
    conversation.append({"role": "user", "content": user_input})
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=conversation,
        max_tokens=150,
        temperature=0.7,
    )
    bot_reply = response.choices[0].message["content"].strip()
    conversation.append({"role": "assistant", "content": bot_reply})
    print(f"Bot: {bot_reply}")

This simple memory window lets the model reference earlier exchanges, producing more coherent dialogues. For production use you’d prune the list to stay under the token limit.

Step 6: Run, Test, and Iterate

Save the file and run it:

python chatbot.py

Try a few questions – “What’s the weather like in Tokyo?” or “Explain quantum entanglement in simple terms.” Notice how the bot follows the conversation thread. If the responses feel off, tweak the temperature (higher = more creative) or max_tokens (longer answers).

Common Mistakes to Avoid

1. **Hard‑coding the API key** – This exposes your credential and can lead to accidental billing. Always use environment variables or a secret manager.
2. **Ignoring rate limits** – The free tier has a request‑per‑minute cap. If you hit a 429 error, add time.sleep(1) between calls or upgrade your plan.
3. **Sending overly long histories** – The model has a context window (≈4,000 tokens for gpt‑3.5‑turbo). Forgetting to truncate old messages will cause “context length exceeded” errors.
4. **Using the wrong model name** – “davinci” is a completion endpoint, not a chat endpoint. Always use “gpt‑3.5‑turbo” (or newer) for ChatCompletion calls.
5. **Neglecting error handling** – Network glitches or authentication failures raise exceptions. Wrap API calls in try/except blocks to keep the bot alive.

Tips and Tricks

• **System prompts** – Add a first message with role: "system" to set the bot’s personality, e.g., {"role":"system","content":"You are a friendly tech assistant."}.
• **Streaming responses** – For a more interactive feel, enable stream=True and print tokens as they arrive.
• **Logging** – Store each conversation in a JSON file for later analysis or fine‑tuning.
• **Deploying** – Once happy, wrap the script in a Flask or FastAPI endpoint and expose it via a webhook for Slack, Discord, or a web UI.
• **Cost control** – Use the max_tokens parameter wisely; a typical 150‑token reply costs only a few cents per thousand calls.

Frequently Asked Questions

Do I need a credit card to use the OpenAI API?

Yes. Even the free trial requires a valid payment method. After the trial you are billed per token usage, but you can set hard limits in the OpenAI dashboard.

Can I run the chatbot offline?

Not with OpenAI’s hosted models. They run in the cloud. For offline use you’d need a locally hosted model such as LLaMA or a distilled version, which involves a completely different setup.

How do I keep the conversation private?

OpenAI stores API data for a limited time to improve services. If you have strict privacy requirements, review OpenAI’s data‑usage policy and consider encrypting any logs before storage.

Conclusion

Building an AI chatbot with Python and the OpenAI API is straightforward: set up a clean environment, secure your API key, install the client library, and write a few lines of code. By adding a conversation buffer, system prompts, and proper error handling you turn a simple script into a robust conversational agent ready for further integration. Experiment with temperature, token limits, and custom prompts to tailor the bot to your niche, and you’ll have a solid foundation for more advanced projects like voice assistants, customer‑support bots, or interactive tutorials.

Photo by Mohamed Nohassi on Unsplash

Etiketlendi: