-
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* export chat to markdown functionality added * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update export.py - Added multiple chat history files - changed export to mention markdown files only - respond with filename in chat * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Edits to /export function Using Markdown formatting when displaying file paths --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sanjiv Das <[email protected]>
- Loading branch information
1 parent
b18b7d0
commit 4814904
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import os | ||
from typing import List | ||
|
||
from jupyter_ai.models import AgentChatMessage, HumanChatMessage | ||
|
||
from .base import BaseChatHandler, SlashCommandRoutingType | ||
|
||
|
||
class ExportChatHandler(BaseChatHandler): | ||
id = "export" | ||
name = "Export chat messages" | ||
help = "Export the chat messages in markdown format" | ||
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 "" | ||
|
||
# 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 | ||
chat_filename = self.get_chat_filename() | ||
with open(chat_filename, "w") as chat_history: | ||
chat_history.write(markdown_content) | ||
|
||
self.reply(f"File saved to `{chat_filename}`") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters