diff --git a/gpt_buddy_bot/app/app_page_templates.py b/gpt_buddy_bot/app/app_page_templates.py index 328fd8d..29a1952 100644 --- a/gpt_buddy_bot/app/app_page_templates.py +++ b/gpt_buddy_bot/app/app_page_templates.py @@ -1,5 +1,4 @@ """Utilities for creating pages in a streamlit app.""" -import pickle import sys import uuid from abc import ABC, abstractmethod @@ -127,8 +126,7 @@ def chat_configs(self) -> ChatOptions: """Return the configs used for the page's chat object.""" if "chat_configs" not in self.state: chat_options_file_path = sys.argv[-1] - with open(chat_options_file_path, "rb") as chat_configs_file: - self.state["chat_configs"] = pickle.load(chat_configs_file) + self.state["chat_configs"] = ChatOptions.from_file(chat_options_file_path) return self.state["chat_configs"] @chat_configs.setter diff --git a/gpt_buddy_bot/chat.py b/gpt_buddy_bot/chat.py index 5513ecc..4ea7bc7 100644 --- a/gpt_buddy_bot/chat.py +++ b/gpt_buddy_bot/chat.py @@ -64,8 +64,7 @@ def cache_dir(self, value): def save_cache(self): """Store the chat's configs and metadata to the cache directory.""" - with open(self.configs_file, "w") as configs_f: - configs_f.write(self.configs.model_dump_json(indent=2)) + self.configs.export(self.configs_file) metadata = self.metadata # Trigger loading metadata if not yet done with open(self.metadata_file, "w") as metadata_f: diff --git a/gpt_buddy_bot/chat_configs.py b/gpt_buddy_bot/chat_configs.py index c753c03..4e81bd3 100644 --- a/gpt_buddy_bot/chat_configs.py +++ b/gpt_buddy_bot/chat_configs.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 """Registration and validation of options.""" import argparse +import json import types import typing from getpass import getuser @@ -68,6 +69,17 @@ def __getitem__(self, item): except AttributeError as error: raise KeyError(item) from error + def export(self, fpath: Path): + """Export the model's data to a file.""" + with open(fpath, "w") as configs_file: + configs_file.write(self.model_dump_json(indent=2, exclude_unset=True)) + + @classmethod + def from_file(cls, fpath: Path): + """Return an instance of the class given configs stored in a json file.""" + with open(fpath, "r") as configs_file: + return cls.model_validate(json.load(configs_file)) + class OpenAiApiCallOptions(BaseConfigModel): """Model for configuring options for OpenAI API calls.""" diff --git a/gpt_buddy_bot/command_definitions.py b/gpt_buddy_bot/command_definitions.py index 64b1c6c..98f1fde 100644 --- a/gpt_buddy_bot/command_definitions.py +++ b/gpt_buddy_bot/command_definitions.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 """Commands supported by the package's script.""" -import pickle import subprocess from . import GeneralConstants @@ -23,9 +22,7 @@ def run_on_terminal(args): def run_on_ui(args): """Run the chat on the browser.""" - with open(GeneralConstants.PARSED_ARGS_FILE, "wb") as chat_options_file: - pickle.dump(ChatOptions.from_cli_args(args), chat_options_file) - + ChatOptions.from_cli_args(args).export(fpath=GeneralConstants.PARSED_ARGS_FILE) try: subprocess.run( [ # noqa: S603, S607