DEV Community

Cover image for Creating a Generative AI Chatbot with Python
Juan Brendon Luna Juarez
Juan Brendon Luna Juarez

Posted on

Creating a Generative AI Chatbot with Python

AI models have become incredibly popular in the development of chatbots. With Python, it's easy to create such a chatbot using deep learning techniques and libraries like transformers, TensorFlow, and PyTorch. In this article, we’ll guide you step by step to build a simple generative AI chatbot using Python.

Why we use a AI Chatbot with Python?
Using an AI chatbot with Python offers several advantages, particularly in the areas of simplicity, flexibility, and access to powerful machine learning frameworks.

  • First step to start with ai chatbot development Install the necessary dependencies
pip install transformers torch

Enter fullscreen mode Exit fullscreen mode

Install virtualenv in console if you don’t have it already:

pip install virtualenv

Enter fullscreen mode Exit fullscreen mode
  • Create a virtual environment
virtualenv chatbot-env
Enter fullscreen mode Exit fullscreen mode
  • We need Activate the environment. I use Windows
chatbot-env\Scripts\activate

Enter fullscreen mode Exit fullscreen mode
  • Importing Required Libraries
from transformers import GPT2LMHeadModel, GPT2Tokenizer
import torch

Enter fullscreen mode Exit fullscreen mode
  • Loading Pre-trained GPT model
model_name = "gpt2"
model = GPT2LMHeadModel.from_pretrained(model_name)
tokenizer = GPT2Tokenizer.from_pretrained(model_name)

model.eval()

Enter fullscreen mode Exit fullscreen mode
  • Creating the Chatbot’s Response Generator
def generate_response(user_input):

    inputs = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")

    outputs = model.generate(inputs, max_length=150, num_return_sequences=1, no_repeat_ngram_size=2, temperature=0.7)

    response = tokenizer.decode(outputs[0], skip_special_tokens=True)

    return response

Enter fullscreen mode Exit fullscreen mode
  • Building the Chatbot Interface.

Finally, we’ll create a simple loop where the user can input text, and the chatbot will generate a response.

print("Hello! I am your chatbot. Type 'quit' to exit.")

while True:
    user_input = input("You: ")

    if user_input.lower() == "quit":
        break

    response = generate_response(user_input)
    print("Chatbot: ", response)

Enter fullscreen mode Exit fullscreen mode
  • Finally Testing the Chatbot!!
python chatbot.py

Enter fullscreen mode Exit fullscreen mode

Conclusions
We've shown you how to create a simple generative AI chatbot using Python. The chatbot uses a pre-trained model to generate responses based on user input. With some fine-tuning and improvements, you can create a more sophisticated and context-aware chatbot. Chatbots like this one can be used in a variety of applications, such as customer service, education, and entertainment.

Top comments (0)