Line Magic

The %ai line magic provides quick commands for managing models, aliases, and conversation state.

Commands

%ai help

Get help and see available commands:

%ai help

%ai list

List available models:

# List all models from all providers
%ai list

%ai list openai
%ai list anthropic
%ai list google
%ai list bedrock

%ai register

Create an alias for a model:

%ai register mymodel anthropic:claude-3-5-sonnet-latest

Now you can use the alias:

%%ai mymodel
Hello, world!

%ai update

Update an existing alias:

%ai update mymodel anthropic:claude-3-opus-latest

%ai delete

Remove an alias:

%ai delete mymodel

%ai reset

Clear the conversation history:

%ai reset

This removes all context from previous messages. Useful when you want to start a fresh conversation without prior context influencing responses.

%ai error

Get help with the last error:

%ai error claude

This sends the most recent error to the specified model for explanation and fix suggestions.

Configuration

Configure AI behavior using Jupyter’s %config magic:

# Set default model
%config AiMagics.default_model_name = "anthropic:claude-3-5-sonnet-latest"

%config AiMagics.default_format = "markdown"

Configuration Options

OptionDescriptionExample
default_model_nameModel used when none specifiedanthropic:claude-3-5-sonnet
default_formatDefault output formatmarkdown, code, text

Provider Configuration

Models require API keys or credentials configured as environment variables:

ProviderEnvironment Variable
OpenAIOPENAI_API_KEY
AnthropicANTHROPIC_API_KEY
GoogleGOOGLE_API_KEY
AWS BedrockAWS credentials configured

Examples

Setting Up Aliases

# Register commonly used models
%ai register fast openai:gpt-4o-mini
%ai register smart anthropic:claude-3-5-sonnet-latest
%ai register cheap anthropic:claude-3-5-haiku-latest

Checking Available Models

# See what's available
%ai list

%ai list anthropic

Debugging Workflow

# Run code that produces an error
my_buggy_code()

%ai error claude

Fresh Start

# Clear all conversation context
%ai reset

%%ai claude
Start of a new conversation