Jupyter AI is a JupyterLab extension that provides a friendly user interface between users and AI models.
This demo showcases the IPython magics Jupyter AI provides out-of-the-box.
Load the IPython extension:
%load_ext jupyter_ai
To start using Jupyter AI, use the %%ai
cell magic with the model specified via the syntax <provider-id>:<model-id>
. Every line after the first should contain the prompt.
Currently, we support the following providers:
ai21
anthropic
cohere
huggingface_hub
openai
openai-chat
sagemaker-endpoint
These are LangChain LLM providers (langchain.llms
). Any model ID that is valid for the corresponding provider is valid in the AI magics. For example, claude-v1.2
is a valid model ID for the anthropic
provider, and one can invoke it via Jupyter AI magics like so:
%%ai anthropic:claude-v1.2
Write a poem about C++.
O C++, language so keen,
With classes, objects and templates so sheen.
Inheritance and polymorphism galore,
Abstracting the complexity to the core.
Support for low and high level alike,
Helping programmers day and night.
Pointers, references and RAII,
OOP and generic programming, my oh my!
Exception handling and lambda's too,
There's so much you help me do!
A powerful language, sometimes complex,
But in the end, truly adept.
That's my poem about C++,
A language with power, through and through!
If your model ID is unique, then we can also infer the provider ID implicitly:
%%ai j2-jumbo-instruct # infers AI21 provider
Write some JavaScript code that prints "hello world" to the console.
Copy code console . log ( "hello world" );
We can call models on HuggingFace Hub directly:
%%ai huggingface_hub:google/flan-t5-xl
What is the capital of New York state?
If that's the question you're looking for, here's the bottom line for New York: $2.13 per day.
It's not surprising then, because as you can see
%%ai huggingface_hub:gpt2
What is the square root of 2?
2 is the number of times a point in a circle is square. Thus, one is squared twice: 2 x 2 = 19 x 18 = 51 / 2 x 2 = 49, and I was
-f/--format
¶You can also pass the -f/--format
argument to specify an IPython display to use to render the output. Valid formats:
markdown
math
html
json
%%ai anthropic:claude-v1.2 -f html
Create a square using SVG with a black border and white fill.
%%ai chatgpt -f math
Generate the 2D heat equation.
You can also interpolate IPython scope into your prompt via curly braces.
poet = "Walt Whitman"
%%ai chatgpt
Write a poem in the style of {poet}
O Captain, my Captain, you have led us through the fray
Through the storm and the turmoil, through the darkest of days
With your unwavering courage, your steadfast heart ablaze
You have lifted us up where the eagles soar and play
Your voice is as thunder, your soul a raging fire
You guide us through the tempest, you lift us ever higher
With you at our helm, we shall never falter or tire
For you are the Captain, and we are your loyal choir
Together we shall march, to the beat of our own drum
We shall conquer every fear, we shall overcome
With our eyes on the prize, and our hearts full of song
We shall claim victory, as we journey ever on
So here's to you, O Captain, our light in the dark
May your courage never falter, may your fire never spark
For in your hands we trust, our fate and our ark
O Captain, my Captain, you are the beating of our heart.
for i in range(0, 5):
print(i)
0 1 2 3 4
%%ai cohere:command-xlarge-nightly
Please explain the code below:
--
{In[11]}
The above code will print the numbers 0 through 4. It will not print the number 5 because the range function includes the first value (i.e. 0) but not the last value (i.e. 5).
Say I just got a homework assignment about "the Laplace equation" from my professor.
The problem is: I have absolutely no idea what they're talking about.
Let's ask an LLM what the assignment is about.
%%ai claude-v1.2
What is the 2d Laplace equation and when is it used? Use LaTeX for equations delimited by `$`.
The 2D Laplace equation is: $$ \nabla^2 f = \frac{\partial^2 f}{\partial x^2} + \frac{\partial^2 f}{\partial y^2} = 0 $$ where $f$ is a scalar function.
It is used to describe steady-state heat flow, electrostatics, fluid flow, and other phenomena in two dimensions under the assumption of no sources. The Laplace equation arises from simplifying the Poisson equation by setting the charge density to 0. It is a key partial differential equation used in modeling many physical systems.
The Laplace equation implies that the sum of second derivatives of $f$ with respect to $x$ and $y$ is zero, which means the curvature of the solution surface is constant. So, the solutions have a minimal surface with constant negative Gaussian curvature.
Does this help explain the 2D Laplace equation? Let me know if you have any other questions!
%%ai j2-jumbo-instruct --format math
Write the 2d Laplace equation in polar coordinates.
%%ai chatgpt
Please generate the Python code to solve the 2D Laplace equation in cartesian coordinates.
Solve the equation on the square domain x=(0,1) and y=(0,1) with vanishing boundary conditions.
Plot the solution using Matplotlib.
Please also provide an explanation.
Here's the Python code to solve the 2D Laplace equation in Cartesian coordinates:
import numpy as np
import matplotlib.pyplot as plt
# Set up grid
nx = 101
ny = 101
nt = 100
dx = 1. / (nx - 1)
dy = 1. / (ny - 1)
x = np.linspace(0, 1, nx)
y = np.linspace(0, 1, ny)
# Initialize solution
u = np.zeros((nx, ny))
# Set boundary conditions
u[0,:] = 0
u[-1,:] = 0
u[:,0] = 0
u[:,-1] = 0
# Set up source term
f = np.zeros((nx, ny))
f[int(nx/2), int(ny/2)] = 1 / (dx * dy)
# Set up iteration parameters
tolerance = 1e-5
omega = 1.8
# Iterative solver
for n in range(nt):
for i in range(1, nx-1):
for j in range(1, ny-1):
u_old = u[i,j]
u[i,j] = (1-omega)*u[i,j] + omega*(dx**2*(u[i+1,j]+u[i-1,j]) + dy**2*(u[i,j+1]+u[i,j-1]) - dx**2*dy**2*f[i,j]) / (2*(dx**2+dy**2))
if abs(u[i,j] - u_old) < tolerance:
break
# Plot solution
X, Y = np.meshgrid(x, y)
plt.contourf(X, Y, u.T, 100, cmap='viridis')
plt.colorbar()
plt.xlabel('x')
plt.ylabel('y')
plt.title('2D Laplace equation solution')
plt.show()
Explanation:
This code uses finite difference methods to solve the 2D Laplace equation, which is given by:
∇²u(x,y) = 0
on the square domain x=(0,1) and y=(0,1) with vanishing boundary conditions.
We set up a grid with nx x ny points in the x and y directions, respectively. We initialize a solution vector u with zeros, and then set the boundary conditions to zero. We also set up a source term f, which is a delta function located at the center of the domain. The source term is used to enforce the vanishing boundary conditions.
We then use an iterative solver to solve the Laplace equation. The solver updates the values of the solution vector at each point on the grid until the difference between consecutive iterations is below a certain tolerance.
Finally, we plot the solution using Matplotlib, which creates a contour plot of the solution on the domain. The contour plot is a visualization of the solution u(x,y) as a height map, which provides a 2D picture of the solution.