Skip to content

Conversation

srdas
Copy link
Collaborator

@srdas srdas commented Jul 29, 2025

Refactored various functionality for the %ai and %%ai magics to use the new litellm backbone added in PR #1426 . Fixes Issue #1432

  1. Updated the function run_ai_cell to invoke litellm instead of langchain.
  2. Modified the register command to alias which is more natural when registering an alias for a model_id.
  3. Modified the delete command to dealias which is more natural.
  4. Did not remove the update command as it is working well and is convenient.
  5. Checked that the help is working and shows the updated commands.
  6. Refactored the list command to show model IDs and aliases.
  7. Checked that the reset command is working.
  8. Rename default_language_model to initial_language_model.
  9. Rename aliases to initial_aliases.
  10. Changed error command to fix, updated it's handling.
  11. Confirmed that this works with new API keys handling for litellm using the .env approach.

See the demo below showing all of it working:

Screen.Recording.2025-07-30.at.2.30.01.PM.mov

Nice to have (among other items):

  1. Update format options to be much more stable using agentic capabilities with better prompts.
  2. See if it is possible to add IPython widgets as another output format option.
  3. Update markdown format to handle tables.
  4. See other TODO commented items in magics.py for consideration.

@srdas srdas added the enhancement New feature or request label Jul 29, 2025
Copy link
Member

@dlqqq dlqqq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srdas This looks great, thank you! Just a few comments. Haven't tested it yet, have to join standup now.

@srdas srdas marked this pull request as ready for review August 12, 2025 00:32
Copy link
Member

@dlqqq dlqqq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srdas This is a great start, thank you! Just one extra comment below.

In addition, I've opened some follow-up issues (titled [magics] ...) for you to explore. They can be addressed in separate PRs.

This is already a fantastic start, and I'm excited to merge this once the last comment is addressed.

Comment on lines +369 to +370
self.transcript.append({"role": "user", "content": prompt})
self.transcript.append({"role": "assistant", "content": output})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you bound this list to be of length self.max_history * 2? Right now, the ai() method is manually truncating self.transcript every time it appends it to the chat history. It would be more reliable to add some logic to bound the length of self.transcript in the _append_exchange() method itself.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is now done. Tested everything to make sure it is working. Here is a notebook if you want to test all the magics features (unzip to use).
magics_litellm.ipynb.zip

@srdas srdas merged commit 4aa7ce4 into jupyterlab:litellm Aug 12, 2025
4 of 8 checks passed
jonahjung22 pushed a commit to jonahjung22/jupyter-ai that referenced this pull request Aug 15, 2025
…rlab#1437)

* Adding `magics` functionality to the `litellm` implementation

* updates for initializing aliases and resolving types, mypy

* Update magics.py

* init alias

* Update test_magics.py

* Update magics.py

* Changed `error` to `fix` in magics

* Update index.md

* Update test_magics.py

* Update magics.py

* streamlining magics

* removed update command

* initial_chat_model

* magics pre-commit

* Update magics.py
@cszhbo
Copy link

cszhbo commented Aug 21, 2025

Is there any example about how to use litellm with %%ai magic?
eg. %%ai litellm:glm4-flash ...

@srdas
Copy link
Collaborator Author

srdas commented Aug 21, 2025

@cszhbo The refactoring of Jupyter AI to use litellm is still WIP. If you'd like to try it, please pull the litellm branch (PR #1426 ) and then install it using

./scripts/dev-setup.sh

See docs: https://jupyter-ai.readthedocs.io/en/v3/contributors/index.html#automatic-development-setup-recommended

To your question about usage examples: This video shows the new workflow with chat and magics. The vast range of models offered by litellm is apparent. (Of course you would need API keys for the providers, you can see the simplified UI for entering these keys in the screen for AI Settings below.)

Screen.Recording.2025-08-21.at.7.45.49.AM.mov

Hope this helps!

@cszhbo
Copy link

cszhbo commented Aug 21, 2025

It is very helpful. Appreciate your detailed demonstration.

dlqqq added a commit that referenced this pull request Aug 21, 2025
* [litellm] Initial migration to LiteLLM (#1425)

* add new /api/ai/models endpoint

* add litellm dependency

* add new model ID input component

* fix custom model ID input

* update ConfigManager to accept litellm model IDs

* update Jupyternaut to use litellm

* remove LangChain partner packages from dependencies

* remove LangChain provider entry points

* pre-commit

* rename /api/ai/models => /api/ai/models/chat

* simplify model settings UI

* [litellm] Remove LangChain from source code (#1435)

* remove LangChain from jupyter_ai_magics

* remove unused useServerInfo() hook in frontend

* remove LangChain references from jupyter_ai

* remove langchain from test & cookiecutter packages

* move all completions code to jupyter_ai.completions

* remove LangChain provider code from ConfigManager

* minor fixes to allow JAI to start

* Implementing model parameters input

* Implement new Secrets UI and REST API (#1439)

* add EnvSecretsManager and secrets REST API

* make ListSecrets endpoint more generic

* fix UpdateSecretsRequest type

* fix bug where last secret cannot be deleted

* remove error log used in dev

* add new <SecretsSection /> component

* tweak UI appearance

* make settings page scrollable, fix #1409

* show no static secrets by default

* show something when no secrets are present

* adjust language

* fix success & error alerts on updating secrets

* fix lint

* fix bug when .gitignore and .env do not exist

* bump to 2s poll interval in EnvSecretsManager

* disable model parameters input for now, fix merge conflicts

* Adding `magics` functionality to the `litellm` implementation (#1437)

* Adding `magics` functionality to the `litellm` implementation

* updates for initializing aliases and resolving types, mypy

* Update magics.py

* init alias

* Update test_magics.py

* Update magics.py

* Changed `error` to `fix` in magics

* Update index.md

* Update test_magics.py

* Update magics.py

* streamlining magics

* removed update command

* initial_chat_model

* magics pre-commit

* Update magics.py

* missing model id in magics (#1457)

Thanks David for approving this.

* Added sublists for providers using %ai (#1458)

* Fix `litellm` branch CI, add `EnvSecretsManager` tests (#1459)

* update prettier ignore file

* fix prettier errors & warnings

* fix mypy errors in jupyter_ai

* skip ConfigManager and inline completion tests until fixed in v3

* add tests for secrets manager

* fix mypy error

* run minimum dependency tests on Python 3.10

* comment out precommit job to be added in future

* updated test_magics.py

---------

Co-authored-by: Sanjiv Das <[email protected]>

* [litellm] Display model IDs to match search string  (#1445)

* Enable the use of API Keys in magics from the `.env` file (#1465)

* magics to use dotenv

* Update magics.py

* Update magics.py

* Add model parameters input (#1456)

* start of backend implementation

* fixes api code

* implemented type mapping

* integrating api with ui

* Working prototype of the model parameter input component with correct model parameters and dropdown feature!

* added api to save parameters to config and removed old model-fields

* code debugging, changing UI, and adding success error message

* latest changes

* fix type safety issues in model-parameters-input

* fix frontend lint checks, update comment

* fix mypy errors, add comments

* small type fix

* Updated coercion typing, improved code functionality, removed unnecessary code

* pass chat model arguments to Jupyternaut

* delete/update existing parameter

* hide params controlling tool usage from frontend

---------

Co-authored-by: David L. Qiu <[email protected]>

* Clean up AI settings page (#1468)

* Clean up AI settings page

* remove debug line added by accident

* Move LiteLLM model list to `jupyter_ai_magics` (#1469)

* move model list to jupyter_ai_magics

* fix mypy

---------

Co-authored-by: Jonah Jung <[email protected]>
Co-authored-by: Sanjiv Das <[email protected]>
Co-authored-by: Jonah Jung <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants