diff --git a/src/llm_chat/chat.py b/src/llm_chat/chat.py index 0d0d989..f63a004 100644 --- a/src/llm_chat/chat.py +++ b/src/llm_chat/chat.py @@ -80,15 +80,41 @@ class Chat: self, settings: OpenAISettings | None = None, context: list[Message] = [], + initial_system_messages: bool = True, ) -> None: self._settings = settings self.conversation = Conversation( - messages=INITIAL_SYSTEM_MESSAGES + context, + messages=INITIAL_SYSTEM_MESSAGES + context + if initial_system_messages + else context, model=self.settings.model, temperature=self.settings.temperature, ) self._start_time = datetime.now(tz=ZoneInfo("UTC")) + @classmethod + def load( + cls, path: Path, api_key: str | None = None, history_dir: Path | None = None + ) -> ChatProtocol: + """Load a chat from a file.""" + with path.open() as f: + conversation = Conversation.model_validate_json(f.read()) + args = { + "model": conversation.model, + "temperature": conversation.temperature, + } + if api_key is not None: + args["api_key"] = api_key + if history_dir is not None: + args["history_dir"] = history_dir + + settings = OpenAISettings(**args) + return cls( + settings=settings, + context=conversation.messages, + initial_system_messages=False, + ) + @property def settings(self) -> OpenAISettings: """Get OpenAI chat settings."""