Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base chat handler refactor for custom slash commands #398

Merged
merged 35 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
162815f
Adds attributes, starts adding to subclasses
JasonWeill Sep 21, 2023
abaf3c6
Consistent syntax
JasonWeill Sep 21, 2023
9aa348f
Help for all handlers
JasonWeill Sep 21, 2023
5086b3c
Fix slash ID error
JasonWeill Sep 21, 2023
fd8529f
Iterate through entry points
JasonWeill Sep 21, 2023
9084554
Fix typo in call to select()
JasonWeill Sep 25, 2023
ffbc583
Moves config to magics, modifies extensions to attempt to load classes
JasonWeill Sep 27, 2023
87db726
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 27, 2023
bfde43f
Moves config to proper location, improves error logging
JasonWeill Sep 28, 2023
e5abafe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 19, 2023
f053e08
WIP: Updates per feedback, adds custom handler
JasonWeill Oct 24, 2023
526366c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 24, 2023
963495b
Removes redundant code, style fixes
JasonWeill Oct 24, 2023
7caf1ef
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 24, 2023
1709d21
Removes unnecessary custom message
JasonWeill Oct 25, 2023
0a6ab9d
Instantiates class
JasonWeill Oct 25, 2023
daecd57
Validates slash ID
JasonWeill Oct 25, 2023
b7b5501
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 25, 2023
a4f33b6
Consistent arguments to chat handlers
JasonWeill Oct 25, 2023
6fcf87e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 25, 2023
178b0be
Refactors to avoid intentionally unused params
JasonWeill Nov 2, 2023
29d4964
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 2, 2023
91a50cb
Updates docs, removes custom handler from source and config
JasonWeill Nov 6, 2023
e04e09b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 6, 2023
7e4d597
Renames process_message to match base class
JasonWeill Nov 6, 2023
3a031e4
Adds needed parameter that had been deleted
JasonWeill Dec 4, 2023
ffb77a1
Joins lines in contributor doc
JasonWeill Dec 5, 2023
74c6d21
Removes natural language routing type, which is not yet used
JasonWeill Dec 5, 2023
bea1f99
Update docs/source/developers/index.md
JasonWeill Dec 6, 2023
1589146
Update docs/source/developers/index.md
JasonWeill Dec 6, 2023
dedeb6f
Update docs/source/developers/index.md
JasonWeill Dec 6, 2023
7fcaa7b
Revises per @3coins, avoids Latinism
JasonWeill Dec 6, 2023
7c98f4e
Removes Configurable, since we do not yet have configurable traits
JasonWeill Dec 6, 2023
55b3f08
Uses Literal for validation
JasonWeill Dec 6, 2023
ef97045
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions docs/source/developers/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,45 @@ class MyProvider(BaseProvider, FakeListLLM):
```

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:

```python
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
JasonWeill marked this conversation as resolved.
Show resolved Hide resolved
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.
2 changes: 1 addition & 1 deletion packages/jupyter-ai/jupyter_ai/chat_handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .ask import AskChatHandler
from .base import BaseChatHandler
from .base import BaseChatHandler, SlashCommandRoutingType
from .clear import ClearChatHandler
from .default import DefaultChatHandler
from .generate import GenerateChatHandler
Expand Down
7 changes: 6 additions & 1 deletion packages/jupyter-ai/jupyter_ai/chat_handlers/ask.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from langchain.memory import ConversationBufferWindowMemory
from langchain.prompts import PromptTemplate

from .base import BaseChatHandler
from .base import BaseChatHandler, SlashCommandRoutingType

PROMPT_TEMPLATE = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.

Expand All @@ -26,6 +26,11 @@ class AskChatHandler(BaseChatHandler):
to the LLM to generate the final reply.
"""

id = "ask"
name = "Ask with Local Data"
help = "Asks a question with retrieval augmented generation (RAG)"
routing_type = SlashCommandRoutingType(slash_id="ask")

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

Expand Down
54 changes: 50 additions & 4 deletions packages/jupyter-ai/jupyter_ai/chat_handlers/base.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,81 @@
import argparse
import os
import time
import traceback

# necessary to prevent circular import
from typing import TYPE_CHECKING, Any, Dict, Optional, Type
from typing import (
TYPE_CHECKING,
Awaitable,
ClassVar,
Dict,
List,
Literal,
Optional,
Type,
)
from uuid import uuid4

from dask.distributed import Client as DaskClient
from jupyter_ai.config_manager import ConfigManager, Logger
from jupyter_ai.models import AgentChatMessage, HumanChatMessage
from jupyter_ai.models import AgentChatMessage, ChatMessage, HumanChatMessage
from jupyter_ai_magics.providers import BaseProvider

# necessary to prevent circular import
from pydantic import BaseModel

if TYPE_CHECKING:
from jupyter_ai.handlers import RootChatHandler


# Chat handler type, with specific attributes for each
class HandlerRoutingType(BaseModel):
routing_method: ClassVar[str] = Literal["slash_command"]
"""The routing method that sends commands to this handler."""


class SlashCommandRoutingType(HandlerRoutingType):
routing_method = "slash_command"

slash_id: Optional[str]
"""Slash ID for routing a chat command to this handler. Only one handler
may declare a particular slash ID. Must contain only alphanumerics and
underscores."""


class BaseChatHandler:
"""Base ChatHandler class containing shared methods and attributes used by
multiple chat handler classes."""

# Class attributes
id: ClassVar[str] = ...
"""ID for this chat handler; should be unique"""

name: ClassVar[str] = ...
"""User-facing name of this handler"""

help: ClassVar[str] = ...
"""What this chat handler does, which third-party models it contacts,
the data it returns to the user, and so on, for display in the UI."""

routing_type: HandlerRoutingType = ...

def __init__(
self,
log: Logger,
config_manager: ConfigManager,
root_chat_handlers: Dict[str, "RootChatHandler"],
model_parameters: Dict[str, Dict],
chat_history: List[ChatMessage],
root_dir: str,
dask_client_future: Awaitable[DaskClient],
):
self.log = log
self.config_manager = config_manager
self._root_chat_handlers = root_chat_handlers
self.model_parameters = model_parameters
self._chat_history = chat_history
self.parser = argparse.ArgumentParser()
self.root_dir = os.path.abspath(os.path.expanduser(root_dir))
self.dask_client_future = dask_client_future
self.llm = None
self.llm_params = None
self.llm_chain = None
JasonWeill marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
10 changes: 7 additions & 3 deletions packages/jupyter-ai/jupyter_ai/chat_handlers/clear.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

from jupyter_ai.models import ChatMessage, ClearMessage

from .base import BaseChatHandler
from .base import BaseChatHandler, SlashCommandRoutingType


class ClearChatHandler(BaseChatHandler):
def __init__(self, chat_history: List[ChatMessage], *args, **kwargs):
id = "clear"
name = "Clear chat messages"
help = "Clears the displayed chat message history only; does not clear the context sent to chat providers"
routing_type = SlashCommandRoutingType(slash_id="clear")

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

async def process_message(self, _):
self._chat_history.clear()
Expand Down
14 changes: 9 additions & 5 deletions packages/jupyter-ai/jupyter_ai/chat_handlers/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
SystemMessagePromptTemplate,
)

from .base import BaseChatHandler
from .base import BaseChatHandler, SlashCommandRoutingType

SYSTEM_PROMPT = """
You are Jupyternaut, a conversational assistant living in JupyterLab to help users.
Expand All @@ -32,10 +32,14 @@


class DefaultChatHandler(BaseChatHandler):
def __init__(self, chat_history: List[ChatMessage], *args, **kwargs):
id = "default"
name = "Default"
help = "Responds to prompts that are not otherwise handled by a chat handler"
routing_type = SlashCommandRoutingType(slash_id=None)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.memory = ConversationBufferWindowMemory(return_messages=True, k=2)
self.chat_history = chat_history

def create_llm_chain(
self, provider: Type[BaseProvider], provider_params: Dict[str, str]
Expand Down Expand Up @@ -80,8 +84,8 @@ def clear_memory(self):
self.reply(reply_message)

# clear transcript for new chat clients
if self.chat_history:
self.chat_history.clear()
if self._chat_history:
self._chat_history.clear()

async def process_message(self, message: HumanChatMessage):
self.get_llm_chain()
Expand Down
10 changes: 6 additions & 4 deletions packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Dict, List, Optional, Type

import nbformat
from jupyter_ai.chat_handlers import BaseChatHandler
from jupyter_ai.chat_handlers import BaseChatHandler, SlashCommandRoutingType
from jupyter_ai.models import HumanChatMessage
from jupyter_ai_magics.providers import BaseProvider
from langchain.chains import LLMChain
Expand Down Expand Up @@ -216,11 +216,13 @@ def create_notebook(outline):


class GenerateChatHandler(BaseChatHandler):
"""Generates a Jupyter notebook given a description."""
id = "generate"
name = "Generate Notebook"
help = "Generates a Jupyter notebook, including name, outline, and section contents"
routing_type = SlashCommandRoutingType(slash_id="generate")

def __init__(self, root_dir: str, *args, **kwargs):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.root_dir = os.path.abspath(os.path.expanduser(root_dir))
self.llm = None

def create_llm_chain(
Expand Down
7 changes: 6 additions & 1 deletion packages/jupyter-ai/jupyter_ai/chat_handlers/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from jupyter_ai.models import AgentChatMessage, HumanChatMessage

from .base import BaseChatHandler
from .base import BaseChatHandler, SlashCommandRoutingType

HELP_MESSAGE = """Hi there! I'm Jupyternaut, your programming assistant.
You can ask me a question using the text box below. You can also use these commands:
Expand All @@ -29,6 +29,11 @@ def HelpMessage():


class HelpChatHandler(BaseChatHandler):
id = "help"
name = "Help"
help = "Displays a help message in the chat message area"
routing_type = SlashCommandRoutingType(slash_id="help")

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

Expand Down
13 changes: 7 additions & 6 deletions packages/jupyter-ai/jupyter_ai/chat_handlers/learn.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@
)
from langchain.vectorstores import FAISS

from .base import BaseChatHandler
from .base import BaseChatHandler, SlashCommandRoutingType

INDEX_SAVE_DIR = os.path.join(jupyter_data_dir(), "jupyter_ai", "indices")
METADATA_SAVE_PATH = os.path.join(INDEX_SAVE_DIR, "metadata.json")


class LearnChatHandler(BaseChatHandler):
def __init__(
self, root_dir: str, dask_client_future: Awaitable[DaskClient], *args, **kwargs
):
id = "learn"
name = "Learn Local Data"
help = "Pass a list of files and directories. Once converted to vector format, you can ask about them with /ask."
routing_type = SlashCommandRoutingType(slash_id="learn")

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.root_dir = root_dir
self.dask_client_future = dask_client_future
self.parser.prog = "/learn"
self.parser.add_argument("-a", "--all-files", action="store_true")
self.parser.add_argument("-v", "--verbose", action="store_true")
Expand Down
Loading
Loading