From 9aa9afea6e46940519c0f8fce46f6374de850228 Mon Sep 17 00:00:00 2001 From: andrewfulton9 Date: Mon, 8 Jul 2024 16:55:18 +0000 Subject: [PATCH 01/13] add preferred dir functionality to BaseChatHandler --- .../jupyter_ai/chat_handlers/base.py | 25 +++++++++++++++++++ .../jupyter_ai/chat_handlers/export.py | 2 +- .../jupyter_ai/chat_handlers/generate.py | 16 ++---------- packages/jupyter-ai/jupyter_ai/extension.py | 2 +- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py index 97392168a..84ed0b93d 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py @@ -48,6 +48,18 @@ class SlashCommandRoutingType(HandlerRoutingType): underscores.""" +def get_preferred_dir(root_dir: str, preferred_dir: str): + if preferred_dir != "": + preferred_dir = os.path.expanduser(preferred_dir) + if not preferred_dir.startswith(root_dir): + preferred_dir = os.path.join(root_dir, preferred_dir) + return os.path.abspath(preferred_dir) + return None + + + + + class BaseChatHandler: """Base ChatHandler class containing shared methods and attributes used by multiple chat handler classes.""" @@ -82,6 +94,7 @@ def __init__( model_parameters: Dict[str, Dict], chat_history: List[ChatMessage], root_dir: str, + preferred_dir: Optional[str], dask_client_future: Awaitable[DaskClient], ): self.log = log @@ -91,11 +104,14 @@ def __init__( self._chat_history = chat_history self.parser = argparse.ArgumentParser() self.root_dir = os.path.abspath(os.path.expanduser(root_dir)) + self.preferred_dir = get_preferred_dir(self.root_dir, preferred_dir) + print(self.preferred_dir) self.dask_client_future = dask_client_future self.llm = None self.llm_params = None self.llm_chain = None + async def on_message(self, message: HumanChatMessage): """ Method which receives a human message, calls `self.get_llm_chain()`, and @@ -300,3 +316,12 @@ def parse_args(self, message): self.reply(response, message) return None return args + + @property + def _output_dir(self): + # preferred dir is preferred, but if it is not specified, + # or if user removed it after startup, fallback to root. + if self.preferred_dir and os.path.exists(self.preferred_dir): + return self.preferred_dir + else: + return self.root_dir diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/export.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/export.py index c51a3a3e2..ef9e050cd 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/export.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/export.py @@ -42,7 +42,7 @@ async def process_message(self, message: HumanChatMessage): else f"chat_history-{datetime.now():%Y-%m-%d-%H-%M-%S}.md" ) # Handles both empty args and double tap key chat_file = os.path.join( - self.root_dir, chat_filename + self._output_dir, chat_filename ) # Do not use timestamp if filename is entered as argument with open(chat_file, "w") as chat_history: chat_history.write(markdown_content) diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py index 520f7c614..02ddbc197 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py @@ -223,14 +223,9 @@ class GenerateChatHandler(BaseChatHandler): uses_llm = True - def __init__(self, preferred_dir: str, log_dir: Optional[str], *args, **kwargs): + def __init__(self, log_dir: Optional[str], *args, **kwargs): super().__init__(*args, **kwargs) self.log_dir = Path(log_dir) if log_dir else None - self.preferred_dir = ( - os.path.abspath(os.path.expanduser(preferred_dir)) - if preferred_dir != "" - else None - ) self.llm = None def create_llm_chain( @@ -289,11 +284,4 @@ async def handle_exc(self, e: Exception, message: HumanChatMessage): response = f"An error occurred while generating the notebook. The error details have been saved to `./{log_path}`.\n\nTry running `/generate` again, as some language models require multiple attempts before a notebook is generated." self.reply(response, message) - @property - def _output_dir(self): - # preferred dir is preferred, but if it is not specified, - # or if user removed it after startup, fallback to root. - if self.preferred_dir and os.path.exists(self.preferred_dir): - return self.preferred_dir - else: - return self.root_dir + diff --git a/packages/jupyter-ai/jupyter_ai/extension.py b/packages/jupyter-ai/jupyter_ai/extension.py index 848a6eed7..bbfa7d163 100644 --- a/packages/jupyter-ai/jupyter_ai/extension.py +++ b/packages/jupyter-ai/jupyter_ai/extension.py @@ -252,12 +252,12 @@ def initialize_settings(self): "root_dir": self.serverapp.root_dir, "dask_client_future": self.settings["dask_client_future"], "model_parameters": self.settings["model_parameters"], + "preferred_dir": self.serverapp.contents_manager.preferred_dir, } default_chat_handler = DefaultChatHandler(**chat_handler_kwargs) clear_chat_handler = ClearChatHandler(**chat_handler_kwargs) generate_chat_handler = GenerateChatHandler( **chat_handler_kwargs, - preferred_dir=self.serverapp.contents_manager.preferred_dir, log_dir=self.error_logs_dir, ) learn_chat_handler = LearnChatHandler(**chat_handler_kwargs) From c9aa90d8956385ab7afcef1cbefb3fe8d487c807 Mon Sep 17 00:00:00 2001 From: andrewfulton9 Date: Mon, 8 Jul 2024 16:59:36 +0000 Subject: [PATCH 02/13] remove print --- packages/jupyter-ai/jupyter_ai/chat_handlers/base.py | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py index 84ed0b93d..6308fb540 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py @@ -105,7 +105,6 @@ def __init__( self.parser = argparse.ArgumentParser() self.root_dir = os.path.abspath(os.path.expanduser(root_dir)) self.preferred_dir = get_preferred_dir(self.root_dir, preferred_dir) - print(self.preferred_dir) self.dask_client_future = dask_client_future self.llm = None self.llm_params = None From 2096237f30db7c22db1f495e40a1745e7e1c8313 Mon Sep 17 00:00:00 2001 From: andrewfulton9 Date: Mon, 8 Jul 2024 17:03:12 +0000 Subject: [PATCH 03/13] cleanup --- .../jupyter_ai/chat_handlers/base.py | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py index 6308fb540..0709200d1 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py @@ -33,6 +33,15 @@ from jupyter_ai.handlers import RootChatHandler +def get_preferred_dir(root_dir: str, preferred_dir: str) -> str | None: + if preferred_dir != "": + preferred_dir = os.path.expanduser(preferred_dir) + if not preferred_dir.startswith(root_dir): + preferred_dir = os.path.join(root_dir, preferred_dir) + return os.path.abspath(preferred_dir) + return None + + # Chat handler type, with specific attributes for each class HandlerRoutingType(BaseModel): routing_method: ClassVar[Union[Literal["slash_command"]]] = ... @@ -48,18 +57,6 @@ class SlashCommandRoutingType(HandlerRoutingType): underscores.""" -def get_preferred_dir(root_dir: str, preferred_dir: str): - if preferred_dir != "": - preferred_dir = os.path.expanduser(preferred_dir) - if not preferred_dir.startswith(root_dir): - preferred_dir = os.path.join(root_dir, preferred_dir) - return os.path.abspath(preferred_dir) - return None - - - - - class BaseChatHandler: """Base ChatHandler class containing shared methods and attributes used by multiple chat handler classes.""" From 5a8b865c3c974d4e2dd19c8a69750fad8585d15e Mon Sep 17 00:00:00 2001 From: andrewfulton9 Date: Mon, 8 Jul 2024 17:06:14 +0000 Subject: [PATCH 04/13] Change /learn to use _output_dir instead of root dir --- packages/jupyter-ai/jupyter_ai/chat_handlers/learn.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/learn.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/learn.py index e8ca6bddc..c7613d8a6 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/learn.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/learn.py @@ -118,7 +118,7 @@ async def process_message(self, message: HumanChatMessage): if remote_type == "arxiv": try: id = args.path[0] - args.path = [arxiv_to_text(id, self.root_dir)] + args.path = [arxiv_to_text(id, self._output_dir)] self.reply( f"Learning arxiv file with id **{id}**, saved in **{args.path[0]}**.", message, @@ -142,7 +142,7 @@ async def process_message(self, message: HumanChatMessage): self.reply(f"{self.parser.format_usage()}", message) return short_path = args.path[0] - load_path = os.path.join(self.root_dir, short_path) + load_path = os.path.join(self._output_dir, short_path) if not os.path.exists(load_path): response = f"Sorry, that path doesn't exist: {load_path}" self.reply(response, message) From 13618c71f4a2340c6c671a4ee5e9cdb195065448 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 8 Jul 2024 17:24:40 +0000 Subject: [PATCH 05/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- packages/jupyter-ai/jupyter_ai/chat_handlers/base.py | 1 - packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py | 2 -- 2 files changed, 3 deletions(-) diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py index 0709200d1..897c9f25f 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py @@ -107,7 +107,6 @@ def __init__( self.llm_params = None self.llm_chain = None - async def on_message(self, message: HumanChatMessage): """ Method which receives a human message, calls `self.get_llm_chain()`, and diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py index 02ddbc197..d4b3282a9 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py @@ -283,5 +283,3 @@ async def handle_exc(self, e: Exception, message: HumanChatMessage): response = f"An error occurred while generating the notebook. The error details have been saved to `./{log_path}`.\n\nTry running `/generate` again, as some language models require multiple attempts before a notebook is generated." self.reply(response, message) - - From 63ce0d0eed4d014141032f4b855d6dc557951e14 Mon Sep 17 00:00:00 2001 From: andrewfulton9 Date: Mon, 8 Jul 2024 16:55:18 +0000 Subject: [PATCH 06/13] add preferred dir functionality to BaseChatHandler --- .../jupyter_ai/chat_handlers/base.py | 25 +++++++++++++++++++ .../jupyter_ai/chat_handlers/export.py | 2 +- .../jupyter_ai/chat_handlers/generate.py | 16 ++---------- packages/jupyter-ai/jupyter_ai/extension.py | 2 +- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py index fd412eed7..aec7512f4 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py @@ -48,6 +48,18 @@ class SlashCommandRoutingType(HandlerRoutingType): underscores.""" +def get_preferred_dir(root_dir: str, preferred_dir: str): + if preferred_dir != "": + preferred_dir = os.path.expanduser(preferred_dir) + if not preferred_dir.startswith(root_dir): + preferred_dir = os.path.join(root_dir, preferred_dir) + return os.path.abspath(preferred_dir) + return None + + + + + class BaseChatHandler: """Base ChatHandler class containing shared methods and attributes used by multiple chat handler classes.""" @@ -82,6 +94,7 @@ def __init__( model_parameters: Dict[str, Dict], chat_history: List[ChatMessage], root_dir: str, + preferred_dir: Optional[str], dask_client_future: Awaitable[DaskClient], ): self.log = log @@ -91,11 +104,14 @@ def __init__( self._chat_history = chat_history self.parser = argparse.ArgumentParser() self.root_dir = os.path.abspath(os.path.expanduser(root_dir)) + self.preferred_dir = get_preferred_dir(self.root_dir, preferred_dir) + print(self.preferred_dir) self.dask_client_future = dask_client_future self.llm = None self.llm_params = None self.llm_chain = None + async def on_message(self, message: HumanChatMessage): """ Method which receives a human message, calls `self.get_llm_chain()`, and @@ -302,3 +318,12 @@ def parse_args(self, message): self.reply(response, message) return None return args + + @property + def _output_dir(self): + # preferred dir is preferred, but if it is not specified, + # or if user removed it after startup, fallback to root. + if self.preferred_dir and os.path.exists(self.preferred_dir): + return self.preferred_dir + else: + return self.root_dir diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/export.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/export.py index c51a3a3e2..ef9e050cd 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/export.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/export.py @@ -42,7 +42,7 @@ async def process_message(self, message: HumanChatMessage): else f"chat_history-{datetime.now():%Y-%m-%d-%H-%M-%S}.md" ) # Handles both empty args and double tap key chat_file = os.path.join( - self.root_dir, chat_filename + self._output_dir, chat_filename ) # Do not use timestamp if filename is entered as argument with open(chat_file, "w") as chat_history: chat_history.write(markdown_content) diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py index 520f7c614..02ddbc197 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py @@ -223,14 +223,9 @@ class GenerateChatHandler(BaseChatHandler): uses_llm = True - def __init__(self, preferred_dir: str, log_dir: Optional[str], *args, **kwargs): + def __init__(self, log_dir: Optional[str], *args, **kwargs): super().__init__(*args, **kwargs) self.log_dir = Path(log_dir) if log_dir else None - self.preferred_dir = ( - os.path.abspath(os.path.expanduser(preferred_dir)) - if preferred_dir != "" - else None - ) self.llm = None def create_llm_chain( @@ -289,11 +284,4 @@ async def handle_exc(self, e: Exception, message: HumanChatMessage): response = f"An error occurred while generating the notebook. The error details have been saved to `./{log_path}`.\n\nTry running `/generate` again, as some language models require multiple attempts before a notebook is generated." self.reply(response, message) - @property - def _output_dir(self): - # preferred dir is preferred, but if it is not specified, - # or if user removed it after startup, fallback to root. - if self.preferred_dir and os.path.exists(self.preferred_dir): - return self.preferred_dir - else: - return self.root_dir + diff --git a/packages/jupyter-ai/jupyter_ai/extension.py b/packages/jupyter-ai/jupyter_ai/extension.py index f97ff9ee9..9aab5c5ba 100644 --- a/packages/jupyter-ai/jupyter_ai/extension.py +++ b/packages/jupyter-ai/jupyter_ai/extension.py @@ -255,12 +255,12 @@ def initialize_settings(self): "root_dir": self.serverapp.root_dir, "dask_client_future": self.settings["dask_client_future"], "model_parameters": self.settings["model_parameters"], + "preferred_dir": self.serverapp.contents_manager.preferred_dir, } default_chat_handler = DefaultChatHandler(**chat_handler_kwargs) clear_chat_handler = ClearChatHandler(**chat_handler_kwargs) generate_chat_handler = GenerateChatHandler( **chat_handler_kwargs, - preferred_dir=self.serverapp.contents_manager.preferred_dir, log_dir=self.error_logs_dir, ) learn_chat_handler = LearnChatHandler(**chat_handler_kwargs) From ec9634af7365b61f1ed96c335280f1c7c0b9a890 Mon Sep 17 00:00:00 2001 From: andrewfulton9 Date: Mon, 8 Jul 2024 16:59:36 +0000 Subject: [PATCH 07/13] remove print --- packages/jupyter-ai/jupyter_ai/chat_handlers/base.py | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py index aec7512f4..13de00b0d 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py @@ -105,7 +105,6 @@ def __init__( self.parser = argparse.ArgumentParser() self.root_dir = os.path.abspath(os.path.expanduser(root_dir)) self.preferred_dir = get_preferred_dir(self.root_dir, preferred_dir) - print(self.preferred_dir) self.dask_client_future = dask_client_future self.llm = None self.llm_params = None From c2b8e5124ae6e3191b8c7449634778390d56aa17 Mon Sep 17 00:00:00 2001 From: andrewfulton9 Date: Mon, 8 Jul 2024 17:03:12 +0000 Subject: [PATCH 08/13] cleanup --- .../jupyter_ai/chat_handlers/base.py | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py index 13de00b0d..cf7ba4aa7 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py @@ -33,6 +33,15 @@ from jupyter_ai.handlers import RootChatHandler +def get_preferred_dir(root_dir: str, preferred_dir: str) -> str | None: + if preferred_dir != "": + preferred_dir = os.path.expanduser(preferred_dir) + if not preferred_dir.startswith(root_dir): + preferred_dir = os.path.join(root_dir, preferred_dir) + return os.path.abspath(preferred_dir) + return None + + # Chat handler type, with specific attributes for each class HandlerRoutingType(BaseModel): routing_method: ClassVar[Union[Literal["slash_command"]]] = ... @@ -48,18 +57,6 @@ class SlashCommandRoutingType(HandlerRoutingType): underscores.""" -def get_preferred_dir(root_dir: str, preferred_dir: str): - if preferred_dir != "": - preferred_dir = os.path.expanduser(preferred_dir) - if not preferred_dir.startswith(root_dir): - preferred_dir = os.path.join(root_dir, preferred_dir) - return os.path.abspath(preferred_dir) - return None - - - - - class BaseChatHandler: """Base ChatHandler class containing shared methods and attributes used by multiple chat handler classes.""" From 590a6714fa43b56ebc4046293b18ef401d126890 Mon Sep 17 00:00:00 2001 From: andrewfulton9 Date: Mon, 8 Jul 2024 17:06:14 +0000 Subject: [PATCH 09/13] Change /learn to use _output_dir instead of root dir --- packages/jupyter-ai/jupyter_ai/chat_handlers/learn.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/learn.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/learn.py index e8ca6bddc..c7613d8a6 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/learn.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/learn.py @@ -118,7 +118,7 @@ async def process_message(self, message: HumanChatMessage): if remote_type == "arxiv": try: id = args.path[0] - args.path = [arxiv_to_text(id, self.root_dir)] + args.path = [arxiv_to_text(id, self._output_dir)] self.reply( f"Learning arxiv file with id **{id}**, saved in **{args.path[0]}**.", message, @@ -142,7 +142,7 @@ async def process_message(self, message: HumanChatMessage): self.reply(f"{self.parser.format_usage()}", message) return short_path = args.path[0] - load_path = os.path.join(self.root_dir, short_path) + load_path = os.path.join(self._output_dir, short_path) if not os.path.exists(load_path): response = f"Sorry, that path doesn't exist: {load_path}" self.reply(response, message) From 38bf7d11ee4e0b08b2e57c22da9d33a2b298e2c3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 8 Jul 2024 17:24:40 +0000 Subject: [PATCH 10/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- packages/jupyter-ai/jupyter_ai/chat_handlers/base.py | 1 - packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py | 2 -- 2 files changed, 3 deletions(-) diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py index cf7ba4aa7..82f62b73a 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py @@ -107,7 +107,6 @@ def __init__( self.llm_params = None self.llm_chain = None - async def on_message(self, message: HumanChatMessage): """ Method which receives a human message, calls `self.get_llm_chain()`, and diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py index 02ddbc197..d4b3282a9 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py @@ -283,5 +283,3 @@ async def handle_exc(self, e: Exception, message: HumanChatMessage): response = f"An error occurred while generating the notebook. The error details have been saved to `./{log_path}`.\n\nTry running `/generate` again, as some language models require multiple attempts before a notebook is generated." self.reply(response, message) - - From e2d59b05bf6d2e61a6a553d72dc3b1335ae1bedc Mon Sep 17 00:00:00 2001 From: andrewfulton9 Date: Tue, 9 Jul 2024 15:49:37 +0000 Subject: [PATCH 11/13] change _output_dir to output_dir --- packages/jupyter-ai/jupyter_ai/chat_handlers/base.py | 2 +- packages/jupyter-ai/jupyter_ai/chat_handlers/export.py | 2 +- packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py | 4 ++-- packages/jupyter-ai/jupyter_ai/chat_handlers/learn.py | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py index 82f62b73a..9b053b1d2 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py @@ -315,7 +315,7 @@ def parse_args(self, message): return args @property - def _output_dir(self): + def output_dir(self): # preferred dir is preferred, but if it is not specified, # or if user removed it after startup, fallback to root. if self.preferred_dir and os.path.exists(self.preferred_dir): diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/export.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/export.py index ef9e050cd..ed478f57e 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/export.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/export.py @@ -42,7 +42,7 @@ async def process_message(self, message: HumanChatMessage): else f"chat_history-{datetime.now():%Y-%m-%d-%H-%M-%S}.md" ) # Handles both empty args and double tap key chat_file = os.path.join( - self._output_dir, chat_filename + self.output_dir, chat_filename ) # Do not use timestamp if filename is entered as argument with open(chat_file, "w") as chat_history: chat_history.write(markdown_content) diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py index d4b3282a9..52398eabe 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py @@ -257,7 +257,7 @@ async def _generate_notebook(self, prompt: str): # create and write the notebook to disk notebook = create_notebook(outline) - final_path = os.path.join(self._output_dir, outline["title"] + ".ipynb") + final_path = os.path.join(self.output_dir, outline["title"] + ".ipynb") nbformat.write(notebook, final_path) return final_path @@ -274,7 +274,7 @@ async def process_message(self, message: HumanChatMessage): async def handle_exc(self, e: Exception, message: HumanChatMessage): timestamp = time.strftime("%Y-%m-%d-%H.%M.%S") - default_log_dir = Path(self._output_dir) / "jupyter-ai-logs" + default_log_dir = Path(self.output_dir) / "jupyter-ai-logs" log_dir = self.log_dir or default_log_dir log_dir.mkdir(parents=True, exist_ok=True) log_path = log_dir / f"generate-{timestamp}.log" diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/learn.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/learn.py index c7613d8a6..ac9388b01 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/learn.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/learn.py @@ -118,7 +118,7 @@ async def process_message(self, message: HumanChatMessage): if remote_type == "arxiv": try: id = args.path[0] - args.path = [arxiv_to_text(id, self._output_dir)] + args.path = [arxiv_to_text(id, self.output_dir)] self.reply( f"Learning arxiv file with id **{id}**, saved in **{args.path[0]}**.", message, @@ -142,7 +142,7 @@ async def process_message(self, message: HumanChatMessage): self.reply(f"{self.parser.format_usage()}", message) return short_path = args.path[0] - load_path = os.path.join(self._output_dir, short_path) + load_path = os.path.join(self.output_dir, short_path) if not os.path.exists(load_path): response = f"Sorry, that path doesn't exist: {load_path}" self.reply(response, message) From 9af4e918b9413a0e25c089e31302d550a6d36731 Mon Sep 17 00:00:00 2001 From: Andrew Fulton Date: Tue, 9 Jul 2024 09:52:55 -0600 Subject: [PATCH 12/13] Update packages/jupyter-ai/jupyter_ai/chat_handlers/base.py Co-authored-by: david qiu --- packages/jupyter-ai/jupyter_ai/chat_handlers/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py index 82f62b73a..30a32c34d 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py @@ -315,7 +315,7 @@ def parse_args(self, message): return args @property - def _output_dir(self): + def output_dir(self) -> str: # preferred dir is preferred, but if it is not specified, # or if user removed it after startup, fallback to root. if self.preferred_dir and os.path.exists(self.preferred_dir): From bd6b77e94a39492de1c365cb57e0aae44ae534c7 Mon Sep 17 00:00:00 2001 From: andrewfulton9 Date: Tue, 9 Jul 2024 15:55:42 +0000 Subject: [PATCH 13/13] add type hint --- packages/jupyter-ai/jupyter_ai/chat_handlers/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py index 9b053b1d2..30a32c34d 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/base.py @@ -315,7 +315,7 @@ def parse_args(self, message): return args @property - def output_dir(self): + def output_dir(self) -> str: # preferred dir is preferred, but if it is not specified, # or if user removed it after startup, fallback to root. if self.preferred_dir and os.path.exists(self.preferred_dir):