Skip to content

Commit

Permalink
Update export.py
Browse files Browse the repository at this point in the history
- Added multiple chat history files
- changed export to mention markdown files only
- respond with filename in chat
  • Loading branch information
srdas committed Mar 16, 2024
1 parent 774849d commit 988a265
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions packages/jupyter-ai/jupyter_ai/chat_handlers/export.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from typing import List

from jupyter_ai.models import AgentChatMessage, HumanChatMessage

from .base import BaseChatHandler, SlashCommandRoutingType

import os

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

uses_llm = False
Expand All @@ -24,10 +22,22 @@ def chat_message_to_markdown(self, message):
else:
return ""

# Multiple chat histories in separate files
def get_chat_filename(self, path='./chat_history.md'):
filename, extension = os.path.splitext(path)
counter = 1
while os.path.exists(path):
path = filename + "_" + str(counter) + ".md"
counter += 1
return path

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_filename = self.get_chat_filename()
with open(chat_filename, "w") as chat_history:
chat_history.write(markdown_content)

self.reply("File saved to " + chat_filename)

0 comments on commit 988a265

Please sign in to comment.