As network engineers, we are well-versed in configuring routers, switches, and firewalls, and some of you have been deep diving into the world of network automation. But have you ever dabbled in Artificial Intelligence (AI)? Particularly, generative AI? In an industry that is actively evolving and finding new ways to solve problems at scale, understanding the basics of AI can be a powerful addition to your skillset. This blog post aims to guide you through the fundamental steps of integrating generative AI into your workflows using Python and OpenAI’s GPT-4.
Before we get started, let’s understand what generative AI is. Generative AI is a form of artificial intelligence that is able to generate new data that resembles a given dataset. For instance, gen AI can produce new human-like text, code, or even create realistic images from an existing large data set without being trained on a specific task to achieve the results. There is so much potential for new applications in network management — from automated troubleshooting assistants to smart alerts and notifications. Now let’s look at an example of examming Cisco CLI to determine the routes and ACLs that exist on a router.
You’ll need Python installed on your computer. You can download it here.
Additionally, you’ll also need to install the following Python packages:
openai
: For interacting with the OpenAI APIdotenv
: For managing environment variablesnetmiko
: For gather Cisco configuration infoYou can install these via pip:
pip install openai python-dotenv netmiko
OpenAI offers an API for their GPT-3, GPT-4 and DALLE models. To get started, you’ll need to create an account on OpenAI’s website and obtain an API key.
Start by importing the required Python libraries.
import os
from netmiko import ConnectHandler
from dotenv import load_dotenv
from openai import OpenAI
Load the environment variables where you’ll keep your OpenAI API key.
load_dotenv()
You can set your organization and API key as follows:
# Create an OpenAI client
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
Now, let’s see which GPT models are available for us to use.
# List the available models
models = client.models.list()
gpt_models = []
for model in models:
print(model.id)
if "gpt-" in model.id:
gpt_models.append(model.id)
print(f"Here are the list of available gpt models: {gpt_models}")
Using Cisco DevNet we will connect to a Cisco CSR1000v to get the routes and ACLs that exist on the device. We will assign each response to its own variable. This will be passed to the GPT model as context in our prompt.
# DevNet Always-On Sandbox
DEVNET_IOS_XE_HOST = "sandbox-iosxr-1.cisco.com"
DEVNET_IOS_XE_PORT = 22
USERNAME = "admin"
PASSWORD = "C1sco12345"
# Define the device information
cisco_devnet = {
'device_type': 'cisco_ios',
'host': DEVNET_IOS_XE_HOST,
'username': USERNAME,
'password': PASSWORD,
'port' : DEVNET_IOS_XE_PORT
}
# Establish an SSH connection to the device
connection = ConnectHandler(**cisco_devnet)
# Send a command to the device
output = connection.send_command("show running-config")
Now you’re all set to interact with GPT-4. For instance, you can ask GPT-3 to provide a creative explanation of generative AI.
# OpenAI Model
MODEL = "gpt-4o"
# OpenAI System Message
SYSTEM = {
"role": "system",
"content": f"""You are a network engineer. You are tasked with analyzing router outputs. \
You answer questions about the router configuration, such as interfaces, routing protocols, and security features, etc. \
When responding, provide detailed information and explanations in a helpful and clear tone.
Here is the router configuration:
{output}
"""
}
messages = [SYSTEM]
while True:
# Get the user input
user_input = {
"role": "user",
"content": input("You: ")
}
# If the user input is "exit", break the loop
if user_input["content"] == "exit":
exit("Conversation ended.")
# Append the user input to the messages
messages.append(user_input)
# Generate a response from ChatGPT
response = client.chat.completions.create(
model=MODEL,
messages=messages
).choices[0].message.content
# Append the assistant response to the messages
messages.append({
"role": "assistant",
"content": response
})
print("ChatGPT: " + response)
Now you can ask the model questions about your current device running configuration.
Automated Troubleshooting: Use generative AI to analyze problems and then automatically generate troubleshooting steps based on the issues detected in the network.
Dynamic Policy Updates: Implement smart policy recommendations generated by the AI model based on your specific network environment.
Natural Language Queries: Implement a chatbot capable of understanding and responding to natural language queries about network status, potential issues, and configurations.
Generative AI opens a world of possibilities not just for developers but for network engineers as well. It adds a layer of intelligence to network operations that can automate and enhance many aspects of networking. Using Python and OpenAI’s GPT-4 is just one example of how you can get started with generative AI. There are many other models that open sourced, along with programming lanaguages that you can use to start today.
This was just a quick blog to help you get started. If you found this helpful or would like to just share your own experiences and projects. I’d love to see how AI is revolutionizing your networking tasks!
Peace,