Load previous conversation

This commit is contained in:
Paul Harrison 2023-08-24 18:35:27 +01:00
parent 8c17d4165a
commit 7e51060ab9
1 changed files with 27 additions and 1 deletions

View File

@ -80,15 +80,41 @@ class Chat:
self, self,
settings: OpenAISettings | None = None, settings: OpenAISettings | None = None,
context: list[Message] = [], context: list[Message] = [],
initial_system_messages: bool = True,
) -> None: ) -> None:
self._settings = settings self._settings = settings
self.conversation = Conversation( self.conversation = Conversation(
messages=INITIAL_SYSTEM_MESSAGES + context, messages=INITIAL_SYSTEM_MESSAGES + context
if initial_system_messages
else context,
model=self.settings.model, model=self.settings.model,
temperature=self.settings.temperature, temperature=self.settings.temperature,
) )
self._start_time = datetime.now(tz=ZoneInfo("UTC")) 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 @property
def settings(self) -> OpenAISettings: def settings(self) -> OpenAISettings:
"""Get OpenAI chat settings.""" """Get OpenAI chat settings."""