ChatGPT is a chatbot model developed by OpenAI. It is a variant of the GPT (Generative Pre-trained Transformer) language model that has been fine-tuned for the task of conversational language modeling.
This means that it has been trained on a large dataset of human conversations in order to learn how to generate natural and coherent responses to user inputs in a conversation.
ChatGPT is designed to be able to carry out a wide range of conversation types, including chit-chat, information-seeking, and task-oriented dialogue.
ChatGPT is based on the GPT language model, which is a state-of-the-art model for natural language processing (NLP) tasks. By fine-tuning the GPT model for the specific task of conversational language modeling, ChatGPT is able to generate highly natural and coherent responses to user inputs in a conversation.
This makes it a valuable resource for building chatbots and other conversational AI systems that can engage in natural and effective dialogue with users.
In addition to its practical applications, ChatGPT is also important from a research perspective because it represents a significant advancement in the field of natural language processing and conversational AI.
The development of chatbot models like ChatGPT helps to advance the state of the art in these areas and paves the way for the development of more sophisticated and capable conversational AI systems in the future.
ChatGPT went live on November 30th, 2022, and we’ve seen tons of articles, YouTube and Instagram videos, from people coming from different professional backgrounds, employing ChatGPT and the tool does its miracles.
In the previous article, I asked ChatGPT to create a $1000 web application, and here’s what happened.
So, in today’s article, I am going to show you how to create a short 20 lines Python script that will enable you to communicate through the terminal/command line, with ChatGPT, so you can get your answers in your own customizable way.
Python Script that Uses ChatGPT
So, let’s start. First thing you need to have Python on your machine. If you don’t have it, you can download it, and install it from Python’s official page.
After this, you can open your favorite IDE (Pycharm, VS Code, Notepad…). Install the virtual environment, and activate it (Pycharm does the installation automatically).
The next step is to install the openai library, which you can do using pip install openai.
Next, you will need to obtain an API key for the ChatGPT API. You can sign up for an API key at the OpenAI website.
Once you have your API key, you can use the following code to start a conversation with ChatGPT:
As with every Python console application, the main action happens in the main part. There, we set the private key we’ve obtained from the OpenAI website. Keep in mind, DO NOT SHARE THIS KEY WITH ANYONE.
After that, we have the message that signals us to put our question in the terminal. After that, we process the question with the chat_with_gpt function. In that function we create a Completion. To understand how a completion works, please check the official OpenAI documentation on Completions here.
Once it stops, the Completion will return a list of choices. The number of choices depends on the n parameter in the create constructor.
A choice looks like this (in this case we’ve asked ChatGPT: What is Python programming language?):
We return the text of the choices, and we print it. In our case, we had the following scenario.
How to sort a list in Python with a code example?
There are a number of ways to sort a list in Python. The most common is to use the built-in sorted() function, which takes a list and returns a new list with the same elements in sorted order.
You can also use the list.sort() method, which sorts a list in place (without creating a new list).
Finally, you can use the sorted() function with a custom key function to sort a list in a custom order.
Here are some examples of how to sort a list in Python:
# Sort a list in ascending order
sorted_list = sorted(my_list)
# Sort a list in descending order
sorted_list = sorted(my_list, reverse=True)
# Sort a list by a custom key function
sorted_list = sorted(my_list, key=my_key_function)
So, here is how to use ChatGPT with Python. Doing this through code will allow you to have the desired customizability. I mean you can create a fancy mobile application and integrate a custom ChatGPT module, instead of installing some random bot that will just repeat answers from a finite set, resulting in an unsatisfied customer base.
We all know Artificial Intelligence is the future, ChatGPT is just a glimpse of what will follow in the next few years.
If you want to try the code out, you can check the Github repo. Fork it, and make your version, and let us know what you’ve added or changed, and how your version looks.
If you’re interested in learning more about Computer Science, in a well-explained, concise way, you can check out some of our previous articles:
- How to Gain a Computer Science Education from MIT University for FREE
- FREE Courses from Harvard University to Become Data Scientist
- Top 40 COMPLETELY FREE Coursera Artificial Intelligence and Computer Science Courses