Skip to content

Commit

Permalink
export chat to markdown functionality added
Browse files Browse the repository at this point in the history
  • Loading branch information
apurvakhatri authored and srdas committed Mar 16, 2024
1 parent b18b7d0 commit 3ef11ce
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/jupyter-ai/jupyter_ai/chat_handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
from .generate import GenerateChatHandler
from .help import HelpChatHandler
from .learn import LearnChatHandler
from .export import ExportChatHandler
33 changes: 33 additions & 0 deletions packages/jupyter-ai/jupyter_ai/chat_handlers/export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from typing import List

from jupyter_ai.models import HumanChatMessage, AgentChatMessage

from .base import BaseChatHandler, SlashCommandRoutingType

class ExportChatHandler(BaseChatHandler):
id = "export"
name = "Export chat messages"
help = "Export the chat messages in various formats"
routing_type = SlashCommandRoutingType(slash_id="export")

uses_llm = False

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

def chat_message_to_markdown(self, message):
if isinstance(message, AgentChatMessage):
return f"**Agent**: {message.body}"
elif isinstance(message, HumanChatMessage):
return f"**{message.client.display_name}**: {message.body}"
else:
return ""


async def process_message(self, _):
markdown_content = "\n\n".join(self.chat_message_to_markdown(msg) for msg in self._chat_history)
# Write the markdown content to a file or do whatever you want with it
with open("./playground/chat_history.md", "w") as chat_history:
chat_history.write(markdown_content)


3 changes: 3 additions & 0 deletions packages/jupyter-ai/jupyter_ai/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
GenerateChatHandler,
HelpChatHandler,
LearnChatHandler,
ExportChatHandler,
)
from .chat_handlers.help import HelpMessage
from .completions.handlers import DefaultInlineCompletionHandler
Expand Down Expand Up @@ -239,12 +240,14 @@ def initialize_settings(self):
retriever = Retriever(learn_chat_handler=learn_chat_handler)
ask_chat_handler = AskChatHandler(**chat_handler_kwargs, retriever=retriever)

export_chat_handler = ExportChatHandler(**chat_handler_kwargs)
jai_chat_handlers = {
"default": default_chat_handler,
"/ask": ask_chat_handler,
"/clear": clear_chat_handler,
"/generate": generate_chat_handler,
"/learn": learn_chat_handler,
"/export": export_chat_handler,
}

help_chat_handler = HelpChatHandler(
Expand Down

0 comments on commit 3ef11ce

Please sign in to comment.