Connect ChatGPT API to Jupyter Notebook

Jenna Inouye

· 3 min read
Thumbnail

Sometimes, I do things for no reason. Sometimes, I create a minimally viable product and use it in real life. Connecting ChatGPT to Jupyter Notebook/JupyterLabs makes it faster and easier to iterate through different prompts. It's certainly not a replacement for a functional front end, but it is an excellent option for those experimenting with text generation.

Setting up your environment

Getting your ChatGPT API Key

  • Go to the OpenAI Platform.
  • Under your Profile, select View API Keys.
  • Click on Create New Secret Key.
  • (Optional) You may need to go to Billing and add a credit card. ChatGPT API is affordable, with a few fractions of a cent per query.

Creating a Jupyter Notebook

Now you need actually to create a Jupyter Notebook. Open Jupyter Lab.

run jupyter-lab

Create a new notebook. In that notebook, add the following code:

import openai
openai.api_key = "YOUR_API_KEY"

Add another code chunk below:

def chatGPT(GPTquery):
    response = openai.ChatCompletion.create(
        model = "gpt-3.5-turbo",
        temperature = 1,
        max_tokens = 2000,
        messages = [
            { "role": "user","content": GPTquery}
        ]
    )
    
    print(response['choices'][0]['message']['content'])

You may want to note a few settings:

  • Model: gpt-3.5-turbo. Model refers to the flavor of ChatGPT we will be using. Another option would be gpt-3.5.
  • Temperature: 1. Temperature is the randomness or creativity of the text. Temperature is set from 0 to 1, with most text around 0.7.
  • max_tokens: 2,000. This is the number of tokens we will use, the maximum length or character count.

Now, you can call ChatGPT as follows:

chatGPT("How are you doing today, chatGPT?")

Using your Jupyter Notebook

None of that seems very helpful. You could just query ChatGPT. But here's some insight into why I wanted to implement this.

Replace the query code with this:

tone = "You are a professional dungeon master experienced in DND, 
with a flourish for the creative."
topic = "5E encounters for a low-level party"

chatGPT("Generate " + topic + " In the following tone: " + tone)

Result:

Encounter 1: "The Mischievous Kobold Ambush"
Location: A dense forest path.

As your party strolls through the serene woodland, a sudden rustling noise 
grabs your attention. Before you can react, a group of mischievous 
kobolds, armed with makeshift weapons, leap out from the underbrush. Their 
beady eyes gleam with a mix of mischief and desperation. It appears they 
are attempting to waylay unsuspecting travelers.

The kobolds, led by their cunning chieftain, snarl and cackle as they 
surround you. With an unexpected burst of creativity, they employ clever 
tactics to gain the upper hand. They hurl smoke bombs to obscure their 
movements and strike from the shadows, testing the mettle of your 
adventurers.

Remember, dear adventurers, quick thinking and resourcefulness will be 
your greatest assets in this encounter. Unleash your own creative 
strategies to outsmart these crafty creatures and claim victory!

Using a Jupyter Notebook, I can quickly iterate without having to rewrite basic prompts. I can change the "topic" to whatever I want without changing the tone.

The art of arguing with AI

ChatGPT isn't a magic utility. Writing ChatGPT prompts can be complex. Sometimes, you need to trick the AI. Sometimes, you need to manipulate it and argue with it. This is all fine, and the system is working as intended. Let's not think too deeply about it.

But for those who are in data analytics and may only know a touch of Python, this does provide a pretty fast solution for testing out different types of query, and potentially adding a layer of automation over an already very automatic process.

About Jenna Inouye

Jenna is a developer and marketer with two cats, three goats, five dogs, 19 chickens, and approximately 48 guppies.

Copyright © 2023 jennain. All rights reserved.