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

Autocomplete UI for slash commands #810

Merged
merged 7 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion packages/jupyter-ai/jupyter_ai/chat_handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Literal,
Optional,
Type,
Union,
)
from uuid import uuid4

Expand All @@ -27,7 +28,7 @@

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


Expand Down
4 changes: 2 additions & 2 deletions packages/jupyter-ai/jupyter_ai/chat_handlers/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

class ExportChatHandler(BaseChatHandler):
id = "export"
name = "Export chat messages"
help = "Export the chat messages in markdown format with timestamps"
name = "Export chat history"
help = "Export chat history to a Markdown file"
routing_type = SlashCommandRoutingType(slash_id="export")

uses_llm = False
Expand Down
2 changes: 2 additions & 0 deletions packages/jupyter-ai/jupyter_ai/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
GlobalConfigHandler,
ModelProviderHandler,
RootChatHandler,
SlashCommandsInfoHandler,
)

JUPYTERNAUT_AVATAR_ROUTE = JupyternautPersona.avatar_route
Expand All @@ -45,6 +46,7 @@ class AiExtension(ExtensionApp):
(r"api/ai/config/?", GlobalConfigHandler),
(r"api/ai/chats/?", RootChatHandler),
(r"api/ai/chats/history?", ChatHistoryHandler),
(r"api/ai/chats/slash_commands?", SlashCommandsInfoHandler),
(r"api/ai/providers?", ModelProviderHandler),
(r"api/ai/providers/embeddings?", EmbeddingsModelProviderHandler),
(r"api/ai/completion/inline/?", DefaultInlineCompletionHandler),
Expand Down
54 changes: 52 additions & 2 deletions packages/jupyter-ai/jupyter_ai/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
from typing import TYPE_CHECKING, Dict, List, Optional

import tornado
from jupyter_ai.chat_handlers import BaseChatHandler
from jupyter_ai.chat_handlers import BaseChatHandler, SlashCommandRoutingType
from jupyter_ai.config_manager import ConfigManager, KeyEmptyError, WriteConflictError
from jupyter_server.base.handlers import APIHandler as BaseAPIHandler
from jupyter_server.base.handlers import JupyterHandler
from langchain.pydantic_v1 import ValidationError
from tornado import web, websocket
from tornado.web import HTTPError

from .completions.models import InlineCompletionRequest
from .models import (
AgentChatMessage,
ChatClient,
Expand All @@ -27,6 +26,8 @@
HumanChatMessage,
ListProvidersEntry,
ListProvidersResponse,
ListSlashCommandsEntry,
ListSlashCommandsResponse,
Message,
UpdateConfigRequest,
)
Expand Down Expand Up @@ -405,3 +406,52 @@ def delete(self, api_key_name: str):
self.config_manager.delete_api_key(api_key_name)
except Exception as e:
raise HTTPError(500, str(e))


class SlashCommandsInfoHandler(BaseAPIHandler):
"""List slash commands that are currently available to the user."""

@property
def config_manager(self) -> ConfigManager:
return self.settings["jai_config_manager"]

@property
def chat_handlers(self) -> Dict[str, "BaseChatHandler"]:
return self.settings["jai_chat_handlers"]

@web.authenticated
def get(self):
response = ListSlashCommandsResponse()

# if no selected LLM, return an empty response
if not self.config_manager.lm_provider:
self.finish(response.json())
return

for id, chat_handler in self.chat_handlers.items():
# filter out any chat handler that is not a slash command
if (
id == "default"
or chat_handler.routing_type.routing_method != "slash_command"
):
continue

# hint the type of this attribute
routing_type: SlashCommandRoutingType = chat_handler.routing_type

# filter out any chat handler that is unsupported by the current LLM
if (
"/" + routing_type.slash_id
in self.config_manager.lm_provider.unsupported_slash_commands
):
continue

response.slash_commands.append(
ListSlashCommandsEntry(
slash_id=routing_type.slash_id, description=chat_handler.help
)
)

# sort slash commands by slash id and deliver the response
response.slash_commands.sort(key=lambda sc: sc.slash_id)
self.finish(response.json())
9 changes: 9 additions & 0 deletions packages/jupyter-ai/jupyter_ai/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,12 @@ class GlobalConfig(BaseModel):
api_keys: Dict[str, str]
completions_model_provider_id: Optional[str]
completions_fields: Dict[str, Dict[str, Any]]


class ListSlashCommandsEntry(BaseModel):
slash_id: str
description: str


class ListSlashCommandsResponse(BaseModel):
slash_commands: List[ListSlashCommandsEntry] = []
Loading
Loading