Load previous conversation
This commit is contained in:
parent
8c17d4165a
commit
7e51060ab9
|
@ -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."""
|
||||
|
|
Loading…
Reference in New Issue