Skip to content

Commit c047e5c

Browse files
authored
Make magic aliases user-customizable (#901)
1 parent 12d069e commit c047e5c

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

docs/source/users/index.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,16 @@ Aliases' names can contain ASCII letters (uppercase and lowercase), numbers, hyp
839839

840840
Aliases must refer to models or `LLMChain` objects; they cannot refer to other aliases.
841841

842+
To customize the aliases on startup you can set the `c.AiMagics.aliases` tratilet in `ipython_config.py`, for example:
843+
844+
```python
845+
c.AiMagics.aliases = {
846+
"my_custom_alias": "my_provider:my_model"
847+
}
848+
```
849+
850+
The location of `ipython_config.py` file is documented in [IPython configuration reference](https://ipython.readthedocs.io/en/stable/config/intro.html).
851+
842852
### Using magic commands with SageMaker endpoints
843853

844854
You can use magic commands with models hosted using Amazon SageMaker.

packages/jupyter-ai-magics/jupyter_ai_magics/magics.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from typing import Optional
99

1010
import click
11+
import traitlets
1112
from IPython import get_ipython
1213
from IPython.core.magic import Magics, line_cell_magic, magics_class
1314
from IPython.display import HTML, JSON, Markdown, Math
@@ -122,6 +123,19 @@ class CellMagicError(BaseException):
122123

123124
@magics_class
124125
class AiMagics(Magics):
126+
127+
aliases = traitlets.Dict(
128+
default_value=MODEL_ID_ALIASES,
129+
value_trait=traitlets.Unicode(),
130+
key_trait=traitlets.Unicode(),
131+
help="""Aliases for model identifiers.
132+
133+
Keys define aliases, values define the provider and the model to use.
134+
The values should include identifiers in in the `provider:model` format.
135+
""",
136+
config=True,
137+
)
138+
125139
def __init__(self, shell):
126140
super().__init__(shell)
127141
self.transcript_openai = []
@@ -145,7 +159,7 @@ def __init__(self, shell):
145159
self.providers = get_lm_providers()
146160

147161
# initialize a registry of custom model/chain names
148-
self.custom_model_registry = MODEL_ID_ALIASES
162+
self.custom_model_registry = self.aliases
149163

150164
def _ai_bulleted_list_models_for_provider(self, provider_id, Provider):
151165
output = ""
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from IPython import InteractiveShell
2+
from traitlets.config.loader import Config
3+
4+
5+
def test_aliases_config():
6+
ip = InteractiveShell()
7+
ip.config = Config()
8+
ip.config.AiMagics.aliases = {"my_custom_alias": "my_provider:my_model"}
9+
ip.extension_manager.load_extension("jupyter_ai_magics")
10+
providers_list = ip.run_line_magic("ai", "list").text
11+
assert "my_custom_alias" in providers_list

0 commit comments

Comments
 (0)