Developers#

The developer documentation is for authors who want to enhance the functionality of Jupyter AI.

If you are interested in contributing to Jupyter AI, please see our contributor’s guide.

Pydantic compatibility#

Jupyter AI is fully compatible with Python environments using Pydantic v1 or Pydantic v2. Jupyter AI imports Pydantic classes from the langchain.pydantic_v1 module. Developers should do the same when they extend Jupyter AI classes.

For more details about using langchain.pydantic_v1 in an environment with Pydantic v2 installed, see the LangChain documentation on Pydantic compatibility.

Jupyter AI module cookiecutter#

We offer a cookiecutter template that can be used to generate a pre-configured Jupyter AI module. This is a Python package that exposes a template model provider and slash command for integration with Jupyter AI. Developers can then extend the generated AI module however they wish.

To generate a new AI module using the cookiecutter, run these commands from the repository root:

pip install cookiecutter
cd packages/
cookiecutter jupyter-ai-module-cookiecutter

The last command will open a wizard that allows you to set the package name and a few other metadata fields. By default, the package will have the name jupyter-ai-test.

To install your new AI module locally and use the generated template provider and slash command:

cd jupyter-ai-test/
pip install -e .

You will then be able to use the test provider and slash command after restarting JupyterLab.

The remainder of this documentation page elaborates on how to define a custom model provider and slash command.

Custom model providers#

You can define new providers using the LangChain framework API. Custom providers inherit from both jupyter-ai’s BaseProvider and langchain’s LLM. You can either import a pre-defined model from LangChain LLM list, or define a custom LLM. In the example below, we define a provider with two models using a dummy FakeListLLM model, which returns responses from the responses keyword argument.

# my_package/my_provider.py
from jupyter_ai_magics import BaseProvider
from langchain_community.llms import FakeListLLM


class MyProvider(BaseProvider, FakeListLLM):
    id = "my_provider"
    name = "My Provider"
    model_id_key = "model"
    models = [
        "model_a",
        "model_b"
    ]
    def __init__(self, **kwargs):
        model = kwargs.get("model_id")
        kwargs["responses"] = (
            ["This is a response from model 'a'"]
            if model == "model_a" else
            ["This is a response from model 'b'"]
        )
        super().__init__(**kwargs)

If the new provider inherits from BaseChatModel, it will be available both in the chat UI and with magic commands. Otherwise, users can only use the new provider with magic commands.

To make the new provider available, you need to declare it as an entry point:

# my_package/pyproject.toml
[project]
name = "my_package"
version = "0.0.1"

[project.entry-points."jupyter_ai.model_providers"]
my-provider = "my_provider:MyProvider"

To test that the above minimal provider package works, install it with:

# from `my_package` directory
pip install -e .

Then, restart JupyterLab. You should now see an info message in the log that mentions your new provider’s id:

[I 2023-10-29 13:56:16.915 AiExtension] Registered model provider `my_provider`.

Custom embeddings providers#

To provide a custom embeddings model an embeddings providers should be defined implementing the API of jupyter-ai’s BaseEmbeddingsProvider and of langchain’s Embeddings abstract class.

from jupyter_ai_magics import BaseEmbeddingsProvider
from langchain.embeddings import FakeEmbeddings

class MyEmbeddingsProvider(BaseEmbeddingsProvider, FakeEmbeddings):
    id = "my_embeddings_provider"
    name = "My Embeddings Provider"
    model_id_key = "model"
    models = ["my_model"]

    def __init__(self, **kwargs):
        super().__init__(size=300, **kwargs)

Jupyter AI uses entry points to discover embedding providers. In the pyproject.toml file, add your custom embedding provider to the [project.entry-points."jupyter_ai.embeddings_model_providers"] section:

[project.entry-points."jupyter_ai.embeddings_model_providers"]
my-provider = "my_provider:MyEmbeddingsProvider"

Prompt templates#

Each provider can define prompt templates for each supported format. A prompt template guides the language model to produce output in a particular format. The default prompt templates are a Python dictionary mapping formats to templates. Developers who write subclasses of BaseProvider can override templates per output format, per model, and based on the prompt being submitted, by implementing their own get_prompt_template function. Each prompt template includes the string {prompt}, which is replaced with the user-provided prompt when the user runs a magic command.

Customizing prompt templates#

To modify the prompt template for a given format, override the get_prompt_template method:

from langchain.prompts import PromptTemplate


class MyProvider(BaseProvider, FakeListLLM):
    # (... properties as above ...)
    def get_prompt_template(self, format) -> PromptTemplate:
        if format === "code":
            return PromptTemplate.from_template(
                "{prompt}\n\nProduce output as source code only, "
                "with no text or explanation before or after it."
            )
        return super().get_prompt_template(format)

Please note that this will only work with Jupyter AI magics (the %ai and %%ai magic commands). Custom prompt templates are not used in the chat interface yet.

Custom slash commands in the chat UI#

You can add a custom slash command to the chat interface by creating a new class that inherits from BaseChatHandler. Set its id, name, help message for display in the user interface, and routing_type. Each custom slash command must have a unique slash command. Slash commands can only contain ASCII letters, numerals, and underscores. Each slash command must be unique; custom slash commands cannot replace built-in slash commands.

Add your custom handler in Python code:

from jupyter_ai.chat_handlers.base import BaseChatHandler, SlashCommandRoutingType
from jupyter_ai.models import HumanChatMessage

class CustomChatHandler(BaseChatHandler):
    id = "custom"
    name = "Custom"
    help = "A chat handler that does something custom"
    routing_type = SlashCommandRoutingType(slash_id="custom")

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    async def process_message(self, message: HumanChatMessage):
        # Put your custom logic here
        self.reply("<your-response>", message)

Jupyter AI uses entry points to support custom slash commands. In the pyproject.toml file, add your custom handler to the [project.entry-points."jupyter_ai.chat_handlers"] section:

[project.entry-points."jupyter_ai.chat_handlers"]
custom = "custom_package:CustomChatHandler"

Then, install your package so that Jupyter AI adds custom chat handlers to the existing chat handlers.