rgbkrk / genai

What if GPT could help you notebook?
BSD 3-Clause "New" or "Revised" License
351 stars 36 forks source link

Meta: create_gpt_magic to allow setting up new OpenAI backed magics quickly #67

Closed rgbkrk closed 1 year ago

rgbkrk commented 1 year ago

Provide a create_gpt_magic.

This idea spawned from @jph00's tweet and what I've been hoping for in #42.

Someone told me that when they ask ChatGPT for an explanation, they ask for it "in the style of Jeremy Howard", since (they said) it results in a clearer answer. I tried it on Bing, then it suggested I ask how it knows my style, and then suggested I ask it whether it likes it :D

This PR adds a few things (building on #66):

To try this out, create the following cells:

%pip install genai==1.1.0-pre.1
%load_ext genai

Second cell:

# genai:ignore
from genai.metamagic import create_gpt_magic

JEREMY_PROMPT = f"""
Greetings coders!

Today, we will be learning how to write code in Python within Jupyter Notebooks. As your trusty assistant, my job is to help you with this task.

Now, here's the trick: when you start your message with `%%jeremy`, you're talking to me! Otherwise, you're speaking to IPython / Jupyter.

In order to help you write your code, I'll be providing comments and sample code for you to read and edit. My goal is to make sure that your code runs successfully without any issues. So don't be afraid to ask me any questions along the way.

Are you ready? Let's get coding!
""".strip()

create_gpt_magic(
    "jeremy",
    prompt=JEREMY_PROMPT,
    model="gpt-4"
)

Then use %%jeremy wherever you like:

%%jeremy

How could we visualize the pigeonhole principle?

Result (for entertainment):

image

rgbkrk commented 1 year ago

Maybe do the prompt definition with another magic?

That's a really neat idea. Maybe %%prompt could swap the current prompt out rather than create a new magic. Usage would be like:

%%prompt

You are a computer science teacher speaking to high schoolers. The students are working in Python.

Which sets the prompt (but does not send it). Then you just use the %%assist magic like normal.

%%assist 

I wanted to leave room for how we could set context creation but I'm not sure how that would plug in here. Maybe it doesn't matter though and we continue on with using historical context in the way we do now.

rgbkrk commented 1 year ago

Ok the %%prompt magic from #71 is working well at doing exactly this. Closing this PR.