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

Save chat history to Jupyter Lab's root directory #770

Merged
merged 13 commits into from
May 8, 2024
4 changes: 4 additions & 0 deletions docs/source/users/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,10 @@ use the `-a` or `--all-files` option.
/learn -r arxiv 2404.18558
```

### Exporting chat history
Use the `/export` command to export the chat history from the current session to a markdown file named `chat_history-YYYY-MM-DD-HH-mm.md`. Using `/export <file_name>` will export the chat history to `<file_name>-YYYY-MM-DD-HH-mm.md` instead. You can export chat history as many times as you like in a single session. Each successive export will include the entire chat history up to that point in the session.


### Additional chat commands

To clear the chat panel, use the `/clear` command. This does not reset the AI model; the model may still remember previous messages that you sent it, and it may use them to inform its responses.
Expand Down
27 changes: 14 additions & 13 deletions packages/jupyter-ai/jupyter_ai/chat_handlers/export.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import argparse
import os
from datetime import datetime
from typing import List

from jupyter_ai.models import AgentChatMessage, HumanChatMessage
Expand All @@ -9,13 +11,15 @@
class ExportChatHandler(BaseChatHandler):
id = "export"
name = "Export chat messages"
help = "Export the chat messages in markdown format"
help = "Export the chat messages in markdown format with timestamps"
3coins marked this conversation as resolved.
Show resolved Hide resolved
routing_type = SlashCommandRoutingType(slash_id="export")

uses_llm = False

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.parser.prog = "/export"
self.parser.add_argument("path", nargs=argparse.REMAINDER)

def chat_message_to_markdown(self, message):
if isinstance(message, AgentChatMessage):
Expand All @@ -25,21 +29,18 @@ 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, _):
# Write the chat history to a markdown file with a timestamp
async def process_message(self, message: HumanChatMessage):
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
chat_filename = self.get_chat_filename()
args = self.parse_args(message)
if len(args.path) > 0:
file_id = args.path[0]
else:
file_id = "chat_history"
outfile = f"{file_id}-{datetime.now():%Y-%m-%d-%H-%M}.md"
chat_filename = os.path.join(self.root_dir, outfile)
with open(chat_filename, "w") as chat_history:
3coins marked this conversation as resolved.
Show resolved Hide resolved
chat_history.write(markdown_content)

Expand Down
Loading