← Back to Blog
Tuesday, March 14 2023

Introducing genai

The Generative AI for IPython

 Youtube LivestreamGithubNotebook Example

Nothing breaks the flow of experimentation in a notebook like an exception. You’re deep in thought, pulling data, and then suddenly, everything stops. Sometimes the error message and traceback are enough to get you back on track. Other times you’re left scratching your head and scouring StackOverflow for solutions.

image

While sometimes overwhelming, there is a wealth of contextual information to help programmers diagnose their errors and fix them. In standard programming environments, we have access to the error and its traceback as well as the code. In an interactive environment, we also have variables in memory and the history of past executions. We can use this context to provide custom suggestions for fixing errors.

Recently, Python started introducing suggestions directly into error messages:

image 1 1

Enter genai

Sb fn8LpwoB2ff7K OD9 UGnrz460ghODWdvQe YYg18qGyxQgLX jC1vHo Bz 8pkdpcw 7AuNkSr4GpWGh

At notebook launch, genai registers as a custom IPython exception handler. When users hit an error, genai automatically sends the error on to OpenAI’s GPT-3.5-turbo model.

By connecting OpenAI’s new ChatCompletion endpoint with IPython’s execution context, genai can recommend tailored suggestions to unblock the user.

Working inside a notebook is naturally a conversation between the user and the system. This conversational aspect of interactive computing lends itself perfectly to invite GPT into their conversation.

Let’s unpack how this works.

OpenAI provides three distinct roles: user, system, and assistant. These map perfectly onto the user and system of the notebook, with ChatGPT forming the assistant.

Get started with Genai

When an exception occurs, genai puts together the overall context of what happened as a series of messages between the user and the system. The first message sets the stage, via the system role, with this prompt:

As a coding assistant, you’ll diagnose errors in Python code written in a Jupyter Notebook. Format your response using GitHub flavored markdown. Provide concise code examples in your response which will be rendered in Markdown in the notebook.

After that we establish the user’s contribution by sending their original code with role: user.

image 2
Introducing genai 9

Finally we send the error message and traceback as a plain string with system role.

image 3 1
Introducing genai 10

This establishes enough context for ChatGPT to write back with a suggestion.

The problem with the code is that it tries to serialize a Decimal object when invoking json.dumps(). JSON supports only limited data types like strings, numbers, dictionaries, booleans, and None. Therefore, we need to convert Decimal objects to either float or string.

The following code snippet shows how to convert the decimal value to a float and then serialize to JSON:

image 4
Introducing genai 11

By incorporating feedback on exceptions, we can help people understand and iterate on their code faster.

This allows people to more freely experiment, iterate, and make mistakes without worry or time consuming debugging because genai is acting as your expert assistant to remove barriers to high quality code.

We’re already noticing that people feel more comfortable experimenting with new libraries and unfamiliar syntax because they know that genai will help them successfully iterate towards their desired outcome.

genai is an open-source package available for anyone to use and contribute to. Exception handling works in JupyerLab, classic notebook, noteable, and even command line ipython.

Code Genai

As part of this release we’re also including an `%%assist` magic that will generate new code cells. This magic takes advantage of not just the previous inputs – genai will send the result of outputs as well which is very useful for sharing the context of the dataframes you’re working with. It’s a fun experiment we’d love to improve on publicly.

image 49 1 1

Learn more

Get started with Genai

Join us for a live session on March 16 at 2:30 PM PT. Have questions? Need help? Want to contribute? Linkedin | Youtube | Facebook

We’d love to collaborate with you on even more IPython integrations with Large Language Models like GPT-3. Try out genai, write issues, and send pull requests. We’d also love to see your notebooks!

How Noteable shortens the journey from data to insights? Organizations must enable their data teams with tools that foster collaboration. Noteable is a collaborative data workspace that powers the journey from raw data to insights. It enables data teams to use code (SQL, Python, R), text (descriptions, annotations), and no-code Data Visualizations to develop collaborative data analysis that non-technical users and data leaders can interact with. Try Noteable today →

 

Posted by

Vivek Sinha