AI magic
Project description
aimagic
Install
Clone this repo, and then:
cd aimagic
pip install -e .
mkdir -p $VIRTUAL_ENV/share/jupyter/nbextensions
rm -f $VIRTUAL_ENV/share/jupyter/nbextensions/nbmeta.js
ln -s ${PWD}/static/nbmeta.js $VIRTUAL_ENV/share/jupyter/nbextensions/
mkdir -p $VIRTUAL_ENV/etc/jupyter/nbconfig/notebook.d
rm -f $VIRTUAL_ENV/etc/jupyter/nbconfig/notebook.d/nbmeta.json
ln -s ${PWD}/jupyter-config/nbmeta.json $VIRTUAL_ENV/etc/jupyter/nbconfig/notebook.d/
export JUPYCFG=$(jupyter --config-dir)
mkdir -p $JUPYCFG/serverconfig
cp customizations/notebook.json $JUPYCFG/serverconfig/
Optional extras:
rm -f $JUPYCFG/custom/custom.css
mkdir -p $JUPYCFG/custom
cp customizations/custom.css $JUPYCFG/custom/
…then restart jupyter
To have the magic available automatically in all notebooks, run:
aimagic_install
You can check it worked:
cat ~/.ipython/profile_default/ipython_config.py
# should print: c.InteractiveShellApp.extensions.append('aimagic')
If you want to tweak the look of aimagic uncomment the line that adds
the custom.css file to the custom subdirectory in your jupyter
config directory.
NB: this project requires nbclassic, or a notebook version less
than 7. It doesn’t work on Jupyter Lab, vscode, etc.
Other useful extensions
You might also like to add these extensions:
pip install nbclassic-collapsible-headings nbclassic-toc2
nbclassic-gist-it has to be installed from a local directory, at least for now, so cd to your git repo folder and run:
# First set $GIST_TOKEN or $GITHUB_TOKEN to a github token with access to gist
git clone git@github.com:AnswerDotAI/nbclassic-gist-it.git
cd nbclassic-gist-it
pip install .
How to use
If you didn’t run aimagic_install, then load the extension in your
notebook:
%load_ext aimagic
Then you can start asking the LLM questions. You’ll need the standard
LLM environment vars set up. You can call %ai as a line magic, which is
what we use for commands that don’t actually call AI, whereas %%ai
is used as a cell magic to ask the model a question.
If we %ai reset, then a new dialog is created.
Nifty features
%ai (line magic) commands
| command | description | usage |
|---|---|---|
| obj | return the magic object | o = %ai obj |
| dialog | create a markdown cell containing the dialog up to the previous cell | %ai dialog |
Put %ai skip as the first line of a cell, and that cell will be
skipped when creating Claude’s dialog. Other than that, the cell runs in
the usual way.
%%ai (cell magic) options
These options all also require that you include text on lines underneath
the %%ai line.
| option | description | usage |
|---|---|---|
| -t | see the prompt that is created for you. see below for more info. | %%ai -t |
| -nv | don’t include vars in the ai context | %%ai -nv |
Trace your prompt
Add -t after your %%ai calls (e.g. %%aip -t) to see the trace –
i.e. to see the prompt that is created for you. Or, to see the entire
session exactly as it’s sent to Claude, set this env var:
import os
os.environ['ANTHROPIC_LOG'] = 'debug'
Keyboard Shortcuts
Here’s a list of keyboard shortcuts you can use to improve your workflow. If you haven’t done so already follow the steps in the Customizations section above to setup the keyboard shortcuts.
| name | description | mode | multicell | shortcut |
|---|---|---|---|---|
| disable ai | add a 0 to an ai cell magic which disables the call to the ai | command | yes | q,d |
| extract code blocks | extract code blocks from the ai reply (must be used on the reply cell) | command | no | q,w |
| skip cell | add %ai skip to the top of all selected cells, which removes them from the ai context |
command | yes | q.s |
| create ai cell magic | prefix the selected cell with %%ai | command | no | q,1 |
Note:
- to make first time setup as easy as possible read the Customization section further up in the readme.
- the keyboard shortcut for each command above can be edited in
Edit Keyboard Shortcuts. - on multicell shortcuts such as skip cell, all cells will be set to the desired state of the first selected cell.
Troubleshooting
If any of the keyboard shortcuts above are not working, it’s possible that they are clashing with one of your existing shortcuts. To see if this is the case:
- Go to
Helpin the navbar and open upEdit Keyboard Shortcuts. - Confirm that the commands (e.g. disable ai) are listed. They should be towards the bottom of the list.
- If the commands are listed but no keys are next to
add shortcutthen a clash has occurred. - In many cases you can resolve this by scrolling up to the
close pagercommand and removing its shortcut by clickingx. - If you refresh your browser, and check
Edit Keyboard Shortcutsagain, the shortcuts for each command should be populated.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aimagic-0.1.0.tar.gz.
File metadata
- Download URL: aimagic-0.1.0.tar.gz
- Upload date:
- Size: 28.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22c3ed06ed55a01142a1a0d988b6ae2a105518f7e85c706c922194d73765aebf
|
|
| MD5 |
04c31bedb307151137c94eb026716107
|
|
| BLAKE2b-256 |
9ae9e2df6c88849cf6a2932f57247983eaf819ec853353f108aed2dadef4974a
|
File details
Details for the file aimagic-0.1.0-py3-none-any.whl.
File metadata
- Download URL: aimagic-0.1.0-py3-none-any.whl
- Upload date:
- Size: 25.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b52fbad4dabd4c11c4ab0df4c14b95c5df53cf63ff9aebea0ee7176aff750ac
|
|
| MD5 |
b5b869752f28ed52c02ecc1ba86dee4c
|
|
| BLAKE2b-256 |
b3fb5be1e04a0231f9077aad86460b4b609908f19b76f8dae43cdfbcd5a130f5
|