Skip to main content

JupyterLab extension for AI-powered prompt cells with variable and function references

Project description

ai-jup: AI-Powered Prompt Cells for JupyterLab

A JupyterLab extension that adds AI-powered prompt cells with special syntax for referencing kernel variables and exposing functions as AI-callable tools. Inspired by fast.ai's Solveit.

CI JupyterLab 4.x Python 3.8+ License MIT

Features

  • $`variable` - Reference kernel variables in prompts (values substituted automatically)
  • &`function` - Expose Python functions as AI-callable tools
  • Streaming responses - Real-time SSE streaming from Anthropic's Claude
  • Full context - AI sees all preceding code cells and kernel state
  • Tool loop - AI can call multiple tools in sequence to complete tasks
  • Built-in file tools - Includes fastcore.tools for file exploration and editing

Quick Start

1. Install

pip install ai_jup

Verify the extension is installed:

jupyter labextension list | grep ai-jup
# Should show: ai-jup v0.1.0 enabled OK

2. Set your API key

export ANTHROPIC_API_KEY="your-key-here"

To persist across sessions, add to your shell profile:

# For zsh (macOS default)
echo 'export ANTHROPIC_API_KEY="your-key-here"' >> ~/.zshrc

# For bash
echo 'export ANTHROPIC_API_KEY="your-key-here"' >> ~/.bashrc

3. Start JupyterLab

jupyter lab

4. Try it out

  1. Press Cmd/Ctrl + Shift + P to insert a prompt cell
  2. Type: What is 2 + 2?
  3. Press Cmd/Ctrl + Shift + Enter to execute

You should see the AI respond with streaming text.

5. Use variables and functions

# In a code cell, define some data
data = [1, 2, 3, 4, 5]

Then in a prompt cell:

What is the sum of $`data`?

Press Cmd/Ctrl + Shift + Enter to execute.

Syntax

$`variable` - Reference Variables

Use $`variable_name` to inject the current value of any Python variable into your prompt:

# In a code cell
sales_data = [
    {'product': 'Widget A', 'units': 150, 'price': 25.99},
    {'product': 'Widget B', 'units': 89, 'price': 45.50},
]
Given $`sales_data`, which product has the highest revenue?

The AI sees the actual data and can provide specific analysis.

&`function` - Expose Functions as Tools

Use &`function_name` to let the AI call Python functions during its response:

# In a code cell
def calculate_revenue(data: list) -> float:
    """Calculate total revenue from sales data."""
    return sum(item['units'] * item['price'] for item in data)

def top_seller(data: list) -> dict:
    """Find the product with the most units sold."""
    return max(data, key=lambda x: x['units'])
Use &`calculate_revenue` and &`top_seller` to analyze $`sales_data`.

The AI can call these functions, see the results, and incorporate them into its response.

Built-in File Tools

ai-jup includes fastcore.tools for file exploration and editing:

from fastcore.tools import view, rg, sed, create, str_replace, insert
Tool Description
view(path, view_range?, nums?) View directory or file contents with optional line range
rg(argstr) Run ripgrep to search for patterns
sed(argstr) Run sed for reading sections of files
create(path, file_text, overwrite?) Create a new file with given content
str_replace(path, old_str, new_str) Replace first occurrence in file
insert(path, insert_line, new_str) Insert text at specified line number

Example: Exploring a Codebase

# Clone a repo to explore
!git clone https://github.com/AnswerDotAI/fastcore --depth 1 -q
repo_path = 'fastcore'

from fastcore.tools import view, rg
Use &`view` to explore $`repo_path` and tell me what this library does.
Then use &`rg` to find all decorator definitions.

The AI will:

  1. Call view('fastcore') to see the directory structure
  2. Call view('fastcore/fastcore/basics.py') to read key files
  3. Call rg('"def.*decorator" fastcore/') to search for patterns
  4. Synthesize findings into a coherent explanation

How It Works

Prompt Cells

Prompt cells are markdown cells with special metadata (ai_jup.isPromptCell: true). They're visually distinguished with a colored border and "AI Prompt" prefix.

Context Gathering

When you execute a prompt cell, ai-jup:

  1. Collects preceding code - All code cells above the prompt are gathered
  2. Resolves $`variables` - Queries the kernel for each variable's type and repr
  3. Gathers &`function` metadata - Gets signatures, docstrings, and parameter info
  4. Builds tool definitions - Converts functions to Anthropic tool format

Tool Execution Loop

When the AI calls a tool:

  1. The tool call is sent to the Jupyter kernel
  2. The function executes with the provided arguments
  3. Results are returned to the AI (supports text, HTML, and images)
  4. The AI can make additional tool calls or finish its response

The loop continues for up to 5 iterations (configurable via max_steps).

Rich Output Handling

Tool results are automatically formatted:

  • DataFrames → Rendered as HTML tables
  • Matplotlib figures → Converted to inline PNG images
  • Text → Displayed in code blocks
  • Errors → Shown with clear error messages

Keyboard Shortcuts

Shortcut Action
Cmd/Ctrl + Shift + P Insert new prompt cell below
Cmd/Ctrl + Shift + Enter Execute prompt cell

Configuration

Environment Variables

Variable Required Description
ANTHROPIC_API_KEY Yes Your Anthropic API key

Supported Models

  • Claude Sonnet 4 (default) - claude-sonnet-4-20250514
  • Claude 3.5 Sonnet - claude-3-5-sonnet-20241022
  • Claude 3 Haiku - claude-3-haiku-20240307

To change the model, edit the cell metadata:

{
  "ai_jup": {
    "isPromptCell": true,
    "model": "claude-3-haiku-20240307"
  }
}

Development

git clone https://github.com/hamelsmu/ai-jup.git
cd ai-jup
just install   # Install in dev mode with live reload
just test      # Run all tests
just lab       # Start JupyterLab
just --list    # See all commands

Requires just (brew install just on macOS).

Architecture

ai-jup/
├── src/                        # TypeScript frontend
│   ├── index.ts                # Extension entry point
│   ├── promptCell.ts           # Prompt cell management
│   ├── kernelConnector.ts      # Kernel introspection
│   ├── promptParser.ts         # $`variable` and &`function` parsing
│   ├── toolResultRenderer.ts   # Tool result formatting
│   └── *.test.ts               # Jest tests
├── ai_jup/                     # Python backend
│   ├── handlers.py             # API endpoints
│   └── __init__.py             # Server extension setup
├── tests/                      # Python test suite
├── style/                      # CSS styles
└── justfile                    # Development commands

API Endpoints

Endpoint Method Description
/ai-jup/prompt POST Stream AI response with tool loop
/ai-jup/tool-execute POST Execute a single tool call
/ai-jup/models GET List available models

Comparison with Solveit

Feature Solveit ai-jup
Variable syntax $`varname` $`varname`
Function syntax &`funcname` &`funcname`
Platform VS Code JupyterLab
API Multiple providers Anthropic Claude
File tools Built-in fastcore.tools

ai-jup uses the same syntax as Solveit for compatibility.

Troubleshooting

Extension not loading after install

If you installed ai-jup but it's not working:

# Verify extension is installed
jupyter labextension list | grep ai-jup

# Restart JupyterLab (required after first install)
# Close JupyterLab and run:
jupyter lab

"ANTHROPIC_API_KEY not set"

The API key must be set in the environment before starting JupyterLab:

export ANTHROPIC_API_KEY="your-key-here"
jupyter lab

If you set the key after JupyterLab started, you need to restart it.

"anthropic package not installed"

pip install anthropic

Keyboard shortcuts not working

Make sure you're in command mode (press Esc first), then try the shortcut. If shortcuts still don't work, check for conflicts in Settings → Advanced Settings → Keyboard Shortcuts.

Tool execution fails

Ensure the function is defined in the kernel before referencing it with &`functionname`. Run the code cell that defines the function first, then execute the prompt cell.

Requirements

  • Python ≥ 3.8
  • JupyterLab ≥ 4.0
  • anthropic ≥ 0.50.0
  • fastcore ≥ 1.7.0

License

MIT License - see LICENSE for details.

Credits

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ai_jup-0.1.2.tar.gz (242.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ai_jup-0.1.2-py3-none-any.whl (35.8 kB view details)

Uploaded Python 3

File details

Details for the file ai_jup-0.1.2.tar.gz.

File metadata

  • Download URL: ai_jup-0.1.2.tar.gz
  • Upload date:
  • Size: 242.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for ai_jup-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7e8107e0daa0859f14778406487b1e668baf194720dee5003fc79a2c2ef75e08
MD5 df908f05612450f3d189843e4f8d9b8f
BLAKE2b-256 1ddae2a1649320a48b76da8d2a54eb8aae27dc588bf1e3182b03f04de341f1aa

See more details on using hashes here.

File details

Details for the file ai_jup-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: ai_jup-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 35.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for ai_jup-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3770e4265111836aaf32440ebdea9cb08f6bc724f414f345af4bfa2fb0451ecc
MD5 48866b480f92de6d34421d75a230968f
BLAKE2b-256 8f96231bc9e32b8aae899f5b65ed82479366ea9413a56d833ff34ef48bc645b8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page