How to Develop Your First AI Chatbot Application with Python: A Beginners Comprehensive Guide
You’ve heard about it, AI, ChatGPT, Machine Learning, and how they are revolutionizing our world. So now you want to make your own AI App, but you don’t know anything about coding an AI, if that’s your case this article is meant for you, the beginners. So without further ado lets get started!
You’ve heard about it, AI, ChatGPT, Machine Learning, and how they are revolutionizing our world. So now you want to make your own AI App, but you don’t know anything about coding an AI, if that’s your case this article is meant for you, the beginners. So without further ado lets get started!
You’ve heard about it, AI, ChatGPT, Machine Learning, and how they are revolutionizing our world. So now you want to make your own AI App, but you don’t know anything about coding an AI, if that’s your case this article is meant for you, the beginners. So without further ado lets get started!
Table Of Contents:
Step #1: Understand The Basics
Step #2: Importing Modules
Step #3: Setting up the API Key
Step #4: Configuring a way to input questions
Step #5: Styling The Text
Step #6: Configuring the OpenAI request
Step #7: Configuring the Exit function
Step #8: Final Steps/Completed Code
Conclusion
Step #1: Understand the basics:
Our first step to building an AI is understanding what coding language we will be using & the basics of what we will be doing today. Today we will be looking & learning at
Python
OpenAI API
CLI’s(command-line-interfaces)
IDLE
Lets go over #1, Python. Python is a popular programming language known for its clean syntax, Python is known to have large community making it easier to learn. Python is great language for coding AI’s, it has all the popular tools & libraries for you to create your own AI.
Now lets go over #2, OpenAI API. An API stands for Application Programming Interface. An API Key is like a password. An API Key is required for you to use an API’s functionality’s. You can get a OpenAI Key at playground.openai.com by going into your settings. Once you get your API key at OpenAI be sure to note it down somewhere.
An IDLE is the integrated development environment for Python. The IDLE is the place where you input your Python Code. You can use any IDLE you want. Today I will be using the Replit Online/Cloud IDLE.
Finally lets go over #3, CLI’s. An CLI is basically a Text Based User Interface(UI), its used to run programs and manage files. A CLI is where you interact with the computer using lines of text.
Step #2: Importing Modules
Now that you’ve learnt the basics lets get coding. First we will be importing the modules that we need. In the main.py file of your IDE enter,
As you can see there are 2 different things we have imported,
The 1st import was OS, importing this gives you the access to use this module. We will be using this to import our OpenAI API key
The 2nd import was OpenAI, this gives you access to the OpenAI library, this module comes with various methods to make requests to OpenAI
Step #3: Setting up the API key:
Remember the API key that I told you to save earlier? This is where it comes into play.
The code above utilizes the OS module to retrieve an environment variable called OPENAI_API_KEY. it then assigns this value to an API key property in OpenAI.
Next, create a new file called .env and type your API key in the following format
Step #4: Configure a way to Input questions:
Once we have configured our API key we need a way to ask a question to the AI, To do this were going to create a variable called Request.
Next, we are going to use Pythons built in Input function to enter an input and assign it to the request value. Were also going to add a /n (newline character) at the end of our request so that every new input starts on a new line
After we will add a print statement to our code so our question gets printed on the console
Finally we will put a while True loop around the request variable so that the program does not end after every request and instead keep running
Step #5: Styling the Text:
Next were going to style our text a bit,
The \033[34m at the start of the text makes the text green, while the \033[0m at the end makes the text reset to white
Step #6: Configuring The OpenAI Request:
After configuring a way to input questions, we need a way to make a request to OpenAI. To do this we’re going to borrow some code from the OpenAI Docs. The code we will be borrowing is the Chat-Completion-Request.
Insert this code after the while True loop and before print(question). Your current code should look like this.
Now lets go through what we just added.
#1. We create a variable called completion. Inside the variable we then make a request to OpenAI’s chat-completion endpoint through the OpenAI library that we imported above. This request takes place with 2 parameters.
The 2 parameters are,
The ChatGPT model we will be using. In this case we will be using gpt-3.5-turbo. We will be using 3.5 turbo because it is the default ChatGPT model. The model is defined in this code snippet,
2. This next set of code will pass our messages as an input, the first message is system message. This prompt tells our AI what it will be doing, and what it will be used for, in this case it’s,
Next, we will have to remove print(request) as now we want an response from OpenAI, therefore we will replace the code with,
In this print statement we did 3 things:
We styled our print statement green
2. We accessed the response through the completion.choices[0].message.content
3. Add a n/ to add spacing to CLI
We then use the + symbol to to put all of these to a single print statement
Step #7: Configuring the exit function:
Our final step is to handle the exit function. This piece of code will stop the program whenever the keyword exit is entered into the console. To do this we will have to use an if statement. This if statement will then go between the while loop & completion variable.
To configure the exit function, we also have to use Python’s built in lower() function and call it on our request variable, which formats everything into a lowercase string. We then assign it to the exit function, making it so that when you enter, exit, the program print Goodbye! and stops.
Step #8: Final Steps/Completed Code:
Your completed code should now look like this,
Conclusion:
Congratulations! You build your very first AI App!, If you want to experiment with your brand new AI bot try changing what the bot does via the messages prompt. Have Fun!
If you want more of these tutorials be sure to drop a follow and wait for more articles!
Source Code: https://github.com/Rouler4wd/Python-ChatGPT
