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

Use jupyterlab-collaborative-chat #1043

Open
wants to merge 15 commits into
base: v3-dev
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from jupyter_ai.chat_handlers.base import BaseChatHandler, SlashCommandRoutingType
from jupyter_ai.models import HumanChatMessage

try:
from jupyterlab_collaborative_chat.ychat import YChat
except:
from typing import Any as YChat


class TestSlashCommand(BaseChatHandler):
"""
Expand All @@ -25,5 +30,5 @@ class TestSlashCommand(BaseChatHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

async def process_message(self, message: HumanChatMessage):
async def process_message(self, message: HumanChatMessage, chat: YChat):
self.reply("This is the `/test` slash command.")
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from jupyter_ai.chat_handlers.base import BaseChatHandler, SlashCommandRoutingType
from jupyter_ai.models import HumanChatMessage

try:
from jupyterlab_collaborative_chat.ychat import YChat
except:
from typing import Any as YChat


class TestSlashCommand(BaseChatHandler):
"""
Expand All @@ -25,5 +30,5 @@ class TestSlashCommand(BaseChatHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

async def process_message(self, message: HumanChatMessage):
self.reply("This is the `/test` slash command.")
async def process_message(self, message: HumanChatMessage, chat: YChat):
self.reply("This is the `/test` slash command.", chat)
5 changes: 5 additions & 0 deletions packages/jupyter-ai/jupyter_ai/chat_handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# The following import is to make sure jupyter_ydoc is imported before
# jupyterlab_collaborative_chat, otherwise it leads to circular import because of the
# YChat relying on YBaseDoc, and jupyter_ydoc registering YChat from the entry point.
import jupyter_ydoc

from .ask import AskChatHandler
from .base import BaseChatHandler, SlashCommandRoutingType
from .clear import ClearChatHandler
Expand Down
19 changes: 12 additions & 7 deletions packages/jupyter-ai/jupyter_ai/chat_handlers/ask.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import argparse
from typing import Dict, Type
from typing import Dict, Optional, Type

from jupyter_ai.models import HumanChatMessage
from jupyter_ai_magics.providers import BaseProvider
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferWindowMemory
from langchain_core.prompts import PromptTemplate

try:
from jupyterlab_collaborative_chat.ychat import YChat
except:
from typing import Any as YChat

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 Down Expand Up @@ -59,32 +64,32 @@ def create_llm_chain(
verbose=False,
)

async def process_message(self, message: HumanChatMessage):
args = self.parse_args(message)
async def process_message(self, message: HumanChatMessage, chat: Optional[YChat]):
args = self.parse_args(message, chat)
if args is None:
return
query = " ".join(args.query)
if not query:
self.reply(f"{self.parser.format_usage()}", message)
self.reply(f"{self.parser.format_usage()}", chat, message)
return

self.get_llm_chain()

try:
with self.pending("Searching learned documents", message):
with self.pending("Searching learned documents", message, chat=chat):
assert self.llm_chain
# TODO: migrate this class to use a LCEL `Runnable` instead of
# `Chain`, then remove the below ignore comment.
result = await self.llm_chain.acall( # type:ignore[attr-defined]
{"question": query}
)
response = result["answer"]
self.reply(response, message)
self.reply(response, chat, message)
except AssertionError as e:
self.log.error(e)
response = """Sorry, an error occurred while reading the from the learned documents.
If you have changed the embedding provider, try deleting the existing index by running
`/learn -d` command and then re-submitting the `learn <directory>` to learn the documents,
and then asking the question again.
"""
self.reply(response, message)
self.reply(response, chat, message)
Loading
Loading