Build a Interactive Chatbot with OpenAI's GPT-3.5 Turbo!

Table of contents

No heading

No headings in the article.

Introduction

Learn how to create an interactive chatbot using OpenAI's GPT-3.5 Turbo model in this step-by-step tutorial. Chatbots have become an essential tool in many industries, providing accurate and informative responses to user queries. With OpenAI's GPT-3.5 Turbo, you can create a powerful language model capable of handling a wide range of tasks, including question-answering and conversation. Follow my guide to develop your own chatbot and discover the ease and effectiveness of using OpenAI's GPT-3.5 Turbo in real-world applications

Step 1: Importing Libraries

import os
import openai

Start by importing the necessary libraries. os is a standard Python library used for interacting with the operating system, while openai is the official library for interacting with the OpenAI API.

Step 2: Authenticating API Key

openai.api_key = os.getenv('OPENAI_API_KEY')

Before you can access the OpenAI API, you must authenticate using your API key. Store your key in an environment variable named 'OPENAI_API_KEY' and use os.getenv to retrieve it. This method ensures that your API key remains secure and hidden from other users.

Step 3: Creating a Continuous Chatbot Loop

while True:

The while True loop enables the chatbot to answer multiple questions in a single session, providing an interactive experience for the user.

Step 4: Accepting User Input

  question = input("\033[34mWhat is your question? \n\033[0m")

Use the input function to receive a question from the user. The text enclosed in the escape sequences \033[34m and \033[0m is displayed in blue, providing a visual distinction for the user input.

Step 5: Exiting the Chatbot

  if question.lower() == 'exit':
    print("\033[31mGoodbye!\033[0m")
    break

Provide an exit mechanism for the chatbot by checking if the user has entered the keyword 'exit'. The escape sequence \033[31m displays the 'Goodbye!' message in red before terminating the loop.

Step 6: Interacting with OpenAI API

  completion = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[{
      "role":
      "system",
      "content":
      "You are a helpful assistant. Answer the given question."
    }, {
      "role": "user",
      "content": question
    }])

Use the openai.ChatCompletion.create function to send the user's question to the OpenAI API. Select the GPT-3.5 Turbo model and provide an array of messages as input. The first message has a 'system' role and instructs the model to act as a helpful assistant, while the second message has a 'user' role and contains the actual question.

Step 7: Displaying the Chatbot's Response

  print("\033[32m" + completion.choices[0].message.content + "\n")

The OpenAI API returns an array of 'choices', each containing a 'message' object. In this case, we only need the first choice. Access the message's 'content' attribute and print the response in green (using the escape sequence \033[32m) to distinguish it from user input.

Full Code Snippet

import os
import openai

openai.api_key = 'os.getenv('OPENAI_API_KEY')'

while True:
  question = input("\033[34mWhat is your question? \n\033[0m")

  if question.lower() == 'exit':
    print("\033[31mGoodbye!\033[0m")
    break

  completion = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[{
      "role":
      "system",
      "content":
      "You are a helpful assistant. Answer the given question."
    }, {
      "role": "user",
      "content": question
    }])

  print("\033[32m" + completion.choices[0].message.content + "\n")

Output of the code:

Conclusion

In this tutorial, we covered the process of creating an interactive AI chatbot using OpenAI's GPT-3.5 Turbo. By following these steps, you can develop a chatbot capable of answering various questions while providing an engaging user experience. This tutorial highlights the ease and effectiveness of using OpenAI's GPT-3.5 Turbo in real-world applications, and it serves as a foundation for future projects that require AI-driven language understanding and generation. Start building your own chatbot today and discover the power of OpenAI's GPT-3.5 Turbo!

Did you find this article valuable?

Support Roy Rebello by becoming a sponsor. Any amount is appreciated!