How to Send User Input for Chatbots – ChatGPT AI Tutorial

In this tutorial, we will learn how to create a simple chatbot using the OpenAI library. Chatbots have become an essential part of many applications, providing assistance, answering user queries, and offering support. With the power of OpenAI, we can create intelligent conversations and provide a more interactive experience for users. We will go through the process of setting up the project, capturing user input, sending API calls, and displaying the responses. By the end of this tutorial, you will have a basic understanding of how to implement a chatbot using the OpenAI library.

Project Files

Download Project Files Here

CTA Small Image
FREE COURSES AT ZENVA
LEARN GAME DEVELOPMENT, PYTHON AND MORE
ACCESS FOR FREE
AVAILABLE FOR A LIMITED TIME ONLY

Creating the Project

In this lesson, we will be creating a simple chatbot using the OpenAI library.

First, create a new folder and file in Visual Studio Code. Name the file “app.py” and save it in the newly created folder.

Importing the Open AI Library

Next, we’re going to import the OpenAI library so that we have access to the API. We’ll also add the API key, which can be generated from the OpenAI website:

import openai

openai.api_key = 'YOUR_KEY_HERE'

Replace the YOUR_KEY_HERE string with your secret key.

Capturing User Input

In order to use the Chat Completion API, we will need to pass a messages list which will start empty. We’ll then capture user input and send the API call:

messages = []

# capture user input
user_input = input('Enter your prompt: ')

# prompt preparation
messages.append({'role': 'user', 'content': user_input})

# send the api call

# share response in console

To capture user input, we’re calling the inbuilt input function. We save this in a variable and add it to the messages list.

Sending the API Call

To send the API call, we have two required parameters, the model and the messages list as per the documentation (available at https://platform.openai.com/docs/api-reference/chat/create):

# send the api call
response = openai.ChatCompletion.create(model='gpt-3.5-turbo', messages=messages)

Note that by the time you watch this lesson, the model may change changed. Feel free to use the most recent one.

Printing the Response

Lastly, we’re going to print out the response. We will access the first choice from the response and get the content of that message:

# share response in console
print(response.choices[0].message)

To check the structure of the response (e.g., how we receive our responses back from the bot), you can go to the documentation link above:

response structure

Note that the role here is set to ‘assistant’ as it’s a response from the chatbot.

Running our code, we see that’s working as expected:

assistant role terminal

Conclusion

Congratulations! You’ve successfully built a simple chatbot using the OpenAI library. By now, you should have a good understanding of how to set up a project, interact with the library, and process user input to generate meaningful responses. Your new chatbot can serve as a foundation for more complex projects, such as adding additional features, training the model for different languages, or even integrating it with other platforms such as messaging apps, websites, or voice-activated systems.

There is so much more to explore in the world of AI and chatbot development. You might want to dive deeper into OpenAI’s offerings, learn more about natural language processing, or experiment with other AI frameworks. As you continue your learning journey, we hope this tutorial serves as a helpful starting point for integrating the power of OpenAI’s chatbot capabilities into your projects.

Want to learn more? Try our complete WEB-BASED CHATBOT WITH PYTHON AND CHATGPT course.

Did you come across any errors in this tutorial? Please let us know by completing this form and we’ll look into it!

FREE COURSES
Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.