Integrating IPython context with Large Language Models

Along with some colleagues, I’ve started experimenting with sending IPython context to OpenAI’s ChatCompletion endpoint, both for exceptions and as an assist (with a %%assist magic). It works in all the Jupyter frontends I’ve tested: notebook, lab, and ipython.

The IPython extension is called genai, and it’s pretty quick to set up:

  • Set up an OPENAI_API_KEY via OpenAI API
  • pip install genai
  • Run %load_ext genai in your first cell

After that start making mistakes as the main feature is automatic exception feedback:

In [5]: df.boxplot('days_open', by="request_category")
   ...: plt.xticks(rotate=45)
AttributeError                            Traceback (most recent call last)
Cell In[5], line 2
      1 df.boxplot('days_open', by="request_category")
----> 2 plt.xticks(rotate=45)

File ~/.pyenv/versions/3.9.9/lib/python3.9/site-packages/matplotlib/, in xticks(ticks, labels, minor, **kwargs)
   1889     labels = ax.get_xticklabels(minor=minor)
   1890     for l in labels:
-> 1891         l._internal_update(kwargs)
   1892 else:
   1893     labels = ax.set_xticklabels(labels, minor=minor, **kwargs)

AttributeError: Text.set() got an unexpected keyword argument 'rotate'

Generates this Markdown output :point_down:t2:

:bulb: Suggestion

The rotate argument is not a valid keyword argument for plt.xticks(). Instead, you should use the rotation argument. Please try the following code:

df.boxplot('days_open', by="request_category")

This should rotate the x-axis tick labels by 45 degrees.

There’s also an %%assist magic that lets you write in natural language (or code if you so choose). It will send some recent inputs and outputs from IPython history to OpenAI to create the next cell:

I’d love to know your thoughts and collaborate on more advancements we can make to bring AI to interactive computing!