Unleash the Power of OpenAI's ChatGPT API: Command-Line Conversations Made Easy with Python

Written by Sebastian on March 8, 2023 · 5 min read

tutorial

Recently conversational AI has become increasingly prevalent, and it’s easy to see why. With the ability to automate mundane tasks, provide customer support, and even generate creative content, conversational AI is rapidly transforming the way we interact with technology. One of the most powerful conversational AI tools available today is OpenAI’s ChatGPT, a language model trained to respond to natural language queries with remarkably human-like answers.

In this blog post, we’ll explore how to unleash the power of ChatGPT’s new API through Python to create conversational apps that can chat with users, answer questions, and even generate creative output. With step-by-step instructions and plenty of examples, we’ll show you how to get started with the ChatGPT API in Python. Whether you’re a developer looking to create a chatbot for your business, a researcher exploring the frontiers of AI, or simply a curious enthusiast eager to explore the cutting edge of natural language processing, this post is for you. So let’s dive in and start building powerful conversational agents with OpenAI’s ChatGPT API & Python!

Retrieve OpenAI API Key

In order to be able to make use of OpenAI’s API from within a Python application we need to retrieve an API key first from the OpenAI dashboard.

To retrieve your OpenAI API key you need to create a user account at https://openai.com/ and access the API Keys section in the OpenAI dashboard to create a new API key.

Image

This key is secret and must not shared with anybody else. We’ll need to use this key later of when implementing the Python script to access OpenAI`s API.

Setting Up The Python Project

Let’s start setting up the Python project. Start creating a new project folder and inside this folder create a new file py-chatgpt.py:

$ mkdir py-chatgpt
$ cd py-chatgpt
$ touch py-chatgpt.py

Make sure to install or upgrade the openai python package:

$ pip install --upgrade openai

We’ll make use of the openai package to access the ChatGPT API.

Implement The Application Logic

Insert the following Python code into py-chatgpt.py:


openai.api_key = "[INSERT YOU OPENAI API KEY HERE]"

history = []

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

    messages = []
    for input_text, completion_text in history:
        messages.append({"role": "user", "content": input_text})
        messages.append({"role": "assistant", "content": completion_text})

    messages.append({"role": "user", "content": user_input})

    completion = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages
    )

    completion_text = completion.choices[0].message.content
    print(completion_text)

    history.append((user_input, completion_text))

    user_input = input("Would you like to continue the conversation? (Y/N) ")
    if user_input.upper() == "N":
        break
    elif user_input.upper() != "Y":
        print("Invalid input. Please enter 'Y' or 'N'.")
        break

Here’s a breakdown of the code:

Run the Application

You can run the Python script by using the python command followed by the name of the file:

$ python py-chatgpt.py

Now you’re being ask to provide the first input. The response will be retrieved from ChatGPT’s language model and provided as output:

Image

After every response you’re being asked if the conversation should be continued. If you want to continue you need to input “Y”. You’re then being asked to provide the next input.

As the gpt-3.5-turbo model is able to also take the history of the conversation into account you can ask subsequent questions which are based on the context of the conversation which has taken place so far.

Conclusion

In conclusion, implementing a chatbot using OpenAI’s new gpt-3.5-turbo language model is an excellent way to provide automated responses to users and enhance the user experience. By following the steps outlined in this blog post, you can easily create your own chatbot using Python and OpenAI’s API. By customizing the GPT model used and tuning the script to fit your specific use case, you can create a chatbot that can communicate with users in a natural and engaging way. With the power of OpenAI and the versatility of Python, the possibilities for creating chatbots are endless. So why not give it a try and create your own chatbot today!