What You’ll Learn

What You’ll Need

ChatGPT is an advanced language model developed by OpenAI, based on the generative pre-trained transformer (GPT) architecture. It’s designed to understand and generate humanlike text based on the input that it receives. ChatGPT can perform a wide range of tasks, including answering questions, providing explanations, generating creative writing, and even engaging in casual conversation.

One of the main features of ChatGPT is its application programming interface (API). This API allows developers to programmatically interact with ChatGPT, enabling them to integrate its capabilities into various applications or systems. Instead of using the traditional GUI, where users type their queries and receive responses, the API allows these interactions to happen through code, making it a powerful tool for automation and integration.

Using the ChatGPT API, developers can:

Overall, the ChatGPT API opens up numerous possibilities for developers and businesses, allowing them to harness advanced artificial intelligence (AI) language processing in a wide array of applications.

OpenAI solutions are best run inside a Python 3 virtual environment. Virtual environments are isolated environments that can be used to install dependencies, known as packages, in isolation from the main operating system and other virtual environments.

For a Linux environment such as Ubuntu on Windows Subsystem for Linux:

First, install venv for Python 3.10:

apt install -y python3.10-venv

After venv has been installed, create a virtual environment:

$ python3 -m venv ciscou

Now, activate the virtual environment:

$ source ciscou/bin/activate
(ciscou) #

For Windows directly, using the terminal, shell, or even PowerShell, the steps are slightly different.

First, ensure that Python is installed on your Windows system. You can download it from the official Python website (https://python.org). During installation, make sure to select the option to Add Python to PATH.

Open the command prompt (cmd) or PowerShell and navigate to the directory where you want to create your virtual environment. Then, run the following command to create a virtual environment named ciscou:

python -m venv ciscou

After creating the virtual environment, you will need to activate it. The command differs slightly, depending on whether you are using the command prompt or PowerShell.

For the command prompt, use:

ciscou\Scripts\activate

Inside the activated Python 3 virtual environment, use the Python package manager, pip, to install OpenAI:

(ciscou) $ pip install openai -U

You will need an OpenAI account and API key to proceed. There are free tiers, which this guide will use, that use the ChatGPT-3.5 Turbo model. For more advanced and modern models, you’ll need a ChatGPT Plus account (monthly subscription fees) to access ChatGPT-4 or ChatGPT-4 Turbo.

Visit https://platform.openai.com to create an account. Once you have an account, visit your profile, then APIs, and generate an API key. Keep this API key secret and safe at all times.

pyATS Logo pyATS Logo

We will use environment variables, and a .env file, to protect our OpenAI API key.

Next, inside the virtual environment, pip install the required Python library, dotenv, to allow us to use it in our Python script:

(ciscou) $ pip python-dotenv

Make a subfolder to hold our code, and launch VS Code:

(ciscou) $ mkdir chatbot
(ciscou) $ cd chatbot
(ciscou) $ /chatbot/ code .

Now that we have created an API key, set up our Python virtual environment with the required packages, and launched VS Code, we can write our ChatGPT client!

Inside VS Code, make a .env file in the subfolder that you created. If you plan on using a Git repository for this code, I suggest you also make a .gitignore file that ignores the .env file so that it is not tracked in Git.

Inside this .env file we will reference our OpenAI API key as such:

OPENAPI_API_KEY="<your API key here>"

Then, make a new Python file called chatbot.py. We will first include the required libraries:

import os
from openai import OpenAI
from dotenv import load_dotenv

Our next step is to load our API key and instantiate the OpenAI API client:

load_dotenv()

api_key = os.getenv("OPENAI_API_KEY")

client = OpenAI(api_key=api_key)

Now that client is established, we can interface with the AI. Let’s make a simple chatbot that runs in a loop so that we can keep asking questions until we decide to quit.

while True:
    question = input('What is your question (type "quit" to exit): ')

    if question.lower() == 'quit':
        break

    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[
                {"role": "system", "content": "You are a chatbot"},
                {"role": "user", "content": f"{question}"},
            ]
        )

    result = ''
    for choice in response.choices:
        result += choice.message.content

    print(f"We asked ChatGPT: {question} - Here is their answer:")
    print("-----------------------------------------------------------------")
    print(result)

There are a couple of components to our API call:

In the context of using the OpenAI API, particularly with the ChatGPT model, the messages array is used to simulate a conversation. Each message in this array is a JSON object that contains two main components: role and content. These components help the model understand the context and the nature of each message in the conversation. Let’s break down the roles and provide examples, especially focusing on the world of Cisco networking.

System Role

The system role is used to provide instructions or context to the AI model. This is not part of the conversation with the user but rather a directive to the model about how it should behave or which role it should assume during the conversation.

Example 1: Setting the AI’s Role

{"role": "system", "content": "You are a chatbot specialized in Cisco networking."}

This tells the AI that it is a chatbot with expertise in Cisco networking, guiding its responses accordingly.

Example 2: Providing Context or Instructions

{"role": "system", "content": "Remember to use Cisco terminology in your explanations."}

This instructs the AI to use specific terminology relevant to Cisco networking in its responses.

User Role

The user role represents the actual questions or statements from the user. This is what the user is saying to or asking the chatbot.

Example 1: Asking a Technical Question

{"role": "user", "content": "What is the difference between OSPF and EIGRP in Cisco routers?"}

Here, the user is asking a specific question about Cisco routing protocols.

Example 2: Seeking Troubleshooting Help

{"role": "user", "content": "My Cisco switch is not recognizing the SFP module. What should I check?"}

In this case, the user is seeking help with a problem related to a Cisco switch.

Putting It Together

In a conversation, these roles work together to create a meaningful exchange. For instance:

System Instruction

{"role": "system", "content": "You are a chatbot knowledgeable in Cisco network troubleshooting."}

User Question

{"role": "user", "content": "How do I configure VLANs on a Cisco switch?"}

System Instruction

{"role": "system", "content": "Provide a step-by-step guide."}

User Follow-Up

{"role": "user", "content": "What are some common mistakes to avoid?"}

In this sequence, the system role sets the context and provides instructions, while the user role drives the conversation with specific questions or statements. The AI model uses this structure to understand both the nature of the conversation and the specific role that it needs to play, ensuring relevant and accurate responses, especially in specialized fields like Cisco networking.

Now that our code is ready to run, and you have some idea of how to configure the messages system instructions and which kind of prompts to ask it, go ahead and run your chatbot!

(ciscou) $ /chatbot/python chatbot.py
(ciscou_ai) root@xyz:~/ciscou_chatbot# python3 chatbot.py
What is your question (type "quit" to exit):

Try different system settings and prompts to continue to learn how to interface with a large language model (LLM) like ChatGPT.

Also, we can add assistants to our prompts. These assistants will help augment and set some additional content to our original prompt.

Adjust the prompt’s API call messages array by including an assistant as such:

    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[
                {"role": "system", "content": "You are a chatbot"},
                {"role": "user", "content": f"{question}"},
                {"role": "assistant", "content": "As a Cisco-focused assistant, I can help you with queries related to network configurations, Cisco products, troubleshooting, and more."}
            ]
        )

This assistant can be tailored to provide specific information or perform tasks related to Cisco products, networking concepts, or troubleshooting. Here’s how you can modify the prompts API call to include such an assistant.

In this modified API call:

By including this assistant role, you’re effectively guiding the chatbot to provide more specialized and relevant responses in the context of Cisco and networking technologies.

Congratulations! In this tutorial, you created a Python virtual environment, installed the required OpenAI and dotenv Python packages, and learned how to write a Python AI-powered ChatGPT client!

Learn More