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-latestNow 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 resetThis 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 claudeThis 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
| Option | Description | Example |
|---|---|---|
default_model_name | Model used when none specified | anthropic:claude-3-5-sonnet |
default_format | Default output format | markdown, code, text |
Provider Configuration
Models require API keys or credentials configured as environment variables:
| Provider | Environment Variable |
|---|---|
| OpenAI | OPENAI_API_KEY |
| Anthropic | ANTHROPIC_API_KEY |
GOOGLE_API_KEY | |
| AWS Bedrock | AWS 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-latestChecking Available Models
# See what's available
%ai list
%ai list anthropicDebugging Workflow
# Run code that produces an error
my_buggy_code()
%ai error claudeFresh Start
# Clear all conversation context
%ai reset
%%ai claude
Start of a new conversation